52 lines
1.4 KiB
Rust
52 lines
1.4 KiB
Rust
|
import T = inst::T;
|
||
|
|
||
|
export min_value, max_value;
|
||
|
export min, max;
|
||
|
export add, sub, mul, div, rem;
|
||
|
export lt, le, eq, ne, ge, gt;
|
||
|
export is_positive, is_negative;
|
||
|
export is_nonpositive, is_nonnegative;
|
||
|
export range;
|
||
|
export compl;
|
||
|
export abs;
|
||
|
|
||
|
const min_value: T = -1 as T << (inst::bits - 1 as T);
|
||
|
const max_value: T = min_value - 1 as T;
|
||
|
|
||
|
pure fn min(x: T, y: T) -> T { if x < y { x } else { y } }
|
||
|
pure fn max(x: T, y: T) -> T { if x > y { x } else { y } }
|
||
|
|
||
|
pure fn add(x: T, y: T) -> T { x + y }
|
||
|
pure fn sub(x: T, y: T) -> T { x - y }
|
||
|
pure fn mul(x: T, y: T) -> T { x * y }
|
||
|
pure fn div(x: T, y: T) -> T { x / y }
|
||
|
pure fn rem(x: T, y: T) -> T { x % y }
|
||
|
|
||
|
pure fn lt(x: T, y: T) -> bool { x < y }
|
||
|
pure fn le(x: T, y: T) -> bool { x <= y }
|
||
|
pure fn eq(x: T, y: T) -> bool { x == y }
|
||
|
pure fn ne(x: T, y: T) -> bool { x != y }
|
||
|
pure fn ge(x: T, y: T) -> bool { x >= y }
|
||
|
pure fn gt(x: T, y: T) -> bool { x > y }
|
||
|
|
||
|
pure fn is_positive(x: T) -> bool { x > 0 as T }
|
||
|
pure fn is_negative(x: T) -> bool { x < 0 as T }
|
||
|
pure fn is_nonpositive(x: T) -> bool { x <= 0 as T }
|
||
|
pure fn is_nonnegative(x: T) -> bool { x >= 0 as T }
|
||
|
|
||
|
#[doc = "Iterate over the range [`lo`..`hi`)"]
|
||
|
fn range(lo: T, hi: T, it: fn(T)) {
|
||
|
let mut i = lo;
|
||
|
while i < hi { it(i); i += 1 as T; }
|
||
|
}
|
||
|
|
||
|
#[doc = "Computes the bitwise complement"]
|
||
|
pure fn compl(i: T) -> T {
|
||
|
-1 as T ^ i
|
||
|
}
|
||
|
|
||
|
#[doc = "Computes the absolute value"]
|
||
|
pure fn abs(i: T) -> T {
|
||
|
if is_negative(i) { -i } else { i }
|
||
|
}
|