summaryrefslogtreecommitdiff
path: root/src/types.c
blob: 2ded2f402f028680a118c7e6a9d1049d51de94ee (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

#include "osm/types.h"

/**
 * WARNING: This file makes the *BOLD* assumption that
 * your platform is using standard IEEE 754 floating
 * point values by default for fp types.
 */

OSMFloatBreakdown osm_float_to_break(OSMFloat f)
{
	OSMFloatBreakdown out;

	out.sign = f >> (OSM_FLOAT_EXPO_LEN + OSM_FLOAT_FRAC_LEN);
	out.mantissa = (f >> OSM_FLOAT_FRAC_LEN) & OSM_FLOAT_EXPO_MASK;
	out.fraction = f & OSM_FLOAT_FRAC_MASK;

	return out;
}

/// Function only to be used with number formats who's mantissa
/// and fraction length are both smaller or equal to double
/// precision IEEE 754 numbers
OSMFloatBreakdown _osm_ieee754_enlarge(uint64_t d, uint16_t m_len, uint16_t f_len)
{
	OSMFloatBreakdown out = {0};

	out.sign = (d >> (m_len + f_len)) & 1;

	uint64_t mask = (1 << (m_len)) - 1; // Mantissa mask
	out.mantissa = (d >> f_len) & mask; // Mantissa biased by the other type's bias
	uint16_t bias = mask >> 1;          // The other type's bias
	
	mask = (1 << f_len) - 1;
	out.fraction = d & mask;
	
	// Create corrected mantissa and fraction (if not denormal)
	if (out.mantissa >= bias)
	{
		// non-negative exponent
		out.mantissa -= bias;
		out.mantissa += OSM_FLOAT_EXPO_BIAS;
	}
	else if (out.mantissa < bias && out.mantissa > 0)
	{
		// true exponent (abs value)
		out.mantissa = bias - out.mantissa;
		// corrected for double precision numbers (won't overflow since we use this function with smaller mantissa)
		out.mantissa = OSM_FLOAT_EXPO_BIAS - out.mantissa;
		// corrected fraction based on difference in bit lengths
		out.fraction = out.fraction << (OSM_FLOAT_FRAC_LEN - f_len);
	}
	
	// Handle zero and denormal
	if (out.mantissa == 0)
	{
		if (out.fraction == 0)
			return out;
		
		// Denormal numbers (TODO)
		out.mantissa = OSM_FLOAT_EXPO_BIAS - bias;
		if (OSM_FLOAT_FRAC_LEN >= f_len)
			out.fraction = out.fraction << (OSM_FLOAT_FRAC_LEN - f_len);  // corrected fraction based on difference in bit lengths
		bias = (uint16_t) ceil(log2(out.fraction));
		
	}

	return out;
}

/*
 * WARNING: This code assumes that the compiler
 * supports IEEE 754 floating point numbers.
 *
 * Should probably be updated with more formats if I was
 * feeling frisky.
 *
 * Supports half, single, and double precision IEEE 754
 */
OSMFloatBreakdown osm_native_float_to_break(double d)
{
	OSMFloatBreakdown out = {0};

	switch(sizeof(double))
	{
		case 2: {
			uint16_t bits = *(uint16_t *) &d;
			out = _osm_ieee754_enlarge(bits, 5, 10);
		} break;

		case 4: {
			uint32_t bits = * (uint32_t *) &d;
			out = _osm_ieee754_enlarge(bits, 8, 23);
		} break;

		case 8: {
			uint64_t bits = * (uint64_t *) &d;
			out = osm_float_to_break(bits);
		} break;

		default:
			fprintf(stderr, "\nERROR libopensmarts: unable to convert float from double to OSMFloat.\n");
			break;
	}

	return out;
}

OSMFloat osm_break_to_float(OSMFloatBreakdown b)
{
	OSMFloat out;
	
	if (b.sign)
		out = 1;
	out = out << (OSM_FLOAT_FRAC_LEN + OSM_FLOAT_EXPO_LEN);

	out |= (OSMFloat)(b.mantissa & OSM_FLOAT_EXPO_MASK) << OSM_FLOAT_FRAC_LEN;

	out |= b.fraction & OSM_FLOAT_FRAC_MASK;

	return out;
}

bool _osm_is_nan(const OSMFloatBreakdown b)
{
	return b.mantissa == OSM_FLOAT_EXPO_MASK && b.fraction != 0;
}

int8_t _osm_is_infinity(const OSMFloatBreakdown b)
{
	if (b.mantissa == OSM_FLOAT_EXPO_MASK && b.fraction == 0)
	{
		if (b.sign)
			return -1;
		return 1;
	}
	return 0;
}

/// Handle NaN
double _osm_ieee754_nan(OSMFloatBreakdown b, uint16_t m_len, uint16_t f_len)
{
	uint64_t out = 0;
	uint64_t mask = (1 << m_len) - 1;

	// sign
	out |= b.sign << (m_len + f_len);
	// mantissa
	out |= mask << f_len;
	
	// fraction
	if (f_len >= OSM_FLOAT_FRAC_LEN)
		out |= b.fraction << (f_len - OSM_FLOAT_FRAC_LEN);
	else
		out |= b.fraction >> (OSM_FLOAT_FRAC_LEN - f_len);

	// Make sure it's still nan just in case all the
	// fraction bits got cleared
	mask = (1 << f_len) - 1;
	if ((out & mask) == 0)
		out |= 1;

#ifdef _OSM_FLOAT_USE_HIGH_BITS
	out = out << (64 - (m_len + f_len + 1));
#endif
	return *(double *)&out;
}

/// Handle subnormal numbers
double _osm_ieee754_subnormal(OSMFloatBreakdown b, uint16_t m_len, uint16_t f_len)
{
	// TODO
	uint64_t out = 0;
#ifdef _OSM_FLOAT_USE_HIGH_BITS
	out = out << (64 - (m_len + f_len + 1));
#endif
	return *(double *)&out;
}

/**
 * Converts a breakdown to a floating point number with given mantissa and
 * fraction length (so long as the format is less than or equal to 64 bits)
 */
double _osm_ieee754_assemble(OSMFloatBreakdown b, uint16_t m_len, uint16_t f_len)
{

	if (_osm_is_nan(b))
		return _osm_ieee754_nan(b, m_len, f_len);
	else if (b.mantissa == 0)
		return _osm_ieee754_subnormal(b, m_len, f_len);

	uint64_t out = 0;
	
	// Sign (assumed to be at end)
	out |= b.sign << (m_len + f_len);
	
	// Mantissa mask
	uint64_t mask = (1 << m_len) - 1;
	
	// Handle mantissa
	uint64_t bias = mask >> 1;
	if (b.mantissa > OSM_FLOAT_EXPO_BIAS)
	{
		b.mantissa -= OSM_FLOAT_EXPO_BIAS;
		b.mantissa += bias;
		if (b.mantissa >= mask)
		{
			// Create infinity
			out |= mask << f_len;
#ifdef _OSM_FLOAT_USE_HIGH_BITS
			out = out << (64 - (m_len + f_len + 1));
#endif
			return *(double *)&out;
		}
	}
	else
	{
		b.mantissa = OSM_FLOAT_EXPO_BIAS - b.mantissa;
		b.mantissa = bias - b.mantissa;

		if (b.mantissa >= mask || b.mantissa == 0)
		{
			// wrapped around, should create subnormal
			return _osm_ieee754_subnormal(b, m_len, f_len);
		}
	}

	// write mantissa
	out |= (b.mantissa & mask) << f_len;

	// Fraction (assumed to be low bits)
	mask = (1 << f_len) - 1;

	if (f_len >= OSM_FLOAT_FRAC_LEN)
		out |= b.fraction << (f_len - OSM_FLOAT_FRAC_LEN);
	else
		out |= b.fraction >> (OSM_FLOAT_FRAC_LEN - f_len);

#ifdef _OSM_FLOAT_USE_HIGH_BITS
	out = out << (64 - (m_len + f_len + 1));
#endif

	return *(double *)&out;
}

/*
 * WARNING: This code assumes that the compiler
 * supports IEEE 754 floating point numbers.
 *
 * WARNING: This code flushes subnormal values to zero
 *
 * WARNING: This code flushes out of bounds values to infinity
 *
 * INFO: The fraction part of NaN values are set to 1 in some cases
 *
 * Should probably be updated with more formats if I was
 * feeling frisky.
 */
double osm_break_to_native_float(OSMFloatBreakdown b)
{
	switch(sizeof(double))
	{
		case 2: {
			return _osm_ieee754_assemble(b, 5, 10);
		} break;
		
		case 4: {
			return _osm_ieee754_assemble(b, 8, 23);
		} break;
		
		case 8: {
			OSMFloat f = osm_break_to_float(b);
			return * (double *) &f;
		} break;
		
		default:
			fprintf(stderr, "\nERROR libopensmarts: unable to convert float from OSMFloat to double.\n");
			break;
	}

	return 0;
}

double osm_float_to_native(OSMFloat f)
{
	return
		osm_break_to_native_float(
			osm_float_to_break(f)
		);
}

OSMFloat osm_native_to_float(double d)
{
	return
		osm_break_to_float(
			osm_native_float_to_break(d)
		);
}

bool osm_is_nan(OSMFloat f)
{
	return _osm_is_nan(osm_float_to_break(f));
}

int8_t osm_is_infinity(OSMFloat f)
{
	return _osm_is_infinity(osm_float_to_break(f));
}


/// End of float funcs


OSMColor osm_rgb_to_color(uint8_t r, uint8_t g, uint8_t b)
{
	OSMColor out = {
		.r = r,
		.g = g,
		.b = b,
		.extra = vect_init(sizeof(OSMColorChannel)),
	};
	return out;
}

OSMColor osm_color_copy(OSMColor *color)
{

	OSMColor out = {
		.r = color->r,
		.g = color->g,
		.b = color->b,
		.extra = vect_init(sizeof(OSMColorChannel)),
	};

	for (uint8_t i = 0; i < color->extra.count; i++)
	{
		OSMColorChannel *c = vect_get(&color->extra, i);
		osm_add_channel(&out, c->val, c->name);
	}

	return out;
}

void osm_color_free(OSMColor *color)
{
	color->r = 0;
	color->g = 0;
	color->b = 0;

	for (uint8_t i = 0; i < color->extra.count; i++)
	{
		OSMColorChannel *c = vect_get(&color->extra, i);
		free(c->name);
	}

	vect_end(&color->extra);
}

void osm_add_channel(OSMColor *color, uint8_t val, uint8_t *name)
{
	size_t len = strlen((char *)name) + 1;
	OSMColorChannel add = {
		.val = val,
		.name = malloc(len)
	};

	memcpy(add.name, name, len);

	vect_push(&color->extra, &add);
}