shithub: mc

Download patch

ref: ff204601124da81b70dfa97f3072eee8eebb99bb
parent: 64046d70836a316dd7ea1fc544cf5e30cb41b947
author: Ori Bernstein <[email protected]>
date: Tue Oct 29 07:29:02 EDT 2013

Add endian flipping functions.

    These assume a little endian system.

--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -4,6 +4,7 @@
     blat.myr \
     chartype.myr \
     die.myr \
+    endian.myr \
     env.myr \
     error.myr \
     extremum.myr \
--- /dev/null
+++ b/libstd/endian.myr
@@ -1,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
+}