1 /* Test for builtin abs, labs, llabs, imaxabs.  Test for __builtin versions. */
2 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
3 
4 #include <limits.h>
5 typedef __INTMAX_TYPE__ intmax_t;
6 #define INTMAX_MAX __INTMAX_MAX__
7 
8 extern void abort (void);
9 extern void link_error (void);
10 
11 void
main_test(void)12 main_test (void)
13 {
14   /* For each type, test both runtime and compile time (constant folding)
15      optimization.  */
16   volatile int i0 = 0, i1 = 1, im1 = -1, imin = -INT_MAX, imax = INT_MAX;
17   volatile long l0 = 0L, l1 = 1L, lm1 = -1L, lmin = -LONG_MAX, lmax = LONG_MAX;
18   volatile long long ll0 = 0LL, ll1 = 1LL, llm1 = -1LL;
19   volatile long long llmin = -__LONG_LONG_MAX__, llmax = __LONG_LONG_MAX__;
20   volatile intmax_t imax0 = 0, imax1 = 1, imaxm1 = -1;
21   volatile intmax_t imaxmin = -INTMAX_MAX, imaxmax = INTMAX_MAX;
22   if (__builtin_abs (i0) != 0)
23     abort ();
24   if (__builtin_abs (0) != 0)
25     link_error ();
26   if (__builtin_abs (i1) != 1)
27     abort ();
28   if (__builtin_abs (1) != 1)
29     link_error ();
30   if (__builtin_abs (im1) != 1)
31     abort ();
32   if (__builtin_abs (-1) != 1)
33     link_error ();
34   if (__builtin_abs (imin) != INT_MAX)
35     abort ();
36   if (__builtin_abs (-INT_MAX) != INT_MAX)
37     link_error ();
38   if (__builtin_abs (imax) != INT_MAX)
39     abort ();
40   if (__builtin_abs (INT_MAX) != INT_MAX)
41     link_error ();
42   if (__builtin_labs (l0) != 0L)
43     abort ();
44   if (__builtin_labs (0L) != 0L)
45     link_error ();
46   if (__builtin_labs (l1) != 1L)
47     abort ();
48   if (__builtin_labs (1L) != 1L)
49     link_error ();
50   if (__builtin_labs (lm1) != 1L)
51     abort ();
52   if (__builtin_labs (-1L) != 1L)
53     link_error ();
54   if (__builtin_labs (lmin) != LONG_MAX)
55     abort ();
56   if (__builtin_labs (-LONG_MAX) != LONG_MAX)
57     link_error ();
58   if (__builtin_labs (lmax) != LONG_MAX)
59     abort ();
60   if (__builtin_labs (LONG_MAX) != LONG_MAX)
61     link_error ();
62   if (__builtin_llabs (ll0) != 0LL)
63     abort ();
64   if (__builtin_llabs (0LL) != 0LL)
65     link_error ();
66   if (__builtin_llabs (ll1) != 1LL)
67     abort ();
68   if (__builtin_llabs (1LL) != 1LL)
69     link_error ();
70   if (__builtin_llabs (llm1) != 1LL)
71     abort ();
72   if (__builtin_llabs (-1LL) != 1LL)
73     link_error ();
74   if (__builtin_llabs (llmin) != __LONG_LONG_MAX__)
75     abort ();
76   if (__builtin_llabs (-__LONG_LONG_MAX__) != __LONG_LONG_MAX__)
77     link_error ();
78   if (__builtin_llabs (llmax) != __LONG_LONG_MAX__)
79     abort ();
80   if (__builtin_llabs (__LONG_LONG_MAX__) != __LONG_LONG_MAX__)
81     link_error ();
82   if (__builtin_imaxabs (imax0) != 0)
83     abort ();
84   if (__builtin_imaxabs (0) != 0)
85     link_error ();
86   if (__builtin_imaxabs (imax1) != 1)
87     abort ();
88   if (__builtin_imaxabs (1) != 1)
89     link_error ();
90   if (__builtin_imaxabs (imaxm1) != 1)
91     abort ();
92   if (__builtin_imaxabs (-1) != 1)
93     link_error ();
94   if (__builtin_imaxabs (imaxmin) != INTMAX_MAX)
95     abort ();
96   if (__builtin_imaxabs (-INTMAX_MAX) != INTMAX_MAX)
97     link_error ();
98   if (__builtin_imaxabs (imaxmax) != INTMAX_MAX)
99     abort ();
100   if (__builtin_imaxabs (INTMAX_MAX) != INTMAX_MAX)
101     link_error ();
102 }
103