1/* Copyright (C) 2004  Free Software Foundation.
2
3   Verify that built-in folding of various math "power" functions is
4   correctly performed by the compiler.
5
6   Written by Kaveh Ghazi, 2004-03-11.  */
7
8/* { dg-do link } */
9/* { dg-options "-ffast-math" } */
10/* { dg-add-options c99_runtime } */
11/* { dg-skip-if "PR44214" { *-*-* } { "-O0" } { "" } } */
12
13#include "../builtins-config.h"
14
15#ifdef HAVE_C99_RUNTIME
16#define C99CODE(CODE) CODE
17#else
18#define C99CODE(CODE) 0
19#endif
20
21#define PROTOTYPE(FN) extern double FN(double); extern float FN##f(float); \
22  extern long double FN##l(long double);
23#define PROTOTYPE2(FN) extern double FN(double, double); \
24  extern float FN##f(float, float); \
25  extern long double FN##l(long double, long double);
26
27PROTOTYPE(fabs)
28PROTOTYPE(sqrt)
29PROTOTYPE(cbrt)
30PROTOTYPE2(pow)
31
32void test(double d1, double d2, double d3,
33	  float f1, float f2, float f3,
34	  long double ld1, long double ld2, long double ld3)
35{
36  /* Test N1root(N2root(x)) -> pow(x,1/(N1*N2)).  */
37  /* E.g. sqrt(cbrt(x)) -> pow(x,1/6).  */
38  /* The `ABS' argument is `fabs' when the transformation only works
39     for nonnegative arguments.  Otherwise it's blank.  */
40#define ROOT_ROOT(FN1,N1,FN2,N2,ABS) \
41 extern void link_failure_##FN1##_##FN2(void); \
42 if (FN1(FN2(ABS(d1))) != pow(ABS(d1),1.0/(N1*N2)) \
43     || C99CODE (FN1##f(FN2##f(ABS(f1))) != powf(ABS(f1),1.0F/(N1*N2))) \
44     || C99CODE (FN1##l(FN2##l(ABS(ld1))) != powl(ABS(ld1),1.0L/(N1*N2)))) \
45    link_failure_##FN1##_##FN2()
46
47  ROOT_ROOT(sqrt,2,sqrt,2,);
48  ROOT_ROOT(sqrt,2,cbrt,3,);
49  ROOT_ROOT(cbrt,3,sqrt,2,);
50  ROOT_ROOT(cbrt,3,cbrt,3,fabs);
51
52  /* Test pow(Nroot(x),y) -> pow(x,y/N).  */
53  /* The `ABS' argument is `fabs' when the transformation only works
54     for nonnegative arguments.  Otherwise it's blank.  */
55#define POW_ROOT(FN,N,ABS) \
56 extern void link_failure_pow_##FN(void); \
57 if (pow(FN(ABS(d1)), d2) != pow(ABS(d1),d2/N) \
58     || powf(FN##f(ABS(f1)),f2) != powf(ABS(f1),f2/N) \
59     || powl(FN##l(ABS(ld1)),ld2) != powl(ABS(ld1),ld2/N)) \
60    link_failure_pow_##FN()
61
62  POW_ROOT(sqrt,2,);
63  POW_ROOT(cbrt,3,fabs);
64
65  /* Test Nroot(pow(x,y)) -> pow(x,y/N).  */
66  /* The `ABS' argument is `fabs' when the transformation only works
67     for nonnegative arguments.  Otherwise it's blank.  */
68#define ROOT_POW(FN,N,ABS) \
69 extern void link_failure_##FN##_pow(void); \
70 if (FN(pow(ABS(d1), d2)) != pow(ABS(d1),d2/N) \
71     || FN##f(powf(ABS(f1),f2)) != powf(ABS(f1),f2/N) \
72     || FN##l(powl(ABS(ld1),ld2)) != powl(ABS(ld1),ld2/N)) \
73    link_failure_##FN##_pow()
74
75  ROOT_POW(sqrt,2,fabs);
76  ROOT_POW(cbrt,3,fabs);
77
78  /* Test pow(pow(x,y),z) -> pow(x,y*z).  */
79#define POW_POW \
80 extern void link_failure_pow_pow(void); \
81 if (pow(pow(fabs(d1), d2), d3) != pow(fabs(d1),d2*d3) \
82     || powf(powf(fabs(f1),f2),f3) != powf(fabs(f1),f2*f3) \
83     || powl(powl(fabs(ld1),ld2),ld3) != powl(fabs(ld1),ld2*ld3)) \
84    link_failure_pow_pow()
85
86  POW_POW;
87
88  /* Test Nroot(x)*Nroot(y) -> Nroot(x*y).  */
89#define ROOT_X_ROOT(FN) \
90 extern void link_failure_root_x_root(void); \
91 if (FN(d1)*FN(d2) != FN(d1*d2) \
92     || FN##f(f1)*FN##f(f2) != FN##f(f1*f2) \
93     || FN##l(ld1)*FN##l(ld2) != FN##l(ld1*ld2)) \
94    link_failure_root_x_root()
95
96  ROOT_X_ROOT(sqrt);
97  ROOT_X_ROOT(cbrt);
98
99  /* Test pow(x,y)*pow(x,z) -> pow(x,y+z).  */
100#define POW_X_POW \
101 extern void link_failure_pow_x_pow(void); \
102 if (pow(d1,d2)*pow(d1,d3) != pow(d1,d2+d3) \
103     || powf(f1,f2)*powf(f1,f3) != powf(f1,f2+f3) \
104     || powl(ld1,ld2)*powl(ld1,ld3) != powl(ld1,ld2+ld3)) \
105    link_failure_pow_x_pow()
106
107  POW_X_POW;
108
109}
110
111int main (void)
112{
113  return 0;
114}
115