OSDN Git Service

am 57df00a4: Merge "Improve math tests to allow a specific ulp bound."
[android-x86/bionic.git] / tests / math_test.cpp
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 _GNU_SOURCE 1
18 #include <math.h>
19
20 // This include (and the associated definition of __test_capture_signbit)
21 // must be placed before any files that include <cmath> (gtest.h in this case).
22 //
23 // <math.h> is required to define generic macros signbit, isfinite and
24 // several other such functions.
25 //
26 // <cmath> is required to undef declarations of these macros in the global
27 // namespace and make equivalent functions available in namespace std. Our
28 // stlport implementation does this only for signbit, isfinite, isinf and
29 // isnan.
30 //
31 // NOTE: We don't write our test using std::signbit because we want to be
32 // sure that we're testing the bionic version of signbit. The C++ libraries
33 // are free to reimplement signbit or delegate to compiler builtins if they
34 // please.
35
36 namespace {
37 template<typename T> inline int test_capture_signbit(const T in) {
38   return signbit(in);
39 }
40 template<typename T> inline int test_capture_isfinite(const T in) {
41   return isfinite(in);
42 }
43 template<typename T> inline int test_capture_isnan(const T in) {
44   return isnan(in);
45 }
46 template<typename T> inline int test_capture_isinf(const T in) {
47   return isinf(in);
48 }
49 }
50
51 #include "math_data_test.h"
52
53 #include <gtest/gtest.h>
54
55 #include <fenv.h>
56 #include <float.h>
57 #include <limits.h>
58 #include <stdint.h>
59
60 #include <private/ScopeGuard.h>
61
62 float float_subnormal() {
63   union {
64     float f;
65     uint32_t i;
66   } u;
67   u.i = 0x007fffff;
68   return u.f;
69 }
70
71 double double_subnormal() {
72   union {
73     double d;
74     uint64_t i;
75   } u;
76   u.i = 0x000fffffffffffffLL;
77   return u.d;
78 }
79
80 long double ldouble_subnormal() {
81   union {
82     long double e;
83     unsigned char c[sizeof(long double)];
84   } u;
85
86   // Subnormals must have a zero exponent and non zero significand.
87   // On all supported representation the 17 bit (counting from either sides)
88   // is part of the significand so it should be enough to set that.
89   // It also applies for the case sizeof(double) = sizeof(long double)
90   for (unsigned int i = 0; i < sizeof(long double); i++) {
91     u.c[i] = 0x00;
92   }
93   u.c[sizeof(long double) - 3] = 0x80;
94   u.c[2] = 0x80;
95
96   return u.e;
97 }
98
99 TEST(math, fpclassify) {
100   ASSERT_EQ(FP_INFINITE, fpclassify(INFINITY));
101   ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VALF));
102   ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VAL));
103   ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VALL));
104
105   ASSERT_EQ(FP_NAN, fpclassify(nanf("")));
106   ASSERT_EQ(FP_NAN, fpclassify(nan("")));
107   ASSERT_EQ(FP_NAN, fpclassify(nanl("")));
108
109   ASSERT_EQ(FP_NORMAL, fpclassify(1.0f));
110   ASSERT_EQ(FP_NORMAL, fpclassify(1.0));
111   ASSERT_EQ(FP_NORMAL, fpclassify(1.0L));
112
113   ASSERT_EQ(FP_SUBNORMAL, fpclassify(float_subnormal()));
114   ASSERT_EQ(FP_SUBNORMAL, fpclassify(double_subnormal()));
115   ASSERT_EQ(FP_SUBNORMAL, fpclassify(ldouble_subnormal()));
116
117   ASSERT_EQ(FP_ZERO, fpclassify(0.0f));
118   ASSERT_EQ(FP_ZERO, fpclassify(0.0));
119   ASSERT_EQ(FP_ZERO, fpclassify(0.0L));
120 }
121
122 TEST(math, isfinite) {
123   ASSERT_TRUE(test_capture_isfinite(123.0f));
124   ASSERT_TRUE(test_capture_isfinite(123.0));
125   ASSERT_TRUE(test_capture_isfinite(123.0L));
126   ASSERT_FALSE(test_capture_isfinite(HUGE_VALF));
127   ASSERT_FALSE(test_capture_isfinite(HUGE_VAL));
128   ASSERT_FALSE(test_capture_isfinite(HUGE_VALL));
129 }
130
131 TEST(math, isinf) {
132   ASSERT_FALSE(test_capture_isinf(123.0f));
133   ASSERT_FALSE(test_capture_isinf(123.0));
134   ASSERT_FALSE(test_capture_isinf(123.0L));
135   ASSERT_TRUE(test_capture_isinf(HUGE_VALF));
136   ASSERT_TRUE(test_capture_isinf(HUGE_VAL));
137   ASSERT_TRUE(test_capture_isinf(HUGE_VALL));
138 }
139
140 TEST(math, isnan) {
141   ASSERT_FALSE(test_capture_isnan(123.0f));
142   ASSERT_FALSE(test_capture_isnan(123.0));
143   ASSERT_FALSE(test_capture_isnan(123.0L));
144   ASSERT_TRUE(test_capture_isnan(nanf("")));
145   ASSERT_TRUE(test_capture_isnan(nan("")));
146   ASSERT_TRUE(test_capture_isnan(nanl("")));
147 }
148
149 TEST(math, isnormal) {
150   ASSERT_TRUE(isnormal(123.0f));
151   ASSERT_TRUE(isnormal(123.0));
152   ASSERT_TRUE(isnormal(123.0L));
153   ASSERT_FALSE(isnormal(float_subnormal()));
154   ASSERT_FALSE(isnormal(double_subnormal()));
155   ASSERT_FALSE(isnormal(ldouble_subnormal()));
156 }
157
158 // TODO: isgreater, isgreaterequals, isless, islessequal, islessgreater, isunordered
159 TEST(math, signbit) {
160   ASSERT_EQ(0, test_capture_signbit(0.0f));
161   ASSERT_EQ(0, test_capture_signbit(0.0));
162   ASSERT_EQ(0, test_capture_signbit(0.0L));
163
164   ASSERT_EQ(0, test_capture_signbit(1.0f));
165   ASSERT_EQ(0, test_capture_signbit(1.0));
166   ASSERT_EQ(0, test_capture_signbit(1.0L));
167
168   ASSERT_NE(0, test_capture_signbit(-1.0f));
169   ASSERT_NE(0, test_capture_signbit(-1.0));
170   ASSERT_NE(0, test_capture_signbit(-1.0L));
171 }
172
173 TEST(math, __fpclassifyd) {
174 #if defined(__GLIBC__)
175 #define __fpclassifyd __fpclassify
176 #endif
177   ASSERT_EQ(FP_INFINITE, __fpclassifyd(HUGE_VAL));
178   ASSERT_EQ(FP_NAN, __fpclassifyd(nan("")));
179   ASSERT_EQ(FP_NORMAL, __fpclassifyd(1.0));
180   ASSERT_EQ(FP_SUBNORMAL, __fpclassifyd(double_subnormal()));
181   ASSERT_EQ(FP_ZERO, __fpclassifyd(0.0));
182 }
183
184 TEST(math, __fpclassifyf) {
185   ASSERT_EQ(FP_INFINITE, __fpclassifyf(HUGE_VALF));
186   ASSERT_EQ(FP_NAN, __fpclassifyf(nanf("")));
187   ASSERT_EQ(FP_NORMAL, __fpclassifyf(1.0f));
188   ASSERT_EQ(FP_SUBNORMAL, __fpclassifyf(float_subnormal()));
189   ASSERT_EQ(FP_ZERO, __fpclassifyf(0.0f));
190 }
191
192 TEST(math, __fpclassifyl) {
193   EXPECT_EQ(FP_INFINITE, __fpclassifyl(HUGE_VALL));
194   EXPECT_EQ(FP_NAN, __fpclassifyl(nanl("")));
195   EXPECT_EQ(FP_NORMAL, __fpclassifyl(1.0L));
196   EXPECT_EQ(FP_SUBNORMAL, __fpclassifyl(ldouble_subnormal()));
197   EXPECT_EQ(FP_ZERO, __fpclassifyl(0.0L));
198 }
199
200 TEST(math, finitef) {
201   ASSERT_TRUE(finitef(123.0f));
202   ASSERT_FALSE(finitef(HUGE_VALF));
203 }
204
205 TEST(math, __isfinite) {
206 #if defined(__GLIBC__)
207 #define __isfinite __finite
208 #endif
209   ASSERT_TRUE(__isfinite(123.0));
210   ASSERT_FALSE(__isfinite(HUGE_VAL));
211 }
212
213 TEST(math, __isfinitef) {
214 #if defined(__GLIBC__)
215 #define __isfinitef __finitef
216 #endif
217   ASSERT_TRUE(__isfinitef(123.0f));
218   ASSERT_FALSE(__isfinitef(HUGE_VALF));
219 }
220
221 TEST(math, __isfinitel) {
222 #if defined(__GLIBC__)
223 #define __isfinitel __finitel
224 #endif
225   ASSERT_TRUE(__isfinitel(123.0L));
226   ASSERT_FALSE(__isfinitel(HUGE_VALL));
227 }
228
229 TEST(math, finite) {
230   ASSERT_TRUE(finite(123.0));
231   ASSERT_FALSE(finite(HUGE_VAL));
232 }
233
234 TEST(math, isinf_function) {
235   // The isinf macro deals with all three types; the isinf function is for doubles.
236   ASSERT_FALSE((isinf)(123.0));
237   ASSERT_TRUE((isinf)(HUGE_VAL));
238 }
239
240 TEST(math, __isinff) {
241   ASSERT_FALSE(__isinff(123.0f));
242   ASSERT_TRUE(__isinff(HUGE_VALF));
243 }
244
245 TEST(math, __isinfl) {
246   ASSERT_FALSE(__isinfl(123.0L));
247   ASSERT_TRUE(__isinfl(HUGE_VALL));
248 }
249
250 TEST(math, isnan_function) {
251   // The isnan macro deals with all three types; the isnan function is for doubles.
252   ASSERT_FALSE((isnan)(123.0));
253   ASSERT_TRUE((isnan)(nan("")));
254 }
255
256 TEST(math, __isnanf) {
257   ASSERT_FALSE(__isnanf(123.0f));
258   ASSERT_TRUE(__isnanf(nanf("")));
259 }
260
261 TEST(math, __isnanl) {
262   ASSERT_FALSE(__isnanl(123.0L));
263   ASSERT_TRUE(__isnanl(nanl("")));
264 }
265
266 TEST(math, isnanf) {
267   ASSERT_FALSE(isnanf(123.0f));
268   ASSERT_TRUE(isnanf(nanf("")));
269 }
270
271 TEST(math, __isnormal) {
272 #if defined(__BIONIC__)
273   ASSERT_TRUE(__isnormal(123.0));
274   ASSERT_FALSE(__isnormal(double_subnormal()));
275 #else // __BIONIC__
276   GTEST_LOG_(INFO) << "glibc doesn't have __isnormal.\n";
277 #endif // __BIONIC__
278 }
279
280 TEST(math, __isnormalf) {
281 #if defined(__BIONIC__)
282   ASSERT_TRUE(__isnormalf(123.0f));
283   ASSERT_FALSE(__isnormalf(float_subnormal()));
284 #else // __BIONIC__
285   GTEST_LOG_(INFO) << "glibc doesn't have __isnormalf.\n";
286 #endif // __BIONIC__
287 }
288
289 TEST(math, __isnormall) {
290 #if defined(__BIONIC__)
291   ASSERT_TRUE(__isnormall(123.0L));
292   ASSERT_FALSE(__isnormall(ldouble_subnormal()));
293 #else // __BIONIC__
294   GTEST_LOG_(INFO) << "glibc doesn't have __isnormall.\n";
295 #endif // __BIONIC__
296 }
297
298 TEST(math, __signbit) {
299   ASSERT_EQ(0, __signbit(0.0));
300   ASSERT_EQ(0, __signbit(1.0));
301   ASSERT_NE(0, __signbit(-1.0));
302 }
303
304 TEST(math, __signbitf) {
305   ASSERT_EQ(0, __signbitf(0.0f));
306   ASSERT_EQ(0, __signbitf(1.0f));
307   ASSERT_NE(0, __signbitf(-1.0f));
308 }
309
310 TEST(math, __signbitl) {
311   ASSERT_EQ(0L, __signbitl(0.0L));
312   ASSERT_EQ(0L, __signbitl(1.0L));
313   ASSERT_NE(0L, __signbitl(-1.0L));
314 }
315
316 TEST(math, acos) {
317   ASSERT_DOUBLE_EQ(M_PI/2.0, acos(0.0));
318 }
319
320 TEST(math, acosf) {
321   ASSERT_FLOAT_EQ(static_cast<float>(M_PI)/2.0f, acosf(0.0f));
322 }
323
324 TEST(math, acosl) {
325   ASSERT_DOUBLE_EQ(M_PI/2.0L, acosl(0.0L));
326 }
327
328 TEST(math, asin) {
329   ASSERT_DOUBLE_EQ(0.0, asin(0.0));
330 }
331
332 TEST(math, asinf) {
333   ASSERT_FLOAT_EQ(0.0f, asinf(0.0f));
334 }
335
336 TEST(math, asinl) {
337   ASSERT_DOUBLE_EQ(0.0L, asinl(0.0L));
338 }
339
340 TEST(math, atan) {
341   ASSERT_DOUBLE_EQ(0.0, atan(0.0));
342 }
343
344 TEST(math, atanf) {
345   ASSERT_FLOAT_EQ(0.0f, atanf(0.0f));
346 }
347
348 TEST(math, atanl) {
349   ASSERT_DOUBLE_EQ(0.0L, atanl(0.0L));
350 }
351
352 TEST(math, atan2) {
353   ASSERT_DOUBLE_EQ(0.0, atan2(0.0, 0.0));
354 }
355
356 TEST(math, atan2f) {
357   ASSERT_FLOAT_EQ(0.0f, atan2f(0.0f, 0.0f));
358 }
359
360 TEST(math, atan2l) {
361   ASSERT_DOUBLE_EQ(0.0L, atan2l(0.0L, 0.0L));
362 }
363
364 TEST(math, cos) {
365   ASSERT_DOUBLE_EQ(1.0, cos(0.0));
366 }
367
368 TEST(math, cosf) {
369   ASSERT_FLOAT_EQ(1.0f, cosf(0.0f));
370 }
371
372 TEST(math, cosl) {
373   ASSERT_DOUBLE_EQ(1.0L, cosl(0.0L));
374 }
375
376 TEST(math, sin) {
377   ASSERT_DOUBLE_EQ(0.0, sin(0.0));
378 }
379
380 TEST(math, sinf) {
381   ASSERT_FLOAT_EQ(0.0f, sinf(0.0f));
382 }
383
384 TEST(math, sinl) {
385   ASSERT_DOUBLE_EQ(0.0L, sinl(0.0L));
386 }
387
388 TEST(math, tan) {
389   ASSERT_DOUBLE_EQ(0.0, tan(0.0));
390 }
391
392 TEST(math, tanf) {
393   ASSERT_FLOAT_EQ(0.0f, tanf(0.0f));
394 }
395
396 TEST(math, tanl) {
397   ASSERT_DOUBLE_EQ(0.0L, tanl(0.0L));
398 }
399
400 TEST(math, acosh) {
401   ASSERT_DOUBLE_EQ(0.0, acosh(1.0));
402 }
403
404 TEST(math, acoshf) {
405   ASSERT_FLOAT_EQ(0.0f, acoshf(1.0f));
406 }
407
408 TEST(math, acoshl) {
409   ASSERT_DOUBLE_EQ(0.0L, acoshl(1.0L));
410 }
411
412 TEST(math, asinh) {
413   ASSERT_DOUBLE_EQ(0.0, asinh(0.0));
414 }
415
416 TEST(math, asinhf) {
417   ASSERT_FLOAT_EQ(0.0f, asinhf(0.0f));
418 }
419
420 TEST(math, asinhl) {
421   ASSERT_DOUBLE_EQ(0.0L, asinhl(0.0L));
422 }
423
424 TEST(math, atanh) {
425   ASSERT_DOUBLE_EQ(0.0, atanh(0.0));
426 }
427
428 TEST(math, atanhf) {
429   ASSERT_FLOAT_EQ(0.0f, atanhf(0.0f));
430 }
431
432 TEST(math, atanhl) {
433   ASSERT_DOUBLE_EQ(0.0L, atanhl(0.0L));
434 }
435
436 TEST(math, cosh) {
437   ASSERT_DOUBLE_EQ(1.0, cosh(0.0));
438 }
439
440 TEST(math, coshf) {
441   ASSERT_FLOAT_EQ(1.0f, coshf(0.0f));
442 }
443
444 TEST(math, coshl) {
445   ASSERT_DOUBLE_EQ(1.0L, coshl(0.0L));
446 }
447
448 TEST(math, sinh) {
449   ASSERT_DOUBLE_EQ(0.0, sinh(0.0));
450 }
451
452 TEST(math, sinhf) {
453   ASSERT_FLOAT_EQ(0.0f, sinhf(0.0f));
454 }
455
456 TEST(math, sinhl) {
457   ASSERT_DOUBLE_EQ(0.0L, sinhl(0.0L));
458 }
459
460 TEST(math, tanh) {
461   ASSERT_DOUBLE_EQ(0.0, tanh(0.0));
462 }
463
464 TEST(math, tanhf) {
465   ASSERT_FLOAT_EQ(0.0f, tanhf(0.0f));
466 }
467
468 TEST(math, tanhl) {
469   ASSERT_DOUBLE_EQ(0.0L, tanhl(0.0L));
470 }
471
472 TEST(math, log) {
473   ASSERT_DOUBLE_EQ(1.0, log(M_E));
474 }
475
476 TEST(math, logf) {
477   ASSERT_FLOAT_EQ(1.0f, logf(static_cast<float>(M_E)));
478 }
479
480 TEST(math, logl) {
481   ASSERT_DOUBLE_EQ(1.0L, logl(M_E));
482 }
483
484 TEST(math, log2) {
485   ASSERT_DOUBLE_EQ(12.0, log2(4096.0));
486 }
487
488 TEST(math, log2f) {
489   ASSERT_FLOAT_EQ(12.0f, log2f(4096.0f));
490 }
491
492 TEST(math, log2l) {
493   ASSERT_DOUBLE_EQ(12.0L, log2l(4096.0L));
494 }
495
496 TEST(math, log10) {
497   ASSERT_DOUBLE_EQ(3.0, log10(1000.0));
498 }
499
500 TEST(math, log10f) {
501   ASSERT_FLOAT_EQ(3.0f, log10f(1000.0f));
502 }
503
504 TEST(math, log10l) {
505   ASSERT_DOUBLE_EQ(3.0L, log10l(1000.0L));
506 }
507
508 TEST(math, cbrt) {
509   ASSERT_DOUBLE_EQ(3.0, cbrt(27.0));
510 }
511
512 TEST(math, cbrtf) {
513   ASSERT_FLOAT_EQ(3.0f, cbrtf(27.0f));
514 }
515
516 TEST(math, cbrtl) {
517   ASSERT_DOUBLE_EQ(3.0L, cbrtl(27.0L));
518 }
519
520 TEST(math, sqrt) {
521   ASSERT_DOUBLE_EQ(2.0, sqrt(4.0));
522 }
523
524 TEST(math, sqrtf) {
525   ASSERT_FLOAT_EQ(2.0f, sqrtf(4.0f));
526 }
527
528 TEST(math, sqrtl) {
529   ASSERT_DOUBLE_EQ(2.0L, sqrtl(4.0L));
530 }
531
532 TEST(math, exp) {
533   ASSERT_DOUBLE_EQ(1.0, exp(0.0));
534   ASSERT_DOUBLE_EQ(M_E, exp(1.0));
535 }
536
537 TEST(math, expf) {
538   ASSERT_FLOAT_EQ(1.0f, expf(0.0f));
539   ASSERT_FLOAT_EQ(static_cast<float>(M_E), expf(1.0f));
540 }
541
542 TEST(math, expl) {
543   ASSERT_DOUBLE_EQ(1.0L, expl(0.0L));
544   ASSERT_DOUBLE_EQ(M_E, expl(1.0L));
545 }
546
547 TEST(math, exp2) {
548   ASSERT_DOUBLE_EQ(8.0, exp2(3.0));
549 }
550
551 TEST(math, exp2f) {
552   ASSERT_FLOAT_EQ(8.0f, exp2f(3.0f));
553 }
554
555 TEST(math, exp2l) {
556   ASSERT_DOUBLE_EQ(8.0L, exp2l(3.0L));
557 }
558
559 TEST(math, expm1) {
560   ASSERT_DOUBLE_EQ(M_E - 1.0, expm1(1.0));
561 }
562
563 TEST(math, expm1f) {
564   ASSERT_FLOAT_EQ(static_cast<float>(M_E) - 1.0f, expm1f(1.0f));
565 }
566
567 TEST(math, expm1l) {
568   ASSERT_DOUBLE_EQ(M_E - 1.0L, expm1l(1.0L));
569 }
570
571 TEST(math, pow) {
572   ASSERT_TRUE(isnan(pow(nan(""), 3.0)));
573   ASSERT_DOUBLE_EQ(1.0, (pow(1.0, nan(""))));
574   ASSERT_TRUE(isnan(pow(2.0, nan(""))));
575   ASSERT_DOUBLE_EQ(8.0, pow(2.0, 3.0));
576 }
577
578 TEST(math, powf) {
579   ASSERT_TRUE(isnanf(powf(nanf(""), 3.0f)));
580   ASSERT_FLOAT_EQ(1.0f, (powf(1.0f, nanf(""))));
581   ASSERT_TRUE(isnanf(powf(2.0f, nanf(""))));
582   ASSERT_FLOAT_EQ(8.0f, powf(2.0f, 3.0f));
583 }
584
585 TEST(math, powl) {
586   ASSERT_TRUE(__isnanl(powl(nanl(""), 3.0L)));
587   ASSERT_DOUBLE_EQ(1.0L, (powl(1.0L, nanl(""))));
588   ASSERT_TRUE(__isnanl(powl(2.0L, nanl(""))));
589   ASSERT_DOUBLE_EQ(8.0L, powl(2.0L, 3.0L));
590 }
591
592 TEST(math, ceil) {
593   ASSERT_DOUBLE_EQ(1.0, ceil(0.9));
594 }
595
596 TEST(math, ceilf) {
597   ASSERT_FLOAT_EQ(1.0f, ceilf(0.9f));
598 }
599
600 TEST(math, ceill) {
601   ASSERT_DOUBLE_EQ(1.0L, ceill(0.9L));
602 }
603
604 TEST(math, floor) {
605   ASSERT_DOUBLE_EQ(1.0, floor(1.1));
606 }
607
608 TEST(math, floorf) {
609   ASSERT_FLOAT_EQ(1.0f, floorf(1.1f));
610 }
611
612 TEST(math, floorl) {
613   ASSERT_DOUBLE_EQ(1.0L, floorl(1.1L));
614 }
615
616 TEST(math, fabs) {
617   ASSERT_DOUBLE_EQ(1.0, fabs(-1.0));
618 }
619
620 TEST(math, fabsf) {
621   ASSERT_FLOAT_EQ(1.0f, fabsf(-1.0f));
622 }
623
624 TEST(math, fabsl) {
625   ASSERT_DOUBLE_EQ(1.0L, fabsl(-1.0L));
626 }
627
628 TEST(math, ldexp) {
629   ASSERT_DOUBLE_EQ(16.0, ldexp(2.0, 3.0));
630 }
631
632 TEST(math, ldexpf) {
633   ASSERT_FLOAT_EQ(16.0f, ldexpf(2.0f, 3.0f));
634 }
635
636 TEST(math, ldexpl) {
637   ASSERT_DOUBLE_EQ(16.0L, ldexpl(2.0L, 3.0));
638 }
639
640 TEST(math, fmod) {
641   ASSERT_DOUBLE_EQ(2.0, fmod(12.0, 10.0));
642 }
643
644 TEST(math, fmodf) {
645   ASSERT_FLOAT_EQ(2.0f, fmodf(12.0f, 10.0f));
646 }
647
648 TEST(math, fmodl) {
649   ASSERT_DOUBLE_EQ(2.0L, fmodl(12.0L, 10.0L));
650 }
651
652 TEST(math, remainder) {
653   ASSERT_DOUBLE_EQ(2.0, remainder(12.0, 10.0));
654 }
655
656 TEST(math, remainderf) {
657   ASSERT_FLOAT_EQ(2.0f, remainderf(12.0f, 10.0f));
658 }
659
660 TEST(math, remainderl) {
661   ASSERT_DOUBLE_EQ(2.0L, remainderl(12.0L, 10.0L));
662 }
663
664 TEST(math, drem) {
665   ASSERT_DOUBLE_EQ(2.0, drem(12.0, 10.0));
666 }
667
668 TEST(math, dremf) {
669   ASSERT_FLOAT_EQ(2.0f, dremf(12.0f, 10.0f));
670 }
671
672 TEST(math, fmax) {
673   ASSERT_DOUBLE_EQ(12.0, fmax(12.0, 10.0));
674   ASSERT_DOUBLE_EQ(12.0, fmax(12.0, nan("")));
675   ASSERT_DOUBLE_EQ(12.0, fmax(nan(""), 12.0));
676 }
677
678 TEST(math, fmaxf) {
679   ASSERT_FLOAT_EQ(12.0f, fmaxf(12.0f, 10.0f));
680   ASSERT_FLOAT_EQ(12.0f, fmaxf(12.0f, nanf("")));
681   ASSERT_FLOAT_EQ(12.0f, fmaxf(nanf(""), 12.0f));
682 }
683
684 TEST(math, fmaxl) {
685   ASSERT_DOUBLE_EQ(12.0L, fmaxl(12.0L, 10.0L));
686   ASSERT_DOUBLE_EQ(12.0L, fmaxl(12.0L, nanl("")));
687   ASSERT_DOUBLE_EQ(12.0L, fmaxl(nanl(""), 12.0L));
688 }
689
690 TEST(math, fmin) {
691   ASSERT_DOUBLE_EQ(10.0, fmin(12.0, 10.0));
692   ASSERT_DOUBLE_EQ(12.0, fmin(12.0, nan("")));
693   ASSERT_DOUBLE_EQ(12.0, fmin(nan(""), 12.0));
694 }
695
696 TEST(math, fminf) {
697   ASSERT_FLOAT_EQ(10.0f, fminf(12.0f, 10.0f));
698   ASSERT_FLOAT_EQ(12.0f, fminf(12.0f, nanf("")));
699   ASSERT_FLOAT_EQ(12.0f, fminf(nanf(""), 12.0f));
700 }
701
702 TEST(math, fminl) {
703   ASSERT_DOUBLE_EQ(10.0L, fminl(12.0L, 10.0L));
704   ASSERT_DOUBLE_EQ(12.0L, fminl(12.0L, nanl("")));
705   ASSERT_DOUBLE_EQ(12.0L, fminl(nanl(""), 12.0L));
706 }
707
708 TEST(math, fma) {
709   ASSERT_DOUBLE_EQ(10.0, fma(2.0, 3.0, 4.0));
710 }
711
712 TEST(math, fmaf) {
713   ASSERT_FLOAT_EQ(10.0f, fmaf(2.0f, 3.0f, 4.0f));
714 }
715
716 TEST(math, fmal) {
717   ASSERT_DOUBLE_EQ(10.0L, fmal(2.0L, 3.0L, 4.0L));
718 }
719
720 TEST(math, hypot) {
721   ASSERT_DOUBLE_EQ(5.0, hypot(3.0, 4.0));
722 }
723
724 TEST(math, hypotf) {
725   ASSERT_FLOAT_EQ(5.0f, hypotf(3.0f, 4.0f));
726 }
727
728 TEST(math, hypotl) {
729   ASSERT_DOUBLE_EQ(5.0L, hypotl(3.0L, 4.0L));
730 }
731
732 TEST(math, erf) {
733   ASSERT_DOUBLE_EQ(0.84270079294971489, erf(1.0));
734 }
735
736 TEST(math, erff) {
737   ASSERT_FLOAT_EQ(0.84270078f, erff(1.0f));
738 }
739
740 TEST(math, erfl) {
741   ASSERT_DOUBLE_EQ(0.84270079294971489L, erfl(1.0L));
742 }
743
744 TEST(math, erfc) {
745   ASSERT_DOUBLE_EQ(0.15729920705028513, erfc(1.0));
746 }
747
748 TEST(math, erfcf) {
749   ASSERT_FLOAT_EQ(0.15729921f, erfcf(1.0f));
750 }
751
752 TEST(math, erfcl) {
753   ASSERT_DOUBLE_EQ(0.15729920705028513l, erfcl(1.0L));
754 }
755
756 TEST(math, lrint) {
757   auto guard = make_scope_guard([]() {
758     fesetenv(FE_DFL_ENV);
759   });
760
761   fesetround(FE_UPWARD); // lrint/lrintf/lrintl obey the rounding mode.
762   ASSERT_EQ(1235, lrint(1234.01));
763   ASSERT_EQ(1235, lrintf(1234.01f));
764   ASSERT_EQ(1235, lrintl(1234.01L));
765   fesetround(FE_TOWARDZERO); // lrint/lrintf/lrintl obey the rounding mode.
766   ASSERT_EQ(1234, lrint(1234.01));
767   ASSERT_EQ(1234, lrintf(1234.01f));
768   ASSERT_EQ(1234, lrintl(1234.01L));
769
770   fesetround(FE_UPWARD); // llrint/llrintf/llrintl obey the rounding mode.
771   ASSERT_EQ(1235L, llrint(1234.01));
772   ASSERT_EQ(1235L, llrintf(1234.01f));
773   ASSERT_EQ(1235L, llrintl(1234.01L));
774   fesetround(FE_TOWARDZERO); // llrint/llrintf/llrintl obey the rounding mode.
775   ASSERT_EQ(1234L, llrint(1234.01));
776   ASSERT_EQ(1234L, llrintf(1234.01f));
777   ASSERT_EQ(1234L, llrintl(1234.01L));
778 }
779
780 TEST(math, rint) {
781   auto guard = make_scope_guard([]() {
782     fesetenv(FE_DFL_ENV);
783   });
784
785   fesetround(FE_UPWARD); // rint/rintf/rintl obey the rounding mode.
786   feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag.
787   ASSERT_EQ(1234.0, rint(1234.0));
788   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
789   ASSERT_EQ(1235.0, rint(1234.01));
790   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0);
791
792   feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag.
793   ASSERT_EQ(1234.0f, rintf(1234.0f));
794   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
795   ASSERT_EQ(1235.0f, rintf(1234.01f));
796   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0);
797
798   feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag.
799   ASSERT_EQ(1234.0, rintl(1234.0L));
800   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
801   ASSERT_EQ(1235.0, rintl(1234.01L));
802   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0);
803
804   fesetround(FE_TOWARDZERO); // rint/rintf obey the rounding mode.
805   ASSERT_EQ(1234.0, rint(1234.01));
806   ASSERT_EQ(1234.0f, rintf(1234.01f));
807   ASSERT_EQ(1234.0, rintl(1234.01L));
808 }
809
810 TEST(math, nearbyint) {
811   auto guard = make_scope_guard([]() {
812     fesetenv(FE_DFL_ENV);
813   });
814   fesetround(FE_UPWARD); // nearbyint/nearbyintf/nearbyintl obey the rounding mode.
815   feclearexcept(FE_ALL_EXCEPT); // nearbyint/nearbyintf/nearbyintl don't set the FE_INEXACT flag.
816   ASSERT_EQ(1234.0, nearbyint(1234.0));
817   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
818   ASSERT_EQ(1235.0, nearbyint(1234.01));
819   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
820
821   feclearexcept(FE_ALL_EXCEPT);
822   ASSERT_EQ(1234.0f, nearbyintf(1234.0f));
823   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
824   ASSERT_EQ(1235.0f, nearbyintf(1234.01f));
825   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
826
827   feclearexcept(FE_ALL_EXCEPT); // nearbyint/nearbyintf/nearbyintl don't set the FE_INEXACT flag.
828   ASSERT_EQ(1234.0, nearbyintl(1234.0L));
829   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
830   ASSERT_EQ(1235.0, nearbyintl(1234.01L));
831   ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
832
833   fesetround(FE_TOWARDZERO); // nearbyint/nearbyintf/nearbyintl obey the rounding mode.
834   ASSERT_EQ(1234.0, nearbyint(1234.01));
835   ASSERT_EQ(1234.0f, nearbyintf(1234.01f));
836   ASSERT_EQ(1234.0, nearbyintl(1234.01L));
837 }
838
839 TEST(math, lround) {
840   auto guard = make_scope_guard([]() {
841     fesetenv(FE_DFL_ENV);
842   });
843   fesetround(FE_UPWARD); // lround ignores the rounding mode.
844   ASSERT_EQ(1234, lround(1234.01));
845   ASSERT_EQ(1234, lroundf(1234.01f));
846   ASSERT_EQ(1234, lroundl(1234.01L));
847 }
848
849 TEST(math, llround) {
850   auto guard = make_scope_guard([]() {
851     fesetenv(FE_DFL_ENV);
852   });
853   fesetround(FE_UPWARD); // llround ignores the rounding mode.
854   ASSERT_EQ(1234L, llround(1234.01));
855   ASSERT_EQ(1234L, llroundf(1234.01f));
856   ASSERT_EQ(1234L, llroundl(1234.01L));
857 }
858
859 TEST(math, ilogb) {
860   ASSERT_EQ(FP_ILOGB0, ilogb(0.0));
861   ASSERT_EQ(FP_ILOGBNAN, ilogb(nan("")));
862   ASSERT_EQ(INT_MAX, ilogb(HUGE_VAL));
863   ASSERT_EQ(0, ilogb(1.0));
864   ASSERT_EQ(3, ilogb(10.0));
865 }
866
867 TEST(math, ilogbf) {
868   ASSERT_EQ(FP_ILOGB0, ilogbf(0.0f));
869   ASSERT_EQ(FP_ILOGBNAN, ilogbf(nanf("")));
870   ASSERT_EQ(INT_MAX, ilogbf(HUGE_VALF));
871   ASSERT_EQ(0, ilogbf(1.0f));
872   ASSERT_EQ(3, ilogbf(10.0f));
873 }
874
875 TEST(math, ilogbl) {
876   ASSERT_EQ(FP_ILOGB0, ilogbl(0.0L));
877   ASSERT_EQ(FP_ILOGBNAN, ilogbl(nanl("")));
878   ASSERT_EQ(INT_MAX, ilogbl(HUGE_VALL));
879   ASSERT_EQ(0L, ilogbl(1.0L));
880   ASSERT_EQ(3L, ilogbl(10.0L));
881 }
882
883 TEST(math, logb) {
884   ASSERT_EQ(-HUGE_VAL, logb(0.0));
885   ASSERT_TRUE(isnan(logb(nan(""))));
886   ASSERT_TRUE(isinf(logb(HUGE_VAL)));
887   ASSERT_EQ(0.0, logb(1.0));
888   ASSERT_EQ(3.0, logb(10.0));
889 }
890
891 TEST(math, logbf) {
892   ASSERT_EQ(-HUGE_VALF, logbf(0.0f));
893   ASSERT_TRUE(isnanf(logbf(nanf(""))));
894   ASSERT_TRUE(__isinff(logbf(HUGE_VALF)));
895   ASSERT_EQ(0.0f, logbf(1.0f));
896   ASSERT_EQ(3.0f, logbf(10.0f));
897 }
898
899 TEST(math, logbl) {
900   ASSERT_EQ(-HUGE_VAL, logbl(0.0L));
901   ASSERT_TRUE(isnan(logbl(nanl(""))));
902   ASSERT_TRUE(isinf(logbl(HUGE_VALL)));
903   ASSERT_EQ(0.0L, logbl(1.0L));
904   ASSERT_EQ(3.0L, logbl(10.0L));
905 }
906
907 TEST(math, log1p) {
908   ASSERT_EQ(-HUGE_VAL, log1p(-1.0));
909   ASSERT_TRUE(isnan(log1p(nan(""))));
910   ASSERT_TRUE(isinf(log1p(HUGE_VAL)));
911   ASSERT_DOUBLE_EQ(1.0, log1p(M_E - 1.0));
912 }
913
914 TEST(math, log1pf) {
915   ASSERT_EQ(-HUGE_VALF, log1pf(-1.0f));
916   ASSERT_TRUE(isnanf(log1pf(nanf(""))));
917   ASSERT_TRUE(__isinff(log1pf(HUGE_VALF)));
918   ASSERT_FLOAT_EQ(1.0f, log1pf(static_cast<float>(M_E) - 1.0f));
919 }
920
921 TEST(math, log1pl) {
922   ASSERT_EQ(-HUGE_VALL, log1pl(-1.0L));
923   ASSERT_TRUE(isnan(log1pl(nanl(""))));
924   ASSERT_TRUE(isinf(log1pl(HUGE_VALL)));
925   ASSERT_DOUBLE_EQ(1.0L, log1pl(M_E - 1.0L));
926 }
927
928 TEST(math, fdim) {
929   ASSERT_DOUBLE_EQ(0.0, fdim(1.0, 1.0));
930   ASSERT_DOUBLE_EQ(1.0, fdim(2.0, 1.0));
931   ASSERT_DOUBLE_EQ(0.0, fdim(1.0, 2.0));
932 }
933
934 TEST(math, fdimf) {
935   ASSERT_FLOAT_EQ(0.0f, fdimf(1.0f, 1.0f));
936   ASSERT_FLOAT_EQ(1.0f, fdimf(2.0f, 1.0f));
937   ASSERT_FLOAT_EQ(0.0f, fdimf(1.0f, 2.0f));
938 }
939
940 TEST(math, fdiml) {
941   ASSERT_DOUBLE_EQ(0.0L, fdiml(1.0L, 1.0L));
942   ASSERT_DOUBLE_EQ(1.0L, fdiml(2.0L, 1.0L));
943   ASSERT_DOUBLE_EQ(0.0L, fdiml(1.0L, 2.0L));
944 }
945
946 TEST(math, round) {
947   auto guard = make_scope_guard([]() {
948     fesetenv(FE_DFL_ENV);
949   });
950   fesetround(FE_TOWARDZERO); // round ignores the rounding mode and always rounds away from zero.
951   ASSERT_DOUBLE_EQ(1.0, round(0.5));
952   ASSERT_DOUBLE_EQ(-1.0, round(-0.5));
953   ASSERT_DOUBLE_EQ(0.0, round(0.0));
954   ASSERT_DOUBLE_EQ(-0.0, round(-0.0));
955   ASSERT_TRUE(isnan(round(nan(""))));
956   ASSERT_DOUBLE_EQ(HUGE_VAL, round(HUGE_VAL));
957 }
958
959 TEST(math, roundf) {
960   auto guard = make_scope_guard([]() {
961     fesetenv(FE_DFL_ENV);
962   });
963   fesetround(FE_TOWARDZERO); // roundf ignores the rounding mode and always rounds away from zero.
964   ASSERT_FLOAT_EQ(1.0f, roundf(0.5f));
965   ASSERT_FLOAT_EQ(-1.0f, roundf(-0.5f));
966   ASSERT_FLOAT_EQ(0.0f, roundf(0.0f));
967   ASSERT_FLOAT_EQ(-0.0f, roundf(-0.0f));
968   ASSERT_TRUE(isnanf(roundf(nanf(""))));
969   ASSERT_FLOAT_EQ(HUGE_VALF, roundf(HUGE_VALF));
970 }
971
972 TEST(math, roundl) {
973   auto guard = make_scope_guard([]() {
974     fesetenv(FE_DFL_ENV);
975   });
976   fesetround(FE_TOWARDZERO); // roundl ignores the rounding mode and always rounds away from zero.
977   ASSERT_DOUBLE_EQ(1.0L, roundl(0.5L));
978   ASSERT_DOUBLE_EQ(-1.0L, roundl(-0.5L));
979   ASSERT_DOUBLE_EQ(0.0L, roundl(0.0L));
980   ASSERT_DOUBLE_EQ(-0.0L, roundl(-0.0L));
981   ASSERT_TRUE(isnan(roundl(nanl(""))));
982   ASSERT_DOUBLE_EQ(HUGE_VALL, roundl(HUGE_VALL));
983 }
984
985 TEST(math, trunc) {
986   auto guard = make_scope_guard([]() {
987     fesetenv(FE_DFL_ENV);
988   });
989   fesetround(FE_UPWARD); // trunc ignores the rounding mode and always rounds toward zero.
990   ASSERT_DOUBLE_EQ(1.0, trunc(1.5));
991   ASSERT_DOUBLE_EQ(-1.0, trunc(-1.5));
992   ASSERT_DOUBLE_EQ(0.0, trunc(0.0));
993   ASSERT_DOUBLE_EQ(-0.0, trunc(-0.0));
994   ASSERT_TRUE(isnan(trunc(nan(""))));
995   ASSERT_DOUBLE_EQ(HUGE_VAL, trunc(HUGE_VAL));
996 }
997
998 TEST(math, truncf) {
999   auto guard = make_scope_guard([]() {
1000     fesetenv(FE_DFL_ENV);
1001   });
1002   fesetround(FE_UPWARD); // truncf ignores the rounding mode and always rounds toward zero.
1003   ASSERT_FLOAT_EQ(1.0f, truncf(1.5f));
1004   ASSERT_FLOAT_EQ(-1.0f, truncf(-1.5f));
1005   ASSERT_FLOAT_EQ(0.0f, truncf(0.0f));
1006   ASSERT_FLOAT_EQ(-0.0f, truncf(-0.0f));
1007   ASSERT_TRUE(isnan(truncf(nanf(""))));
1008   ASSERT_FLOAT_EQ(HUGE_VALF, truncf(HUGE_VALF));
1009 }
1010
1011 TEST(math, truncl) {
1012   auto guard = make_scope_guard([]() {
1013     fesetenv(FE_DFL_ENV);
1014   });
1015   fesetround(FE_UPWARD); // truncl ignores the rounding mode and always rounds toward zero.
1016   ASSERT_DOUBLE_EQ(1.0L, truncl(1.5L));
1017   ASSERT_DOUBLE_EQ(-1.0L, truncl(-1.5L));
1018   ASSERT_DOUBLE_EQ(0.0L, truncl(0.0L));
1019   ASSERT_DOUBLE_EQ(-0.0L, truncl(-0.0L));
1020   ASSERT_TRUE(isnan(truncl(nan(""))));
1021   ASSERT_DOUBLE_EQ(HUGE_VALL, truncl(HUGE_VALL));
1022 }
1023
1024 TEST(math, nextafter) {
1025   ASSERT_DOUBLE_EQ(0.0, nextafter(0.0, 0.0));
1026   ASSERT_DOUBLE_EQ(4.9406564584124654e-324, nextafter(0.0, 1.0));
1027   ASSERT_DOUBLE_EQ(-4.9406564584124654e-324, nextafter(0.0, -1.0));
1028 }
1029
1030 TEST(math, nextafterf) {
1031   ASSERT_FLOAT_EQ(0.0f, nextafterf(0.0f, 0.0f));
1032   ASSERT_FLOAT_EQ(1.4012985e-45f, nextafterf(0.0f, 1.0f));
1033   ASSERT_FLOAT_EQ(-1.4012985e-45f, nextafterf(0.0f, -1.0f));
1034 }
1035
1036 TEST(math, nextafterl) {
1037   ASSERT_DOUBLE_EQ(0.0L, nextafterl(0.0L, 0.0L));
1038   // Use a runtime value to accomodate the case when
1039   // sizeof(double) == sizeof(long double)
1040   long double smallest_positive = ldexpl(1.0L, LDBL_MIN_EXP - LDBL_MANT_DIG);
1041   ASSERT_DOUBLE_EQ(smallest_positive, nextafterl(0.0L, 1.0L));
1042   ASSERT_DOUBLE_EQ(-smallest_positive, nextafterl(0.0L, -1.0L));
1043 }
1044
1045 TEST(math, nexttoward) {
1046   ASSERT_DOUBLE_EQ(0.0, nexttoward(0.0, 0.0L));
1047   ASSERT_DOUBLE_EQ(4.9406564584124654e-324, nexttoward(0.0, 1.0L));
1048   ASSERT_DOUBLE_EQ(-4.9406564584124654e-324, nexttoward(0.0, -1.0L));
1049 }
1050
1051 TEST(math, nexttowardf) {
1052   ASSERT_FLOAT_EQ(0.0f, nexttowardf(0.0f, 0.0L));
1053   ASSERT_FLOAT_EQ(1.4012985e-45f, nexttowardf(0.0f, 1.0L));
1054   ASSERT_FLOAT_EQ(-1.4012985e-45f, nexttowardf(0.0f, -1.0L));
1055 }
1056
1057 TEST(math, nexttowardl) {
1058   ASSERT_DOUBLE_EQ(0.0L, nexttowardl(0.0L, 0.0L));
1059   // Use a runtime value to accomodate the case when
1060   // sizeof(double) == sizeof(long double)
1061   long double smallest_positive = ldexpl(1.0L, LDBL_MIN_EXP - LDBL_MANT_DIG);
1062   ASSERT_DOUBLE_EQ(smallest_positive, nexttowardl(0.0L, 1.0L));
1063   ASSERT_DOUBLE_EQ(-smallest_positive, nexttowardl(0.0L, -1.0L));
1064 }
1065
1066 TEST(math, copysign) {
1067   ASSERT_DOUBLE_EQ(0.0, copysign(0.0, 1.0));
1068   ASSERT_DOUBLE_EQ(-0.0, copysign(0.0, -1.0));
1069   ASSERT_DOUBLE_EQ(2.0, copysign(2.0, 1.0));
1070   ASSERT_DOUBLE_EQ(-2.0, copysign(2.0, -1.0));
1071 }
1072
1073 TEST(math, copysignf) {
1074   ASSERT_FLOAT_EQ(0.0f, copysignf(0.0f, 1.0f));
1075   ASSERT_FLOAT_EQ(-0.0f, copysignf(0.0f, -1.0f));
1076   ASSERT_FLOAT_EQ(2.0f, copysignf(2.0f, 1.0f));
1077   ASSERT_FLOAT_EQ(-2.0f, copysignf(2.0f, -1.0f));
1078 }
1079
1080 TEST(math, copysignl) {
1081   ASSERT_DOUBLE_EQ(0.0L, copysignl(0.0L, 1.0L));
1082   ASSERT_DOUBLE_EQ(-0.0L, copysignl(0.0L, -1.0L));
1083   ASSERT_DOUBLE_EQ(2.0L, copysignl(2.0L, 1.0L));
1084   ASSERT_DOUBLE_EQ(-2.0L, copysignl(2.0L, -1.0L));
1085 }
1086
1087 TEST(math, significand) {
1088   ASSERT_DOUBLE_EQ(0.0, significand(0.0));
1089   ASSERT_DOUBLE_EQ(1.2, significand(1.2));
1090   ASSERT_DOUBLE_EQ(1.53125, significand(12.25));
1091 }
1092
1093 TEST(math, significandf) {
1094   ASSERT_FLOAT_EQ(0.0f, significandf(0.0f));
1095   ASSERT_FLOAT_EQ(1.2f, significandf(1.2f));
1096   ASSERT_FLOAT_EQ(1.53125f, significandf(12.25f));
1097 }
1098
1099 TEST(math, significandl) {
1100   ASSERT_DOUBLE_EQ(0.0L, significandl(0.0L));
1101   ASSERT_DOUBLE_EQ(1.2L, significandl(1.2L));
1102   ASSERT_DOUBLE_EQ(1.53125L, significandl(12.25L));
1103 }
1104
1105 TEST(math, scalb) {
1106   ASSERT_DOUBLE_EQ(12.0, scalb(3.0, 2.0));
1107 }
1108
1109 TEST(math, scalbf) {
1110   ASSERT_FLOAT_EQ(12.0f, scalbf(3.0f, 2.0f));
1111 }
1112
1113 TEST(math, scalbln) {
1114   ASSERT_DOUBLE_EQ(12.0, scalbln(3.0, 2L));
1115 }
1116
1117 TEST(math, scalblnf) {
1118   ASSERT_FLOAT_EQ(12.0f, scalblnf(3.0f, 2L));
1119 }
1120
1121 TEST(math, scalblnl) {
1122   ASSERT_DOUBLE_EQ(12.0L, scalblnl(3.0L, 2L));
1123 }
1124
1125 TEST(math, scalbn) {
1126   ASSERT_DOUBLE_EQ(12.0, scalbn(3.0, 2));
1127 }
1128
1129 TEST(math, scalbnf) {
1130   ASSERT_FLOAT_EQ(12.0f, scalbnf(3.0f, 2));
1131 }
1132
1133 TEST(math, scalbnl) {
1134   ASSERT_DOUBLE_EQ(12.0L, scalbnl(3.0L, 2));
1135 }
1136
1137 TEST(math, gamma) {
1138   ASSERT_DOUBLE_EQ(log(24.0), gamma(5.0));
1139 }
1140
1141 TEST(math, gammaf) {
1142   ASSERT_FLOAT_EQ(logf(24.0f), gammaf(5.0f));
1143 }
1144
1145 TEST(math, gamma_r) {
1146 #if defined(__BIONIC__)
1147   int sign;
1148   ASSERT_DOUBLE_EQ(log(24.0), gamma_r(5.0, &sign));
1149   ASSERT_EQ(1, sign);
1150 #else // __BIONIC__
1151   GTEST_LOG_(INFO) << "glibc doesn't have gamma_r.\n";
1152 #endif // __BIONIC__
1153 }
1154
1155 TEST(math, gammaf_r) {
1156 #if defined(__BIONIC__)
1157   int sign;
1158   ASSERT_FLOAT_EQ(logf(24.0f), gammaf_r(5.0f, &sign));
1159   ASSERT_EQ(1, sign);
1160 #else // __BIONIC__
1161   GTEST_LOG_(INFO) << "glibc doesn't have gammaf_r.\n";
1162 #endif // __BIONIC__
1163 }
1164
1165 TEST(math, lgamma) {
1166   ASSERT_DOUBLE_EQ(log(24.0), lgamma(5.0));
1167 }
1168
1169 TEST(math, lgammaf) {
1170   ASSERT_FLOAT_EQ(logf(24.0f), lgammaf(5.0f));
1171 }
1172
1173 TEST(math, lgammal) {
1174   ASSERT_DOUBLE_EQ(logl(24.0L), lgammal(5.0L));
1175 }
1176
1177 TEST(math, lgamma_r) {
1178   int sign;
1179   ASSERT_DOUBLE_EQ(log(24.0), lgamma_r(5.0, &sign));
1180   ASSERT_EQ(1, sign);
1181 }
1182
1183 TEST(math, lgamma_r_17471883) {
1184   int sign;
1185
1186   sign = 0;
1187   ASSERT_DOUBLE_EQ(HUGE_VAL, lgamma_r(0.0, &sign));
1188   ASSERT_EQ(1, sign);
1189   sign = 0;
1190   ASSERT_DOUBLE_EQ(HUGE_VAL, lgamma_r(-0.0, &sign));
1191   ASSERT_EQ(-1, sign);
1192 }
1193
1194 TEST(math, lgammaf_r) {
1195   int sign;
1196   ASSERT_FLOAT_EQ(logf(24.0f), lgammaf_r(5.0f, &sign));
1197   ASSERT_EQ(1, sign);
1198 }
1199
1200 TEST(math, lgammaf_r_17471883) {
1201   int sign;
1202
1203   sign = 0;
1204   ASSERT_FLOAT_EQ(HUGE_VALF, lgammaf_r(0.0f, &sign));
1205   ASSERT_EQ(1, sign);
1206   sign = 0;
1207   ASSERT_FLOAT_EQ(HUGE_VALF, lgammaf_r(-0.0f, &sign));
1208   ASSERT_EQ(-1, sign);
1209 }
1210
1211 TEST(math, lgammal_r) {
1212   int sign;
1213   ASSERT_DOUBLE_EQ(log(24.0L), lgamma_r(5.0L, &sign));
1214   ASSERT_EQ(1, sign);
1215 }
1216
1217 TEST(math, lgammal_r_17471883) {
1218   int sign;
1219
1220   sign = 0;
1221   ASSERT_DOUBLE_EQ(HUGE_VAL, lgammal_r(0.0L, &sign));
1222   ASSERT_EQ(1, sign);
1223   sign = 0;
1224   ASSERT_DOUBLE_EQ(HUGE_VAL, lgammal_r(-0.0L, &sign));
1225   ASSERT_EQ(-1, sign);
1226 }
1227
1228 TEST(math, tgamma) {
1229   ASSERT_DOUBLE_EQ(24.0, tgamma(5.0));
1230 }
1231
1232 TEST(math, tgammaf) {
1233   ASSERT_FLOAT_EQ(24.0f, tgammaf(5.0f));
1234 }
1235
1236 TEST(math, tgammal) {
1237   ASSERT_DOUBLE_EQ(24.0L, tgammal(5.0L));
1238 }
1239
1240 TEST(math, j0) {
1241   ASSERT_DOUBLE_EQ(1.0, j0(0.0));
1242   ASSERT_DOUBLE_EQ(0.76519768655796661, j0(1.0));
1243 }
1244
1245 TEST(math, j0f) {
1246   ASSERT_FLOAT_EQ(1.0f, j0f(0.0f));
1247   ASSERT_FLOAT_EQ(0.76519769f, j0f(1.0f));
1248 }
1249
1250 TEST(math, j1) {
1251   ASSERT_DOUBLE_EQ(0.0, j1(0.0));
1252   ASSERT_DOUBLE_EQ(0.44005058574493355, j1(1.0));
1253 }
1254
1255 TEST(math, j1f) {
1256   ASSERT_FLOAT_EQ(0.0f, j1f(0.0f));
1257   ASSERT_FLOAT_EQ(0.44005057f, j1f(1.0f));
1258 }
1259
1260 TEST(math, jn) {
1261   ASSERT_DOUBLE_EQ(0.0, jn(4, 0.0));
1262   ASSERT_DOUBLE_EQ(0.0024766389641099553, jn(4, 1.0));
1263 }
1264
1265 TEST(math, jnf) {
1266   ASSERT_FLOAT_EQ(0.0f, jnf(4, 0.0f));
1267   ASSERT_FLOAT_EQ(0.0024766389f, jnf(4, 1.0f));
1268 }
1269
1270 TEST(math, y0) {
1271   ASSERT_DOUBLE_EQ(-HUGE_VAL, y0(0.0));
1272   ASSERT_DOUBLE_EQ(0.08825696421567697, y0(1.0));
1273 }
1274
1275 TEST(math, y0f) {
1276   ASSERT_FLOAT_EQ(-HUGE_VALF, y0f(0.0f));
1277   ASSERT_FLOAT_EQ(0.088256963f, y0f(1.0f));
1278 }
1279
1280 TEST(math, y1) {
1281   ASSERT_DOUBLE_EQ(-HUGE_VAL, y1(0.0));
1282   ASSERT_DOUBLE_EQ(-0.78121282130028868, y1(1.0));
1283 }
1284
1285 TEST(math, y1f) {
1286   ASSERT_FLOAT_EQ(-HUGE_VALF, y1f(0.0f));
1287   ASSERT_FLOAT_EQ(-0.78121281f, y1f(1.0f));
1288 }
1289
1290 TEST(math, yn) {
1291   ASSERT_DOUBLE_EQ(-HUGE_VAL, yn(4, 0.0));
1292   ASSERT_DOUBLE_EQ(-33.278423028972114, yn(4, 1.0));
1293 }
1294
1295 TEST(math, ynf) {
1296   ASSERT_FLOAT_EQ(-HUGE_VALF, ynf(4, 0.0f));
1297   ASSERT_FLOAT_EQ(-33.278423f, ynf(4, 1.0f));
1298 }
1299
1300 TEST(math, frexp) {
1301   int exp;
1302   double dr = frexp(1024.0, &exp);
1303   ASSERT_DOUBLE_EQ(1024.0, scalbn(dr, exp));
1304 }
1305
1306 TEST(math, frexpf) {
1307   int exp;
1308   float fr = frexpf(1024.0f, &exp);
1309   ASSERT_FLOAT_EQ(1024.0f, scalbnf(fr, exp));
1310 }
1311
1312 TEST(math, frexpl) {
1313   int exp;
1314   long double ldr = frexpl(1024.0L, &exp);
1315   ASSERT_DOUBLE_EQ(1024.0L, scalbnl(ldr, exp));
1316 }
1317
1318 TEST(math, modf) {
1319   double di;
1320   double df = modf(123.75, &di);
1321   ASSERT_DOUBLE_EQ(123.0, di);
1322   ASSERT_DOUBLE_EQ(0.75, df);
1323 }
1324
1325 TEST(math, modff) {
1326   float fi;
1327   float ff = modff(123.75f, &fi);
1328   ASSERT_FLOAT_EQ(123.0f, fi);
1329   ASSERT_FLOAT_EQ(0.75f, ff);
1330 }
1331
1332 TEST(math, modfl) {
1333   long double ldi;
1334   long double ldf = modfl(123.75L, &ldi);
1335   ASSERT_DOUBLE_EQ(123.0L, ldi);
1336   ASSERT_DOUBLE_EQ(0.75L, ldf);
1337 }
1338
1339 TEST(math, remquo) {
1340   int q;
1341   double d = remquo(13.0, 4.0, &q);
1342   ASSERT_EQ(3, q);
1343   ASSERT_DOUBLE_EQ(1.0, d);
1344 }
1345
1346 TEST(math, remquof) {
1347   int q;
1348   float f = remquof(13.0f, 4.0f, &q);
1349   ASSERT_EQ(3, q);
1350   ASSERT_FLOAT_EQ(1.0, f);
1351 }
1352
1353 TEST(math, remquol) {
1354   int q;
1355   long double ld = remquol(13.0L, 4.0L, &q);
1356   ASSERT_DOUBLE_EQ(3L, q);
1357   ASSERT_DOUBLE_EQ(1.0L, ld);
1358 }
1359
1360 // https://code.google.com/p/android/issues/detail?id=6697
1361 TEST(math, frexpf_public_bug_6697) {
1362   int exp;
1363   float fr = frexpf(14.1f, &exp);
1364   ASSERT_FLOAT_EQ(14.1f, scalbnf(fr, exp));
1365 }
1366
1367 TEST(math, exp2_STRICT_ALIGN_OpenBSD_bug) {
1368   // OpenBSD/x86's libm had a bug here, but it was already fixed in FreeBSD:
1369   // http://svnweb.FreeBSD.org/base/head/lib/msun/src/math_private.h?revision=240827&view=markup
1370   ASSERT_DOUBLE_EQ(5.0, exp2(log2(5)));
1371   ASSERT_FLOAT_EQ(5.0f, exp2f(log2f(5)));
1372   ASSERT_DOUBLE_EQ(5.0L, exp2l(log2l(5)));
1373 }
1374
1375 TEST(math, nextafterl_OpenBSD_bug) {
1376   // OpenBSD/x86's libm had a bug here.
1377   ASSERT_TRUE(nextafter(1.0, 0.0) - 1.0 < 0.0);
1378   ASSERT_TRUE(nextafterf(1.0f, 0.0f) - 1.0f < 0.0f);
1379   ASSERT_TRUE(nextafterl(1.0L, 0.0L) - 1.0L < 0.0L);
1380 }
1381
1382 #include "math_cos_intel_data.h"
1383 TEST(math, cos_intel) {
1384   DoMathDataTest<1>(g_cos_intel_data, cos);
1385 }
1386
1387 #include "math_cosf_intel_data.h"
1388 TEST(math, cosf_intel) {
1389   DoMathDataTest<1>(g_cosf_intel_data, cosf);
1390 }
1391
1392 #include "math_exp_intel_data.h"
1393 TEST(math, exp_intel) {
1394   DoMathDataTest<1>(g_exp_intel_data, exp);
1395 }
1396
1397 #include "math_expf_intel_data.h"
1398 TEST(math, expf_intel) {
1399   DoMathDataTest<1>(g_expf_intel_data, expf);
1400 }
1401
1402 #include "math_log_intel_data.h"
1403 TEST(math, log_intel) {
1404   DoMathDataTest<1>(g_log_intel_data, log);
1405 }
1406
1407 #include "math_logf_intel_data.h"
1408 TEST(math, logf_intel) {
1409   DoMathDataTest<1>(g_logf_intel_data, logf);
1410 }
1411
1412 #include "math_pow_intel_data.h"
1413 TEST(math, pow_intel) {
1414   DoMathDataTest<1>(g_pow_intel_data, pow);
1415 }
1416
1417 #include "math_powf_intel_data.h"
1418 TEST(math, powf_intel) {
1419   DoMathDataTest<1>(g_powf_intel_data, powf);
1420 }
1421
1422 #include "math_sin_intel_data.h"
1423 TEST(math, sin_intel) {
1424   DoMathDataTest<1>(g_sin_intel_data, sin);
1425 }
1426
1427 #include "math_sincos_intel_data.h"
1428 TEST(math, sincos_intel) {
1429   DoMathDataTest<1>(g_sincos_intel_data, sincos);
1430 }
1431
1432 #include "math_sincosf_intel_data.h"
1433 TEST(math, sincosf_intel) {
1434   DoMathDataTest<1>(g_sincosf_intel_data, sincosf);
1435 }
1436
1437 #include "math_sinf_intel_data.h"
1438 TEST(math, sinf_intel) {
1439   DoMathDataTest<1>(g_sinf_intel_data, sinf);
1440 }
1441
1442 #include "math_tan_intel_data.h"
1443 TEST(math, tan_intel) {
1444   DoMathDataTest<1>(g_tan_intel_data, tan);
1445 }
1446
1447 #include "math_tanf_intel_data.h"
1448 TEST(math, tanf_intel) {
1449   DoMathDataTest<1>(g_tanf_intel_data, tanf);
1450 }