1/*- 2 * Copyright (c) 2010 David Schultz <das@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27/* 28 * Tests for nearbyint{,f,l}() 29 * 30 * TODO: 31 * - adapt tests for rint(3) 32 * - tests for harder values (more mantissa bits than float) 33 * - tests in other rounding modes 34 */ 35 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD$"); 38 39#include <assert.h> 40#include <fenv.h> 41#include <math.h> 42#include <stdio.h> 43 44#define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \ 45 FE_OVERFLOW | FE_UNDERFLOW) 46 47/* 48 * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0. 49 * Fail an assertion if they differ. 50 */ 51static int 52fpequal(long double d1, long double d2) 53{ 54 55 if (d1 != d2) 56 return (isnan(d1) && isnan(d2)); 57 return (copysignl(1.0, d1) == copysignl(1.0, d2)); 58} 59 60static void testit(int testnum, float in, float out) 61{ 62 63 feclearexcept(ALL_STD_EXCEPT); 64 assert(fpequal(out, nearbyintf(in))); 65 assert(fpequal(-out, nearbyintf(-in))); 66 assert(fetestexcept(ALL_STD_EXCEPT) == 0); 67 68 assert(fpequal(out, nearbyint(in))); 69 assert(fpequal(-out, nearbyint(-in))); 70 assert(fetestexcept(ALL_STD_EXCEPT) == 0); 71 72 assert(fpequal(out, nearbyintl(in))); 73 assert(fpequal(-out, nearbyintl(-in))); 74 assert(fetestexcept(ALL_STD_EXCEPT) == 0); 75 76 printf("ok %d\t\t# nearbyint(%g)\n", testnum, in); 77} 78 79static const float tests[] = { 80/* input output (expected) */ 81 0.0, 0.0, 82 0.5, 0.0, 83 M_PI, 3, 84 65536.5, 65536, 85 INFINITY, INFINITY, 86 NAN, NAN, 87}; 88 89int 90main(int argc, char *argv[]) 91{ 92 static const int ntests = sizeof(tests) / sizeof(tests[0]) / 2; 93 int i; 94 95 printf("1..%d\n", ntests); 96 for (i = 0; i < ntests; i++) 97 testit(i + 1, tests[i * 2], tests[i * 2 + 1]); 98 99 return (0); 100} 101