| 1 |
/* Copyright (C) 2007 Free Software Foundation. |
| 2 |
|
| 3 |
Verify that built-in folding of frexp is correctly performed by the |
| 4 |
compiler. |
| 5 |
|
| 6 |
Origin: Kaveh R. Ghazi, February 21, 2007. */ |
| 7 |
|
| 8 |
/* { dg-do link } */ |
| 9 |
/* { dg-options "-fno-finite-math-only" { target sh*-*-* } } */ |
| 10 |
/* In order to fold algebraic exprs below, targets with "composite" |
| 11 |
floating point formats need -funsafe-math-optimizations. */ |
| 12 |
/* { dg-require-effective-target inf } */ |
| 13 |
/* { dg-options "-funsafe-math-optimizations" { target powerpc*-*-* } } */ |
| 14 |
/* { dg-additional-options "-Wmaybe-uninitialized" } */ |
| 15 |
|
| 16 |
extern void link_error(int); |
| 17 |
|
| 18 |
/* Return TRUE if the sign of X != sign of Y. This is important when |
| 19 |
comparing signed zeros. */ |
| 20 |
#define CKSGN_F(X,Y) \ |
| 21 |
(__builtin_copysignf(1.0F,(X)) != __builtin_copysignf(1.0F,(Y))) |
| 22 |
#define CKSGN(X,Y) \ |
| 23 |
(__builtin_copysign(1.0,(X)) != __builtin_copysign(1.0,(Y))) |
| 24 |
#define CKSGN_L(X,Y) \ |
| 25 |
(__builtin_copysignl(1.0L,(X)) != __builtin_copysignl(1.0L,(Y))) |
| 26 |
|
| 27 |
/* We can only check the exponent when optimizing since we rely on |
| 28 |
other optimizations to propagate the value. TRUE means an error |
| 29 |
occurred. */ |
| 30 |
#ifdef __OPTIMIZE__ |
| 31 |
#define CKEXP(X,Y) X != Y |
| 32 |
#else |
| 33 |
#define CKEXP(X,Y) 0 |
| 34 |
#endif |
| 35 |
|
| 36 |
/* Test that frexp(ARG,&i) == RES && i == EXP. Check the sign in |
| 37 |
case we get -0.0. */ |
| 38 |
#define TESTIT_FREXP(ARG,RES,EXP) do { \ |
| 39 |
int i = 12345; \ |
| 40 |
if (__builtin_frexpf(ARG##f,&i) != RES##f \ |
| 41 |
|| CKEXP(i,EXP) \ |
| 42 |
|| CKSGN_F(__builtin_frexpf(ARG##f,&i),RES##f)) \ |
| 43 |
link_error(__LINE__); \ |
| 44 |
i = 12345; \ |
| 45 |
if (__builtin_frexp(ARG,&i) != RES \ |
| 46 |
|| CKEXP(i,EXP) \ |
| 47 |
|| CKSGN(__builtin_frexp(ARG,&i),RES)) \ |
| 48 |
link_error(__LINE__); \ |
| 49 |
i = 12345; \ |
| 50 |
if (__builtin_frexpl(ARG##l,&i) != RES##l \ |
| 51 |
|| CKEXP(i,EXP) \ |
| 52 |
|| CKSGN_L(__builtin_frexpl(ARG##l,&i),RES##l)) \ |
| 53 |
link_error(__LINE__); \ |
| 54 |
} while (0) |
| 55 |
|
| 56 |
int __attribute__ ((__noipa__)) |
| 57 |
bar (int x) |
| 58 |
{ |
| 59 |
(void) x; |
| 60 |
return 42; |
| 61 |
} |
| 62 |
|
| 63 |
/* Test that FUNCRES(frexp(NEG FUNCARG(ARGARG),&i)) is false. Check |
| 64 |
the sign as well. Ensure side-effects are evaluated in the second |
| 65 |
frexp argument. */ |
| 66 |
#define TESTIT_FREXP2(NEG,FUNCARG,ARGARG,FUNCRES) do { \ |
| 67 |
int i, j = 5; \ |
| 68 |
if (!__builtin_##FUNCRES##f(__builtin_frexpf(NEG __builtin_##FUNCARG##f(ARGARG),&i)) \ |
| 69 |
|| CKSGN_F(__builtin_frexpf(NEG __builtin_##FUNCARG##f(ARGARG),(j++,&i)), NEG __builtin_##FUNCARG##f(ARGARG)) \ |
| 70 |
|| CKEXP(j,6)) \ |
| 71 |
link_error(__LINE__); \ |
| 72 |
if (CKEXP(bar(i),42)) \ |
| 73 |
__builtin_abort(); \ |
| 74 |
if (!__builtin_##FUNCRES(__builtin_frexp(NEG __builtin_##FUNCARG(ARGARG),&i)) \ |
| 75 |
|| CKSGN(__builtin_frexp(NEG __builtin_##FUNCARG(ARGARG),(j++,&i)), NEG __builtin_##FUNCARG(ARGARG)) \ |
| 76 |
|| CKEXP(j,7)) \ |
| 77 |
link_error(__LINE__); \ |
| 78 |
if (CKEXP(bar(i),42)) \ |
| 79 |
__builtin_abort(); \ |
| 80 |
if (!__builtin_##FUNCRES##l(__builtin_frexpl(NEG __builtin_##FUNCARG##l(ARGARG),&i)) \ |
| 81 |
|| CKSGN_L(__builtin_frexpl(NEG __builtin_##FUNCARG##l(ARGARG),(j++,&i)), NEG __builtin_##FUNCARG##l(ARGARG)) \ |
| 82 |
|| CKEXP(j,8)) \ |
| 83 |
link_error(__LINE__); \ |
| 84 |
if (CKEXP(bar(i),42)) \ |
| 85 |
__builtin_abort(); \ |
| 86 |
} while (0) |
| 87 |
|
| 88 |
void __attribute__ ((__noinline__)) |
| 89 |
foo(void) |
| 90 |
{ |
| 91 |
/* Test that frexp(ARG1,&i) -> ARG2 && i == ARG3. */ |
| 92 |
TESTIT_FREXP (-0x1p40, -0.5, 41); |
| 93 |
TESTIT_FREXP (-0x1p30, -0.5, 31); |
| 94 |
TESTIT_FREXP (-0x1p20, -0.5, 21); |
| 95 |
TESTIT_FREXP (-0x1p10, -0.5, 11); |
| 96 |
TESTIT_FREXP (-0x1p5, -0.5, 6); |
| 97 |
TESTIT_FREXP (-100/3.0, -100/192.0, 6); |
| 98 |
TESTIT_FREXP (-1.5, -0.75, 1); |
| 99 |
TESTIT_FREXP (-1.0, -0.5, 1); |
| 100 |
TESTIT_FREXP (-1/3.0, -2/3.0, -1); |
| 101 |
TESTIT_FREXP (-1/9.0, -8/9.0, -3); |
| 102 |
TESTIT_FREXP (-0x1p-5, -0.5, -4); |
| 103 |
TESTIT_FREXP (-0x1p-10, -0.5, -9); |
| 104 |
TESTIT_FREXP (-0x1p-20, -0.5, -19); |
| 105 |
TESTIT_FREXP (-0x1p-30, -0.5, -29); |
| 106 |
TESTIT_FREXP (-0x1p-40, -0.5, -39); |
| 107 |
TESTIT_FREXP (-0.0, -0.0, 0); |
| 108 |
TESTIT_FREXP (0.0, 0.0, 0); |
| 109 |
TESTIT_FREXP (0x1p-40, 0.5, -39); |
| 110 |
TESTIT_FREXP (0x1p-30, 0.5, -29); |
| 111 |
TESTIT_FREXP (0x1p-20, 0.5, -19); |
| 112 |
TESTIT_FREXP (0x1p-10, 0.5, -9); |
| 113 |
TESTIT_FREXP (0x1p-5, 0.5, -4); |
| 114 |
TESTIT_FREXP (1/9.0, 8/9.0, -3); |
| 115 |
TESTIT_FREXP (1/3.0, 2/3.0, -1); |
| 116 |
TESTIT_FREXP (1.0, 0.5, 1); |
| 117 |
TESTIT_FREXP (1.5, 0.75, 1); |
| 118 |
TESTIT_FREXP (100/3.0, 100/192.0, 6); |
| 119 |
TESTIT_FREXP (0x1p5, 0.5, 6); |
| 120 |
TESTIT_FREXP (0x1p10, 0.5, 11); |
| 121 |
TESTIT_FREXP (0x1p20, 0.5, 21); |
| 122 |
TESTIT_FREXP (0x1p30, 0.5, 31); |
| 123 |
TESTIT_FREXP (0x1p40, 0.5, 41); |
| 124 |
|
| 125 |
/* Test for frexp(+-Inf,&i) -> +-Inf and frexp(+-NaN,&i) -> +-NaN. |
| 126 |
Exponent is left unspecified, but we test for side-effects. */ |
| 127 |
TESTIT_FREXP2 ( ,inf, , isinf); |
| 128 |
TESTIT_FREXP2 (- ,inf, , isinf); |
| 129 |
#ifdef __OPTIMIZE__ |
| 130 |
TESTIT_FREXP2 ( ,nan, "", isnan); |
| 131 |
TESTIT_FREXP2 (- ,nan, "", isnan); |
| 132 |
#endif |
| 133 |
} |
| 134 |
|
| 135 |
int main() |
| 136 |
{ |
| 137 |
foo (); |
| 138 |
|
| 139 |
return 0; |
| 140 |
} |