summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/torture/builtin-ldexp-1.c
blob: 94560a8abb42c0ffdf650643d383f0cfc7991144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Copyright (C) 2007  Free Software Foundation.

   Verify that built-in folding of ldexp et al. is correctly performed
   by the compiler.

   Origin: Kaveh R. Ghazi,  February 17, 2007.  */

/* { dg-do link } */
/* { dg-options "-fno-finite-math-only" { target sh*-*-* } } */

extern void link_error(int);

/* Return TRUE if the sign of X != sign of Y.  This is important when
   comparing signed zeros.  */
#define CKSGN_F(X,Y) \
  (__builtin_copysignf(1.0F,(X)) != __builtin_copysignf(1.0F,(Y)))
#define CKSGN(X,Y) \
  (__builtin_copysign(1.0,(X)) != __builtin_copysign(1.0,(Y)))
#define CKSGN_L(X,Y) \
  (__builtin_copysignl(1.0L,(X)) != __builtin_copysignl(1.0L,(Y)))

/* Test that FUNC(ARG1,ARG2) == RES.  Check the sign for -0.0.  */
#define TESTIT(FUNC,ARG1,ARG2,RES) do { \
  if (__builtin_##FUNC##f(ARG1##f,ARG2) != RES##f \
      || CKSGN_F(__builtin_##FUNC##f(ARG1##f,ARG2),RES##f)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC(ARG1,ARG2) != RES \
      || CKSGN(__builtin_##FUNC(ARG1,ARG2),RES)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC##l(ARG1##l,ARG2) != RES##l \
      || CKSGN_L(__builtin_##FUNC##l(ARG1##l,ARG2),RES##l)) \
    link_error(__LINE__); \
  } while (0)

/* Test that (long)FUNC(ARG1,ARG2) == (long)RES.  The cast is
   necessary when RES is not a constant.  */
#define TESTIT2(FUNC,ARG1,ARG2,RES) do { \
  if ((long)__builtin_##FUNC##f(ARG1##f,ARG2) != (long)RES##f) \
    link_error(__LINE__); \
  if ((long)__builtin_##FUNC(ARG1,ARG2) != (long)RES) \
    link_error(__LINE__); \
  if ((long)__builtin_##FUNC##l(ARG1##l,ARG2) != (long)RES##l) \
    link_error(__LINE__); \
  } while (0)

/* Test that FUNCRES(FUNC(NEG FUNCARG(ARGARG),ARG2)) is false.  Check
   the sign as well.  */
#ifndef __SPU__
#define TESTIT3(FUNC,NEG,FUNCARG,ARGARG,ARG2,FUNCRES) do { \
  if (!__builtin_##FUNCRES##f(__builtin_##FUNC##f(NEG __builtin_##FUNCARG##f(ARGARG),ARG2)) \
      || CKSGN_F(__builtin_##FUNC##f(NEG __builtin_##FUNCARG##f(ARGARG),ARG2), NEG __builtin_##FUNCARG##f(ARGARG))) \
    link_error(__LINE__); \
  if (!__builtin_##FUNCRES(__builtin_##FUNC(NEG __builtin_##FUNCARG(ARGARG),ARG2)) \
      || CKSGN(__builtin_##FUNC(NEG __builtin_##FUNCARG(ARGARG),ARG2), NEG __builtin_##FUNCARG(ARGARG))) \
    link_error(__LINE__); \
  if (!__builtin_##FUNCRES##l(__builtin_##FUNC##l(NEG __builtin_##FUNCARG##l(ARGARG),ARG2)) \
      || CKSGN_L(__builtin_##FUNC##l(NEG __builtin_##FUNCARG##l(ARGARG),ARG2), NEG __builtin_##FUNCARG##l(ARGARG))) \
    link_error(__LINE__); \
  } while (0)
#else
#define TESTIT3(FUNC,NEG,FUNCARG,ARGARG,ARG2,FUNCRES) do { \
  /* SPU single-precision floating point format does not support Inf or Nan.  */ \
  if (!__builtin_##FUNCRES(__builtin_##FUNC(NEG __builtin_##FUNCARG(ARGARG),ARG2)) \
      || CKSGN(__builtin_##FUNC(NEG __builtin_##FUNCARG(ARGARG),ARG2), NEG __builtin_##FUNCARG(ARGARG))) \
    link_error(__LINE__); \
  if (!__builtin_##FUNCRES##l(__builtin_##FUNC##l(NEG __builtin_##FUNCARG##l(ARGARG),ARG2)) \
      || CKSGN_L(__builtin_##FUNC##l(NEG __builtin_##FUNCARG##l(ARGARG),ARG2), NEG __builtin_##FUNCARG##l(ARGARG))) \
    link_error(__LINE__); \
  } while (0)
#endif

/* Using foo==MIN/MAX float values, test that FUNC(foo,EXP) == foo*exp2(EXP),
   and also that FUNC(foo,-EXP) == foo*exp2(-EXP).  */
#define TESTIT4(FUNC,EXP) do { \
  if (__builtin_##FUNC##f(__FLT_MIN__,EXP) != __FLT_MIN__*__builtin_exp2f(EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC##f(-__FLT_MIN__,EXP) != -__FLT_MIN__*__builtin_exp2f(EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC(__DBL_MIN__,EXP) != __DBL_MIN__*__builtin_exp2(EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC(-__DBL_MIN__,EXP) != -__DBL_MIN__*__builtin_exp2(EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC##l(__LDBL_MIN__,EXP) != __LDBL_MIN__*__builtin_exp2l(EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC##l(-__LDBL_MIN__,EXP) != -__LDBL_MIN__*__builtin_exp2l(EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC##f(__FLT_MAX__,-EXP) != __FLT_MAX__*__builtin_exp2f(-EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC##f(-__FLT_MAX__,-EXP) != -__FLT_MAX__*__builtin_exp2f(-EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC(__DBL_MAX__,-EXP) != __DBL_MAX__*__builtin_exp2(-EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC(-__DBL_MAX__,-EXP) != -__DBL_MAX__*__builtin_exp2(-EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC##l(__LDBL_MAX__,-EXP) != __LDBL_MAX__*__builtin_exp2l(-EXP)) \
    link_error(__LINE__); \
  if (__builtin_##FUNC##l(-__LDBL_MAX__,-EXP) != -__LDBL_MAX__*__builtin_exp2l(-EXP)) \
    link_error(__LINE__); \
  } while (0)

void __attribute__ ((__noinline__))
foo(float xf, double x, long double xl, int i, long l)
{
  /* f(0.0, i) -> 0.0 and f(-0.0, i) -> -0.0.  */
  TESTIT (ldexp, 0.0, i, 0.0);
  TESTIT (ldexp, -0.0, i, -0.0);
  TESTIT (scalbn, 0.0, i, 0.0);
  TESTIT (scalbn, -0.0, i, -0.0);
  TESTIT (scalbln, 0.0, l, 0.0);
  TESTIT (scalbln, -0.0, l, -0.0);
  
  /* f(x,0) -> x.  */
  TESTIT2 (ldexp, x, 0, x);
  TESTIT2 (scalbn, x, 0, x);
  TESTIT2 (scalbln, x, 0, x);

  /* f(Inf,i) -> Inf and f(NaN,i) -> NaN.  */
  TESTIT3 (ldexp, , inf, , i, isinf);
  TESTIT3 (ldexp, -, inf, , i, isinf);
  TESTIT3 (ldexp, , nan, "", i, isnan);
  TESTIT3 (ldexp, -, nan, "", i, isnan);

  TESTIT3 (scalbn, , inf, , i, isinf);
  TESTIT3 (scalbn, -, inf, , i, isinf);
  TESTIT3 (scalbn, , nan, "", i, isnan);
  TESTIT3 (scalbn, -, nan, "", i, isnan);

  TESTIT3 (scalbln, , inf, , i, isinf);
  TESTIT3 (scalbln, -, inf, , i, isinf);
  TESTIT3 (scalbln, , nan, "", i, isnan);
  TESTIT3 (scalbln, -, nan, "", i, isnan);

  /* Evaluate when both arguments are constant.  */
  TESTIT (ldexp, 5.0, 3, 40.0);
  TESTIT (ldexp, -5.0, 3, -40.0);
  TESTIT (ldexp, 5.0, -3, 0.625);
  TESTIT (ldexp, -5.0, -3, -0.625);

  TESTIT (ldexp, 1000.0, 5, 32000.0);
  TESTIT (ldexp, -1000.0, 5, -32000.0);
  TESTIT (ldexp, 1000.0, -5, 31.25);
  TESTIT (ldexp, -1000.0, -5, -31.25);

  /* f(x,N) -> x*exp2(N), using MIN/MAX constants for x and constant N.  */
  TESTIT4 (ldexp, 1);
  TESTIT4 (ldexp, 2);
  TESTIT4 (ldexp, 3);
  TESTIT4 (ldexp, 5);
  TESTIT4 (ldexp, 9);
  TESTIT4 (ldexp, 10);
  TESTIT4 (ldexp, 12);
  TESTIT4 (ldexp, 18);
  TESTIT4 (ldexp, 25);
  TESTIT4 (ldexp, 50);
  TESTIT4 (ldexp, 75);
  TESTIT4 (ldexp, 100);
  TESTIT4 (ldexp, 123);

  /* These are folded when float radix is two.  */
#if __FLT_RADIX__ == 2
  TESTIT (scalbn, 5.0, 3, 40.0);
  TESTIT (scalbn, -5.0, 3, -40.0);
  TESTIT (scalbn, 5.0, -3, 0.625);
  TESTIT (scalbn, -5.0, -3, -0.625);

  TESTIT (scalbn, 1000.0, 5, 32000.0);
  TESTIT (scalbn, -1000.0, 5, -32000.0);
  TESTIT (scalbn, 1000.0, -5, 31.25);
  TESTIT (scalbn, -1000.0, -5, -31.25);

  TESTIT4 (scalbn, 1);
  TESTIT4 (scalbn, 2);
  TESTIT4 (scalbn, 3);
  TESTIT4 (scalbn, 5);
  TESTIT4 (scalbn, 9);
  TESTIT4 (scalbn, 10);
  TESTIT4 (scalbn, 12);
  TESTIT4 (scalbn, 18);
  TESTIT4 (scalbn, 25);
  TESTIT4 (scalbn, 50);
  TESTIT4 (scalbn, 75);
  TESTIT4 (scalbn, 100);
  TESTIT4 (scalbn, 123);

  TESTIT (scalbln, 5.0, 3, 40.0);
  TESTIT (scalbln, -5.0, 3, -40.0);
  TESTIT (scalbln, 5.0, -3, 0.625);
  TESTIT (scalbln, -5.0, -3, -0.625);

  TESTIT (scalbln, 1000.0, 5, 32000.0);
  TESTIT (scalbln, -1000.0, 5, -32000.0);
  TESTIT (scalbln, 1000.0, -5, 31.25);
  TESTIT (scalbln, -1000.0, -5, -31.25);

  TESTIT4 (scalbln, 1);
  TESTIT4 (scalbln, 2);
  TESTIT4 (scalbln, 3);
  TESTIT4 (scalbln, 5);
  TESTIT4 (scalbln, 9);
  TESTIT4 (scalbln, 10);
  TESTIT4 (scalbln, 12);
  TESTIT4 (scalbln, 18);
  TESTIT4 (scalbln, 25);
  TESTIT4 (scalbln, 50);
  TESTIT4 (scalbln, 75);
  TESTIT4 (scalbln, 100);
  TESTIT4 (scalbln, 123);
#endif
}

int main()
{
  foo (0, 0, 0, 0, 0);
  
  return 0;
}