]>
Commit | Line | Data |
---|---|---|
f7eac6eb RM |
1 | /* @(#)s_modf.c 5.1 93/09/24 */ |
2 | /* | |
3 | * ==================================================== | |
4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | |
5 | * | |
6 | * Developed at SunPro, a Sun Microsystems, Inc. business. | |
7 | * Permission to use, copy, modify, and distribute this | |
cccda09f | 8 | * software is freely granted, provided that this notice |
f7eac6eb RM |
9 | * is preserved. |
10 | * ==================================================== | |
11 | */ | |
12 | ||
13 | #if defined(LIBM_SCCS) && !defined(lint) | |
14 | static char rcsid[] = "$NetBSD: s_modf.c,v 1.8 1995/05/10 20:47:55 jtc Exp $"; | |
15 | #endif | |
16 | ||
17 | /* | |
cccda09f | 18 | * modf(double x, double *iptr) |
f7eac6eb RM |
19 | * return fraction part of x, and return x's integral part in *iptr. |
20 | * Method: | |
21 | * Bit twiddling. | |
22 | * | |
23 | * Exception: | |
24 | * No exception. | |
25 | */ | |
26 | ||
27 | #include "math.h" | |
28 | #include "math_private.h" | |
29 | ||
30 | #ifdef __STDC__ | |
31 | static const double one = 1.0; | |
32 | #else | |
33 | static double one = 1.0; | |
34 | #endif | |
35 | ||
36 | #ifdef __STDC__ | |
37 | double __modf(double x, double *iptr) | |
38 | #else | |
39 | double __modf(x, iptr) | |
40 | double x,*iptr; | |
41 | #endif | |
42 | { | |
43 | int32_t i0,i1,j0; | |
44 | u_int32_t i; | |
45 | EXTRACT_WORDS(i0,i1,x); | |
46 | j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */ | |
47 | if(j0<20) { /* integer part in high x */ | |
48 | if(j0<0) { /* |x|<1 */ | |
49 | INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */ | |
50 | return x; | |
51 | } else { | |
52 | i = (0x000fffff)>>j0; | |
53 | if(((i0&i)|i1)==0) { /* x is integral */ | |
f7eac6eb | 54 | *iptr = x; |
c57abfa7 | 55 | INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */ |
f7eac6eb RM |
56 | return x; |
57 | } else { | |
58 | INSERT_WORDS(*iptr,i0&(~i),0); | |
59 | return x - *iptr; | |
60 | } | |
61 | } | |
62 | } else if (j0>51) { /* no fraction part */ | |
f7eac6eb | 63 | *iptr = x*one; |
c57abfa7 UD |
64 | /* We must handle NaNs separately. */ |
65 | if (j0 == 0x400 && ((i0 & 0xfffff) | i1)) | |
66 | return x*one; | |
67 | INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */ | |
f7eac6eb RM |
68 | return x; |
69 | } else { /* fraction part in low x */ | |
70 | i = ((u_int32_t)(0xffffffff))>>(j0-20); | |
71 | if((i1&i)==0) { /* x is integral */ | |
f7eac6eb | 72 | *iptr = x; |
f752bfe3 | 73 | INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */ |
f7eac6eb RM |
74 | return x; |
75 | } else { | |
76 | INSERT_WORDS(*iptr,i0,i1&(~i)); | |
77 | return x - *iptr; | |
78 | } | |
79 | } | |
80 | } | |
81 | weak_alias (__modf, modf) | |
cccda09f UD |
82 | #ifdef NO_LONG_DOUBLE |
83 | strong_alias (__modf, __modfl) | |
84 | weak_alias (__modf, modfl) | |
85 | #endif |