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