diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-04-14 22:07:45 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-04-16 12:31:34 -0700 |
| commit | 903cb0e3a5273f68c5c27dbfbfbe2e4c6721bd37 (patch) | |
| tree | 1318da03df0e97bdd3793b17779c3e7b294b1ff1 /src/libcore/uint-template.rs | |
| parent | 6bb181341b05221df7b10a61eca60e6011292f52 (diff) | |
core: Factor out uint/u8/16/32/64 mods into uint-template
Diffstat (limited to 'src/libcore/uint-template.rs')
| -rw-r--r-- | src/libcore/uint-template.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs new file mode 100644 index 00000000000..0b95aadcb6a --- /dev/null +++ b/src/libcore/uint-template.rs @@ -0,0 +1,45 @@ +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; + +const min_value: T = 0 as T; +const max_value: T = 0 as T - 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 { + max_value ^ i +} |
