1 | /* s_modfl.c -- long double version of s_modf.c. |
2 | * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz. |
3 | */ |
4 | |
5 | /* |
6 | * ==================================================== |
7 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
8 | * |
9 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
10 | * Permission to use, copy, modify, and distribute this |
11 | * software is freely granted, provided that this notice |
12 | * is preserved. |
13 | * ==================================================== |
14 | */ |
15 | |
16 | #if defined(LIBM_SCCS) && !defined(lint) |
17 | static char rcsid[] = "$NetBSD: $" ; |
18 | #endif |
19 | |
20 | /* |
21 | * modfl(long double x, long double *iptr) |
22 | * return fraction part of x, and return x's integral part in *iptr. |
23 | * Method: |
24 | * Bit twiddling. |
25 | * |
26 | * Exception: |
27 | * No exception. |
28 | */ |
29 | |
30 | #include <math.h> |
31 | #include <math_private.h> |
32 | #include <libm-alias-ldouble.h> |
33 | |
34 | static const _Float128 one = 1.0; |
35 | |
36 | _Float128 __modfl(_Float128 x, _Float128 *iptr) |
37 | { |
38 | int64_t i0,i1,j0; |
39 | uint64_t i; |
40 | GET_LDOUBLE_WORDS64(i0,i1,x); |
41 | j0 = ((i0>>48)&0x7fff)-0x3fff; /* exponent of x */ |
42 | if(j0<48) { /* integer part in high x */ |
43 | if(j0<0) { /* |x|<1 */ |
44 | /* *iptr = +-0 */ |
45 | SET_LDOUBLE_WORDS64(*iptr,i0&0x8000000000000000ULL,0); |
46 | return x; |
47 | } else { |
48 | i = (0x0000ffffffffffffLL)>>j0; |
49 | if(((i0&i)|i1)==0) { /* x is integral */ |
50 | *iptr = x; |
51 | /* return +-0 */ |
52 | SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0); |
53 | return x; |
54 | } else { |
55 | SET_LDOUBLE_WORDS64(*iptr,i0&(~i),0); |
56 | return x - *iptr; |
57 | } |
58 | } |
59 | } else if (j0>111) { /* no fraction part */ |
60 | *iptr = x*one; |
61 | /* We must handle NaNs separately. */ |
62 | if (j0 == 0x4000 && ((i0 & 0x0000ffffffffffffLL) | i1)) |
63 | return x*one; |
64 | /* return +-0 */ |
65 | SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0); |
66 | return x; |
67 | } else { /* fraction part in low x */ |
68 | i = -1ULL>>(j0-48); |
69 | if((i1&i)==0) { /* x is integral */ |
70 | *iptr = x; |
71 | /* return +-0 */ |
72 | SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0); |
73 | return x; |
74 | } else { |
75 | SET_LDOUBLE_WORDS64(*iptr,i0,i1&(~i)); |
76 | return x - *iptr; |
77 | } |
78 | } |
79 | } |
80 | libm_alias_ldouble (__modf, modf) |
81 | |