[m-dev.] for review: change semantics of integer shifts

Simon Taylor stayl at cs.mu.OZ.AU
Fri Mar 19 17:55:13 AEDT 1999


 
> I think the sentence "The predicates and functions in this module do not
> check for overflow." is over-specification -- we might want to change
> that in some future release.  So I suggest the following instead:
> 
> % The behaviour of a computation for which overflow occurs is undefined.
> % (In the current implementation, the predicates and functions in this
> % module do not check for overflow, and the results you get are those
> % delivered by the C compiler.  However, future implementations
> % might check for overflow.)

OK.

> The tests marked "<=== here" above all have undefined behaviour
> according to the specification, because they cause overflow.
> So either you should not test those cases, or if you do, then you
> should separate out those tests and clearly document that these tests
> are testing something which is not specified by the specification.
> 
> It would be a good idea to test shifting values left or right by zero bits.
> It would also be a good idea to test shifting values that are not an
> exact power of two.


Index: tests/hard_coded/shift_test.m
===================================================================
RCS file: shift_test.m
diff -N shift_test.m
--- /dev/null	Fri Mar 19 17:50:18 1999
+++ shift_test.m	Fri Mar 19 17:45:52 1999
@@ -0,0 +1,58 @@
+% test the handling of `>>', `<<', unchecked_left_shift
+% and unchecked_right_shift.  
+
+:- module shift_test.
+:- interface.
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+:- import_module int, list, string.
+
+main -->
+	shift_test((<<), "<<", 64, 0, 64),
+	shift_test((<<), "<<", 64, 2, 256),
+	shift_test((<<), "<<", -64, 2, -256),
+	shift_test((<<), "<<", 64, -2, 16),
+	shift_test((<<), "<<", -64, -2, -16),
+	shift_test((<<), "<<", 64, -256, 0),
+	shift_test((<<), "<<", -64, -256, -1),
+	shift_test((<<), "<<", 25, 3, 200),
+	shift_test((<<), "<<", -25, 3, -200),
+	shift_test((<<), "<<", 25, -3, 3),
+	shift_test((<<), "<<", -25, -3, -4),
+
+	shift_test((>>), ">>", 64, 0, 64),
+	shift_test((>>), ">>", 64, 2, 16),
+	shift_test((>>), ">>", -64, 2, -16),
+	shift_test((>>), ">>", 64, -2, 256),
+	shift_test((>>), ">>", -64, -2, -256),
+	shift_test((>>), ">>", 64, 256, 0),
+	shift_test((>>), ">>", -64, 256, -1),
+	shift_test((>>), ">>", 25, 3, 3),
+	shift_test((>>), ">>", -25, 3, -4),
+	shift_test((>>), ">>", 25, -3, 200),
+	shift_test((>>), ">>", -25, -3, -200),
+
+	shift_test(unchecked_left_shift, "unchecked_left_shift",
+		64, 2, 256),
+	shift_test(unchecked_right_shift, "unchecked_right_shift",
+		-64, 2, -16),
+
+	io__write_string("The following cases test undefined behaviour\n"),
+	io__write_string("(they cause overflow):\n"),
+	shift_test((<<), "<<", 64, 256, 0),
+	shift_test((<<), "<<", -64, 256, 0),
+	shift_test((>>), ">>", 64, -256, 0),
+	shift_test((>>), ">>", -64, -256, 0).
+
+:- pred shift_test((func(int, int) = int)::(func(in, in) = out is det),
+	string::in, int::in, int::in, int::in,
+	io__state::di, io__state::uo) is det.
+
+shift_test(Func, FuncName, Left, Right, Result) -->
+	io__format("%d %s %d = %d (%d)\n",
+		[i(Left), s(FuncName), i(Right),
+		i(Func(Left, Right)), i(Result)]).
+
Index: tests/hard_coded/shift_test.exp
===================================================================
RCS file: shift_test.exp
diff -N shift_test.exp
--- /dev/null	Fri Mar 19 17:50:18 1999
+++ shift_test.exp	Fri Mar 19 17:47:20 1999
@@ -0,0 +1,30 @@
+64 << 0 = 64 (64)
+64 << 2 = 256 (256)
+-64 << 2 = -256 (-256)
+64 << -2 = 16 (16)
+-64 << -2 = -16 (-16)
+64 << -256 = 0 (0)
+-64 << -256 = -1 (-1)
+25 << 3 = 200 (200)
+-25 << 3 = -200 (-200)
+25 << -3 = 3 (3)
+-25 << -3 = -4 (-4)
+64 >> 0 = 64 (64)
+64 >> 2 = 16 (16)
+-64 >> 2 = -16 (-16)
+64 >> -2 = 256 (256)
+-64 >> -2 = -256 (-256)
+64 >> 256 = 0 (0)
+-64 >> 256 = -1 (-1)
+25 >> 3 = 3 (3)
+-25 >> 3 = -4 (-4)
+25 >> -3 = 200 (200)
+-25 >> -3 = -200 (-200)
+64 unchecked_left_shift 2 = 256 (256)
+-64 unchecked_right_shift 2 = -16 (-16)
+The following cases test undefined behaviour
+(they cause overflow):
+64 << 256 = 0 (0)
+-64 << 256 = 0 (0)
+64 >> -256 = 0 (0)
+-64 >> -256 = 0 (0)



More information about the developers mailing list