]> Joshua Wise's Git repositories - snipe.git/blobdiff - util/word32.sml
Initial import of l5c
[snipe.git] / util / word32.sml
index d830c9da5b17df00002616f63db22cf994865b1b..c25c2855c224dc37647bf0c7b64b666a910d876a 100644 (file)
 
 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
This page took 0.024675 seconds and 4 git commands to generate.