summaryrefslogtreecommitdiff
path: root/libstd/endian.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2013-10-29 11:29:02 -0400
committerOri Bernstein <ori@eigenstate.org>2013-10-29 11:29:02 -0400
commitff204601124da81b70dfa97f3072eee8eebb99bb (patch)
tree8ef428f610ba986522c6973773cfe699965e974c /libstd/endian.myr
parent64046d70836a316dd7ea1fc544cf5e30cb41b947 (diff)
downloadmc-ff204601124da81b70dfa97f3072eee8eebb99bb.tar.gz
Add endian flipping functions.
These assume a little endian system.
Diffstat (limited to 'libstd/endian.myr')
-rw-r--r--libstd/endian.myr32
1 files changed, 32 insertions, 0 deletions
diff --git a/libstd/endian.myr b/libstd/endian.myr
new file mode 100644
index 0000000..4f01c64
--- /dev/null
+++ b/libstd/endian.myr
@@ -0,0 +1,32 @@
+pkg std =
+ generic hosttonet : (v : @a -> @a)
+ generic nettohost : (v : @a -> @a)
+;;
+
+/* FIXME: we only support little endian platforms right now,
+ so we assume a little endian machine. FIX THIS. */
+generic hosttonet = {v : @a::(tcint,tcnum,tctest)
+ var i
+ var ret
+
+ ret = 0
+ for i = 0; i < sizeof(@a); i++
+ ret <<= 8
+ ret |= v & 0xff
+ v >>= 8
+ ;;
+ -> ret
+}
+
+generic nettohost = {v : @a::(tcint,tcnum,tctest)
+ var i
+ var ret
+
+ ret = 0
+ for i = 0; i < sizeof(@a); i++
+ ret <<= 8
+ ret |= v & 0xff
+ v >>= 8
+ ;;
+ -> ret
+}