aboutsummaryrefslogtreecommitdiffstats
path: root/src/float/float.c
blob: 2b95636885bc49bded08bf48aa37fcd2bebfbd2b (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* SPDX-License-Identifier: LGPL-2.1-only */

/*
 * The Rin Library – floating point module
 *
 * Copyright (C) 2015 Gediminas Jakutis
 */

#include "rin/float.h"
#include "rin/float_types.h"
#include "float_private.h"

/*
 * TODO:
 * · Doxygen-ize comments
 * · Diagnostics
 * · moar portability
 * · ??
 */

/*
 * signbit functions return the sign bit of a floating point number
 * in the position where it is found on the raw float data itself.
 * Set sign bit means it is negative
 */
uint32_t rin_signbitf(const float num)
{
	uint32_t ret;
	ret = rin_float_to_uint(num);
	return ret & 0x80000000u;
}

uint64_t rin_signbitd(const double num)
{
	uint64_t ret;
	ret = rin_double_to_ulong(num);
	return ret & 0x8000000000000000ull;
}

/*
 * hexstring functions format a string of the hexidecimal representation
 * of the raw floating point number data
 *
 * str has to point to enough memory to hold the said string
 *
 * mode hold modebits. Has to be either RIN_HEXSTRING_DEFAULT or any of
 * the available modes ORed togther.
 * Only one non-default mode is available at the moment.
 */

char *rin_float_to_hexstring(const float num, char *str, unsigned int mode)
{
	char buffer[16] = { '\0' };
	uint32_t tmp;
	size_t ccnt;

	tmp = rin_float_to_uint(num);
	ccnt = sprintf(buffer, "%x", tmp);

	if (mode & RIN_HEXSTRING_NOPREFIX) {
		memcpy(str, "00000000", 9);
		memcpy(&str[8 - ccnt], buffer, ccnt);
	} else {
		memcpy(str, "0x00000000", 11);
		memcpy(&str[10 - ccnt], buffer, ccnt);
	}

	return str;
}

char *rin_double_to_hexstring(const double num, char *str, unsigned int mode)
{
	char buffer[32] = { '\0' };
	uint64_t tmp;
	size_t ccnt;

	tmp = rin_double_to_ulong(num);
	ccnt = sprintf(buffer, "%lx", tmp);

	if (mode & RIN_HEXSTRING_NOPREFIX) {
		memcpy(str, "0000000000000000", 17);
		memcpy(&str[16 - ccnt], buffer, ccnt);
	} else {
		memcpy(str, "0x0000000000000000", 19);
		memcpy(&str[18 - ccnt], buffer, ccnt);
	}

	return str;
}

/*
 * These comparison functions use Units In Last Place (ULPs) to check if the numbers
 * are close enough to be considered equal, which addresses most shortcomings of
 * epsilon-based comparison.
 *
 * TODO: Still not implemented optimally and can produce results worse than epsilon
 * comparison in some situations.
 */

unsigned int rin_compare_float(const float a, const float b, uint32_t max_ulps)
{
	/* in case signs differ, "regular" comparison makes no sense */
	if (rin_signbitf(a) != rin_signbitf(b)) {
		/* unless it's a case of a signed zero, so check for that */
		if (a == b) {
			return 0;
		} else {
			return 1;
		}
	} else {
		return ((uint32_t) abs(rin_float_to_int(a) - rin_float_to_int(b))) > max_ulps;
	}
}

unsigned int rin_compare_vec2(const struct rin_vec2 a, const struct rin_vec2 b, uint32_t max_ulps)
{
	unsigned int ret;

	ret = rin_compare_float(a.x, b.x, max_ulps);
	ret += rin_compare_float(a.y, b.y, max_ulps);

	return ret;
}

unsigned int rin_compare_vec3(const struct rin_vec3 a, const struct rin_vec3 b, uint32_t max_ulps)
{
	unsigned int ret;

	ret = rin_compare_float(a.x, b.x, max_ulps);
	ret += rin_compare_float(a.y, b.y, max_ulps);
	ret += rin_compare_float(a.z, b.z, max_ulps);

	return ret;
}

unsigned int rin_compare_vec4(const struct rin_vec4 a, const struct rin_vec4 b, uint32_t max_ulps)
{
	unsigned int ret;

	ret = rin_compare_float(a.x, b.x, max_ulps);
	ret += rin_compare_float(a.y, b.y, max_ulps);
	ret += rin_compare_float(a.z, b.z, max_ulps);
	ret += rin_compare_float(a.w, b.w, max_ulps);

	return ret;
}

unsigned int rin_compare_double(const double a, const double b, uint64_t max_ulps)
{
	/* in case signs differ, "regular" comparison makes no sense */
	if (rin_signbitd(a) != rin_signbitd(b)) {
		/* unless it's a case of a signed zero, so check for that */
		if (a == b) {
			return 0;
		} else {
			return 1;
		}
	} else {
		return ((uint64_t) labs(rin_double_to_long(a) - rin_double_to_long(b))) > max_ulps;
	}
}

unsigned int rin_compare_vec2d(const struct rin_vec2d a, const struct rin_vec2d b, uint64_t max_ulps)
{
	unsigned int ret;

	ret = rin_compare_double(a.x, b.x, max_ulps);
	ret += rin_compare_double(a.y, b.y, max_ulps);

	return ret;
}

unsigned int rin_compare_vec3d(const struct rin_vec3d a, const struct rin_vec3d b, uint64_t max_ulps)
{
	unsigned int ret;

	ret = rin_compare_double(a.x, b.x, max_ulps);
	ret += rin_compare_double(a.y, b.y, max_ulps);
	ret += rin_compare_double(a.z, b.z, max_ulps);

	return ret;
}

unsigned int rin_compare_vec4d(const struct rin_vec4d a, const struct rin_vec4d b, uint64_t max_ulps)
{
	unsigned int ret;

	ret = rin_compare_double(a.x, b.x, max_ulps);
	ret += rin_compare_double(a.y, b.y, max_ulps);
	ret += rin_compare_double(a.z, b.z, max_ulps);
	ret += rin_compare_double(a.w, b.w, max_ulps);

	return ret;
}

/*
 * These [float_type]_to_[integer_type] functions do type punning in a
 * standards-compliant / without invoking undefined behavior, which
 * happens in most of the [sadly] widely used methods to do this.
 */

uint32_t rin_float_to_uint(const float num)
{
	uint32_t ret;

	memcpy(&ret, &num, sizeof(num));

	return ret;
}

int32_t rin_float_to_int(const float num)
{
	int32_t ret;

	memcpy(&ret, &num, sizeof(num));

	return ret;
}

float rin_uint_to_float(const uint32_t num)
{
	float ret;

	memcpy(&ret, &num, sizeof(num));

	return ret;
}

float rin_int_to_float(const int32_t num)
{
	float ret;

	memcpy(&ret, &num, sizeof(num));

	return ret;
}

uint64_t rin_double_to_ulong(const double num)
{
	uint64_t ret;

	memcpy(&ret, &num, sizeof(num));

	return ret;
}

int64_t rin_double_to_long(const double num)
{
	int64_t ret;

	memcpy(&ret, &num, sizeof(num));

	return ret;
}

double rin_ulong_to_double(const uint64_t num)
{
	double ret;

	memcpy(&ret, &num, sizeof(num));

	return ret;
}

double rin_long_to_double(const int64_t num)
{
	double ret;

	memcpy(&ret, &num, sizeof(num));

	return ret;
}