genericopenlibs/openenvcore/libm/src/s_modf.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*--------------------------------------------------------------------
       
     2  *© Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
     3  *--------------------------------------------------------------------
       
     4 */
       
     5 /* @(#)s_modf.c 5.1 93/09/24 */
       
     6 /*
       
     7  * ====================================================
       
     8  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
       
     9  *
       
    10  * Developed at SunPro, a Sun Microsystems, Inc. business.
       
    11  * Permission to use, copy, modify, and distribute this
       
    12  * software is freely granted, provided that this notice
       
    13  * is preserved.
       
    14  * ====================================================
       
    15  */
       
    16 #ifndef __SYMBIAN32__
       
    17 #ifndef lint
       
    18 static char rcsid[] = "$FreeBSD: src/lib/msun/src/s_modf.c,v 1.7 2002/05/28 18:15:04 alfred Exp $";
       
    19 #endif
       
    20 #endif //__SYMBIAN32__
       
    21 
       
    22 /*
       
    23  * modf(double x, double *iptr)
       
    24  * return fraction part of x, and return x's integral part in *iptr.
       
    25  * Method:
       
    26  *	Bit twiddling.
       
    27  *
       
    28  * Exception:
       
    29  *	No exception.
       
    30  */
       
    31 
       
    32 #include <math.h>
       
    33 #include "math_private.h"
       
    34 
       
    35 static const double one = 1.0;
       
    36 
       
    37 EXPORT_C double modf(double x, double *iptr)
       
    38 {
       
    39 	int32_t i0,i1,j0;
       
    40 	u_int32_t i;
       
    41 	EXTRACT_WORDS(i0,i1,x);
       
    42 	/*if x is NAN, return NAN with iptr = NAN*/
       
    43 	#ifdef __SYMBIAN32__	
       
    44 	if ( __isnan(x) )
       
    45 	{	*iptr = x;
       
    46 	 	return x;
       
    47 	}
       
    48 	#endif //__SYMBIAN32__
       
    49 	j0 = ((i0>>20)&0x7ff)-0x3ff;	/* exponent of x */
       
    50 	if(j0<20) {			/* integer part in high x */
       
    51 	    if(j0<0) {			/* |x|<1 */
       
    52 	        INSERT_WORDS(*iptr,i0&0x80000000,0);	/* *iptr = +-0 */
       
    53 		return x;
       
    54 	    } else {
       
    55 		i = (0x000fffff)>>j0;
       
    56 		if(((i0&i)|i1)==0) {		/* x is integral */
       
    57 		    u_int32_t high;
       
    58 		    *iptr = x;
       
    59 		    GET_HIGH_WORD(high,x);
       
    60 		    INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
       
    61 		    return x;
       
    62 		} else {
       
    63 		    INSERT_WORDS(*iptr,i0&(~i),0);
       
    64 		    return x - *iptr;
       
    65 		}
       
    66 	    }
       
    67 	} else if (j0>51) {		/* no fraction part */
       
    68 	    u_int32_t high;
       
    69 	    *iptr = x*one;
       
    70 	    GET_HIGH_WORD(high,x);
       
    71 	    INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
       
    72 	    return x;
       
    73 	} else {			/* fraction part in low x */
       
    74 	    i = ((u_int32_t)(0xffffffff))>>(j0-20);
       
    75 	    if((i1&i)==0) { 		/* x is integral */
       
    76 	        u_int32_t high;
       
    77 		*iptr = x;
       
    78 		GET_HIGH_WORD(high,x);
       
    79 		INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
       
    80 		return x;
       
    81 	    } else {
       
    82 	        INSERT_WORDS(*iptr,i0,i1&(~i));
       
    83 		return x - *iptr;
       
    84 	    }
       
    85 	}
       
    86 }