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

Subversion Repositories openrisc

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

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

powered by: WebSVN 2.1.0

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