95 lines
2.6 KiB
Text
95 lines
2.6 KiB
Text
/* Two's complement Signed Integers
|
||
*/
|
||
export {
|
||
import "./lt-stdlib/bool.lt";
|
||
|
||
let int-sign =
|
||
λ x: machine.Int64~machine.Word
|
||
↦ (bit-and (bit-shr (x des machine.Word) 63) 1) as Bool;
|
||
|
||
let int-neg =
|
||
λ x: machine.Int64~machine.Word
|
||
↦ i+ ((bit-neg x) as machine.Int64) 1;
|
||
|
||
let int-abs =
|
||
λ x: machine.Int64~machine.Word
|
||
↦ if( int-sign x ) { int-neg x; } else { x; };
|
||
|
||
let int-lt =
|
||
λ{ a: machine.Int64~machine.Word;
|
||
b: machine.Int64~machine.Word; }
|
||
↦ int-sign (i- a b);
|
||
|
||
let int-gt =
|
||
λ{ a: machine.Int64~machine.Word;
|
||
b: machine.Int64~machine.Word; }
|
||
↦ int-sign (i- b a);
|
||
|
||
let int-neq =
|
||
λ{ a: machine.Int64~machine.Word;
|
||
b: machine.Int64~machine.Word; }
|
||
↦ (bit-xor a b) as Bool;
|
||
|
||
let int-eq =
|
||
λ{ a: machine.Int64~machine.Word;
|
||
b: machine.Int64~machine.Word; }
|
||
↦ bool-neg (int-neq a b);
|
||
|
||
let int-lte =
|
||
λ{ a: machine.Int64~machine.Word;
|
||
b: machine.Int64~machine.Word; }
|
||
↦ bool-or (int-lt a b) (int-eq a b);
|
||
|
||
let int-gte =
|
||
λ{ a: machine.Int64~machine.Word;
|
||
b: machine.Int64~machine.Word; }
|
||
↦ bool-or (int-gt a b) (int-eq a b);
|
||
|
||
let int-min =
|
||
λ{ a: machine.Int64~machine.Word;
|
||
b: machine.Int64~machine.Word; }
|
||
↦ if (int-lt a b) { a; } else { b; };
|
||
|
||
let int-max =
|
||
λ{ a:machine.Int64~machine.Word;
|
||
b:machine.Int64~machine.Word; }
|
||
↦ if (int-gt a b) { a; } else { b; };
|
||
|
||
/*
|
||
type machine.UInt64 = <UnsignedBinaryInt 64> ~ machine.Word;
|
||
impl ℤ_2^64 for machine.UInt64 {
|
||
let + = machine.UInt64.add;
|
||
let - = machine.UInt64.sub;
|
||
let * = machine.UInt64.mul;
|
||
let / = machine.UInt64.div;
|
||
let % = machine.UInt64.rem;
|
||
};
|
||
|
||
type machine.Int64 = <TwosComplementInt 64> ~ machine.Word;
|
||
let machine.Int64.add =
|
||
λ{ a: machine.Int64;
|
||
b: machine.Int64; }
|
||
↦ ((u+ ((a des machine.Word) as machine.UInt64)
|
||
((b des machine.Word) as machine.UInt64))
|
||
des machine.Word) as machine.Int64;
|
||
|
||
type ℤ =~ <PosInt 2^64 BigEndian> ~ <Seq <Digit 2^64>~ℤ_2^64~machine.UInt64>;
|
||
impl <ℤ -2^63-1 +2^63> for <TwosComplementInt 64> {
|
||
let + = machine.Int64.add;
|
||
let - = machine.Int64.sub;
|
||
let * = machine.Int64.mul;
|
||
let / = machine.Int64.div;
|
||
let % = machine.Int64.rem;
|
||
};
|
||
*/
|
||
/*
|
||
let int-onescomplement-neg =
|
||
λ x : <ℤ -2^63 +2^63> ~ <OnesComplementInt 64> ~ machine.Word
|
||
↦ (bit-neg x) as <ℤ -2^63 +2^63> ~ <OnesComplementInt 64>;
|
||
|
||
let int-twoscomplement-neg =
|
||
λ x : <ℤ -2^63-1 +2^63> ~ <TwosComplementInt 64> ~ machine.Word
|
||
↦ (i+ 1 ((bit-neg x) as machine.Int64)) as <ℤ -2^63-1 +2^63>;
|
||
*/
|
||
|
||
};
|