Imported Upstream version 2.9.0
[debian/cc1111] / 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 #  define mullong(a,b) mullong_wrapper(a,b)
12 #  if defined(type_c) && !defined(WORDS_BIGENDIAN)
13 #    define _SDCC_NO_ASM_LIB_FUNCS 1
14 #    define __near
15 #    define long int
16 #    include "device/lib/_mullong.c"
17 #  endif
18 #else
19 #  if defined(type_c)
20 #    define _SDCC_NO_ASM_LIB_FUNCS 1
21 #  endif
22 #  include "device/lib/_mullong.c"
23 #  define mullong _mullong
24 #endif
25
26 /* gcc 2.95.2 on usf-cf-x86-linux-1 (debian 2.2) has a bug with
27  * packing structs
28  */
29 #if defined(PORT_HOST)
30
31 #define TYPE_TARGET_CHAR  TYPE_BYTE
32 #define TYPE_TARGET_INT   TYPE_WORD
33 #define TYPE_TARGET_LONG  TYPE_DWORD
34 #define TYPE_TARGET_UCHAR TYPE_UBYTE
35 #define TYPE_TARGET_UINT  TYPE_UWORD
36 #define TYPE_TARGET_ULONG TYPE_UDWORD
37
38 #if defined(type_c) && !defined(WORDS_BIGENDIAN)
39 struct
40 {
41   char c1;
42   short i;
43   char c2;
44 } pack_test;
45
46 TYPE_TARGET_LONG
47 mullong_wrapper (TYPE_TARGET_LONG a, TYPE_TARGET_LONG b)
48 {
49   if (sizeof(pack_test) == 4)
50     /* length of struct ok: use SDCC library */
51     return _mullong (a, b);
52   else
53     {
54       /* buggy gcc: use generic multiplication */
55       return a * b;
56     }
57 }
58
59 #else
60
61 TYPE_TARGET_LONG
62 mullong_wrapper (TYPE_TARGET_LONG a, TYPE_TARGET_LONG b)
63 {
64     return a * b;
65 }
66
67 #endif
68
69 #endif
70
71 void
72 testlibmullong(void)
73 {
74   ASSERT(mullong (         0,          0) ==          0);
75   ASSERT(mullong (     0x100,      0x100) ==    0x10000);
76   ASSERT(mullong (0x01020304,          3) == 0x0306090c);
77   ASSERT(mullong (         3, 0x01020304) == 0x0306090c);
78   ASSERT(mullong (0x000000ff,          2) == 0x000001fe);
79   ASSERT(mullong (         2, 0x000000ff) == 0x000001fe);
80   ASSERT(mullong (0x00007fff,          4) == 0x0001fffc);
81   ASSERT(mullong (         4, 0x00007fff) == 0x0001fffc);
82   ASSERT(mullong (0x003fffff,          8) == 0x01fffff8);
83   ASSERT(mullong (         8, 0x003fffff) == 0x01fffff8);
84
85   ASSERT(mullong (      0x33,       0x34) == 0x00000a5c);
86   ASSERT(mullong (      0x34,       0x33) == 0x00000a5c);
87   ASSERT(mullong (    0x3334,     0x3536) == 0x0aa490f8);
88   ASSERT(mullong (    0x3536,     0x3334) == 0x0aa490f8);
89   ASSERT(mullong (  0x333435,   0x363738) == 0x0e98ce98);
90   ASSERT(mullong (  0x363738,   0x333435) == 0x0e98ce98);
91   ASSERT(mullong (0x33343536, 0x3738393a) == 0x777d143c);
92   ASSERT(mullong (0x3738393a, 0x33343536) == 0x777d143c);
93
94   ASSERT(mullong (      0xff,       0xfe) == 0x0000fd02);
95   ASSERT(mullong (      0xfe,       0xff) == 0x0000fd02);
96   ASSERT(mullong (    0xfffe,     0xfdfc) == 0xfdfa0408);
97   ASSERT(mullong (    0xfdfc,     0xfffe) == 0xfdfa0408);
98   ASSERT(mullong (  0xfffefd,   0xfcfbfa) == 0xfa0d1212);
99   ASSERT(mullong (  0xfcfbfa,   0xfffefd) == 0xfa0d1212);
100   ASSERT(mullong (0xfffefdfc, 0xfbfaf9f8) == 0x20282820);
101   ASSERT(mullong (0xfbfaf9f8, 0xfffefdfc) == 0x20282820);
102
103   ASSERT(mullong (0xff000000, 0xff000000) ==          0);
104   ASSERT(mullong (0xffff0000, 0xffff0000) ==          0);
105   ASSERT(mullong (0xfffffe00, 0xfffffd00) == 0x00060000);
106   ASSERT(mullong (0xfffffd00, 0xfffffe00) == 0x00060000);
107   ASSERT(mullong (0xfffffefd, 0xfffffcfb) == 0x00030e0f);
108   ASSERT(mullong (0xfffffcfb, 0xfffffefd) == 0x00030e0f);
109
110   ASSERT(mullong (0xffffffff, 0xffffffff) ==          1);
111 }