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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [coremark/] [fmod.c] - Blame information for rev 362

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 355 julius
 
2
typedef unsigned int u_int32_t;
3
typedef signed int int32_t;
4
 
5
// Big endian struct...
6
typedef union
7
{
8
  double value;
9
  struct
10
  {
11
    u_int32_t msw;
12
    u_int32_t lsw;
13
  } parts;
14
} ieee_double_shape_type;
15
 
16
 
17
/*
18
typedef union
19
{
20
  double value;
21
  struct
22
  {
23
    u_int32_t lsw;
24
    u_int32_t msw;
25
  } parts;
26
} ieee_double_shape_type;
27
 
28
*/
29
/* Get two 32 bit ints from a double.  */
30
 
31
#define EXTRACT_WORDS(ix0,ix1,d)                                \
32
do {                                                            \
33
  ieee_double_shape_type ew_u;                                  \
34
  ew_u.value = (d);                                             \
35
  (ix0) = ew_u.parts.msw;                                       \
36
  (ix1) = ew_u.parts.lsw;                                       \
37
} while (0)
38
 
39
/* Get the more significant 32 bit int from a double.  */
40
 
41
#define GET_HIGH_WORD(i,d)                                      \
42
do {                                                            \
43
  ieee_double_shape_type gh_u;                                  \
44
  gh_u.value = (d);                                             \
45
  (i) = gh_u.parts.msw;                                         \
46
} while (0)
47
 
48
/* Get the less significant 32 bit int from a double.  */
49
 
50
#define GET_LOW_WORD(i,d)                                       \
51
do {                                                            \
52
  ieee_double_shape_type gl_u;                                  \
53
  gl_u.value = (d);                                             \
54
  (i) = gl_u.parts.lsw;                                         \
55
} while (0)
56
 
57
/* Set a double from two 32 bit ints.  */
58
 
59
#define INSERT_WORDS(d,ix0,ix1)                                 \
60
do {                                                            \
61
  ieee_double_shape_type iw_u;                                  \
62
  iw_u.parts.msw = (ix0);                                       \
63
  iw_u.parts.lsw = (ix1);                                       \
64
  (d) = iw_u.value;                                             \
65
} while (0)
66
 
67
/* Set the more significant 32 bits of a double from an int.  */
68
 
69
#define SET_HIGH_WORD(d,v)                                      \
70
do {                                                            \
71
  ieee_double_shape_type sh_u;                                  \
72
  sh_u.value = (d);                                             \
73
  sh_u.parts.msw = (v);                                         \
74
  (d) = sh_u.value;                                             \
75
} while (0)
76
 
77
/* Set the less significant 32 bits of a double from an int.  */
78
 
79
#define SET_LOW_WORD(d,v)                                       \
80
do {                                                            \
81
  ieee_double_shape_type sl_u;                                  \
82
  sl_u.value = (d);                                             \
83
  sl_u.parts.lsw = (v);                                         \
84
  (d) = sl_u.value;                                             \
85
} while (0)
86
 
87
/* A union which permits us to convert between a float and a 32 bit
88
   int.  */
89
 
90
typedef union
91
{
92
  float value;
93
  u_int32_t word;
94
} ieee_float_shape_type;
95
 
96
/* Get a 32 bit int from a float.  */
97
 
98
#define GET_FLOAT_WORD(i,d)                                     \
99
do {                                                            \
100
  ieee_float_shape_type gf_u;                                   \
101
  gf_u.value = (d);                                             \
102
  (i) = gf_u.word;                                              \
103
} while (0)
104
 
105
/* Set a float from a 32 bit int.  */
106
 
107
#define SET_FLOAT_WORD(d,i)                                     \
108
do {                                                            \
109
  ieee_float_shape_type sf_u;                                   \
110
  sf_u.word = (i);                                              \
111
  (d) = sf_u.value;                                             \
112
} while (0)
113
 
114
 
115
static const double one = 1.0;
116
 
117
double modf(double x, double *iptr)
118
{
119
        int32_t i0,i1,j0;
120
        u_int32_t i;
121
        EXTRACT_WORDS(i0,i1,x);
122
        j0 = ((i0>>20)&0x7ff)-0x3ff;    /* exponent of x */
123
        if(j0<20) {                     /* integer part in high x */
124
            if(j0<0) {                   /* |x|<1 */
125
                INSERT_WORDS(*iptr,i0&0x80000000,0);     /* *iptr = +-0 */
126
                return x;
127
            } else {
128
                i = (0x000fffff)>>j0;
129
                if(((i0&i)|i1)==0) {             /* x is integral */
130
                    u_int32_t high;
131
                    *iptr = x;
132
                    GET_HIGH_WORD(high,x);
133
                    INSERT_WORDS(x,high&0x80000000,0);   /* return +-0 */
134
                    return x;
135
                } else {
136
                    INSERT_WORDS(*iptr,i0&(~i),0);
137
                    return x - *iptr;
138
                }
139
            }
140
        } else if (j0>51) {             /* no fraction part */
141
            u_int32_t high;
142
            *iptr = x*one;
143
            GET_HIGH_WORD(high,x);
144
            INSERT_WORDS(x,high&0x80000000,0);   /* return +-0 */
145
            return x;
146
        } else {                        /* fraction part in low x */
147
            i = ((u_int32_t)(0xffffffff))>>(j0-20);
148
            if((i1&i)==0) {              /* x is integral */
149
                u_int32_t high;
150
                *iptr = x;
151
                GET_HIGH_WORD(high,x);
152
                INSERT_WORDS(x,high&0x80000000,0);       /* return +-0 */
153
                return x;
154
            } else {
155
                INSERT_WORDS(*iptr,i0,i1&(~i));
156
                return x - *iptr;
157
            }
158
        }
159
}

powered by: WebSVN 2.1.0

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