added workaround for packing bug in gcc 21.95.2
[fw/sdcc] / support / regression / tests / libmullong.c
1 /* Test _mullong.c from library
2
3     type: asm, c
4  */
5 #include <testfwk.h>
6
7 #define type_{type}
8
9 #if defined(PORT_HOST)
10 #  include "../../../../../sdccconf.h"
11 #  if defined(type_c) && !defined(WORDS_BIGENDIAN)
12 #    define _SDCC_NO_ASM_LIB_FUNCS 1
13 #    define near
14 #    define long int
15 #    include "../../../../../device/lib/_mullong.c"
16 #    define mullong(a,b) mullong_wrapper(a,b)
17 #  else
18 #    define mullong(a,b) (a*b)
19 #  endif
20 #else
21 #  if defined(type_c)
22 #    define _SDCC_NO_ASM_LIB_FUNCS 1
23 #  endif
24 #  include "../../../../../device/lib/_mullong.c"
25 #  define mullong _mullong
26 #endif
27
28 /* gcc 2.95.2 on usf-cf-x86-linux-1 (debian 2.2) has a bug with
29  * packing structs
30  */
31 #if defined(PORT_HOST) && defined(type_c) && !defined(WORDS_BIGENDIAN)
32 struct
33 {
34   char c1;
35   short i;
36   char c2;
37 } pack_test;
38         
39 long
40 mullong_wrapper (long a, long b)
41 {
42   if (sizeof(pack_test) == 4)
43     /* length of struct ok: use SDCC library */
44     return _mullong (a, b);
45   else
46     /* buggy gcc: use generic multiplication */
47     return a * b;
48 }
49 #endif
50
51 void
52 testlibmullong(void)
53 {
54   ASSERT(mullong (         0,          0) ==          0);
55   ASSERT(mullong (     0x100,      0x100) ==    0x10000);
56   ASSERT(mullong (0x01020304,          3) == 0x0306090c);
57   ASSERT(mullong (         3, 0x01020304) == 0x0306090c);
58   ASSERT(mullong (0x000000ff,          2) == 0x000001fe);
59   ASSERT(mullong (         2, 0x000000ff) == 0x000001fe);
60   ASSERT(mullong (0x00007fff,          4) == 0x0001fffc);
61   ASSERT(mullong (         4, 0x00007fff) == 0x0001fffc);
62   ASSERT(mullong (0x003fffff,          8) == 0x01fffff8);
63   ASSERT(mullong (         8, 0x003fffff) == 0x01fffff8);
64
65   ASSERT(mullong (      0x33,       0x34) == 0x00000a5c);
66   ASSERT(mullong (      0x34,       0x33) == 0x00000a5c);
67   ASSERT(mullong (    0x3334,     0x3536) == 0x0aa490f8);
68   ASSERT(mullong (    0x3536,     0x3334) == 0x0aa490f8);
69   ASSERT(mullong (  0x333435,   0x363738) == 0x0e98ce98);
70   ASSERT(mullong (  0x363738,   0x333435) == 0x0e98ce98);
71   ASSERT(mullong (0x33343536, 0x3738393a) == 0x777d143c);
72   ASSERT(mullong (0x3738393a, 0x33343536) == 0x777d143c);
73
74   ASSERT(mullong (      0xff,       0xfe) == 0x0000fd02);
75   ASSERT(mullong (      0xfe,       0xff) == 0x0000fd02);
76   ASSERT(mullong (    0xfffe,     0xfdfc) == 0xfdfa0408);
77   ASSERT(mullong (    0xfdfc,     0xfffe) == 0xfdfa0408);
78   ASSERT(mullong (  0xfffefd,   0xfcfbfa) == 0xfa0d1212);
79   ASSERT(mullong (  0xfcfbfa,   0xfffefd) == 0xfa0d1212);
80   ASSERT(mullong (0xfffefdfc, 0xfbfaf9f8) == 0x20282820);
81   ASSERT(mullong (0xfbfaf9f8, 0xfffefdfc) == 0x20282820);
82
83   ASSERT(mullong (0xff000000, 0xff000000) ==          0);
84   ASSERT(mullong (0xffff0000, 0xffff0000) ==          0);
85   ASSERT(mullong (0xfffffe00, 0xfffffd00) == 0x00060000);
86   ASSERT(mullong (0xfffffd00, 0xfffffe00) == 0x00060000);
87   ASSERT(mullong (0xfffffefd, 0xfffffcfb) == 0x00030e0f);
88   ASSERT(mullong (0xfffffcfb, 0xfffffefd) == 0x00030e0f);
89
90   ASSERT(mullong (0xffffffff, 0xffffffff) ==          1);
91 }