summaryrefslogtreecommitdiff
path: root/lib/std/endian.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/endian.myr')
-rw-r--r--lib/std/endian.myr32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/std/endian.myr b/lib/std/endian.myr
new file mode 100644
index 0000000..8bac74f
--- /dev/null
+++ b/lib/std/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::(integral,numeric)
+ 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::(integral,numeric)
+ var i
+ var ret
+
+ ret = 0
+ for i = 0; i < sizeof(@a); i++
+ ret <<= 8
+ ret |= v & 0xff
+ v >>= 8
+ ;;
+ -> ret
+}