shithub: mc

ref: fde9deac883c3f2ae6ffb7d05b4f5e404f5c2249
dir: /lib/std/endian.myr/

View raw version
pkg std =
	generic hosttonet	: (v : @a -> @a) :: integral,numeric @a
	generic nettohost	: (v : @a -> @a) :: integral,numeric @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 @a
	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 @a
	var i
	var ret

	ret = 0
	for i = 0; i < sizeof(@a); i++
		ret <<= 8
		ret |= v & 0xff 
		v >>= 8
	;;
	-> ret
}