Print this page
11210 libm should be cstyle(1ONBLD) clean


   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 #pragma weak __lrintl = lrintl
  31 
  32 #include <sys/isa_defs.h> /* _ILP32 */
  33 #include "libm.h"
  34 
  35 #if defined(_ILP32)
  36 #if defined(__sparc)
  37 
  38 #include "fma.h"
  39 #include "fenv_inlines.h"
  40 
  41 long
  42 lrintl(long double x) {

  43         union {
  44                 unsigned int i[4];
  45                 long double q;
  46         } xx;
  47         union {
  48                 unsigned int i;
  49                 float f;
  50         } tt;

  51         unsigned int hx, sx, frac, l, 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 > 0x401e0000) { /* |x| > 2^31 + ... or x is nan */
  61                 /* convert an out-of-range float */
  62                 tt.i = sx | 0x7f000000;
  63                 return ((long) tt.f);
  64         } else if ((hx | xx.i[1] | xx.i[2] | xx.i[3]) == 0) /* x is zero */
  65                 return (0L);

  66 
  67         /* get the rounding mode */
  68         __fenv_getfsr32(&fsr);
  69         rm = fsr >> 30;
  70 
  71         /* flip the sense of directed roundings if x is negative */
  72         if (sx)
  73                 rm ^= rm >> 1;
  74 
  75         /* handle |x| < 1 */
  76         if (hx < 0x3fff0000) {
  77                 dummy = 1.0e30F; /* x is nonzero, so raise inexact */
  78                 dummy += 1.0e-30F;
  79                 if (rm == FSR_RP || (rm == FSR_RN && (hx >= 0x3ffe0000 &&
  80                         ((hx & 0xffff) | xx.i[1] | xx.i[2] | xx.i[3]))))

  81                         return (sx ? -1L : 1L);

  82                 return (0L);
  83         }
  84 
  85         /* extract the integer and fractional parts of x */
  86         j = 0x406f - (hx >> 16);          /* 91 <= j <= 112 */
  87         xx.i[0] = 0x10000 | (xx.i[0] & 0xffff);

  88         if (j >= 96) {                               /* 96 <= j <= 112 */
  89                 l = xx.i[0] >> (j - 96);
  90                 frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96));

  91                 if (((xx.i[1] << 1) << (127 - j)) | xx.i[2] | xx.i[3])
  92                         frac |= 1;
  93         } else {                                /* 91 <= j <= 95 */
  94                 l = (xx.i[0] << (96 - j)) | (xx.i[1] >> (j - 64));
  95                 frac = (xx.i[1] << (96 - j)) | (xx.i[2] >> (j - 64));

  96                 if ((xx.i[2] << (96 - j)) | xx.i[3])
  97                         frac |= 1;
  98         }
  99 
 100         /* round */
 101         if (frac && (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000U ||
 102                 (frac == 0x80000000 && (l & 1))))))
 103                 l++;
 104 
 105         /* check for result out of range (note that z is |x| at this point) */
 106         if (l > 0x80000000U || (l == 0x80000000U && !sx)) {
 107                 tt.i = sx | 0x7f000000;
 108                 return ((long) tt.f);
 109         }
 110 
 111         /* raise inexact if need be */
 112         if (frac) {
 113                 dummy = 1.0e30F;
 114                 dummy += 1.0e-30F;
 115         }
 116 
 117         /* negate result if need be */
 118         if (sx)
 119                 l = -l;
 120         return ((long) l);

 121 }
 122 #elif defined(__x86)
 123 long
 124 lrintl(long double x) {

 125         /*
 126          * Note: The following code works on x86 (in the default rounding
 127          * precision mode), but one ought to just use the fistpl instruction
 128          * instead.
 129          */
 130         union {
 131                 unsigned i[3];
 132                 long double e;
 133         } xx, yy;

 134         int ex;
 135 
 136         xx.e = x;
 137         ex = xx.i[2] & 0x7fff;

 138         if (ex < 0x403e) {   /* |x| < 2^63 */
 139                 /* add and subtract a power of two to round x to an integer */
 140                 yy.i[2] = (xx.i[2] & 0x8000) | 0x403e;
 141                 yy.i[1] = 0x80000000;
 142                 yy.i[0] = 0;
 143                 x = (x + yy.e) - yy.e;
 144         }
 145 
 146         /* now x is nan, inf, or integral */
 147         return ((long) x);
 148 }
 149 #else
 150 #error Unknown architecture
 151 #endif  /* defined(__sparc) || defined(__x86) */
 152 #else
 153 #error Unsupported architecture
 154 #endif  /* defined(_ILP32) */


   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 __lrintl = lrintl
  32 
  33 #include <sys/isa_defs.h>         /* _ILP32 */
  34 #include "libm.h"
  35 
  36 #if defined(_ILP32)
  37 #if defined(__sparc)

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