[M3devel] libm3/Swap?
Jay K
jay.krell at cornell.edu
Sat May 15 09:08:03 CEST 2010
libm3/.../swap.m3:
FROM Word IMPORT Or, And, Not, LeftShift, RightShift, Extract;
CONST
B0 = 16_FF;
B1 = 16_FF00;
B2 = 16_FF0000;
B3 = 16_FF000000;
(* These will all be zero on machines with BYTESIZE(INTEGER) = 32 *)
B4 = LeftShift(B3, 8);
B5 = LeftShift(B4, 8);
B6 = LeftShift(B5, 8);
B7 = LeftShift(B6, 8);
CONST SignExt32 = ARRAY [0..1] OF Word.T {0, Not(16_FFFFFFFF)};
(* Swaps the lower 4 bytes of i *)
PROCEDURE Swap4 (i: Int32): Int32 =
BEGIN
IF BYTESIZE(INTEGER) = 4 THEN
RETURN Or(Or(RightShift(And(B3, i), 24), RightShift(And(B2, i), 8)),
Or(LeftShift(And(B1, i), 8), LeftShift(And(B0, i), 24)));
ELSIF BYTESIZE(INTEGER) = 8 THEN
RETURN
Or(SignExt32[Extract(i, 7, 1)],
Or(Or(RightShift(And(B3, i), 24), RightShift(And(B2, i), 8)),
Or(LeftShift(And(B1, i), 8), LeftShift(And(B0, i), 24))));
ELSE
RAISE Failure;
END;
END Swap4;
What is the meaning of the 7?
In Extract(i, 7, 1)?
Presumably it should be 63??
I guess I'll write a test..
Is there a better idiom that doesn't expose architectures to compile code for other word sizes?
Maybe fork the file and include_dir(WORD_SIZE)?
Like is done in m3core/src/C?
- Jay
More information about the M3devel
mailing list