Back to index

glibc  2.9
e_remainderl.c
Go to the documentation of this file.
00001 /* e_remainderl.c -- long double version of e_remainder.c.
00002  * Conversion to long double by Ulrich Drepper,
00003  * Cygnus Support, drepper@cygnus.com.
00004  */
00005 
00006 /*
00007  * ====================================================
00008  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00009  *
00010  * Developed at SunPro, a Sun Microsystems, Inc. business.
00011  * Permission to use, copy, modify, and distribute this
00012  * software is freely granted, provided that this notice
00013  * is preserved.
00014  * ====================================================
00015  */
00016 
00017 #if defined(LIBM_SCCS) && !defined(lint)
00018 static char rcsid[] = "$NetBSD: $";
00019 #endif
00020 
00021 /* __ieee754_remainderl(x,p)
00022  * Return :
00023  *     returns  x REM p  =  x - [x/p]*p as if in infinite
00024  *     precise arithmetic, where [x/p] is the (infinite bit)
00025  *     integer nearest x/p (in half way case choose the even one).
00026  * Method :
00027  *     Based on fmod() return x-[x/p]chopped*p exactlp.
00028  */
00029 
00030 #include "math.h"
00031 #include "math_private.h"
00032 
00033 #ifdef __STDC__
00034 static const long double zero = 0.0;
00035 #else
00036 static long double zero = 0.0;
00037 #endif
00038 
00039 
00040 #ifdef __STDC__
00041        long double __ieee754_remainderl(long double x, long double p)
00042 #else
00043        long double __ieee754_remainderl(x,p)
00044        long double x,p;
00045 #endif
00046 {
00047        u_int32_t sx,sex,sep,x0,x1,p0,p1;
00048        long double p_half;
00049 
00050        GET_LDOUBLE_WORDS(sex,x0,x1,x);
00051        GET_LDOUBLE_WORDS(sep,p0,p1,p);
00052        sx = sex&0x8000;
00053        sep &= 0x7fff;
00054        sex &= 0x7fff;
00055 
00056     /* purge off exception values */
00057        if((sep|p0|p1)==0) return (x*p)/(x*p);    /* p = 0 */
00058        if((sex==0x7fff)||                 /* x not finite */
00059          ((sep==0x7fff)&&                 /* p is NaN */
00060           ((p0|p1)!=0)))
00061            return (x*p)/(x*p);
00062 
00063 
00064        if (sep<0x7ffe) x = __ieee754_fmodl(x,p+p);      /* now x < 2p */
00065        if (((sex-sep)|(x0-p0)|(x1-p1))==0) return zero*x;
00066        x  = fabsl(x);
00067        p  = fabsl(p);
00068        if (sep<0x0002) {
00069            if(x+x>p) {
00070               x-=p;
00071               if(x+x>=p) x -= p;
00072            }
00073        } else {
00074            p_half = 0.5*p;
00075            if(x>p_half) {
00076               x-=p;
00077               if(x>=p_half) x -= p;
00078            }
00079        }
00080        GET_LDOUBLE_EXP(sex,x);
00081        SET_LDOUBLE_EXP(x,sex^sx);
00082        return x;
00083 }