| 1 | /* s_modfl.c -- long double version of s_modf.c. |
| 2 | */ |
| 3 | |
| 4 | /* |
| 5 | * ==================================================== |
| 6 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
| 7 | * |
| 8 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
| 9 | * Permission to use, copy, modify, and distribute this |
| 10 | * software is freely granted, provided that this notice |
| 11 | * is preserved. |
| 12 | * ==================================================== |
| 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * modfl(long double x, long double *iptr) |
| 17 | * return fraction part of x, and return x's integral part in *iptr. |
| 18 | * Method: |
| 19 | * Bit twiddling. |
| 20 | * |
| 21 | * Exception: |
| 22 | * No exception. |
| 23 | */ |
| 24 | |
| 25 | #include <math.h> |
| 26 | #include <math_private.h> |
| 27 | #include <libm-alias-ldouble.h> |
| 28 | |
| 29 | static const long double one = 1.0; |
| 30 | |
| 31 | long double |
| 32 | __modfl(long double x, long double *iptr) |
| 33 | { |
| 34 | int32_t i0,i1,j0; |
| 35 | uint32_t i,se; |
| 36 | GET_LDOUBLE_WORDS(se,i0,i1,x); |
| 37 | j0 = (se&0x7fff)-0x3fff; /* exponent of x */ |
| 38 | if(j0<32) { /* integer part in high x */ |
| 39 | if(j0<0) { /* |x|<1 */ |
| 40 | SET_LDOUBLE_WORDS(*iptr,se&0x8000,0,0); /* *iptr = +-0 */ |
| 41 | return x; |
| 42 | } else { |
| 43 | i = (0x7fffffff)>>j0; |
| 44 | if(((i0&i)|i1)==0) { /* x is integral */ |
| 45 | *iptr = x; |
| 46 | SET_LDOUBLE_WORDS(x,se&0x8000,0,0); /* return +-0 */ |
| 47 | return x; |
| 48 | } else { |
| 49 | SET_LDOUBLE_WORDS(*iptr,se,i0&(~i),0); |
| 50 | return x - *iptr; |
| 51 | } |
| 52 | } |
| 53 | } else if (__builtin_expect(j0>63, 0)) { /* no fraction part */ |
| 54 | *iptr = x*one; |
| 55 | /* We must handle NaNs separately. */ |
| 56 | if (j0 == 0x4000 && ((i0 & 0x7fffffff) | i1)) |
| 57 | return x*one; |
| 58 | SET_LDOUBLE_WORDS(x,se&0x8000,0,0); /* return +-0 */ |
| 59 | return x; |
| 60 | } else { /* fraction part in low x */ |
| 61 | i = ((uint32_t)(0x7fffffff))>>(j0-32); |
| 62 | if((i1&i)==0) { /* x is integral */ |
| 63 | *iptr = x; |
| 64 | SET_LDOUBLE_WORDS(x,se&0x8000,0,0); /* return +-0 */ |
| 65 | return x; |
| 66 | } else { |
| 67 | SET_LDOUBLE_WORDS(*iptr,se,i0,i1&(~i)); |
| 68 | return x - *iptr; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | libm_alias_ldouble (__modf, modf) |
| 73 | |