X-Git-Url: http://git.joshuawise.com/snipe.git/blobdiff_plain/12aa4087bee3e70f170d7457794921de4e385227..075222c771a121b12d6f07f39af868ae14cd06a1:/util/word32.sml?ds=inline diff --git a/util/word32.sml b/util/word32.sml index d830c9d..c25c285 100644 --- a/util/word32.sml +++ b/util/word32.sml @@ -14,16 +14,25 @@ signature WORD32_SIGNED = sig + val TMAX : Word32.word (* largest signed positive word, 2^31-1 *) val TMIN : Word32.word (* smallest signed negative word -2^31 *) val ZERO : Word32.word (* 0 *) val fromString : string -> Word32.word option (* parse from string, no sign *) (* raises Overflow if not 0 <= n < 2^32 *) val toString : Word32.word -> string (* print to string, with sign *) + val abs : Word32.word -> Word32.word + val adiv : Word32.word * Word32.word -> Word32.word + val amod : Word32.word * Word32.word -> Word32.word + val lt : Word32.word * Word32.word -> bool + val gt : Word32.word * Word32.word -> bool + val le : Word32.word * Word32.word -> bool + val ge : Word32.word * Word32.word -> bool end structure Word32Signed :> WORD32_SIGNED = struct + val TMIN = Word32.<<(Word32.fromInt(1), Word.fromInt(Word32.wordSize-1)) val TMAX = Word32.-(TMIN, Word32.fromInt(1)) val ZERO = Word32.fromInt(0) @@ -36,6 +45,22 @@ struct fun toString (w) = if neg w - then "-" ^ Word32.fmt StringCvt.DEC (Word32.~(w)) - else Word32.fmt StringCvt.DEC w + then "-0x" ^ Word32.fmt StringCvt.HEX (Word32.~(w)) + else "0x" ^ Word32.fmt StringCvt.HEX w + + fun toInt32 w = Int32.fromLarge (Word32.toLargeInt w) + fun fromInt32 i = Word32.fromLargeInt (Int32.toLarge i) + + fun abs w = if neg w then Word32.~ (w) else w + fun adiv (a,b) = fromInt32 (Int32.div (toInt32 a, toInt32 b)) + fun amod (a,b) = fromInt32 (Int32.mod (toInt32 a, if neg a andalso neg b then toInt32 b + else if neg b then toInt32 (abs b) + else if neg a then toInt32 (Word32.~ b) + else toInt32 b)) + + fun lt (a,b) = Int32.compare (toInt32 a, toInt32 b) = LESS + fun gt (a,b) = Int32.compare (toInt32 a, toInt32 b) = GREATER + fun le (a,b) = case Int32.compare (toInt32 a, toInt32 b) of LESS => true | EQUAL => true | _ => false + fun ge (a,b) = case Int32.compare (toInt32 a, toInt32 b) of GREATER => true | EQUAL => true | _ => false + end