1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2021 Keith Packard
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #define __STDC_WANT_IEC_60559_BFP_EXT__
37 #define _GNU_SOURCE
38 #include <stdio.h>
39 #include <math.h>
40 #include <fenv.h>
41 #include <stdlib.h>
42 #ifdef _HAVE_COMPLEX
43 #include <complex.h>
44 #endif
45 
46 double d1, d2, d3;
47 float f1, f2, f3;
48 int i1;
49 long int li1;
50 long long int lli1;
51 
52 #ifdef _TEST_LONG_DOUBLE
53 long double l1, l2, l3;
54 #endif
55 
56 #ifdef _HAVE_COMPLEX
57 double complex cd1, cd2, cd3;
58 float complex cf1, cf2, cf3;
59 
60 #ifdef _TEST_LONG_DOUBLE
61 long double complex cl1, cl2, cl3;
62 #endif
63 
64 #endif
65 
66 fexcept_t fex;
67 fenv_t fen;
68 femode_t fem;
69 
70 /*
71  * Touch test to make sure all of the expected math functions exist
72  */
73 
74 int
main(void)75 main(void)
76 {
77     printf("sizeof float %ld double %ld long double %ld\n",
78 	   (long) sizeof(float), (long) sizeof(double), (long) sizeof(long double));
79     d1 = atan (d1);
80     d1 = cos (d1);
81     d1 = sin (d1);
82     d1 = tan (d1);
83     d1 = tanh (d1);
84     d1 = frexp (d1, &i1);
85     d1 = modf (d1, &d2);
86     d1 = ceil (d1);
87     d1 = fabs (d1);
88     d1 = floor (d1);
89     d1 = acos (d1);
90     d1 = asin (d1);
91     d1 = atan2 (d1, d2);
92     d1 = cosh (d1);
93     d1 = sinh (d1);
94     d1 = exp (d1);
95     d1 = ldexp (d1, i1);
96     d1 = log (d1);
97     d1 = log10 (d1);
98     d1 = pow (d1, d2);
99     d1 = sqrt (d1);
100     d1 = fmod (d1, d2);
101 
102     i1 = finite (d1);
103     i1 = finitef (f1);
104 #ifdef _TEST_LONG_DOUBLE
105 #if defined(_HAVE_BUILTIN_FINITEL) || defined(_HAVE_BUILTIN_ISFINITE)
106     i1 = finitel (l1);
107 #endif
108 #ifdef _HAVE_BUILTIN_ISINFL
109     i1 = isinfl (l1);
110 #endif
111 #ifdef _HAVE_BUILTIN_ISNANL
112     i1 = isnanl (l1);
113 #endif
114 #endif
115     i1 = isinff (f1);
116     i1 = isnanf (f1);
117     i1 = isinf (d1);
118     i1 = isnan (d1);
119 
120     i1 = __isinff (f1);
121     i1 = __isinfd (d1);
122     i1 = __isnanf (f1);
123     i1 = __isnand (d1);
124     i1 = __fpclassifyf (f1);
125     i1 = __fpclassifyd (d1);
126     i1 = __signbitf (f1);
127     i1 = __signbitd (d1);
128 
129 /* Non ANSI double precision functions.  */
130 
131     d1 = infinity ();
132     d1 = nan ("");
133     d1 = copysign (d1, d2);
134     d1 = logb (d1);
135     i1 = ilogb (d1);
136 
137     d1 = asinh (d1);
138     d1 = cbrt (d1);
139     d1 = nextafter (d1, d2);
140     d1 = rint (d1);
141     d1 = scalbn (d1, i1);
142 
143     d1 = exp2 (d1);
144     d1 = scalbln (d1, li1);
145     d1 = tgamma (d1);
146     d1 = nearbyint (d1);
147     li1 = lrint (d1);
148     lli1 = llrint (d1);
149     d1 = round (d1);
150     li1 = lround (d1);
151     lli1 = llround (d1);
152     d1 = trunc (d1);
153     d1 = remquo (d1, d2, &i1);
154     d1 = fdim (d1, d2);
155     d1 = fmax (d1, d2);
156     d1 = fmin (d1, d2);
157     d1 = fma (d1, d2, d3);
158 
159     d1 = log1p (d1);
160     d1 = expm1 (d1);
161 
162     d1 = acosh (d1);
163     d1 = atanh (d1);
164     d1 = remainder (d1, d2);
165     d1 = gamma (d1);
166     d1 = lgamma (d1);
167     d1 = erf (d1);
168     d1 = erfc (d1);
169     d1 = log2 (d1);
170 
171     d1 = hypot (d1, d2);
172 
173 /* Single precision versions of ANSI functions.  */
174 
175     f1 = atanf (f1);
176     f1 = cosf (f1);
177     f1 = sinf (f1);
178     f1 = tanf (f1);
179     f1 = tanhf (f1);
180     f1 = frexpf (f1, &i1);
181     f1 = modff (f1, &f2);
182     f1 = ceilf (f1);
183     f1 = fabsf (f1);
184     f1 = floorf (f1);
185 
186     f1 = acosf (f1);
187     f1 = asinf (f1);
188     f1 = atan2f (f1, f2);
189     f1 = coshf (f1);
190     f1 = sinhf (f1);
191     f1 = expf (f1);
192     f1 = ldexpf (f1, i1);
193     f1 = logf (f1);
194     f1 = log10f (f1);
195     f1 = powf (f1, f2);
196     f1 = sqrtf (f1);
197     f1 = fmodf (f1, f2);
198 
199 /* Other single precision functions.  */
200 
201     f1 = exp2f (f1);
202     f1 = scalblnf (f1, li1);
203     f1 = tgammaf (f1);
204     f1 = nearbyintf (f1);
205     li1 = lrintf (f1);
206     lli1 = llrintf (f1);
207     f1 = roundf (f1);
208     li1 = lroundf (f1);
209     lli1 = llroundf (f1);
210     f1 = truncf (f1);
211     f1 = remquof (f1, f2, &i1);
212     f1 = fdimf (f1, f2);
213     f1 = fmaxf (f1, f2);
214     f1 = fminf (f1, f2);
215     f1 = fmaf (f1, f2, f3);
216 
217     f1 = infinityf ();
218     f1 = nanf ("");
219     f1 = copysignf (f1, f2);
220     f1 = logbf (f1);
221     i1 = ilogbf (f1);
222 
223     f1 = asinhf (f1);
224     f1 = cbrtf (f1);
225     f1 = nextafterf (f1, f2);
226     f1 = rintf (f1);
227     f1 = scalbnf (f1, i1);
228     f1 = log1pf (f1);
229     f1 = expm1f (f1);
230 
231     f1 = acoshf (f1);
232     f1 = atanhf (f1);
233     f1 = remainderf (f1, f2);
234     f1 = gammaf (f1);
235     f1 = lgammaf (f1);
236     f1 = erff (f1);
237     f1 = erfcf (f1);
238     f1 = log2f (f1);
239     f1 = hypotf (f1, f2);
240 
241 #ifdef _TEST_LONG_DOUBLE
242     l1 = frexpl (l1, &i1);
243     l1 = ldexpl (l1, i1);
244     l1 = sqrtl (l1);
245     l1 = hypotl (l1, l2);
246     l1 = nanl ("");
247     i1 = ilogbl (l1);
248     l1 = logbl (l1);
249     l1 = scalbnl (l1, i1);
250     l1 = scalblnl (l1, li1);
251     l1 = nearbyintl (l1);
252     l1 = rintl (l1);
253     li1 = lrintl (l1);
254     lli1 = llrintl (l1);
255     l1 = roundl (l1);
256     l1 = lroundl (l1);
257     lli1 = llroundl (l1);
258     l1 = truncl (l1);
259     l1 = fmaxl (l1, l2);
260     l1 = fminl (l1, l2);
261     l1 = hypotl (l1, l2);
262     l1 = sqrtl (l1);
263     l1 = fabsl (l1);
264     l1 = copysignl (l1, l2);
265 
266     l1 = ceill (l1);
267     l1 = floorl (l1);
268 
269 #ifdef _HAVE_LONG_DOUBLE_MATH
270     l1 = atanl (l1);
271     l1 = cosl (l1);
272     l1 = sinl (l1);
273     l1 = tanl (l1);
274     l1 = tanhl (l1);
275     l1 = modfl (l1, &l2);
276     l1 = log1pl (l1);
277     l1 = expm1l (l1);
278 
279     l1 = acosl (l1);
280     l1 = asinl (l1);
281     l1 = atan2l (l1, l2);
282     l1 = coshl (l1);
283     l1 = sinhl (l1);
284     l1 = expl (l1);
285     l1 = logl (l1);
286     l1 = log10l (l1);
287     l1 = powl (l1, l2);
288     l1 = fmodl (l1, l2);
289 
290     l1 = asinhl (l1);
291     l1 = cbrtl (l1);
292     l1 = log2l (l1);
293     l1 = exp2l (l1);
294     l1 = tgammal (l1);
295     l1 = remquol (l1, l2, &i1);
296     l1 = fdiml (l1, l2);
297     l1 = fmal (l1, l2, l3);
298     l1 = acoshl (l1);
299     l1 = atanhl (l1);
300     l1 = remainderl (l1, l2);
301     l1 = lgammal (l1);
302     l1 = erfl (l1);
303     l1 = erfcl (l1);
304     f1 = dreml (l1, l2);
305     sincosl (l1, &l2, &l3);
306     l1 = exp10l (l1);
307     l1 = pow10l (l1);
308     f1 = nexttowardf (f1, l1);
309     d1 = nexttoward (d1, l1);
310     l1 = nextafterl (l1, l2);
311     l1 = nexttowardl (l1, l2);
312 #endif /* _HAVE_LONG_DOUBLE_MATH */
313 #endif /* _TEST_LONG_DOUBLE */
314 
315     d1 = drem (d1, d2);
316     f1 = dremf (f1, f2);
317     d1 = lgamma_r (d1, &i1);
318     f1 = lgammaf_r (f1, &i1);
319 
320     d1 = y0 (d1);
321     d1 = y1 (d1);
322     d1 = yn (i1, d1);
323     d1 = j0 (d1);
324     d1 = j1 (d1);
325     d1 = jn (i1, d1);
326 
327     f1 = y0f (f1);
328     f1 = y1f (f1);
329     f1 = ynf (i1, f2);
330     f1 = j0f (f1);
331     f1 = j1f (f1);
332     f1 = jnf (i1, f2);
333 
334     sincos (d1, &d2, &d3);
335     sincosf (f1, &f2, &f3);
336     d1 = exp10 (d1);
337     d1 = pow10 (d1);
338     f1 = exp10f (f1);
339     f1 = pow10f (f1);
340 
341 #ifdef _HAVE_COMPLEX
342     cd1 = cacos(cd1);
343     cf1 = cacosf(cf1);
344 
345 /* 7.3.5.2 The casin functions */
346     cd1 = casin(cd1);
347     cf1 = casinf(cf1);
348 
349 /* 7.3.5.1 The catan functions */
350     cd1 = catan(cd1);
351     cf1 = catanf(cf1);
352 
353 /* 7.3.5.1 The ccos functions */
354     cd1 = ccos(cd1);
355     cf1 = ccosf(cf1);
356 
357 /* 7.3.5.1 The csin functions */
358     cd1 = csin(cd1);
359     cf1 = csinf(cf1);
360 
361 /* 7.3.5.1 The ctan functions */
362     cd1 = ctan(cd1);
363     cf1 = ctanf(cf1);
364 
365 /* 7.3.6 Hyperbolic functions */
366 /* 7.3.6.1 The cacosh functions */
367     cd1 = cacosh(cd1);
368     cf1 = cacoshf(cf1);
369 
370 /* 7.3.6.2 The casinh functions */
371     cd1 = casinh(cd1);
372     cf1 = casinhf(cf1);
373 
374 /* 7.3.6.3 The catanh functions */
375     cd1 = catanh(cd1);
376     cf1 = catanhf(cf1);
377 
378 /* 7.3.6.4 The ccosh functions */
379     cd1 = ccosh(cd1);
380     cf1 = ccoshf(cf1);
381 
382 /* 7.3.6.5 The csinh functions */
383     cd1 = csinh(cd1);
384     cf1 = csinhf(cf1);
385 
386 /* 7.3.6.6 The ctanh functions */
387     cd1 = ctanh(cd1);
388     cf1 = ctanhf(cf1);
389 
390 /* 7.3.7 Exponential and logarithmic functions */
391 /* 7.3.7.1 The cexp functions */
392     cd1 = cexp(cd1);
393     cf1 = cexpf(cf1);
394 
395 /* 7.3.7.2 The clog functions */
396     cd1 = clog(cd1);
397     cf1 = clogf(cf1);
398 
399 /* 7.3.8 Power and absolute-value functions */
400 /* 7.3.8.1 The cabs functions */
401     d1 = cabs(cd1) ;
402     f1 = cabsf(cf1) ;
403 
404 /* 7.3.8.2 The cpow functions */
405     cd1 = cpow(cd1, cd2);
406     cf1 = cpowf(cf1, cf2);
407 
408 /* 7.3.8.3 The csqrt functions */
409     cd1 = csqrt(cd1);
410     cf1 = csqrtf(cf1);
411 
412 /* 7.3.9 Manipulation functions */
413 /* 7.3.9.1 The carg functions */
414     d1 = carg(cd1);
415     f1 = cargf(cf1);
416 
417 /* 7.3.9.2 The cimag functions */
418     d1 = cimag(cd1);
419     f1 = cimagf(cf1);
420 
421 /* 7.3.9.3 The conj functions */
422     cd1 = conj(cd1);
423     cf1 = conjf(cf1);
424 
425 /* 7.3.9.4 The cproj functions */
426     cd1 = cproj(cd1);
427     cf1 = cprojf(cf1);
428 
429 /* 7.3.9.5 The creal functions */
430     d1 = creal(cd1);
431     f1 = crealf(cf1);
432 
433 #if __GNU_VISIBLE
434     cd1 = clog10(cd1);
435     cf1 = clog10f(cf1);
436 #endif
437 
438 #ifdef _TEST_LONG_DOUBLE
439     cl1 =  csqrtl(cl1);
440     l1 = cabsl(cl1) ;
441     cl1 = cprojl(cl1);
442     l1 = creall(cl1);
443     cl1 = conjl(cl1);
444     l1 = cimagl(cl1);
445 
446 #ifdef _HAVE_LONG_DOUBLE_MATH
447     l1 = cargl(cl1);
448     cl1 = casinl(cl1);
449     cl1 = cacosl(cl1);
450     cl1 = catanl(cl1);
451     cl1 = ccosl(cl1);
452     cl1 = csinl(cl1);
453     cl1 = ctanl(cl1);
454     cl1 = cacoshl(cl1);
455     cl1 = casinhl(cl1);
456     cl1 = catanhl(cl1);
457     cl1 = ccoshl(cl1);
458     cl1 = csinhl(cl1);
459     cl1 = ctanhl(cl1);
460     cl1 = cexpl(cl1);
461     cl1 = clogl(cl1);
462     cl1 = cpowl(cl1, cl2);
463 #if __GNU_VISIBLE
464     cl1 = clog10l(cl1);
465 #endif
466 #endif /* _HAVE_LONG_DOUBLE_MATH */
467 
468 #endif /* _TEST_LONG_DOUBLE */
469 
470 #endif /* _HAVE_COMPLEX */
471 
472     i1 = feclearexcept(FE_ALL_EXCEPT);
473     i1 = fegetexceptflag(&fex, FE_ALL_EXCEPT);
474     i1 = feraiseexcept(0);
475     i1 = fesetexceptflag(&fex, FE_ALL_EXCEPT);
476     i1 = fetestexcept(FE_ALL_EXCEPT);
477 
478     i1 = fegetround();
479     i1 = fesetround(FE_TONEAREST);
480 
481     i1 = fegetenv(&fen);
482     i1 = feholdexcept(&fen);
483     i1 = fesetenv(&fen);
484     i1 = feupdateenv(&fen);
485 
486     i1 = feenableexcept(FE_ALL_EXCEPT);
487     i1 = fedisableexcept(FE_ALL_EXCEPT);
488     i1 = fegetexcept();
489 
490     i1 = fegetmode(&fem);
491     i1 = fesetmode(&fem);
492     i1 = fesetexcept(FE_ALL_EXCEPT);
493 
494     return 0;
495 }
496