1 /* $OpenBSD: lrint_test.c,v 1.3 2019/02/21 18:14:16 bluhm Exp $ */ 2 /*- 3 * Copyright (c) 2005-2008 David Schultz <das@FreeBSD.org> 4 * All rights reserved. 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 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * Test for lrint(), lrintf(), llrint(), and llrintf(). 30 */ 31 32 #include <sys/cdefs.h> 33 /* $FreeBSD: head/lib/msun/tests/lrint_test.c 314650 2017-03-04 10:07:46Z ngie $ */ 34 35 #include <assert.h> 36 #include <fenv.h> 37 #include <limits.h> 38 #include <math.h> 39 #include <stdio.h> 40 41 /* 42 * XXX The volatile here is to avoid gcc's bogus constant folding and work 43 * around the lack of support for the FENV_ACCESS pragma. 44 */ 45 #define test(func, x, result, excepts) do { \ 46 volatile double _d = x; \ 47 assert(feclearexcept(FE_ALL_EXCEPT) == 0); \ 48 assert((func)(_d) == (result) || fetestexcept(FE_INVALID)); \ 49 assert(fetestexcept(FE_ALL_EXCEPT) == (excepts)); \ 50 } while (0) 51 52 #define testall(x, result, excepts) do { \ 53 test(lrint, x, result, excepts); \ 54 test(lrintf, x, result, excepts); \ 55 test(lrintl, x, result, excepts); \ 56 test(llrint, x, result, excepts); \ 57 test(llrintf, x, result, excepts); \ 58 test(llrintl, x, result, excepts); \ 59 } while (0) 60 61 #define IGNORE 0 62 63 #pragma STDC FENV_ACCESS ON 64 65 static void 66 run_tests(void) 67 { 68 69 assert(fesetround(FE_DOWNWARD) == 0); 70 testall(0.75, 0, FE_INEXACT); 71 testall(-0.5, -1, FE_INEXACT); 72 73 assert(fesetround(FE_TONEAREST) == 0); 74 testall(0.0, 0, 0); 75 testall(0.25, 0, FE_INEXACT); 76 testall(0.5, 0, FE_INEXACT); 77 testall(-2.5, -2, FE_INEXACT); 78 testall(1.0, 1, 0); 79 testall(0x12345000p0, 0x12345000, 0); 80 testall(0x1234.fp0, 0x1235, FE_INEXACT); 81 testall(INFINITY, IGNORE, FE_INVALID); 82 testall(NAN, IGNORE, FE_INVALID); 83 84 #if (LONG_MAX == 0x7fffffffl) 85 assert(fesetround(FE_UPWARD) == 0); 86 test(lrint, 0x7fffffff.8p0, IGNORE, FE_INVALID); 87 test(lrint, -0x80000000.4p0, -0x80000000l, FE_INEXACT); 88 89 assert(fesetround(FE_DOWNWARD) == 0); 90 test(lrint, -0x80000000.8p0, IGNORE, FE_INVALID); 91 test(lrint, 0x80000000.0p0, IGNORE, FE_INVALID); 92 test(lrint, 0x7fffffff.4p0, 0x7fffffffl, FE_INEXACT); 93 test(lrintf, 0x80000000.0p0f, IGNORE, FE_INVALID); 94 test(lrintf, 0x7fffff80.0p0f, 0x7fffff80l, 0); 95 96 assert(fesetround(FE_TOWARDZERO) == 0); 97 test(lrint, 0x7fffffff.8p0, 0x7fffffffl, FE_INEXACT); 98 test(lrint, -0x80000000.8p0, -0x80000000l, FE_INEXACT); 99 test(lrint, 0x80000000.0p0, IGNORE, FE_INVALID); 100 test(lrintf, 0x80000000.0p0f, IGNORE, FE_INVALID); 101 test(lrintf, 0x7fffff80.0p0f, 0x7fffff80l, 0); 102 #elif (LONG_MAX == 0x7fffffffffffffffll) 103 assert(fesetround(FE_TONEAREST) == 0); 104 test(lrint, 0x8000000000000000.0p0, IGNORE, FE_INVALID); 105 test(lrintf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID); 106 test(lrint, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00l, 0); 107 test(lrintf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000l, 0); 108 test(lrint, -0x8000000000000800.0p0, IGNORE, FE_INVALID); 109 test(lrintf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID); 110 test(lrint, -0x8000000000000000.0p0, -0x8000000000000000l, 0); 111 test(lrintf, -0x8000000000000000.0p0f, -0x8000000000000000l, 0); 112 #else 113 #error "Unsupported long size" 114 #endif 115 116 #if (LLONG_MAX == 0x7fffffffffffffffLL) 117 assert(fesetround(FE_TONEAREST) == 0); 118 test(llrint, 0x8000000000000000.0p0, IGNORE, FE_INVALID); 119 test(llrintf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID); 120 test(llrint, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00ll, 0); 121 test(llrintf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000ll, 0); 122 test(llrint, -0x8000000000000800.0p0, IGNORE, FE_INVALID); 123 test(llrintf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID); 124 test(llrint, -0x8000000000000000.0p0, -0x8000000000000000ll, 0); 125 test(llrintf, -0x8000000000000000.0p0f, -0x8000000000000000ll, 0); 126 #else 127 #error "Unsupported long long size" 128 #endif 129 } 130 131 int 132 main(void) 133 { 134 135 printf("1..1\n"); 136 137 run_tests(); 138 139 printf("ok 1 - lrint\n"); 140 141 return (0); 142 } 143