File: float.c

package info (click to toggle)
haskell-ieee754 0.7.8-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 184 kB
  • ctags: 101
  • sloc: haskell: 1,264; ansic: 276; makefile: 18
file content (112 lines) | stat: -rw-r--r-- 2,250 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

#include <float.h>
#include <math.h>
#include <stdint.h>


#define REAL             float
#define REAL_ABS         fabsf

#define REAL_MIN_NORMAL  FLT_MIN
#define REAL_EPSILON     FLT_EPSILON
#define REAL_MAX         FLT_MAX
#define REAL_MANT_DIG    FLT_MANT_DIG

#define FEQREL           feqrelf
#include "feqrel_source.c"

union float_t {
    float f;
    uint32_t w;
};

int
identicalf (float x, float y)
{
    union float_t ux = { x };
    union float_t uy = { y };
    return ux.w == uy.w;
}

float
copysignf (float x, float y)
{
    union float_t ux = { x };
    union float_t uy = { y };
    union float_t uz;
    uint32_t val  = ux.w & 0x7FFFFFFF;
    uint32_t sign = uy.w & 0x80000000;
    uz.w = sign | val;
    return uz.f;
}

/* ported from tango/math/IEEE.d */
float
hsi7_nextupf (float x)
{
    union float_t ps = { x };

    if ((ps.w & 0x7F800000) == 0x7F800000) {
        /* First, deal with NANs and infinity */
        if (x == -INFINITY) return -REAL_MAX;
        return x; /* +INF and NAN are unchanged. */
    }
    if (ps.w & 0x80000000)  { /* Negative number */
        if (ps.w == 0x80000000) { /* it was negative zero */
            ps.w = 0x00000001; /* change to smallest subnormal */
            return ps.f;
        }
        --ps.w;
    } else { /* Positive number */
        ++ps.w;
    }
    return ps.f;
}

/* ported from tango/math/IEEE.d */
float
hsi7_nextdownf (float x)
{
    return -hsi7_nextupf(-x);
}

/* ported from tango/math/IEEE.d */
float
ieeemeanf (float x, float y)
{
    if (!((x>=0 && y>=0) || (x<=0 && y<=0))) return NAN;
    
    union float_t ul;
    union float_t xl = { x };
    union float_t yl = { y };
    uint32_t m = ((xl.w & 0x7FFFFFFF) + (yl.w & 0x7FFFFFFF)) >> 1;
    m |= (xl.w & 0x80000000);
    ul.w = m;
    
    return ul.f;
}

float
mknanf (uint32_t payload)
{
    union float_t ux = { NAN };
    
    /* get sign, exponent, and quiet bit from NAN */    
    ux.w &= 0xFFC00000; 
    
    /* ignore sign, exponent, and quiet bit in payload */
    payload &= 0x003FFFFF;
    ux.w |= payload;

    return ux.f;
}

uint32_t
getnanf (float x)
{
    union float_t payload = { x };
    
    /* clear sign, exponent, and quiet bit */
    payload.w &= 0x003FFFFF;
    return payload.w;
}