Blame sysdeps/ieee754/ldbl-128ibm/s_modfl.c

Packit 6c4009
/* s_modfl.c -- long double version of s_modf.c.
Packit 6c4009
 * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * ====================================================
Packit 6c4009
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Developed at SunPro, a Sun Microsystems, Inc. business.
Packit 6c4009
 * Permission to use, copy, modify, and distribute this
Packit 6c4009
 * software is freely granted, provided that this notice
Packit 6c4009
 * is preserved.
Packit 6c4009
 * ====================================================
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#if defined(LIBM_SCCS) && !defined(lint)
Packit 6c4009
static char rcsid[] = "$NetBSD: $";
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * modfl(long double x, long double *iptr)
Packit 6c4009
 * return fraction part of x, and return x's integral part in *iptr.
Packit 6c4009
 * Method:
Packit 6c4009
 *	Bit twiddling.
Packit 6c4009
 *
Packit 6c4009
 * Exception:
Packit 6c4009
 *	No exception.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <math_ldbl_opt.h>
Packit 6c4009
Packit 6c4009
static const long double one = 1.0;
Packit 6c4009
Packit 6c4009
long double __modfl(long double x, long double *iptr)
Packit 6c4009
{
Packit 6c4009
	int64_t i0,i1,j0;
Packit 6c4009
	uint64_t i;
Packit 6c4009
	double xhi, xlo;
Packit 6c4009
Packit 6c4009
	ldbl_unpack (x, &xhi, &xlo;;
Packit 6c4009
	EXTRACT_WORDS64 (i0, xhi);
Packit 6c4009
	EXTRACT_WORDS64 (i1, xlo);
Packit 6c4009
	i1 &= 0x000fffffffffffffLL;
Packit 6c4009
	j0 = ((i0>>52)&0x7ff)-0x3ff;	/* exponent of x */
Packit 6c4009
	if(j0<52) {			/* integer part in high x */
Packit 6c4009
	    if(j0<0) {			/* |x|<1 */
Packit 6c4009
		/* *iptr = +-0 */
Packit 6c4009
		INSERT_WORDS64 (xhi, i0&0x8000000000000000ULL);
Packit 6c4009
		*iptr = xhi;
Packit 6c4009
		return x;
Packit 6c4009
	    } else {
Packit 6c4009
		i = (0x000fffffffffffffLL)>>j0;
Packit 6c4009
		if(((i0&i)|(i1&0x7fffffffffffffffLL))==0) {		/* x is integral */
Packit 6c4009
		    *iptr = x;
Packit 6c4009
		    /* return +-0 */
Packit 6c4009
		    INSERT_WORDS64 (xhi, i0&0x8000000000000000ULL);
Packit 6c4009
		    x = xhi;
Packit 6c4009
		    return x;
Packit 6c4009
		} else {
Packit 6c4009
		    INSERT_WORDS64 (xhi, i0&(~i));
Packit 6c4009
		    *iptr = xhi;
Packit 6c4009
		    return x - *iptr;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	} else if (j0>103) {		/* no fraction part */
Packit 6c4009
	    *iptr = x*one;
Packit 6c4009
	    /* We must handle NaNs separately.  */
Packit 6c4009
	    if ((i0 & 0x7fffffffffffffffLL) > 0x7ff0000000000000LL)
Packit 6c4009
	      return x*one;
Packit 6c4009
	    /* return +-0 */
Packit 6c4009
	    INSERT_WORDS64 (xhi, i0&0x8000000000000000ULL);
Packit 6c4009
	    x = xhi;
Packit 6c4009
	    return x;
Packit 6c4009
	} else {			/* fraction part in low x */
Packit 6c4009
	    i = -1ULL>>(j0-52);
Packit 6c4009
	    if((i1&i)==0) { 		/* x is integral */
Packit 6c4009
		*iptr = x;
Packit 6c4009
		/* return +-0 */
Packit 6c4009
		INSERT_WORDS64 (xhi, i0&0x8000000000000000ULL);
Packit 6c4009
		x = xhi;
Packit 6c4009
		return x;
Packit 6c4009
	    } else {
Packit 6c4009
		INSERT_WORDS64 (xhi, i0);
Packit 6c4009
		INSERT_WORDS64 (xlo, i1&(~i));
Packit 6c4009
		*iptr = ldbl_pack (xhi, xlo);
Packit 6c4009
		return x - *iptr;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
}
Packit 6c4009
#if IS_IN (libm)
Packit 6c4009
long_double_symbol (libm, __modfl, modfl);
Packit 6c4009
#else
Packit 6c4009
long_double_symbol (libc, __modfl, modfl);
Packit 6c4009
#endif