Debian Codegraph @

terps/tads/tads3/test/test_shr.cpp

raw
1 /*
2 * Test of our custom SHR macros. This tests that our SHR macros produce
3 * correct treatment of the sign bit for the logical vs arithmetic shifts.
4 */
5
6 #define OS_CUSTOM_ASHR
7 #define OS_CUSTOM_LSHR
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include "t3std.h"
12
13 #define INT32_BIT (sizeof(int32_t) * CHAR_BIT)
14
15 static void native_ashr(int32_t a, int32_t b, int32_t target)
16 {
17 int32_t actual = a >> b;
18 printf("signed %ld >> %ld = %ld", a, b, actual);
19 if (actual != target)
20 printf(" *** Expected %ld ***", target);
21 printf("\n");
22 }
23
24 static void native_lshr(int32_t a, int32_t b, int32_t target)
25 {
26 int32_t actual = (int32_t)(((uint32_t)a) >> ((uint32_t)b));
27 printf("unsigned %ld >> %ld = %ld", a, b, actual);
28 if (actual != target)
29 printf(" *** Expected %ld ***", target);
30 printf("\n");
31 }
32
33 static void custom_ashr(int32_t a, int32_t b, int32_t target)
34 {
35 int32_t actual = t3_ashr(a, b);
36 printf("%ld ASHR %ld = %ld", a, b, actual);
37 if (actual != target)
38 printf(" *** Expected %ld ***", target);
39 printf("\n");
40 }
41
42 static void custom_lshr(int32_t a, int32_t b, int32_t target)
43 {
44 int32_t actual = t3_lshr(a, b);
45 printf("%ld LSHR %ld = %ld", a, b, actual);
46 if (actual != target)
47 printf(" *** Expected %ld ***", target);
48 printf("\n");
49 }
50
51 void main()
52 {
53 /* test our ASHR detection macro on this platform */
54 printf("ASHR preprocessor test:\n");
55 #if ((-1 >> 1) != -1)
56 printf("Your compiler's signed>>signed does NOT appear to use ASHR, so"
57 "\nTADS 3 will use the CUSTOM ASHR code by default.");
58 #else
59 printf("Your compiler's signed>>signed appears to use ASHR, so"
60 "\nTADS 3 will use the native >> for ASHR by default.");
61 #endif
62
63 /* test our LSHR detection macro on this platform */
64 printf("\n\nLSHR preprocessor test:\n");
65 #if ((ULONG_MAX >> 1UL) != ULONG_MAX/2)
66 printf("Your compiler's unsigned>>unsigned does NOT appear to use LSHR, so"
67 "\nTADS 3 will use the CUSTOM LSHR code by default.");
68 #else
69 printf("Your compiler's unsigned>>unsigned appears to use LSHR, so"
70 "\nTADS 3 will use native >> for LSHR by default.");
71 #endif
72
73 /* test native ASHR on this platform */
74 printf("\n\nNative signed >> signed -> ASHR tests:\n");
75 native_ashr(0, 1, 0);
76 native_ashr(1, 1, 0);
77 native_ashr(2, 1, 1);
78 native_ashr(LONG_MAX-1, 1, LONG_MAX/2);
79 native_ashr(~0, 1, ~0);
80 native_ashr(-1, 1, -1);
81 native_ashr(-2, 1, -1);
82 native_ashr(-3, 1, -2);
83 native_ashr(-4, 2, -1);
84 native_ashr(LONG_MIN+1, 1, -1073741824);
85 native_ashr(LONG_MIN, 1, -1073741824);
86
87 /* test our custom ASHR macro */
88 printf("\nCustom ASHR tests:\n");
89 custom_ashr(0, 1, 0);
90 custom_ashr(1, 1, 0);
91 custom_ashr(2, 1, 1);
92 custom_ashr(LONG_MAX-1, 1, LONG_MAX/2);
93 native_ashr(~0, 1, ~0);
94 native_ashr(-1, 1, -1);
95 custom_ashr(-2, 1, -1);
96 custom_ashr(-3, 1, -2);
97 custom_ashr(-4, 2, -1);
98 custom_ashr(LONG_MIN+1, 1, LONG_MIN/2);
99 custom_ashr(LONG_MIN, 1, LONG_MIN/2);
100
101 /* test native LSHR on this platform */
102 printf("\nNative unsigned >> unsigned -> LSHR tests:\n");
103 native_lshr(0, 1, 0);
104 native_lshr(1, 1, 0);
105 native_lshr(2, 1, 1);
106 native_lshr(LONG_MAX-1, 1, LONG_MAX/2);
107 native_lshr(~0, 1, ~0 & ~(1 << (INT32_BIT-1)));
108 native_lshr(LONG_MIN+1, 1, ((ulong)LONG_MAX+1)/2);
109 native_lshr(LONG_MIN, 1, ((ulong)LONG_MAX+1)/2);
110
111 /* test our custom LSHR macro */
112 printf("\nCustom LSHR tests:\n");
113 custom_lshr(0, 1, 0);
114 custom_lshr(1, 1, 0);
115 custom_lshr(2, 1, 1);
116 custom_lshr(LONG_MAX-1, 1, LONG_MAX/2);
117 custom_lshr(LONG_MIN+1, 1, ((ulong)LONG_MAX+1)/2);
118 custom_lshr(LONG_MIN, 1, ((ulong)LONG_MAX+1)/2);
119 }