Skip to content

Commit

Permalink
feat!: int operations other than widen/narrow have only one width arg (
Browse files Browse the repository at this point in the history
…#1455)

BREAKING CHANGE: `idivmod_checked`, `idivmod`, `idiv`, `idiv_checked`,
`imod`, `ishl`, `ishr`, `irotl`, `irotr` operations now only have one
width argument for all inputs and outputs rather than two.

---------

Co-authored-by: Alec Edgington <54802828+cqc-alec@users.noreply.github.com>
  • Loading branch information
ss2165 and cqc-alec committed Aug 21, 2024
1 parent 95b3949 commit c39ed15
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 290 deletions.
45 changes: 19 additions & 26 deletions hugr-core/src/std_extensions/arithmetic/int_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,60 +113,53 @@ impl MakeOpDef for IntOpDef {

fn signature(&self) -> SignatureFunc {
use IntOpDef::*;
let tv0 = int_tv(0);
match self {
iwiden_s | iwiden_u => CustomValidator::new(
int_polytype(2, vec![int_tv(0)], vec![int_tv(1)]),
int_polytype(2, vec![tv0.clone()], vec![int_tv(1)]),
IOValidator { f_ge_s: false },
)
.into(),
inarrow_s | inarrow_u => CustomValidator::new(
int_polytype(2, int_tv(0), sum_ty_with_err(int_tv(1))),
int_polytype(2, tv0.clone(), sum_ty_with_err(int_tv(1))),
IOValidator { f_ge_s: true },
)
.into(),
itobool => int_polytype(0, vec![int_type(0)], type_row![BOOL_T]).into(),
ifrombool => int_polytype(0, type_row![BOOL_T], vec![int_type(0)]).into(),
ieq | ine | ilt_u | ilt_s | igt_u | igt_s | ile_u | ile_s | ige_u | ige_s => {
int_polytype(1, vec![int_tv(0); 2], type_row![BOOL_T]).into()
int_polytype(1, vec![tv0; 2], type_row![BOOL_T]).into()
}
imax_u | imax_s | imin_u | imin_s | iadd | isub | imul | iand | ior | ixor => {
ibinop_sig().into()
}
ineg | iabs | inot => iunop_sig().into(),
//TODO inline
idivmod_checked_u | idivmod_checked_s => {
let intpair: TypeRowRV = vec![int_tv(0), int_tv(1)].into();
let intpair: TypeRowRV = vec![tv0; 2].into();
int_polytype(
2,
1,
intpair.clone(),
sum_ty_with_err(Type::new_tuple(intpair)),
)
}
.into(),
idivmod_u | idivmod_s => {
let intpair: TypeRowRV = vec![int_tv(0), int_tv(1)].into();
int_polytype(2, intpair.clone(), intpair.clone())
let intpair: TypeRowRV = vec![tv0; 2].into();
int_polytype(1, intpair.clone(), intpair.clone())
}
.into(),
idiv_u | idiv_s => int_polytype(2, vec![int_tv(0), int_tv(1)], vec![int_tv(0)]).into(),
idiv_u | idiv_s => int_polytype(1, vec![tv0.clone(); 2], vec![tv0]).into(),
idiv_checked_u | idiv_checked_s => {
int_polytype(2, vec![int_tv(0), int_tv(1)], sum_ty_with_err(int_tv(0))).into()
int_polytype(1, vec![tv0.clone(); 2], sum_ty_with_err(tv0)).into()
}
imod_checked_u | imod_checked_s => int_polytype(
2,
vec![int_tv(0), int_tv(1).clone()],
sum_ty_with_err(int_tv(1)),
)
.into(),
imod_u | imod_s => {
int_polytype(2, vec![int_tv(0), int_tv(1).clone()], vec![int_tv(1)]).into()
}
ishl | ishr | irotl | irotr => {
int_polytype(2, vec![int_tv(0), int_tv(1)], vec![int_tv(0)]).into()
imod_checked_u | imod_checked_s => {
int_polytype(1, vec![tv0.clone(); 2], sum_ty_with_err(tv0)).into()
}
imod_u | imod_s => int_polytype(1, vec![tv0.clone(); 2], vec![tv0]).into(),
ishl | ishr | irotl | irotr => int_polytype(1, vec![tv0.clone(); 2], vec![tv0]).into(),
itostring_u | itostring_s => PolyFuncTypeRV::new(
vec![LOG_WIDTH_TYPE_PARAM],
FuncValueType::new(vec![int_tv(0)], vec![STRING_TYPE]),
FuncValueType::new(vec![tv0], vec![STRING_TYPE]),
)
.into(),
}
Expand Down Expand Up @@ -200,13 +193,13 @@ impl MakeOpDef for IntOpDef {
isub => "subtraction modulo 2^N (signed and unsigned versions are the same op)",
ineg => "negation modulo 2^N (signed and unsigned versions are the same op)",
imul => "multiplication modulo 2^N (signed and unsigned versions are the same op)",
idivmod_checked_u => "given unsigned integers 0 <= n < 2^N, 0 <= m < 2^M, generates unsigned q, r where \
idivmod_checked_u => "given unsigned integers 0 <= n < 2^N, 0 <= m < 2^N, generates unsigned q, r where \
q*m+r=n, 0<=r<m (m=0 is an error)",
idivmod_u => "given unsigned integers 0 <= n < 2^N, 0 <= m < 2^M, generates unsigned q, r where \
idivmod_u => "given unsigned integers 0 <= n < 2^N, 0 <= m < 2^N, generates unsigned q, r where \
q*m+r=n, 0<=r<m (m=0 will call panic)",
idivmod_checked_s => "given signed integer -2^{N-1} <= n < 2^{N-1} and unsigned 0 <= m < 2^M, generates \
idivmod_checked_s => "given signed integer -2^{N-1} <= n < 2^{N-1} and unsigned 0 <= m < 2^N, generates \
signed q and unsigned r where q*m+r=n, 0<=r<m (m=0 is an error)",
idivmod_s => "given signed integer -2^{N-1} <= n < 2^{N-1} and unsigned 0 <= m < 2^M, generates \
idivmod_s => "given signed integer -2^{N-1} <= n < 2^{N-1} and unsigned 0 <= m < 2^N, generates \
signed q and unsigned r where q*m+r=n, 0<=r<m (m=0 will call panic)",
idiv_checked_u => "as idivmod_checked_u but discarding the second output",
idiv_u => "as idivmod_u but discarding the second output",
Expand Down
Loading

0 comments on commit c39ed15

Please sign in to comment.