1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak nearbyintl = __nearbyintl
  32 
  33 #include "libm.h"
  34 #include "fma.h"
  35 #include "fenv_inlines.h"
  36 
  37 #if defined(__sparc)
  38 static union {
  39         unsigned i;
  40         float f;
  41 } snan = { 0x7f800001 };
  42 long double
  43 __nearbyintl(long double x)
  44 {
  45         union {
  46                 unsigned i[4];
  47                 long double q;
  48         } xx;
  49 
  50         unsigned hx, sx, i, frac;
  51         unsigned int fsr;
  52         int rm, j;
  53         volatile float dummy;
  54 
  55         xx.q = x;
  56         sx = xx.i[0] & 0x80000000;
  57         hx = xx.i[0] & ~0x80000000;
  58 
  59         /* handle trivial cases */
  60         if (hx >= 0x406f0000) {              /* x is nan, inf, or already integral */
  61                 /* check for signaling nan */
  62                 if ((hx > 0x7fff0000 || (hx == 0x7fff0000 && (xx.i[1] |
  63                     xx.i[2] | xx.i[3]))) && !(hx & 0x8000)) {
  64                         dummy = snan.f;
  65                         dummy += snan.f;
  66                         xx.i[0] = sx | hx | 0x8000;
  67                 }
  68 
  69                 return (xx.q);
  70         } else if ((hx | xx.i[1] | xx.i[2] | xx.i[3]) == 0) {   /* x is zero */
  71                 return (x);
  72         }
  73 
  74         /* get the rounding mode */
  75         __fenv_getfsr32(&fsr);
  76         rm = fsr >> 30;
  77 
  78         /* flip the sense of directed roundings if x is negative */
  79         if (sx)
  80                 rm ^= rm >> 1;
  81 
  82         /* handle |x| < 1 */
  83         if (hx < 0x3fff0000) {
  84                 if (rm == FSR_RP || (rm == FSR_RN && (hx >= 0x3ffe0000 && ((hx &
  85                     0xffff) | xx.i[1] | xx.i[2] | xx.i[3]))))
  86                         xx.i[0] = sx | 0x3fff0000;
  87                 else
  88                         xx.i[0] = sx;
  89 
  90                 xx.i[1] = xx.i[2] = xx.i[3] = 0;
  91                 return (xx.q);
  92         }
  93 
  94         /* round x at the integer bit */
  95         j = 0x406f - (hx >> 16);
  96 
  97         if (j >= 96) {
  98                 i = 1 << (j - 96);
  99                 frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96));
 100 
 101                 if ((xx.i[1] & (i - 1)) | xx.i[2] | xx.i[3])
 102                         frac |= 1;
 103 
 104                 if (!frac)
 105                         return (x);
 106 
 107                 xx.i[1] = xx.i[2] = xx.i[3] = 0;
 108                 xx.i[0] &= ~(i - 1);
 109 
 110                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 111                     (frac == 0x80000000 && (xx.i[0] & i)))))
 112                         xx.i[0] += i;
 113         } else if (j >= 64) {
 114                 i = 1 << (j - 64);
 115                 frac = ((xx.i[1] << 1) << (95 - j)) | (xx.i[2] >> (j - 64));
 116 
 117                 if ((xx.i[2] & (i - 1)) | xx.i[3])
 118                         frac |= 1;
 119 
 120                 if (!frac)
 121                         return (x);
 122 
 123                 xx.i[2] = xx.i[3] = 0;
 124                 xx.i[1] &= ~(i - 1);
 125 
 126                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 127                     (frac == 0x80000000 && (xx.i[1] & i))))) {
 128                         xx.i[1] += i;
 129 
 130                         if (xx.i[1] == 0)
 131                                 xx.i[0]++;
 132                 }
 133         } else if (j >= 32) {
 134                 i = 1 << (j - 32);
 135                 frac = ((xx.i[2] << 1) << (63 - j)) | (xx.i[3] >> (j - 32));
 136 
 137                 if (xx.i[3] & (i - 1))
 138                         frac |= 1;
 139 
 140                 if (!frac)
 141                         return (x);
 142 
 143                 xx.i[3] = 0;
 144                 xx.i[2] &= ~(i - 1);
 145 
 146                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 147                     (frac == 0x80000000 && (xx.i[2] & i))))) {
 148                         xx.i[2] += i;
 149 
 150                         if (xx.i[2] == 0)
 151                                 if (++xx.i[1] == 0)
 152                                         xx.i[0]++;
 153                 }
 154         } else {
 155                 i = 1 << j;
 156                 frac = (xx.i[3] << 1) << (31 - j);
 157 
 158                 if (!frac)
 159                         return (x);
 160 
 161                 xx.i[3] &= ~(i - 1);
 162 
 163                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 164                     (frac == 0x80000000 && (xx.i[3] & i))))) {
 165                         xx.i[3] += i;
 166 
 167                         if (xx.i[3] == 0)
 168                                 if (++xx.i[2] == 0)
 169                                         if (++xx.i[1] == 0)
 170                                                 xx.i[0]++;
 171                 }
 172         }
 173 
 174         return (xx.q);
 175 }
 176 #elif defined(__x86)
 177 /* inline template */
 178 extern long double frndint(long double);
 179 long double
 180 __nearbyintl(long double x)
 181 {
 182         long double z;
 183         unsigned oldcwsw, cwsw;
 184 
 185         /* save the control and status words, mask the inexact exception */
 186         __fenv_getcwsw(&oldcwsw);
 187         cwsw = oldcwsw | 0x00200000;
 188         __fenv_setcwsw(&cwsw);
 189 
 190         z = frndint(x);
 191 
 192         /*
 193          * restore the control and status words, preserving all but the
 194          * inexact flag
 195          */
 196         __fenv_getcwsw(&cwsw);
 197         oldcwsw |= (cwsw & 0x1f);
 198         __fenv_setcwsw(&oldcwsw);
 199 
 200         return (z);
 201 }
 202 #else
 203 #error Unknown architecture
 204 #endif