[MLton-commit] r4822
Vesa Karvonen
vesak at mlton.org
Tue Nov 14 09:08:09 PST 2006
Added conversions to/from big/little endian byte vectors.
----------------------------------------------------------------------
U mltonlib/trunk/com/ssh/extended-basis/unstable/detail/mk-word-ext.fun
U mltonlib/trunk/com/ssh/extended-basis/unstable/public/word.sig
----------------------------------------------------------------------
Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/mk-word-ext.fun
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/mk-word-ext.fun 2006-11-14 12:59:50 UTC (rev 4821)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/mk-word-ext.fun 2006-11-14 17:08:09 UTC (rev 4822)
@@ -11,8 +11,33 @@
open W
type t = word
val bounds as (minWord, maxWord) = (fromInt 0, fromInt~1)
+ val numBytes = Int.quot (Int.+ (wordSize, 7), 8)
+ local
+ fun mk fold bs =
+ if numBytes <> Word8Vector.length bs then
+ raise Subscript
+ else
+ fold (fn (b, w) =>
+ W.orb (W.<< (w, 0w8), W.fromLarge (Word8.toLarge b)))
+ (W.fromInt 0)
+ bs
+ in
+ val fromBigBytes = mk Word8Vector.foldl
+ val fromLittleBytes = mk Word8Vector.foldr
+ end
val fromWord = fromLarge o Word.toLarge
val fromWordX = fromLarge o Word.toLargeX
+ local
+ fun mk idx w =
+ Word8Vector.tabulate
+ (numBytes,
+ fn i =>
+ Word8.fromLarge
+ (W.toLarge (W.>> (w, Word.* (0w8, Word.fromInt (idx i))))))
+ in
+ val toBigBytes = mk (fn i => Int.- (Int.- (numBytes, 1), i))
+ val toLittleBytes = mk (fn i => i)
+ end
val toWord = Word.fromLarge o toLarge
val toWordX = Word.fromLarge o toLargeX
val embString = (toString, fromString)
Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/public/word.sig
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/public/word.sig 2006-11-14 12:59:50 UTC (rev 4821)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/word.sig 2006-11-14 17:08:09 UTC (rev 4822)
@@ -17,6 +17,12 @@
(** == Bounds == *)
+ val numBytes : int
+ (**
+ * The number of bytes (8-bit words) it takes to store a {word}. This
+ * is always equal to {(wordSize + 7) quot 8}.
+ *)
+
val maxWord : word
(**
* The maximal representable {word}. This is always equal to {fromInt
@@ -36,6 +42,22 @@
(** == Conversions == *)
+ val fromBigBytes : Word8Vector.vector -> word
+ (**
+ * Converts a vector of bytes in big-endian order to a word. Raises
+ * {Subscript} if the length of the given vector is not equal to
+ * {numBytes}. Extra bits, if any, in the most significant byte are
+ * ignored.
+ *)
+
+ val fromLittleBytes : Word8Vector.vector -> word
+ (**
+ * Converts a vector of bytes in little-endian order to a word. Raises
+ * {Subscript} if the length of the given vector is not equal to
+ * {numBytes}. Extra bits, if any, in the most significant byte are
+ * ignored.
+ *)
+
val fromWord : Word.word -> word
(**
* Converts the given word {w : Word.word} to the value {w(mod
@@ -53,6 +75,20 @@
* significant bit of {w}.
*)
+ val toBigBytes : word -> Word8Vector.vector
+ (**
+ * Converts the given word to a vector of bytes in big-endian order.
+ * Extra bits, if any, in the most significant byte will be set to
+ * zeroes.
+ *)
+
+ val toLittleBytes : word -> Word8Vector.vector
+ (**
+ * Converts the given word to a vector of bytes in little-endian order.
+ * Extra bits, if any, in the most significant byte will be set to
+ * zeroes.
+ *)
+
val toWord : word -> Word.word
(**
* Converts the given word {w : word} to the value {w(mod
More information about the MLton-commit
mailing list