1*25c28e83SPiotr Jasiukajtis /* 2*25c28e83SPiotr Jasiukajtis * CDDL HEADER START 3*25c28e83SPiotr Jasiukajtis * 4*25c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the 5*25c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License"). 6*25c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License. 7*25c28e83SPiotr Jasiukajtis * 8*25c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*25c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing. 10*25c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions 11*25c28e83SPiotr Jasiukajtis * and limitations under the License. 12*25c28e83SPiotr Jasiukajtis * 13*25c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each 14*25c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*25c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the 16*25c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying 17*25c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner] 18*25c28e83SPiotr Jasiukajtis * 19*25c28e83SPiotr Jasiukajtis * CDDL HEADER END 20*25c28e83SPiotr Jasiukajtis */ 21*25c28e83SPiotr Jasiukajtis 22*25c28e83SPiotr Jasiukajtis /* 23*25c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24*25c28e83SPiotr Jasiukajtis */ 25*25c28e83SPiotr Jasiukajtis /* 26*25c28e83SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 27*25c28e83SPiotr Jasiukajtis * Use is subject to license terms. 28*25c28e83SPiotr Jasiukajtis */ 29*25c28e83SPiotr Jasiukajtis 30*25c28e83SPiotr Jasiukajtis #pragma weak nearbyint = __nearbyint 31*25c28e83SPiotr Jasiukajtis 32*25c28e83SPiotr Jasiukajtis /* 33*25c28e83SPiotr Jasiukajtis * nearbyint(x) returns the nearest fp integer to x in the direction 34*25c28e83SPiotr Jasiukajtis * corresponding to the current rounding direction without raising 35*25c28e83SPiotr Jasiukajtis * the inexact exception. 36*25c28e83SPiotr Jasiukajtis * 37*25c28e83SPiotr Jasiukajtis * nearbyint(x) is x unchanged if x is +/-0 or +/-inf. If x is NaN, 38*25c28e83SPiotr Jasiukajtis * nearbyint(x) is also NaN. 39*25c28e83SPiotr Jasiukajtis */ 40*25c28e83SPiotr Jasiukajtis 41*25c28e83SPiotr Jasiukajtis #include "libm.h" 42*25c28e83SPiotr Jasiukajtis #include <fenv.h> 43*25c28e83SPiotr Jasiukajtis 44*25c28e83SPiotr Jasiukajtis double 45*25c28e83SPiotr Jasiukajtis __nearbyint(double x) { 46*25c28e83SPiotr Jasiukajtis union { 47*25c28e83SPiotr Jasiukajtis unsigned i[2]; 48*25c28e83SPiotr Jasiukajtis double d; 49*25c28e83SPiotr Jasiukajtis } xx; 50*25c28e83SPiotr Jasiukajtis unsigned hx, sx, i, frac; 51*25c28e83SPiotr Jasiukajtis int rm, j; 52*25c28e83SPiotr Jasiukajtis 53*25c28e83SPiotr Jasiukajtis xx.d = x; 54*25c28e83SPiotr Jasiukajtis sx = xx.i[HIWORD] & 0x80000000; 55*25c28e83SPiotr Jasiukajtis hx = xx.i[HIWORD] & ~0x80000000; 56*25c28e83SPiotr Jasiukajtis 57*25c28e83SPiotr Jasiukajtis /* handle trivial cases */ 58*25c28e83SPiotr Jasiukajtis if (hx >= 0x43300000) { /* x is nan, inf, or already integral */ 59*25c28e83SPiotr Jasiukajtis if (hx >= 0x7ff00000) /* x is inf or nan */ 60*25c28e83SPiotr Jasiukajtis #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN) 61*25c28e83SPiotr Jasiukajtis return (hx >= 0x7ff80000 ? x : x + x); 62*25c28e83SPiotr Jasiukajtis /* assumes sparc-like QNaN */ 63*25c28e83SPiotr Jasiukajtis #else 64*25c28e83SPiotr Jasiukajtis return (x + x); 65*25c28e83SPiotr Jasiukajtis #endif 66*25c28e83SPiotr Jasiukajtis return (x); 67*25c28e83SPiotr Jasiukajtis } else if ((hx | xx.i[LOWORD]) == 0) /* x is zero */ 68*25c28e83SPiotr Jasiukajtis return (x); 69*25c28e83SPiotr Jasiukajtis 70*25c28e83SPiotr Jasiukajtis /* get the rounding mode */ 71*25c28e83SPiotr Jasiukajtis rm = fegetround(); 72*25c28e83SPiotr Jasiukajtis 73*25c28e83SPiotr Jasiukajtis /* flip the sense of directed roundings if x is negative */ 74*25c28e83SPiotr Jasiukajtis if (sx && (rm == FE_UPWARD || rm == FE_DOWNWARD)) 75*25c28e83SPiotr Jasiukajtis rm = (FE_UPWARD + FE_DOWNWARD) - rm; 76*25c28e83SPiotr Jasiukajtis 77*25c28e83SPiotr Jasiukajtis /* handle |x| < 1 */ 78*25c28e83SPiotr Jasiukajtis if (hx < 0x3ff00000) { 79*25c28e83SPiotr Jasiukajtis if (rm == FE_UPWARD || (rm == FE_TONEAREST && 80*25c28e83SPiotr Jasiukajtis (hx >= 0x3fe00000 && ((hx & 0xfffff) | xx.i[LOWORD])))) 81*25c28e83SPiotr Jasiukajtis xx.i[HIWORD] = sx | 0x3ff00000; 82*25c28e83SPiotr Jasiukajtis else 83*25c28e83SPiotr Jasiukajtis xx.i[HIWORD] = sx; 84*25c28e83SPiotr Jasiukajtis xx.i[LOWORD] = 0; 85*25c28e83SPiotr Jasiukajtis return (xx.d); 86*25c28e83SPiotr Jasiukajtis } 87*25c28e83SPiotr Jasiukajtis 88*25c28e83SPiotr Jasiukajtis /* round x at the integer bit */ 89*25c28e83SPiotr Jasiukajtis j = 0x433 - (hx >> 20); 90*25c28e83SPiotr Jasiukajtis if (j >= 32) { 91*25c28e83SPiotr Jasiukajtis i = 1 << (j - 32); 92*25c28e83SPiotr Jasiukajtis frac = ((xx.i[HIWORD] << 1) << (63 - j)) | 93*25c28e83SPiotr Jasiukajtis (xx.i[LOWORD] >> (j - 32)); 94*25c28e83SPiotr Jasiukajtis if (xx.i[LOWORD] & (i - 1)) 95*25c28e83SPiotr Jasiukajtis frac |= 1; 96*25c28e83SPiotr Jasiukajtis if (!frac) 97*25c28e83SPiotr Jasiukajtis return (x); 98*25c28e83SPiotr Jasiukajtis xx.i[LOWORD] = 0; 99*25c28e83SPiotr Jasiukajtis xx.i[HIWORD] &= ~(i - 1); 100*25c28e83SPiotr Jasiukajtis if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) && 101*25c28e83SPiotr Jasiukajtis ((frac > 0x80000000u) || ((frac == 0x80000000) && 102*25c28e83SPiotr Jasiukajtis (xx.i[HIWORD] & i))))) 103*25c28e83SPiotr Jasiukajtis xx.i[HIWORD] += i; 104*25c28e83SPiotr Jasiukajtis } else { 105*25c28e83SPiotr Jasiukajtis i = 1 << j; 106*25c28e83SPiotr Jasiukajtis frac = (xx.i[LOWORD] << 1) << (31 - j); 107*25c28e83SPiotr Jasiukajtis if (!frac) 108*25c28e83SPiotr Jasiukajtis return (x); 109*25c28e83SPiotr Jasiukajtis xx.i[LOWORD] &= ~(i - 1); 110*25c28e83SPiotr Jasiukajtis if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) && 111*25c28e83SPiotr Jasiukajtis (frac > 0x80000000u || ((frac == 0x80000000) && 112*25c28e83SPiotr Jasiukajtis (xx.i[LOWORD] & i))))) { 113*25c28e83SPiotr Jasiukajtis xx.i[LOWORD] += i; 114*25c28e83SPiotr Jasiukajtis if (xx.i[LOWORD] == 0) 115*25c28e83SPiotr Jasiukajtis xx.i[HIWORD]++; 116*25c28e83SPiotr Jasiukajtis } 117*25c28e83SPiotr Jasiukajtis } 118*25c28e83SPiotr Jasiukajtis return (xx.d); 119*25c28e83SPiotr Jasiukajtis } 120