From 18f22506bad9e3168b2c741c14af4b1a701fc6ee Mon Sep 17 00:00:00 2001 From: uz Date: Thu, 12 Jul 2012 21:11:21 +0000 Subject: [PATCH] Added a test program to test the 16 bit shift ops. git-svn-id: svn://svn.cc65.org/cc65/trunk@5782 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- testcode/lib/shift-test.c | 136 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 testcode/lib/shift-test.c diff --git a/testcode/lib/shift-test.c b/testcode/lib/shift-test.c new file mode 100644 index 000000000..5712f4534 --- /dev/null +++ b/testcode/lib/shift-test.c @@ -0,0 +1,136 @@ +#include +#include + + + + +static unsigned UnsignedShiftLeft1 (unsigned Val) +/* Shift an unsigned left by 1 */ +{ + __AX__ = Val; + asm ("stx tmp1"); + asm ("asl a"); + asm ("rol tmp1"); + asm ("ldx tmp1"); + return __AX__; +} + + + +static unsigned UnsignedShiftRight1 (unsigned Val) +/* Shift an unsigned right by 1 */ +{ + __AX__ = Val; + asm ("stx tmp1"); + asm ("lsr tmp1"); + asm ("ror a"); + asm ("ldx tmp1"); + return __AX__; +} + + + +static int SignedShiftRight1 (int Val) +/* Shift a signed right by 1 */ +{ + __AX__ = Val; + asm ("stx tmp1"); + asm ("cpx #$80"); + asm ("ror tmp1"); + asm ("ror a"); + asm ("ldx tmp1"); + return __AX__; +} + + + +static void TestUnsignedLeftShift (void) +/* Test left shift. This is identical for signed and unsigned ints */ +{ + unsigned L, R, V; + printf ("Testing unsigned left shift:\n"); + L = 0; + do { + V = L; + for (R = 0; R < 16; ++R) { + /* Check it */ + if ((L << R) != V) { + fprintf (stderr, + "Failed: %u << %u != %u (%u)\n", + L, R, V, L << R); + exit (1); + } + V = UnsignedShiftLeft1 (V); + } + if ((L & 0xFF) == 0) { + printf ("%04X ", L); + } + } while (++L != 0); + printf ("\n"); +} + + + +static void TestUnsignedRightShift (void) +/* Test unsigned right shift. */ +{ + unsigned L, R, V; + printf ("Testing unsigned right shift:\n"); + L = 0; + do { + V = L; + for (R = 0; R < 16; ++R) { + /* Check it */ + if ((L >> R) != V) { + fprintf (stderr, + "Failed: %u >> %u != %u (%u)\n", + L, R, V, L >> R); + exit (1); + } + V = UnsignedShiftRight1 (V); + } + if ((L & 0xFF) == 0) { + printf ("%04X ", L); + } + } while (++L != 0); + printf ("\n"); +} + + + +static void TestSignedRightShift (void) +/* Test signed right shift. */ +{ + int L, R, V; + printf ("Testing signed right shift:\n"); + L = 0; + do { + V = L; + for (R = 0; R < 16; ++R) { + /* Check it */ + if ((L >> R) != V) { + fprintf (stderr, + "Failed: %d >> %d != %d (%d)\n", + L, R, V, L >> R); + exit (1); + } + V = SignedShiftRight1 (V); + } + if ((L & 0xFF) == 0) { + printf ("%04X ", L); + } + } while (++L != 0); + printf ("\n"); +} + + + +int main (void) +{ + TestUnsignedLeftShift (); + TestUnsignedRightShift (); + TestSignedRightShift (); + printf ("\nOk!\n"); + + return 0; +} -- 2.39.5