aboutsummaryrefslogtreecommitdiffstats
path: root/libgamma_internal_translate_to_64.c
blob: 5452fa2ced1fa617590826add4fc08959c3274c6 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


/**
 * Concatenation of all ramps
 */
#define ALL red


/**
 * Preform installation in an `for (i = 0; i < n; i++)`
 * loop and do a `break` afterwords
 */
#define TRANSLATE(instruction)  for (i = 0; i < n; i++) instruction;  break


/**
 * Convert a [0, 1] `float` to a full range `uint64_t`
 * and mark sure rounding errors does not cause the
 * value be 0 instead of ~0 and vice versa
 * 
 * @param   value  To `float` to convert
 * @return         The value as an `uint64_t`
 */
static uint64_t
float_to_64(float value)
{
	/* TODO Which is faster? */
  
#if defined(HAVE_INT128) && __WORDSIZE == 64
	/* `__int128` is a GNU C extension, which
	 * (because it is not ISO C) emits a warning
	 * under -pedantic */
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wpedantic"

	/* In GCC we can use `__int128`, this is
	 * a signed 128-bit integer. It fits all
	 * uint64_t values but also native values,
	 * which is a nice because it eleminates
	 * some overflow condition tests. It is
	 * also more readable. */

	/* Convert to integer */
	__int128 product = (__int128)(value * (float)UINT64_MAX);
	/* Negative overflow */
	if (product > UINT64_MAX)
		return UINT64_MAX;
	/* Positive overflow */
	if (product < 0)
		return 0;
	/* Did not overflow */
	return (uint64_t)product;

# pragma GCC diagnostic pop
#else

	/* If we are not using GCC we cannot be
	 * sure that we have `__int128` so we have
	 * to use `uint64_t` and perform overflow
	 * checkes based on the input value */
  
	/* Convert to integer. */
	uint64_t product = (uint64_t)(value * (float)UINT64_MAX);
	/* Negative overflow,
	 * if the input is less than 0.5 but
	 * the output is greater then we got
	 * -1 when we should have gotten 0 */
	if (value < 0.1f && product > 0xF000000000000000ULL)
		return 0;
	/* Positive overflow,
	 * if the input is greater than 0.5
	 * but the output is less then we got
	 * 0 when we should have gotten ~0 */
	else if (value > 0.9f && product < 0x1000000000000000ULL)
		return (uint64_t)~0;
	/* Did not overflow */
	return product;

#endif
}


/**
 * Convert a [0, 1] `double` to a full range `uint64_t`
 * and mark sure rounding errors does not cause the
 * value be 0 instead of ~0 and vice versa
 * 
 * @param   value  To `double` to convert
 * @return         The value as an `uint64_t`
 */
static uint64_t
double_to_64(double value)
{
	/* TODO Which is faster? */

#if defined(HAVE_INT128) && __WORDSIZE == 64
	/* `__int128` is a GNU C extension, which
	 * (because it is not ISO C) emits a warning
	 * under -pedantic */
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wpedantic"

	/* In GCC we can use `__int128`, this is
	 * a signed 128-bit integer. It fits all
	 * uint64_t values but also native values,
	 * which is a nice because it eleminates
	 * some overflow condition tests. It is
	 * also more readable. */

	/* Convert to integer */
	__int128 product = (__int128)(value * (double)UINT64_MAX);
	/* Negative overflow */
	if (product > UINT64_MAX)
		return UINT64_MAX;
	/* Positive overflow */
	if (product < 0)
		return 0;
	/* Did not overflow */
	return (uint64_t)product;

# pragma GCC diagnostic pop
#else

	/* If we are not using GCC we cannot be
	 * sure that we have `__int128` so we have
	 * to use `uint64_t` and perform overflow
	 * checkes based on the input value. */

	/* Convert to integer. */
	uint64_t product = (uint64_t)(value * (double)UINT64_MAX);
	/* Negative overflow,
	 * if the input is less than 0.5 but
	 * the output is greater then we got
	 * -1 when we should have gotten 0 */
	if (value < (double)0.1f && product > 0xF000000000000000ULL)
		product = 0;
	/* Positive overflow,
	 * if the input is greater than 0.5
	 * but the output is less then we got
	 * 0 when we should have gotten ~0 */
	else if (value > (double)0.9f && (product < 0x1000000000000000ULL))
		product = (uint64_t)~0;
	/* Did not overflow */
	return product;

#endif
}


/**
 * Convert any set of gamma ramps into a 64-bit integer array with all channels
 * 
 * @param  depth  The depth of the gamma ramp, `-1` for `float`, `-2` for `double`
 * @param  n      The grand size of gamma ramps (sum of all channels' sizes)
 * @param  out    Output array
 * @param  in     Input gamma ramps
 */
void
libgamma_internal_translate_to_64(signed depth, size_t n, uint64_t *restrict out, const union gamma_ramps_any *restrict in)
{
	size_t i;
	switch (depth) {
	/* Translate integer */
	case  8:  TRANSLATE(out[i] = (uint64_t)in->bits8. ALL[i] * UINT64_C(0x0101010101010101));
	case 16:  TRANSLATE(out[i] = (uint64_t)in->bits16.ALL[i] * UINT64_C(0x0001000100010001));
	case 32:  TRANSLATE(out[i] = (uint64_t)in->bits32.ALL[i] * UINT64_C(0x0000000100000001));
	/* Identity translation */
	case 64:  TRANSLATE(out[i] = in->bits64.ALL[i]);
	/* Translate floating point */
	case -1:  TRANSLATE(out[i] =  float_to_64(in->float_single.ALL[i]));
	case -2:  TRANSLATE(out[i] = double_to_64(in->float_double.ALL[i]));
	default:
		/* This is not possible */
		abort();
		break;
	}
}