OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libm/] [math/] [ef_fmod.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
/* ef_fmod.c -- float version of e_fmod.c.
2
 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
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
/*
17
 * __ieee754_fmodf(x,y)
18
 * Return x mod y in exact arithmetic
19
 * Method: shift and subtract
20
 */
21
 
22
#include "fdlibm.h"
23
 
24
#ifdef __STDC__
25
static const float one = 1.0, Zero[] = {0.0, -0.0,};
26
#else
27
static float one = 1.0, Zero[] = {0.0, -0.0,};
28
#endif
29
 
30
#ifdef __STDC__
31
        float __ieee754_fmodf(float x, float y)
32
#else
33
        float __ieee754_fmodf(x,y)
34
        float x,y ;
35
#endif
36
{
37
        __int32_t n,hx,hy,hz,ix,iy,sx,i;
38
 
39
        GET_FLOAT_WORD(hx,x);
40
        GET_FLOAT_WORD(hy,y);
41
        sx = hx&0x80000000;             /* sign of x */
42
        hx ^=sx;                /* |x| */
43
        hy &= 0x7fffffff;       /* |y| */
44
 
45
    /* purge off exception values */
46
        if(hy==0||(hx>=0x7f800000)||             /* y=0,or x not finite */
47
           (hy>0x7f800000))                     /* or y is NaN */
48
            return (x*y)/(x*y);
49
        if(hx<hy) return x;                     /* |x|<|y| return x */
50
        if(hx==hy)
51
            return Zero[(__uint32_t)sx>>31];    /* |x|=|y| return x*0*/
52
 
53
    /* determine ix = ilogb(x) */
54
        if(hx<0x00800000) {     /* subnormal x */
55
            for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
56
        } else ix = (hx>>23)-127;
57
 
58
    /* determine iy = ilogb(y) */
59
        if(hy<0x00800000) {     /* subnormal y */
60
            for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1;
61
        } else iy = (hy>>23)-127;
62
 
63
    /* set up {hx,lx}, {hy,ly} and align y to x */
64
        if(ix >= -126)
65
            hx = 0x00800000|(0x007fffff&hx);
66
        else {          /* subnormal x, shift x to normal */
67
            n = -126-ix;
68
            hx = hx<<n;
69
        }
70
        if(iy >= -126)
71
            hy = 0x00800000|(0x007fffff&hy);
72
        else {          /* subnormal y, shift y to normal */
73
            n = -126-iy;
74
            hy = hy<<n;
75
        }
76
 
77
    /* fix point fmod */
78
        n = ix - iy;
79
        while(n--) {
80
            hz=hx-hy;
81
            if(hz<0){hx = hx+hx;}
82
            else {
83
                if(hz==0)                /* return sign(x)*0 */
84
                    return Zero[(__uint32_t)sx>>31];
85
                hx = hz+hz;
86
            }
87
        }
88
        hz=hx-hy;
89
        if(hz>=0) {hx=hz;}
90
 
91
    /* convert back to floating value and restore the sign */
92
        if(hx==0)                        /* return sign(x)*0 */
93
            return Zero[(__uint32_t)sx>>31];
94
        while(hx<0x00800000) {          /* normalize x */
95
            hx = hx+hx;
96
            iy -= 1;
97
        }
98
        if(iy>= -126) {         /* normalize output */
99
            hx = ((hx-0x00800000)|((iy+127)<<23));
100
            SET_FLOAT_WORD(x,hx|sx);
101
        } else {                /* subnormal output */
102
            n = -126 - iy;
103
            hx >>= n;
104
            SET_FLOAT_WORD(x,hx|sx);
105
            x *= one;           /* create necessary signal */
106
        }
107
        return x;               /* exact output */
108
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.