1 /* Test floating-point conversions.  */
2 /* Origin: Joseph Myers <joseph@codesourcery.com> */
3 
4 #include <limits.h>
5 extern void abort (void);
6 extern void exit (int);
7 
8 /* Not all platforms support TImode integers; logic as in
9    gcc.dg/titype-1.c.  */
10 #if (defined(__LP64__) && !defined(__hppa__)) || defined(_WIN64) || defined(__SPU__)
11 typedef int TItype __attribute__ ((mode (TI)));
12 typedef unsigned int UTItype __attribute__ ((mode (TI)));
13 #else
14 typedef long TItype;
15 typedef unsigned long UTItype;
16 #endif
17 
18 /* TEST_I_F(I, U, F, P, M) tests conversions between the pair of
19    signed and unsigned integer types I and U and the floating-point
20    type F, where P is the binary precision of the floating point type
21    and M is the MAX_EXP value for that type (so 2^M overflows, 2^(M-1)
22    does not).  We test conversions of the values 0, 1, 0x7...f,
23    0x8...0, 0xf...f.  We also test conversions of values half way
24    between two representable values (rounding both ways), just above
25    half way, and just below half way.  */
26 #define TEST_I_F(I, U, F, P, M)					\
27 do {								\
28   TEST_I_F_VAL (I, F, (I)0, 1);					\
29   TEST_I_F_VAL (I, F, (I)1, 1);					\
30   TEST_I_F_VAL (I, F, (I)(((U)~(U)0) >> 1), P_OK1 (P, I));	\
31   TEST_I_F_VAL (I, F, (I)(U)~(((U)~(U)0) >> 1), M_OK1 (M, I));	\
32   TEST_I_F_VAL (I, F, (I)(U)~(U)0, 1);				\
33   TEST_I_F_VAL (I, F, HVAL0S (P, I), P_OK (P, I));		\
34   TEST_I_F_VAL (I, F, HVAL0S (P, I) + 1, P_OK (P, I));		\
35   TEST_I_F_VAL (I, F, HVAL0S (P, I) - 1, P_OK (P, I));		\
36   TEST_I_F_VAL (I, F, HVAL1S (P, I), P_OK (P, I));		\
37   TEST_I_F_VAL (I, F, HVAL1S (P, I) + 1, P_OK (P, I));		\
38   TEST_I_F_VAL (I, F, HVAL1S (P, I) - 1, P_OK (P, I));		\
39   TEST_I_F_VAL (I, F, -HVAL0S (P, I), P_OK (P, I));		\
40   TEST_I_F_VAL (I, F, -HVAL0S (P, I) + 1, P_OK (P, I));		\
41   TEST_I_F_VAL (I, F, -HVAL0S (P, I) - 1, P_OK (P, I));		\
42   TEST_I_F_VAL (I, F, -HVAL1S (P, I), P_OK (P, I));		\
43   TEST_I_F_VAL (I, F, -HVAL1S (P, I) + 1, P_OK (P, I));		\
44   TEST_I_F_VAL (I, F, -HVAL1S (P, I) - 1, P_OK (P, I));		\
45   TEST_I_F_VAL (U, F, (U)0, 1);					\
46   TEST_I_F_VAL (U, F, (U)1, 1);					\
47   TEST_I_F_VAL (U, F, (U)(((U)~(U)0) >> 1), P_OK1 (P, U));	\
48   TEST_I_F_VAL (U, F, (U)~(((U)~(U)0) >> 1), M_OK1 (M, U));	\
49   TEST_I_F_VAL (U, F, (U)~(U)0, P_OK (P, U));			\
50   TEST_I_F_VAL (U, F, HVAL0U (P, U), P_OK (P, U));		\
51   TEST_I_F_VAL (U, F, HVAL0U (P, U) + 1, P_OK (P, U));		\
52   TEST_I_F_VAL (U, F, HVAL0U (P, U) - 1, P_OK (P, U));		\
53   TEST_I_F_VAL (U, F, HVAL1U (P, U), P_OK (P, U));		\
54   TEST_I_F_VAL (U, F, HVAL1U (P, U) + 1, P_OK (P, U));		\
55   TEST_I_F_VAL (U, F, HVAL1U (P, U) - 1, P_OK (P, U));		\
56   TEST_I_F_VAL (I, F, WVAL0S (I), M_OK2 (M, U));		\
57   TEST_I_F_VAL (I, F, -WVAL0S (I), M_OK2 (M, U));		\
58 } while (0)
59 
60 #define P_OK(P, T) ((P) >= sizeof(T) * CHAR_BIT)
61 #define P_OK1(P, T) ((P) >= sizeof(T) * CHAR_BIT - 1)
62 #define M_OK1(M, T) ((M) > sizeof(T) * CHAR_BIT - 1)
63 #define M_OK2(M, T) ((M) > sizeof(T) * CHAR_BIT / 2 - 1)
64 #define HVAL0U(P, U) (U)(P_OK (P, U)					 \
65 			 ? (U)1						 \
66 			 : (((U)1 << (sizeof(U) * CHAR_BIT - 1))	 \
67 			    + ((U)1 << (sizeof(U) * CHAR_BIT - 1 - P))))
68 #define HVAL1U(P, U) (U)(P_OK (P, U)					 \
69 			 ? (U)1						 \
70 			 : (((U)1 << (sizeof(U) * CHAR_BIT - 1))	 \
71 			    + ((U)3 << (sizeof(U) * CHAR_BIT - 1 - P))))
72 #define HVAL0S(P, S) (S)(P_OK1 (P, S)					 \
73 			 ? (S)1						 \
74 			 : (((S)1 << (sizeof(S) * CHAR_BIT - 2))	 \
75 			    + ((S)1 << (sizeof(S) * CHAR_BIT - 2 - P))))
76 #define HVAL1S(P, S) (S)(P_OK1 (P, S)					 \
77 			 ? (S)1						 \
78 			 : (((S)1 << (sizeof(S) * CHAR_BIT - 2))	 \
79 			    + ((S)3 << (sizeof(S) * CHAR_BIT - 2 - P))))
80 #define WVAL0S(S) (S)((S)1 << (sizeof(S) * CHAR_BIT / 2 - 1))
81 
82 #define TEST_I_F_VAL(IT, FT, VAL, PREC_OK)		\
83 do {							\
84   static volatile IT ivin, ivout;			\
85   static volatile FT fv1, fv2;				\
86   ivin = (VAL);						\
87   fv1 = (VAL);						\
88   fv2 = ivin;						\
89   ivout = fv2;						\
90   if (ivin != (VAL)					\
91       || ((PREC_OK) && ivout != ivin)			\
92       || ((PREC_OK) && ivout != (VAL))			\
93       || fv1 != (FT) (VAL) || fv2 != (FT) (VAL)		\
94       || fv1 != fv2)					\
95     abort ();						\
96 } while (0)
97