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 __llrintl = llrintl
  32 #if defined(__sparcv9) || defined(__amd64)
  33 #pragma weak lrintl = llrintl
  34 #pragma weak __lrintl = llrintl
  35 #endif
  36 
  37 #include "libm.h"
  38 
  39 #if defined(__sparc)
  40 #include "fma.h"
  41 #include "fenv_inlines.h"
  42 
  43 long long
  44 llrintl(long double x)
  45 {
  46         union {
  47                 unsigned i[4];
  48                 long double q;
  49         } xx;
  50         union {
  51                 unsigned i[2];
  52                 long long l;
  53         } zz;
  54         union {
  55                 unsigned i;
  56                 float f;
  57         } tt;
  58 
  59         unsigned int hx, sx, frac, fsr;
  60         int rm, j;
  61         volatile float dummy;
  62 
  63         xx.q = x;
  64         sx = xx.i[0] & 0x80000000;
  65         hx = xx.i[0] & ~0x80000000;
  66 
  67         /* handle trivial cases */
  68         if (hx > 0x403e0000) {               /* |x| > 2^63 + ... or x is nan */
  69                 /* convert an out-of-range float */
  70                 tt.i = sx | 0x7f000000;
  71                 return ((long long)tt.f);
  72         } else if ((hx | xx.i[1] | xx.i[2] | xx.i[3]) == 0) {   /* x is zero */
  73                 return (0LL);
  74         }
  75 
  76         /* get the rounding mode */
  77         __fenv_getfsr32(&fsr);
  78         rm = fsr >> 30;
  79 
  80         /* flip the sense of directed roundings if x is negative */
  81         if (sx)
  82                 rm ^= rm >> 1;
  83 
  84         /* handle |x| < 1 */
  85         if (hx < 0x3fff0000) {
  86                 dummy = 1.0e30f;        /* x is nonzero, so raise inexact */
  87                 dummy += 1.0e-30f;
  88 
  89                 if (rm == FSR_RP || (rm == FSR_RN && (hx >= 0x3ffe0000 && ((hx &
  90                     0xffff) | xx.i[1] | xx.i[2] | xx.i[3]))))
  91                         return (sx ? -1LL : 1LL);
  92 
  93                 return (0LL);
  94         }
  95 
  96         /* extract the integer and fractional parts of x */
  97         j = 0x406f - (hx >> 16);
  98         xx.i[0] = 0x10000 | (xx.i[0] & 0xffff);
  99 
 100         if (j >= 96) {
 101                 zz.i[0] = 0;
 102                 zz.i[1] = xx.i[0] >> (j - 96);
 103                 frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96));
 104 
 105                 if (((xx.i[1] << 1) << (127 - j)) | xx.i[2] | xx.i[3])
 106                         frac |= 1;
 107         } else if (j >= 64) {
 108                 zz.i[0] = xx.i[0] >> (j - 64);
 109                 zz.i[1] = ((xx.i[0] << 1) << (95 - j)) | (xx.i[1] >> (j - 64));
 110                 frac = ((xx.i[1] << 1) << (95 - j)) | (xx.i[2] >> (j - 64));
 111 
 112                 if (((xx.i[2] << 1) << (95 - j)) | xx.i[3])
 113                         frac |= 1;
 114         } else {
 115                 zz.i[0] = ((xx.i[0] << 1) << (63 - j)) | (xx.i[1] >> (j - 32));
 116                 zz.i[1] = ((xx.i[1] << 1) << (63 - j)) | (xx.i[2] >> (j - 32));
 117                 frac = ((xx.i[2] << 1) << (63 - j)) | (xx.i[3] >> (j - 32));
 118 
 119                 if ((xx.i[3] << 1) << (63 - j))
 120                         frac |= 1;
 121         }
 122 
 123         /* round */
 124         if (frac && (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 125             (frac == 0x80000000 && (zz.i[1] & 1)))))) {
 126                 if (++zz.i[1] == 0)
 127                         zz.i[0]++;
 128         }
 129 
 130         /* check for result out of range (note that z is |x| at this point) */
 131         if (zz.i[0] > 0x80000000u || (zz.i[0] == 0x80000000 && (zz.i[1] ||
 132             !sx))) {
 133                 tt.i = sx | 0x7f000000;
 134                 return ((long long)tt.f);
 135         }
 136 
 137         /* raise inexact if need be */
 138         if (frac) {
 139                 dummy = 1.0e30F;
 140                 dummy += 1.0e-30F;
 141         }
 142 
 143         /* negate result if need be */
 144         if (sx) {
 145                 zz.i[0] = ~zz.i[0];
 146                 zz.i[1] = -zz.i[1];
 147 
 148                 if (zz.i[1] == 0)
 149                         zz.i[0]++;
 150         }
 151 
 152         return (zz.l);
 153 }
 154 #elif defined(__x86)
 155 long long
 156 llrintl(long double x)
 157 {
 158         /*
 159          * Note: The following code works on x86 (in the default rounding
 160          * precision mode), but one ought to just use the fistpll instruction
 161          * instead.
 162          */
 163         union {
 164                 unsigned i[3];
 165                 long double e;
 166         } xx, yy;
 167 
 168         int ex;
 169 
 170         xx.e = x;
 171         ex = xx.i[2] & 0x7fff;
 172 
 173         if (ex < 0x403e) {           /* |x| < 2^63 */
 174                 /* add and subtract a power of two to round x to an integer */
 175                 yy.i[2] = (xx.i[2] & 0x8000) | 0x403e;
 176                 yy.i[1] = 0x80000000;
 177                 yy.i[0] = 0;
 178                 x = (x + yy.e) - yy.e;
 179         }
 180 
 181         /* now x is nan, inf, or integral */
 182         return ((long long)x);
 183 }
 184 #else
 185 #error Unknown architecture
 186 #endif