]> git.sur5r.net Git - cc65/blob - testcode/lib/shift-test.c
Changed the PC-Engine's configuration file, so that the command line can build 8K...
[cc65] / testcode / lib / shift-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4
5
6
7 static unsigned UnsignedShiftLeft1 (unsigned Val)
8 /* Shift an unsigned left by 1 */
9 {
10     __AX__ = Val;
11     asm ("stx tmp1");
12     asm ("asl a");
13     asm ("rol tmp1");
14     asm ("ldx tmp1");
15     return __AX__;
16 }
17
18
19
20 static unsigned UnsignedShiftRight1 (unsigned Val)
21 /* Shift an unsigned right by 1 */
22 {
23     __AX__ = Val;
24     asm ("stx tmp1");
25     asm ("lsr tmp1");
26     asm ("ror a");
27     asm ("ldx tmp1");
28     return __AX__;
29 }
30
31
32
33 static int SignedShiftRight1 (int Val)
34 /* Shift a signed right by 1 */
35 {
36     __AX__ = Val;
37     asm ("stx tmp1");
38     asm ("cpx #$80");
39     asm ("ror tmp1");
40     asm ("ror a");
41     asm ("ldx tmp1");
42     return __AX__;
43 }
44
45
46
47 static void TestUnsignedLeftShift (void)
48 /* Test left shift. This is identical for signed and unsigned ints */
49 {
50     unsigned L, R, V;
51     printf ("Testing unsigned left shift:\n");
52     L = 0;
53     do {
54         V = L;
55         for (R = 0; R < 16; ++R) {
56             /* Check it */
57             if ((L << R) != V) {
58                 fprintf (stderr,
59                          "Failed: %u << %u != %u (%u)\n",
60                          L, R, V, L << R);
61                 exit (1);
62             }
63             V = UnsignedShiftLeft1 (V);
64         }
65         if ((L & 0xFF) == 0) {
66             printf ("%04X ", L);
67         }
68     } while (++L != 0);
69     printf ("\n");
70 }
71
72
73
74 static void TestUnsignedRightShift (void)
75 /* Test unsigned right shift. */
76 {
77     unsigned L, R, V;
78     printf ("Testing unsigned right shift:\n");
79     L = 0;
80     do {
81         V = L;
82         for (R = 0; R < 16; ++R) {
83             /* Check it */
84             if ((L >> R) != V) {
85                 fprintf (stderr,
86                          "Failed: %u >> %u != %u (%u)\n",
87                          L, R, V, L >> R);
88                 exit (1);
89             }
90             V = UnsignedShiftRight1 (V);
91         }
92         if ((L & 0xFF) == 0) {
93             printf ("%04X ", L);
94         }
95     } while (++L != 0);
96     printf ("\n");
97 }
98
99
100
101 static void TestSignedRightShift (void)
102 /* Test signed right shift. */
103 {
104     int L, R, V;
105     printf ("Testing signed right shift:\n");
106     L = 0;
107     do {
108         V = L;
109         for (R = 0; R < 16; ++R) {
110             /* Check it */
111             if ((L >> R) != V) {
112                 fprintf (stderr,
113                          "Failed: %d >> %d != %d (%d)\n",
114                          L, R, V, L >> R);
115                 exit (1);
116             }
117             V = SignedShiftRight1 (V);
118         }
119         if ((L & 0xFF) == 0) {
120             printf ("%04X ", L);
121         }
122     } while (++L != 0);
123     printf ("\n");
124 }
125
126
127
128 int main (void)
129 {
130     TestUnsignedLeftShift ();
131     TestUnsignedRightShift ();
132     TestSignedRightShift ();
133     printf ("\nOk!\n");
134
135     return 0;
136 }