1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define _DECLARE_C99_LDBL_MATH 1
18
19#include <gtest/gtest.h>
20
21#include <fenv.h>
22#include <limits.h>
23#include <math.h>
24#include <stdint.h>
25
26float float_subnormal() {
27  union {
28    float f;
29    uint32_t i;
30  } u;
31  u.i = 0x007fffff;
32  return u.f;
33}
34
35double double_subnormal() {
36  union {
37    double d;
38    uint64_t i;
39  } u;
40  u.i = 0x000fffffffffffffLL;
41  return u.d;
42}
43
44TEST(math, fpclassify) {
45  ASSERT_EQ(FP_INFINITE, fpclassify(INFINITY));
46  ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VALF));
47  ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VAL));
48
49  ASSERT_EQ(FP_NAN, fpclassify(nanf("")));
50  ASSERT_EQ(FP_NAN, fpclassify(nan("")));
51
52  ASSERT_EQ(FP_NORMAL, fpclassify(1.0f));
53  ASSERT_EQ(FP_NORMAL, fpclassify(1.0));
54
55  ASSERT_EQ(FP_SUBNORMAL, fpclassify(float_subnormal()));
56  ASSERT_EQ(FP_SUBNORMAL, fpclassify(double_subnormal()));
57
58  ASSERT_EQ(FP_ZERO, fpclassify(0.0f));
59  ASSERT_EQ(FP_ZERO, fpclassify(0.0));
60}
61
62/* TODO: stlport breaks the isfinite macro
63TEST(math, isfinite) {
64  ASSERT_TRUE(isfinite(123.0f));
65  ASSERT_TRUE(isfinite(123.0));
66  ASSERT_FALSE(isfinite(HUGE_VALF));
67  ASSERT_FALSE(isfinite(HUGE_VAL));
68}
69*/
70
71TEST(math, isinf) {
72  ASSERT_FALSE(isinf(123.0f));
73  ASSERT_FALSE(isinf(123.0));
74  ASSERT_TRUE(isinf(HUGE_VALF));
75  ASSERT_TRUE(isinf(HUGE_VAL));
76}
77
78TEST(math, isnan) {
79  ASSERT_FALSE(isnan(123.0f));
80  ASSERT_FALSE(isnan(123.0));
81  ASSERT_TRUE(isnan(nanf("")));
82  ASSERT_TRUE(isnan(nan("")));
83}
84
85TEST(math, isnormal) {
86  ASSERT_TRUE(isnormal(123.0f));
87  ASSERT_TRUE(isnormal(123.0));
88  ASSERT_FALSE(isnormal(float_subnormal()));
89  ASSERT_FALSE(isnormal(double_subnormal()));
90}
91
92// TODO: isgreater, isgreaterequals, isless, islessequal, islessgreater, isunordered
93
94/* TODO: stlport breaks the signbit macro
95TEST(math, signbit) {
96  ASSERT_EQ(0, signbit(0.0f));
97  ASSERT_EQ(0, signbit(0.0));
98
99  ASSERT_EQ(0, signbit(1.0f));
100  ASSERT_EQ(0, signbit(1.0));
101
102  ASSERT_NE(0, signbit(-1.0f));
103  ASSERT_NE(0, signbit(-1.0));
104}
105*/
106
107#if defined(__BIONIC__)
108TEST(math, __fpclassifyd) {
109  ASSERT_EQ(FP_INFINITE, __fpclassifyd(HUGE_VAL));
110  ASSERT_EQ(FP_NAN, __fpclassifyd(nan("")));
111  ASSERT_EQ(FP_NORMAL, __fpclassifyd(1.0));
112  ASSERT_EQ(FP_SUBNORMAL, __fpclassifyd(double_subnormal()));
113  ASSERT_EQ(FP_ZERO, __fpclassifyd(0.0));
114}
115#endif
116
117#if defined(__BIONIC__)
118TEST(math, __fpclassifyf) {
119  ASSERT_EQ(FP_INFINITE, __fpclassifyf(HUGE_VALF));
120  ASSERT_EQ(FP_NAN, __fpclassifyf(nanf("")));
121  ASSERT_EQ(FP_NORMAL, __fpclassifyf(1.0f));
122  ASSERT_EQ(FP_SUBNORMAL, __fpclassifyf(float_subnormal()));
123  ASSERT_EQ(FP_ZERO, __fpclassifyf(0.0f));
124}
125#endif
126
127#if defined(__BIONIC__)
128TEST(math, __fpclassifyl) {
129  EXPECT_EQ(FP_INFINITE, __fpclassifyl(HUGE_VALL));
130  EXPECT_EQ(FP_NAN, __fpclassifyl(nanl("")));
131  EXPECT_EQ(FP_NORMAL, __fpclassifyl(1.0));
132  EXPECT_EQ(FP_SUBNORMAL, __fpclassifyl(double_subnormal()));
133  EXPECT_EQ(FP_ZERO, __fpclassifyl(0.0));
134}
135#endif
136
137TEST(math, finitef) {
138  ASSERT_TRUE(finitef(123.0f));
139  ASSERT_FALSE(finitef(HUGE_VALF));
140}
141
142#if defined(__BIONIC__)
143TEST(math, __isfinite) {
144  ASSERT_TRUE(__isfinite(123.0));
145  ASSERT_FALSE(__isfinite(HUGE_VAL));
146}
147#endif
148
149#if defined(__BIONIC__)
150TEST(math, __isfinitef) {
151  ASSERT_TRUE(__isfinitef(123.0f));
152  ASSERT_FALSE(__isfinitef(HUGE_VALF));
153}
154#endif
155
156#if defined(__BIONIC__)
157TEST(math, __isfinitel) {
158  ASSERT_TRUE(__isfinitel(123.0f));
159  ASSERT_FALSE(__isfinitel(HUGE_VALL));
160}
161#endif
162
163TEST(math, finite) {
164  ASSERT_TRUE(finite(123.0));
165  ASSERT_FALSE(finite(HUGE_VAL));
166}
167
168TEST(math, __isinff) {
169  ASSERT_FALSE(__isinff(123.0f));
170  ASSERT_TRUE(__isinff(HUGE_VALF));
171}
172
173TEST(math, __isinfl) {
174  ASSERT_FALSE(__isinfl(123.0));
175  ASSERT_TRUE(__isinfl(HUGE_VALL));
176}
177
178TEST(math, __isnanf) {
179  ASSERT_FALSE(__isnanf(123.0f));
180  ASSERT_TRUE(__isnanf(nanf("")));
181}
182
183TEST(math, __isnanl) {
184  ASSERT_FALSE(__isnanl(123.0));
185  ASSERT_TRUE(__isnanl(nanl("")));
186}
187
188TEST(math, isnanf) {
189  ASSERT_FALSE(isnanf(123.0f));
190  ASSERT_TRUE(isnanf(nanf("")));
191}
192
193#if defined(__BIONIC__)
194TEST(math, __isnormal) {
195  ASSERT_TRUE(__isnormal(123.0));
196  ASSERT_FALSE(__isnormal(double_subnormal()));
197}
198#endif
199
200#if defined(__BIONIC__)
201TEST(math, __isnormalf) {
202  ASSERT_TRUE(__isnormalf(123.0f));
203  ASSERT_FALSE(__isnormalf(float_subnormal()));
204}
205#endif
206
207#if defined(__BIONIC__)
208TEST(math, __isnormall) {
209  ASSERT_TRUE(__isnormall(123.0));
210  ASSERT_FALSE(__isnormall(double_subnormal()));
211}
212#endif
213
214TEST(math, __signbit) {
215  ASSERT_EQ(0, __signbit(0.0));
216  ASSERT_EQ(0, __signbit(1.0));
217  ASSERT_NE(0, __signbit(-1.0));
218}
219
220TEST(math, __signbitf) {
221  ASSERT_EQ(0, __signbitf(0.0f));
222  ASSERT_EQ(0, __signbitf(1.0f));
223  ASSERT_NE(0, __signbitf(-1.0f));
224}
225
226TEST(math, __signbitl) {
227  ASSERT_EQ(0, __signbitl(0.0));
228  ASSERT_EQ(0, __signbitl(1.0));
229  ASSERT_NE(0, __signbitl(-1.0));
230}
231
232TEST(math, acos) {
233  ASSERT_FLOAT_EQ(M_PI/2.0, acos(0.0));
234}
235
236TEST(math, acosf) {
237  ASSERT_FLOAT_EQ(static_cast<float>(M_PI)/2.0f, acosf(0.0f));
238}
239
240TEST(math, acosl) {
241  ASSERT_FLOAT_EQ(M_PI/2.0, acosl(0.0));
242}
243
244TEST(math, asin) {
245  ASSERT_FLOAT_EQ(0.0, asin(0.0));
246}
247
248TEST(math, asinf) {
249  ASSERT_FLOAT_EQ(0.0f, asinf(0.0f));
250}
251
252TEST(math, asinl) {
253  ASSERT_FLOAT_EQ(0.0, asinl(0.0));
254}
255
256TEST(math, atan) {
257  ASSERT_FLOAT_EQ(0.0, atan(0.0));
258}
259
260TEST(math, atanf) {
261  ASSERT_FLOAT_EQ(0.0f, atanf(0.0f));
262}
263
264TEST(math, atanl) {
265  ASSERT_FLOAT_EQ(0.0, atanl(0.0));
266}
267
268TEST(math, atan2) {
269  ASSERT_FLOAT_EQ(0.0, atan2(0.0, 0.0));
270}
271
272TEST(math, atan2f) {
273  ASSERT_FLOAT_EQ(0.0f, atan2f(0.0f, 0.0f));
274}
275
276TEST(math, atan2l) {
277  ASSERT_FLOAT_EQ(0.0, atan2l(0.0, 0.0));
278}
279
280TEST(math, cos) {
281  ASSERT_FLOAT_EQ(1.0, cos(0.0));
282}
283
284TEST(math, cosf) {
285  ASSERT_FLOAT_EQ(1.0f, cosf(0.0f));
286}
287
288TEST(math, cosl) {
289  ASSERT_FLOAT_EQ(1.0, cosl(0.0));
290}
291
292TEST(math, sin) {
293  ASSERT_FLOAT_EQ(0.0, sin(0.0));
294}
295
296TEST(math, sinf) {
297  ASSERT_FLOAT_EQ(0.0f, sinf(0.0f));
298}
299
300TEST(math, sinl) {
301  ASSERT_FLOAT_EQ(0.0, sinl(0.0));
302}
303
304TEST(math, tan) {
305  ASSERT_FLOAT_EQ(0.0, tan(0.0));
306}
307
308TEST(math, tanf) {
309  ASSERT_FLOAT_EQ(0.0f, tanf(0.0f));
310}
311
312TEST(math, tanl) {
313  ASSERT_FLOAT_EQ(0.0, tanl(0.0));
314}
315
316TEST(math, acosh) {
317  ASSERT_FLOAT_EQ(0.0, acosh(1.0));
318}
319
320TEST(math, acoshf) {
321  ASSERT_FLOAT_EQ(0.0f, acoshf(1.0f));
322}
323
324TEST(math, acoshl) {
325  ASSERT_FLOAT_EQ(0.0, acoshl(1.0));
326}
327
328TEST(math, asinh) {
329  ASSERT_FLOAT_EQ(0.0, asinh(0.0));
330}
331
332TEST(math, asinhf) {
333  ASSERT_FLOAT_EQ(0.0f, asinhf(0.0f));
334}
335
336TEST(math, asinhl) {
337  ASSERT_FLOAT_EQ(0.0, asinhl(0.0));
338}
339
340TEST(math, atanh) {
341  ASSERT_FLOAT_EQ(0.0, atanh(0.0));
342}
343
344TEST(math, atanhf) {
345  ASSERT_FLOAT_EQ(0.0f, atanhf(0.0f));
346}
347
348TEST(math, atanhl) {
349  ASSERT_FLOAT_EQ(0.0, atanhl(0.0));
350}
351
352TEST(math, cosh) {
353  ASSERT_FLOAT_EQ(1.0, cosh(0.0));
354}
355
356TEST(math, coshf) {
357  ASSERT_FLOAT_EQ(1.0f, coshf(0.0f));
358}
359
360TEST(math, coshl) {
361  ASSERT_FLOAT_EQ(1.0, coshl(0.0));
362}
363
364TEST(math, sinh) {
365  ASSERT_FLOAT_EQ(0.0, sinh(0.0));
366}
367
368TEST(math, sinhf) {
369  ASSERT_FLOAT_EQ(0.0f, sinhf(0.0f));
370}
371
372TEST(math, sinhl) {
373  ASSERT_FLOAT_EQ(0.0, sinhl(0.0));
374}
375
376TEST(math, tanh) {
377  ASSERT_FLOAT_EQ(0.0, tanh(0.0));
378}
379
380TEST(math, tanhf) {
381  ASSERT_FLOAT_EQ(0.0f, tanhf(0.0f));
382}
383
384TEST(math, tanhl) {
385  ASSERT_FLOAT_EQ(0.0, tanhl(0.0));
386}
387
388TEST(math, log) {
389  ASSERT_FLOAT_EQ(1.0, log(M_E));
390}
391
392TEST(math, logf) {
393  ASSERT_FLOAT_EQ(1.0f, logf(static_cast<float>(M_E)));
394}
395
396TEST(math, logl) {
397  ASSERT_FLOAT_EQ(1.0, logl(M_E));
398}
399
400TEST(math, log2) {
401  ASSERT_FLOAT_EQ(12.0, log2(4096.0));
402}
403
404TEST(math, log2f) {
405  ASSERT_FLOAT_EQ(12.0f, log2f(4096.0f));
406}
407
408TEST(math, log2l) {
409  ASSERT_FLOAT_EQ(12.0, log2l(4096.0));
410}
411
412TEST(math, log10) {
413  ASSERT_FLOAT_EQ(3.0, log10(1000.0));
414}
415
416TEST(math, log10f) {
417  ASSERT_FLOAT_EQ(3.0f, log10f(1000.0f));
418}
419
420TEST(math, log10l) {
421  ASSERT_FLOAT_EQ(3.0, log10l(1000.0));
422}
423
424TEST(math, cbrt) {
425  ASSERT_FLOAT_EQ(3.0, cbrt(27.0));
426}
427
428TEST(math, cbrtf) {
429  ASSERT_FLOAT_EQ(3.0f, cbrtf(27.0f));
430}
431
432TEST(math, cbrtl) {
433  ASSERT_FLOAT_EQ(3.0, cbrtl(27.0));
434}
435
436TEST(math, sqrt) {
437  ASSERT_FLOAT_EQ(2.0, sqrt(4.0));
438}
439
440TEST(math, sqrtf) {
441  ASSERT_FLOAT_EQ(2.0f, sqrtf(4.0f));
442}
443
444TEST(math, sqrtl) {
445  ASSERT_FLOAT_EQ(2.0, sqrtl(4.0));
446}
447
448TEST(math, exp) {
449  ASSERT_FLOAT_EQ(1.0, exp(0.0));
450  ASSERT_FLOAT_EQ(M_E, exp(1.0));
451}
452
453TEST(math, expf) {
454  ASSERT_FLOAT_EQ(1.0f, expf(0.0f));
455  ASSERT_FLOAT_EQ(static_cast<float>(M_E), expf(1.0f));
456}
457
458TEST(math, expl) {
459  ASSERT_FLOAT_EQ(1.0, expl(0.0));
460  ASSERT_FLOAT_EQ(M_E, expl(1.0));
461}
462
463TEST(math, exp2) {
464  ASSERT_FLOAT_EQ(8.0, exp2(3.0));
465}
466
467TEST(math, exp2f) {
468  ASSERT_FLOAT_EQ(8.0f, exp2f(3.0f));
469}
470
471TEST(math, exp2l) {
472  ASSERT_FLOAT_EQ(8.0, exp2l(3.0));
473}
474
475TEST(math, expm1) {
476  ASSERT_FLOAT_EQ(M_E - 1.0, expm1(1.0));
477}
478
479TEST(math, expm1f) {
480  ASSERT_FLOAT_EQ(static_cast<float>(M_E) - 1.0f, expm1f(1.0f));
481}
482
483TEST(math, expm1l) {
484  ASSERT_FLOAT_EQ(M_E - 1.0, expm1l(1.0));
485}
486
487TEST(math, pow) {
488  ASSERT_FLOAT_EQ(8.0, pow(2.0, 3.0));
489}
490
491TEST(math, powf) {
492  ASSERT_FLOAT_EQ(8.0f, powf(2.0f, 3.0f));
493}
494
495TEST(math, powl) {
496  ASSERT_FLOAT_EQ(8.0, powl(2.0, 3.0));
497}
498
499TEST(math, ceil) {
500  ASSERT_FLOAT_EQ(1.0, ceil(0.9));
501}
502
503TEST(math, ceilf) {
504  ASSERT_FLOAT_EQ(1.0f, ceilf(0.9f));
505}
506
507TEST(math, ceill) {
508  ASSERT_FLOAT_EQ(1.0, ceill(0.9));
509}
510
511TEST(math, floor) {
512  ASSERT_FLOAT_EQ(1.0, floor(1.1));
513}
514
515TEST(math, floorf) {
516  ASSERT_FLOAT_EQ(1.0f, floorf(1.1f));
517}
518
519TEST(math, floorl) {
520  ASSERT_FLOAT_EQ(1.0, floorl(1.1));
521}
522
523TEST(math, fabs) {
524  ASSERT_FLOAT_EQ(1.0, fabs(-1.0));
525}
526
527TEST(math, fabsf) {
528  ASSERT_FLOAT_EQ(1.0f, fabsf(-1.0f));
529}
530
531TEST(math, fabsl) {
532  ASSERT_FLOAT_EQ(1.0, fabsl(-1.0));
533}
534
535TEST(math, ldexp) {
536  ASSERT_FLOAT_EQ(16.0, ldexp(2.0, 3.0));
537}
538
539TEST(math, ldexpf) {
540  ASSERT_FLOAT_EQ(16.0f, ldexpf(2.0f, 3.0f));
541}
542
543TEST(math, ldexpl) {
544  ASSERT_FLOAT_EQ(16.0, ldexpl(2.0, 3.0));
545}
546
547TEST(math, fmod) {
548  ASSERT_FLOAT_EQ(2.0, fmod(12.0, 10.0));
549}
550
551TEST(math, fmodf) {
552  ASSERT_FLOAT_EQ(2.0f, fmodf(12.0f, 10.0f));
553}
554
555TEST(math, fmodl) {
556  ASSERT_FLOAT_EQ(2.0, fmodl(12.0, 10.0));
557}
558
559TEST(math, remainder) {
560  ASSERT_FLOAT_EQ(2.0, remainder(12.0, 10.0));
561}
562
563TEST(math, remainderf) {
564  ASSERT_FLOAT_EQ(2.0f, remainderf(12.0f, 10.0f));
565}
566
567TEST(math, remainderl) {
568  ASSERT_FLOAT_EQ(2.0, remainderl(12.0, 10.0));
569}
570
571TEST(math, drem) {
572  ASSERT_FLOAT_EQ(2.0, drem(12.0, 10.0));
573}
574
575TEST(math, dremf) {
576  ASSERT_FLOAT_EQ(2.0f, dremf(12.0f, 10.0f));
577}
578
579TEST(math, fmax) {
580  ASSERT_FLOAT_EQ(12.0, fmax(12.0, 10.0));
581  ASSERT_FLOAT_EQ(12.0, fmax(12.0, nan("")));
582  ASSERT_FLOAT_EQ(12.0, fmax(nan(""), 12.0));
583}
584
585TEST(math, fmaxf) {
586  ASSERT_FLOAT_EQ(12.0f, fmaxf(12.0f, 10.0f));
587  ASSERT_FLOAT_EQ(12.0f, fmaxf(12.0f, nanf("")));
588  ASSERT_FLOAT_EQ(12.0f, fmaxf(nanf(""), 12.0f));
589}
590
591TEST(math, fmaxl) {
592  ASSERT_FLOAT_EQ(12.0, fmaxl(12.0, 10.0));
593  ASSERT_FLOAT_EQ(12.0, fmaxl(12.0, nanl("")));
594  ASSERT_FLOAT_EQ(12.0, fmaxl(nanl(""), 12.0));
595}
596
597TEST(math, fmin) {
598  ASSERT_FLOAT_EQ(10.0, fmin(12.0, 10.0));
599  ASSERT_FLOAT_EQ(12.0, fmin(12.0, nan("")));
600  ASSERT_FLOAT_EQ(12.0, fmin(nan(""), 12.0));
601}
602
603TEST(math, fminf) {
604  ASSERT_FLOAT_EQ(10.0f, fminf(12.0f, 10.0f));
605  ASSERT_FLOAT_EQ(12.0f, fminf(12.0f, nanf("")));
606  ASSERT_FLOAT_EQ(12.0f, fminf(nanf(""), 12.0f));
607}
608
609TEST(math, fminl) {
610  ASSERT_FLOAT_EQ(10.0, fminl(12.0, 10.0));
611  ASSERT_FLOAT_EQ(12.0, fminl(12.0, nan("")));
612  ASSERT_FLOAT_EQ(12.0, fminl(nan(""), 12.0));
613}
614
615TEST(math, fma) {
616  ASSERT_FLOAT_EQ(10.0, fma(2.0, 3.0, 4.0));
617}
618
619TEST(math, fmaf) {
620  ASSERT_FLOAT_EQ(10.0f, fmaf(2.0f, 3.0f, 4.0f));
621}
622
623TEST(math, fmal) {
624  ASSERT_FLOAT_EQ(10.0, fmal(2.0, 3.0, 4.0));
625}
626
627TEST(math, hypot) {
628  ASSERT_FLOAT_EQ(5.0, hypot(3.0, 4.0));
629}
630
631TEST(math, hypotf) {
632  ASSERT_FLOAT_EQ(5.0f, hypotf(3.0f, 4.0f));
633}
634
635TEST(math, hypotl) {
636  ASSERT_FLOAT_EQ(5.0, hypotl(3.0, 4.0));
637}
638
639TEST(math, erf) {
640  ASSERT_FLOAT_EQ(0.84270078, erf(1.0));
641}
642
643TEST(math, erff) {
644  ASSERT_FLOAT_EQ(0.84270078f, erff(1.0f));
645}
646
647TEST(math, erfl) {
648  ASSERT_FLOAT_EQ(0.84270078, erfl(1.0));
649}
650
651TEST(math, erfc) {
652  ASSERT_FLOAT_EQ(0.15729921, erfc(1.0));
653}
654
655TEST(math, erfcf) {
656  ASSERT_FLOAT_EQ(0.15729921f, erfcf(1.0f));
657}
658
659TEST(math, erfcl) {
660  ASSERT_FLOAT_EQ(0.15729921, erfcl(1.0));
661}
662
663TEST(math, lrint) {
664  fesetround(FE_UPWARD); // lrint/lrintf/lrintl obey the rounding mode.
665  ASSERT_EQ(1235, lrint(1234.01));
666  ASSERT_EQ(1235, lrintf(1234.01f));
667  ASSERT_EQ(1235, lrintl(1234.01));
668  fesetround(FE_TOWARDZERO); // lrint/lrintf/lrintl obey the rounding mode.
669  ASSERT_EQ(1234, lrint(1234.01));
670  ASSERT_EQ(1234, lrintf(1234.01f));
671  ASSERT_EQ(1234, lrintl(1234.01));
672
673  fesetround(FE_UPWARD); // llrint/llrintf/llrintl obey the rounding mode.
674  ASSERT_EQ(1235L, llrint(1234.01));
675  ASSERT_EQ(1235L, llrintf(1234.01f));
676  ASSERT_EQ(1235L, llrintl(1234.01));
677  fesetround(FE_TOWARDZERO); // llrint/llrintf/llrintl obey the rounding mode.
678  ASSERT_EQ(1234L, llrint(1234.01));
679  ASSERT_EQ(1234L, llrintf(1234.01f));
680  ASSERT_EQ(1234L, llrintl(1234.01));
681}
682
683TEST(math, rint) {
684  fesetround(FE_UPWARD); // rint/rintf/rintl obey the rounding mode.
685  feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag.
686  ASSERT_EQ(1234.0, rint(1234.0));
687  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
688  ASSERT_EQ(1235.0, rint(1234.01));
689  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0);
690
691  feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag.
692  ASSERT_EQ(1234.0f, rintf(1234.0f));
693  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
694  ASSERT_EQ(1235.0f, rintf(1234.01f));
695  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0);
696
697  feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag.
698  ASSERT_EQ(1234.0, rintl(1234.0));
699  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
700  ASSERT_EQ(1235.0, rintl(1234.01));
701  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0);
702
703  fesetround(FE_TOWARDZERO); // rint/rintf obey the rounding mode.
704  ASSERT_EQ(1234.0, rint(1234.01));
705  ASSERT_EQ(1234.0f, rintf(1234.01f));
706  ASSERT_EQ(1234.0, rintl(1234.01));
707}
708
709TEST(math, nearbyint) {
710  fesetround(FE_UPWARD); // nearbyint/nearbyintf/nearbyintl obey the rounding mode.
711  feclearexcept(FE_ALL_EXCEPT); // nearbyint/nearbyintf/nearbyintl don't set the FE_INEXACT flag.
712  ASSERT_EQ(1234.0, nearbyint(1234.0));
713  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
714  ASSERT_EQ(1235.0, nearbyint(1234.01));
715  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
716
717  feclearexcept(FE_ALL_EXCEPT);
718  ASSERT_EQ(1234.0f, nearbyintf(1234.0f));
719  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
720  ASSERT_EQ(1235.0f, nearbyintf(1234.01f));
721  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
722
723  feclearexcept(FE_ALL_EXCEPT); // nearbyint/nearbyintf/nearbyintl don't set the FE_INEXACT flag.
724  ASSERT_EQ(1234.0, nearbyintl(1234.0));
725  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
726  ASSERT_EQ(1235.0, nearbyintl(1234.01));
727  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
728
729  fesetround(FE_TOWARDZERO); // nearbyint/nearbyintf/nearbyintl obey the rounding mode.
730  ASSERT_EQ(1234.0, nearbyint(1234.01));
731  ASSERT_EQ(1234.0f, nearbyintf(1234.01f));
732  ASSERT_EQ(1234.0, nearbyintl(1234.01));
733}
734
735TEST(math, lround) {
736  fesetround(FE_UPWARD); // lround ignores the rounding mode.
737  ASSERT_EQ(1234, lround(1234.01));
738  ASSERT_EQ(1234, lroundf(1234.01f));
739  ASSERT_EQ(1234, lroundl(1234.01));
740}
741
742TEST(math, llround) {
743  fesetround(FE_UPWARD); // llround ignores the rounding mode.
744  ASSERT_EQ(1234L, llround(1234.01));
745  ASSERT_EQ(1234L, llroundf(1234.01f));
746  ASSERT_EQ(1234L, llroundl(1234.01));
747}
748
749TEST(math, ilogb) {
750  ASSERT_EQ(FP_ILOGB0, ilogb(0.0));
751  ASSERT_EQ(FP_ILOGBNAN, ilogb(nan("")));
752  ASSERT_EQ(INT_MAX, ilogb(HUGE_VAL));
753  ASSERT_EQ(0, ilogb(1.0));
754  ASSERT_EQ(3, ilogb(10.0));
755}
756
757TEST(math, ilogbf) {
758  ASSERT_EQ(FP_ILOGB0, ilogbf(0.0f));
759  ASSERT_EQ(FP_ILOGBNAN, ilogbf(nanf("")));
760  ASSERT_EQ(INT_MAX, ilogbf(HUGE_VALF));
761  ASSERT_EQ(0, ilogbf(1.0f));
762  ASSERT_EQ(3, ilogbf(10.0f));
763}
764
765TEST(math, ilogbl) {
766  ASSERT_EQ(FP_ILOGB0, ilogbl(0.0));
767  ASSERT_EQ(FP_ILOGBNAN, ilogbl(nanl("")));
768  ASSERT_EQ(INT_MAX, ilogbl(HUGE_VALL));
769  ASSERT_EQ(0, ilogbl(1.0));
770  ASSERT_EQ(3, ilogbl(10.0));
771}
772
773TEST(math, logb) {
774  ASSERT_EQ(-HUGE_VAL, logb(0.0));
775  ASSERT_TRUE(isnan(logb(nan(""))));
776  ASSERT_TRUE(isinf(logb(HUGE_VAL)));
777  ASSERT_EQ(0.0, logb(1.0));
778  ASSERT_EQ(3.0, logb(10.0));
779}
780
781TEST(math, logbf) {
782  ASSERT_EQ(-HUGE_VALF, logbf(0.0f));
783  ASSERT_TRUE(isnanf(logbf(nanf(""))));
784  ASSERT_TRUE(__isinff(logbf(HUGE_VALF)));
785  ASSERT_EQ(0.0f, logbf(1.0f));
786  ASSERT_EQ(3.0f, logbf(10.0f));
787}
788
789TEST(math, logbl) {
790  ASSERT_EQ(-HUGE_VAL, logbl(0.0));
791  ASSERT_TRUE(isnan(logbl(nanl(""))));
792  ASSERT_TRUE(isinf(logbl(HUGE_VALL)));
793  ASSERT_EQ(0.0, logbl(1.0));
794  ASSERT_EQ(3.0, logbl(10.0));
795}
796
797TEST(math, log1p) {
798  ASSERT_EQ(-HUGE_VAL, log1p(-1.0));
799  ASSERT_TRUE(isnan(log1p(nan(""))));
800  ASSERT_TRUE(isinf(log1p(HUGE_VAL)));
801  ASSERT_FLOAT_EQ(1.0, log1p(M_E - 1.0));
802}
803
804TEST(math, log1pf) {
805  ASSERT_EQ(-HUGE_VALF, log1pf(-1.0f));
806  ASSERT_TRUE(isnanf(log1pf(nanf(""))));
807  ASSERT_TRUE(__isinff(log1pf(HUGE_VALF)));
808  ASSERT_FLOAT_EQ(1.0f, log1pf(static_cast<float>(M_E) - 1.0f));
809}
810
811TEST(math, log1pl) {
812  ASSERT_EQ(-HUGE_VALL, log1pl(-1.0));
813  ASSERT_TRUE(isnan(log1pl(nanl(""))));
814  ASSERT_TRUE(isinf(log1pl(HUGE_VALL)));
815  ASSERT_FLOAT_EQ(1.0, log1pl(M_E - 1.0));
816}
817
818TEST(math, fdim) {
819  ASSERT_FLOAT_EQ(0.0, fdim(1.0, 1.0));
820  ASSERT_FLOAT_EQ(1.0, fdim(2.0, 1.0));
821  ASSERT_FLOAT_EQ(0.0, fdim(1.0, 2.0));
822}
823
824TEST(math, fdimf) {
825  ASSERT_FLOAT_EQ(0.0f, fdimf(1.0f, 1.0f));
826  ASSERT_FLOAT_EQ(1.0f, fdimf(2.0f, 1.0f));
827  ASSERT_FLOAT_EQ(0.0f, fdimf(1.0f, 2.0f));
828}
829
830TEST(math, fdiml) {
831  ASSERT_FLOAT_EQ(0.0, fdiml(1.0, 1.0));
832  ASSERT_FLOAT_EQ(1.0, fdiml(2.0, 1.0));
833  ASSERT_FLOAT_EQ(0.0, fdiml(1.0, 2.0));
834}
835
836TEST(math, round) {
837  fesetround(FE_TOWARDZERO); // round ignores the rounding mode and always rounds away from zero.
838  ASSERT_FLOAT_EQ(1.0, round(0.5));
839  ASSERT_FLOAT_EQ(-1.0, round(-0.5));
840  ASSERT_FLOAT_EQ(0.0, round(0.0));
841  ASSERT_FLOAT_EQ(-0.0, round(-0.0));
842  ASSERT_TRUE(isnan(round(nan(""))));
843  ASSERT_FLOAT_EQ(HUGE_VAL, round(HUGE_VAL));
844}
845
846TEST(math, roundf) {
847  fesetround(FE_TOWARDZERO); // roundf ignores the rounding mode and always rounds away from zero.
848  ASSERT_FLOAT_EQ(1.0f, roundf(0.5f));
849  ASSERT_FLOAT_EQ(-1.0f, roundf(-0.5f));
850  ASSERT_FLOAT_EQ(0.0f, roundf(0.0f));
851  ASSERT_FLOAT_EQ(-0.0f, roundf(-0.0f));
852  ASSERT_TRUE(isnanf(roundf(nanf(""))));
853  ASSERT_FLOAT_EQ(HUGE_VALF, roundf(HUGE_VALF));
854}
855
856TEST(math, roundl) {
857  fesetround(FE_TOWARDZERO); // roundl ignores the rounding mode and always rounds away from zero.
858  ASSERT_FLOAT_EQ(1.0, roundl(0.5));
859  ASSERT_FLOAT_EQ(-1.0, roundl(-0.5));
860  ASSERT_FLOAT_EQ(0.0, roundl(0.0));
861  ASSERT_FLOAT_EQ(-0.0, roundl(-0.0));
862  ASSERT_TRUE(isnan(roundl(nanl(""))));
863  ASSERT_FLOAT_EQ(HUGE_VALL, roundl(HUGE_VALL));
864}
865
866TEST(math, trunc) {
867  fesetround(FE_UPWARD); // trunc ignores the rounding mode and always rounds toward zero.
868  ASSERT_FLOAT_EQ(1.0, trunc(1.5));
869  ASSERT_FLOAT_EQ(-1.0, trunc(-1.5));
870  ASSERT_FLOAT_EQ(0.0, trunc(0.0));
871  ASSERT_FLOAT_EQ(-0.0, trunc(-0.0));
872  ASSERT_TRUE(isnan(trunc(nan(""))));
873  ASSERT_FLOAT_EQ(HUGE_VAL, trunc(HUGE_VAL));
874}
875
876TEST(math, truncf) {
877  fesetround(FE_UPWARD); // truncf ignores the rounding mode and always rounds toward zero.
878  ASSERT_FLOAT_EQ(1.0f, truncf(1.5f));
879  ASSERT_FLOAT_EQ(-1.0f, truncf(-1.5f));
880  ASSERT_FLOAT_EQ(0.0f, truncf(0.0f));
881  ASSERT_FLOAT_EQ(-0.0f, truncf(-0.0f));
882  ASSERT_TRUE(isnan(truncf(nanf(""))));
883  ASSERT_FLOAT_EQ(HUGE_VALF, truncf(HUGE_VALF));
884}
885
886TEST(math, truncl) {
887  fesetround(FE_UPWARD); // truncl ignores the rounding mode and always rounds toward zero.
888  ASSERT_FLOAT_EQ(1.0, truncl(1.5));
889  ASSERT_FLOAT_EQ(-1.0, truncl(-1.5));
890  ASSERT_FLOAT_EQ(0.0, truncl(0.0));
891  ASSERT_FLOAT_EQ(-0.0, truncl(-0.0));
892  ASSERT_TRUE(isnan(truncl(nan(""))));
893  ASSERT_FLOAT_EQ(HUGE_VALL, truncl(HUGE_VALL));
894}
895
896TEST(math, nextafter) {
897  ASSERT_FLOAT_EQ(0.0, nextafter(0.0, 0.0));
898  ASSERT_FLOAT_EQ(1.4012985e-45, nextafter(0.0, 1.0));
899  ASSERT_FLOAT_EQ(0.0, nextafter(0.0, -1.0));
900}
901
902TEST(math, nextafterf) {
903  ASSERT_FLOAT_EQ(0.0f, nextafterf(0.0f, 0.0f));
904  ASSERT_FLOAT_EQ(1.4012985e-45f, nextafterf(0.0f, 1.0f));
905  ASSERT_FLOAT_EQ(0.0f, nextafterf(0.0f, -1.0f));
906}
907
908TEST(math, nextafterl) {
909  ASSERT_FLOAT_EQ(0.0, nextafterl(0.0, 0.0));
910  ASSERT_FLOAT_EQ(1.4012985e-45, nextafterl(0.0, 1.0));
911  ASSERT_FLOAT_EQ(0.0, nextafterl(0.0, -1.0));
912}
913
914// TODO: nexttoward
915// TODO: nexttowardf
916// TODO: nexttowardl
917
918TEST(math, copysign) {
919  ASSERT_FLOAT_EQ(0.0, copysign(0.0, 1.0));
920  ASSERT_FLOAT_EQ(-0.0, copysign(0.0, -1.0));
921  ASSERT_FLOAT_EQ(2.0, copysign(2.0, 1.0));
922  ASSERT_FLOAT_EQ(-2.0, copysign(2.0, -1.0));
923}
924
925TEST(math, copysignf) {
926  ASSERT_FLOAT_EQ(0.0f, copysignf(0.0f, 1.0f));
927  ASSERT_FLOAT_EQ(-0.0f, copysignf(0.0f, -1.0f));
928  ASSERT_FLOAT_EQ(2.0f, copysignf(2.0f, 1.0f));
929  ASSERT_FLOAT_EQ(-2.0f, copysignf(2.0f, -1.0f));
930}
931
932TEST(math, copysignl) {
933  ASSERT_FLOAT_EQ(0.0f, copysignl(0.0, 1.0));
934  ASSERT_FLOAT_EQ(-0.0f, copysignl(0.0, -1.0));
935  ASSERT_FLOAT_EQ(2.0f, copysignl(2.0, 1.0));
936  ASSERT_FLOAT_EQ(-2.0f, copysignl(2.0, -1.0));
937}
938
939TEST(math, significand) {
940  ASSERT_FLOAT_EQ(0.0, significand(0.0));
941  ASSERT_FLOAT_EQ(1.2, significand(1.2));
942  ASSERT_FLOAT_EQ(1.5375, significand(12.3));
943}
944
945TEST(math, significandf) {
946  ASSERT_FLOAT_EQ(0.0f, significandf(0.0f));
947  ASSERT_FLOAT_EQ(1.2f, significandf(1.2f));
948  ASSERT_FLOAT_EQ(1.5375f, significandf(12.3f));
949}
950
951extern "C" long double significandl(long double); // BSD's <math.h> doesn't declare this.
952
953TEST(math, significandl) {
954  ASSERT_FLOAT_EQ(0.0, significandl(0.0));
955  ASSERT_FLOAT_EQ(1.2, significandl(1.2));
956  ASSERT_FLOAT_EQ(1.5375, significandl(12.3));
957}
958
959TEST(math, scalb) {
960  ASSERT_FLOAT_EQ(12.0, scalb(3.0, 2.0));
961}
962
963TEST(math, scalbf) {
964  ASSERT_FLOAT_EQ(12.0f, scalbf(3.0f, 2.0f));
965}
966
967TEST(math, scalbln) {
968  ASSERT_FLOAT_EQ(12.0, scalbln(3.0, 2L));
969}
970
971TEST(math, scalblnf) {
972  ASSERT_FLOAT_EQ(12.0f, scalblnf(3.0f, 2L));
973}
974
975TEST(math, scalblnl) {
976  ASSERT_FLOAT_EQ(12.0, scalblnl(3.0, 2L));
977}
978
979TEST(math, scalbn) {
980  ASSERT_FLOAT_EQ(12.0, scalbn(3.0, 2));
981}
982
983TEST(math, scalbnf) {
984  ASSERT_FLOAT_EQ(12.0f, scalbnf(3.0f, 2));
985}
986
987TEST(math, scalbnl) {
988  ASSERT_FLOAT_EQ(12.0, scalbnl(3.0, 2));
989}
990
991TEST(math, gamma) {
992  ASSERT_FLOAT_EQ(log(24.0), gamma(5.0));
993}
994
995TEST(math, gammaf) {
996  ASSERT_FLOAT_EQ(logf(24.0f), gammaf(5.0f));
997}
998
999#if defined(__BIONIC__)
1000TEST(math, gamma_r) {
1001  int sign;
1002  ASSERT_FLOAT_EQ(log(24.0), gamma_r(5.0, &sign));
1003  ASSERT_EQ(1, sign);
1004}
1005#endif
1006
1007#if defined(__BIONIC__)
1008TEST(math, gammaf_r) {
1009  int sign;
1010  ASSERT_FLOAT_EQ(logf(24.0f), gammaf_r(5.0f, &sign));
1011  ASSERT_EQ(1, sign);
1012}
1013#endif
1014
1015TEST(math, lgamma) {
1016  ASSERT_FLOAT_EQ(log(24.0), lgamma(5.0));
1017}
1018
1019TEST(math, lgammaf) {
1020  ASSERT_FLOAT_EQ(logf(24.0f), lgammaf(5.0f));
1021}
1022
1023TEST(math, lgammal) {
1024  ASSERT_FLOAT_EQ(logl(24.0), lgammal(5.0));
1025}
1026
1027TEST(math, lgamma_r) {
1028  int sign;
1029  ASSERT_FLOAT_EQ(log(24.0), lgamma_r(5.0, &sign));
1030  ASSERT_EQ(1, sign);
1031}
1032
1033TEST(math, lgammaf_r) {
1034  int sign;
1035  ASSERT_FLOAT_EQ(logf(24.0f), lgammaf_r(5.0f, &sign));
1036  ASSERT_EQ(1, sign);
1037}
1038
1039TEST(math, tgamma) {
1040  ASSERT_FLOAT_EQ(24.0, tgamma(5.0));
1041}
1042
1043TEST(math, tgammaf) {
1044  ASSERT_FLOAT_EQ(24.0f, tgammaf(5.0f));
1045}
1046
1047TEST(math, tgammal) {
1048  ASSERT_FLOAT_EQ(24.0, tgammal(5.0));
1049}
1050
1051TEST(math, j0) {
1052  ASSERT_FLOAT_EQ(1.0, j0(0.0));
1053  ASSERT_FLOAT_EQ(0.76519769, j0(1.0));
1054}
1055
1056TEST(math, j0f) {
1057  ASSERT_FLOAT_EQ(1.0f, j0f(0.0f));
1058  ASSERT_FLOAT_EQ(0.76519769f, j0f(1.0f));
1059}
1060
1061TEST(math, j1) {
1062  ASSERT_FLOAT_EQ(0.0, j1(0.0));
1063  ASSERT_FLOAT_EQ(0.44005057, j1(1.0));
1064}
1065
1066TEST(math, j1f) {
1067  ASSERT_FLOAT_EQ(0.0f, j1f(0.0f));
1068  ASSERT_FLOAT_EQ(0.44005057f, j1f(1.0f));
1069}
1070
1071TEST(math, jn) {
1072  ASSERT_FLOAT_EQ(0.0, jn(4, 0.0));
1073  ASSERT_FLOAT_EQ(0.0024766389, jn(4, 1.0));
1074}
1075
1076TEST(math, jnf) {
1077  ASSERT_FLOAT_EQ(0.0f, jnf(4, 0.0f));
1078  ASSERT_FLOAT_EQ(0.0024766389f, jnf(4, 1.0f));
1079}
1080
1081TEST(math, y0) {
1082  ASSERT_FLOAT_EQ(-HUGE_VAL, y0(0.0));
1083  ASSERT_FLOAT_EQ(0.088256963, y0(1.0));
1084}
1085
1086TEST(math, y0f) {
1087  ASSERT_FLOAT_EQ(-HUGE_VALF, y0f(0.0f));
1088  ASSERT_FLOAT_EQ(0.088256963f, y0f(1.0f));
1089}
1090
1091TEST(math, y1) {
1092  ASSERT_FLOAT_EQ(-HUGE_VAL, y1(0.0));
1093  ASSERT_FLOAT_EQ(-0.78121281, y1(1.0));
1094}
1095
1096TEST(math, y1f) {
1097  ASSERT_FLOAT_EQ(-HUGE_VALF, y1f(0.0f));
1098  ASSERT_FLOAT_EQ(-0.78121281f, y1f(1.0f));
1099}
1100
1101TEST(math, yn) {
1102  ASSERT_FLOAT_EQ(-HUGE_VAL, yn(4, 0.0));
1103  ASSERT_FLOAT_EQ(-33.278423, yn(4, 1.0));
1104}
1105
1106TEST(math, ynf) {
1107  ASSERT_FLOAT_EQ(-HUGE_VALF, ynf(4, 0.0f));
1108  ASSERT_FLOAT_EQ(-33.278423f, ynf(4, 1.0f));
1109}
1110
1111TEST(math, frexp) {
1112  int exp;
1113  double dr = frexp(1024.0, &exp);
1114  ASSERT_FLOAT_EQ(1024.0, scalbn(dr, exp));
1115}
1116
1117TEST(math, frexpf) {
1118  int exp;
1119  float fr = frexpf(1024.0f, &exp);
1120  ASSERT_FLOAT_EQ(1024.0f, scalbnf(fr, exp));
1121}
1122
1123TEST(math, frexpl) {
1124  int exp;
1125  long double ldr = frexpl(1024.0, &exp);
1126  ASSERT_FLOAT_EQ(1024.0, scalbnl(ldr, exp));
1127}
1128
1129TEST(math, modf) {
1130  double di;
1131  double df = modf(123.456, &di);
1132  ASSERT_FLOAT_EQ(123.0, di);
1133  ASSERT_FLOAT_EQ(0.456, df);
1134}
1135
1136TEST(math, modff) {
1137  float fi;
1138  float ff = modff(123.456f, &fi);
1139  ASSERT_FLOAT_EQ(123.0f, fi);
1140  ASSERT_FLOAT_EQ(0.45600128f, ff);
1141}
1142
1143TEST(math, modfl) {
1144  long double ldi;
1145  long double ldf = modfl(123.456, &ldi);
1146  ASSERT_FLOAT_EQ(123.0, ldi);
1147  ASSERT_FLOAT_EQ(0.456, ldf);
1148}
1149
1150TEST(math, remquo) {
1151  int q;
1152  double d = remquo(13.0, 4.0, &q);
1153  ASSERT_EQ(3, q);
1154  ASSERT_FLOAT_EQ(1.0, d);
1155}
1156
1157TEST(math, remquof) {
1158  int q;
1159  float f = remquof(13.0f, 4.0f, &q);
1160  ASSERT_EQ(3, q);
1161  ASSERT_FLOAT_EQ(1.0, f);
1162}
1163
1164TEST(math, remquol) {
1165  int q;
1166  long double ld = remquol(13.0, 4.0, &q);
1167  ASSERT_EQ(3, q);
1168  ASSERT_FLOAT_EQ(1.0, ld);
1169}
1170
1171// https://code.google.com/p/android/issues/detail?id=6697
1172TEST(math, frexpf_public_bug_6697) {
1173  int exp;
1174  float fr = frexpf(14.1f, &exp);
1175  ASSERT_FLOAT_EQ(14.1f, scalbnf(fr, exp));
1176}
1177