ref: 9ac9460f843a40c0bbfc2933ab9fd6f9e46382f6
parent: d8a5f09e1e3367df07413a6a842fa8f2e193a696
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Jan 8 14:47:11 EST 2015
Add integer packing/unpacking code.
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -34,6 +34,7 @@
hasprefix.myr \
hassuffix.myr \
htab.myr \
+ getint.myr \
intparse.myr \
ipparse.myr \
mk.myr \
--- a/libstd/bldfile
+++ b/libstd/bldfile
@@ -39,6 +39,7 @@
hasprefix.myr
hassuffix.myr
htab.myr
+ getint.myr
intparse.myr
ipparse.myr
mk.myr
--- /dev/null
+++ b/libstd/getint.myr
@@ -1,0 +1,64 @@
+pkg std =
+ generic getle64 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe64 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getle32 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe32 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getle16 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe16 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getle8 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe8 : (buf : byte[:] -> @a::(numeric,integral))
+;;
+
+generic getbe64 = {buf -> @a::(numeric,integral)
+ -> ((buf[0] castto(@a::(numeric,integral))) << 0) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 8) | \
+ ((buf[2] castto(@a::(numeric,integral))) << 16) | \
+ ((buf[3] castto(@a::(numeric,integral))) << 24) | \
+ ((buf[4] castto(@a::(numeric,integral))) << 32) | \
+ ((buf[5] castto(@a::(numeric,integral))) << 40) | \
+ ((buf[6] castto(@a::(numeric,integral))) << 48) | \
+ ((buf[7] castto(@a::(numeric,integral))) << 56)
+}
+
+generic getle64 = {buf
+ -> ((buf[7] castto(@a::(numeric,integral))) << 0) | \
+ ((buf[6] castto(@a::(numeric,integral))) << 8) | \
+ ((buf[5] castto(@a::(numeric,integral))) << 16) | \
+ ((buf[4] castto(@a::(numeric,integral))) << 24) | \
+ ((buf[3] castto(@a::(numeric,integral))) << 32) | \
+ ((buf[2] castto(@a::(numeric,integral))) << 40) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 48) | \
+ ((buf[0] castto(@a::(numeric,integral))) << 56)
+}
+
+generic getbe32 = {buf
+ -> ((buf[0] castto(@a::(numeric,integral))) << 0) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 8) | \
+ ((buf[2] castto(@a::(numeric,integral))) << 16) | \
+ ((buf[3] castto(@a::(numeric,integral))) << 24)
+}
+
+generic getle32 = {buf
+ -> ((buf[3] castto(@a::(numeric,integral))) << 24) | \
+ ((buf[2] castto(@a::(numeric,integral))) << 16) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 8) | \
+ ((buf[0] castto(@a::(numeric,integral))) << 0)
+}
+
+generic getbe16 = {buf
+ -> ((buf[0] castto(@a::(numeric,integral))) << 0) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 8)
+}
+
+generic getle16 = {buf
+ -> ((buf[1] castto(@a::(numeric,integral))) << 8) | \
+ ((buf[0] castto(@a::(numeric,integral))) << 0)
+}
+
+generic getbe8 = {buf
+ -> (buf[0] castto(@a::(numeric,integral))) << 0
+}
+
+generic getle8 = {buf
+ -> (buf[0] castto(@a::(numeric,integral))) << 8
+}