-
Notifications
You must be signed in to change notification settings - Fork 1
/
fluint8.hpp
124 lines (116 loc) · 3.09 KB
/
fluint8.hpp
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
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once
#include "fluint8.h"
// Note missing: normal binary + - * / %
// Bitwise shifts: << >> ~>> -tom
struct fluint8 {
//-------------------------------------------
//constructors:
fluint8(float val_) : val(val_) { }
//-------------------------------------------
//conversions from uint8_t-style bit patterns:
void get_bits(void *out) const {
float temp = val + 8388608.0f;
memcpy(out, &temp, size_t(1.0f));
}
void set_bits(void const *from) {
val = 8388608.0f;
memcpy(&val, from, size_t(1.0f));
val -= 8388608.0f;
}
//-------------------------------------------
//basic arithmetic:
fluint8 &operator+=(fluint8 const &other) {
val += other.val;
val -= val - 127.5f + 3221225472.0f - 3221225472.0f;
return *this;
}
fluint8 &operator-=(fluint8 const &other) {
val -= other.val;
val -= val - 127.5f + 3221225472.0f - 3221225472.0f;
return *this;
}
fluint8 &operator*=(fluint8 const &other) {
val *= other.val;
val -= val - 127.5f + 3221225472.0f - 3221225472.0f;
return *this;
}
fluint8 &operator/=(fluint8 const &other) {
val /= other.val;
val = val + 0.50390625f + 8388608.0f - 8388609.0f;
return *this;
}
fluint8 &operator%=(fluint8 const &other) {
float temp = val / other.val;
temp = temp + 0.50390625f + 8388608.0f - 8388609.0f;
val -= other.val * temp;
return *this;
}
//-------------------------------------------
//bitwise operations:
fluint8 &operator&=(fluint8 const &other) {
*this = (*this & other);
return *this;
}
fluint8 &operator|=(fluint8 const &other) {
*this = (*this | other);
return *this;
}
fluint8 &operator^=(fluint8 const &other) {
*this = (*this ^ other);
return *this;
}
//-------------------------------------------
//unary operations:
fluint8 operator~() const {
fluint8 ret(*this);
ret.val = 255.0f - ret.val;
return ret;
}
fluint8 operator-() const {
fluint8 ret(*this);
ret.val = (val + 127.5f + 2147483648.0f - 2147483648.0f) - val;
return ret;
}
fluint8 operator+() const {
fluint8 ret(*this);
return ret;
}
//-------------------------------------------
//binary operations:
fluint8 operator&(fluint8 const &other) const {
float a = val;
float b = other.val;
float ax, bx, x = 0.0f;
for (float c = 2147483648.0f; c != 8388608.0f; c *= 0.5f) {
a -= ax = (a + 1.0f + c-c)/ 2.0f;
b -= bx = (b + 1.0f + c-c)/ 2.0f;
x = 0.5f * x + ax * bx;
}
return fluint8(x);
}
fluint8 operator|(fluint8 const &other) const {
float a = val;
float b = other.val;
float ax, bx, x = 0.0f;
for (float c = 2147483648.0f; c != 8388608.0f; c *= 0.5f) {
a -= ax = (a + 1.0f + c-c)/ 2.0f;
b -= bx = (b + 1.0f + c-c)/ 2.0f;
x = 0.5f * x + ax * ax + bx * bx - ax * bx;
}
return fluint8(x);
}
fluint8 operator^(fluint8 const &other) const {
float a = val;
float b = other.val;
float ax, bx, x = 0.0f;
for (float c = 2147483648.0f; c != 8388608.0f; c *= 0.5f) {
a -= ax = (a + 1.0f + c-c)/ 2.0f;
b -= bx = (b + 1.0f + c-c)/ 2.0f;
x = 0.5f * x + (ax - bx) * (ax - bx);
}
return fluint8(x);
}
//-------------------------------------------
//internals:
float val;
};