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 | #if defined(LIBM_SCCS) && !defined(lint) |
16 | static char rcsid[] = "$NetBSD: $" ; |
17 | #endif |
18 | |
19 | /* |
20 | * modfl(long double x, long double *iptr) |
21 | * return fraction part of x, and return x's integral part in *iptr. |
22 | * Method: |
23 | * Bit twiddling. |
24 | * |
25 | * Exception: |
26 | * No exception. |
27 | */ |
28 | |
29 | #include <math.h> |
30 | #include <math_private.h> |
31 | #include <libm-alias-ldouble.h> |
32 | |
33 | static const _Float128 one = 1.0; |
34 | |
35 | _Float128 __modfl(_Float128 x, _Float128 *iptr) |
36 | { |
37 | int64_t i0,i1,j0; |
38 | uint64_t i; |
39 | GET_LDOUBLE_WORDS64(i0,i1,x); |
40 | j0 = ((i0>>48)&0x7fff)-0x3fff; /* exponent of x */ |
41 | if(j0<48) { /* integer part in high x */ |
42 | if(j0<0) { /* |x|<1 */ |
43 | /* *iptr = +-0 */ |
44 | SET_LDOUBLE_WORDS64(*iptr,i0&0x8000000000000000ULL,0); |
45 | return x; |
46 | } else { |
47 | i = (0x0000ffffffffffffLL)>>j0; |
48 | if(((i0&i)|i1)==0) { /* x is integral */ |
49 | *iptr = x; |
50 | /* return +-0 */ |
51 | SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0); |
52 | return x; |
53 | } else { |
54 | SET_LDOUBLE_WORDS64(*iptr,i0&(~i),0); |
55 | return x - *iptr; |
56 | } |
57 | } |
58 | } else if (j0>111) { /* no fraction part */ |
59 | *iptr = x*one; |
60 | /* We must handle NaNs separately. */ |
61 | if (j0 == 0x4000 && ((i0 & 0x0000ffffffffffffLL) | i1)) |
62 | return x*one; |
63 | /* return +-0 */ |
64 | SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0); |
65 | return x; |
66 | } else { /* fraction part in low x */ |
67 | i = -1ULL>>(j0-48); |
68 | if((i1&i)==0) { /* x is integral */ |
69 | *iptr = x; |
70 | /* return +-0 */ |
71 | SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0); |
72 | return x; |
73 | } else { |
74 | SET_LDOUBLE_WORDS64(*iptr,i0,i1&(~i)); |
75 | return x - *iptr; |
76 | } |
77 | } |
78 | } |
79 | libm_alias_ldouble (__modf, modf) |
80 | |