File: ale_real.h

package info (click to toggle)
ale 0.9.0.3-5
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 7,260 kB
  • sloc: cpp: 21,826; sh: 10,106; xml: 4,129; ansic: 2,343; makefile: 563; perl: 454; exp: 319
file content (319 lines) | stat: -rw-r--r-- 7,787 bytes parent folder | download | duplicates (5)
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
// Copyright 2002, 2004 David Hilvert <dhilvert@auricle.dyndns.org>, 
//                                    <dhilvert@ugcs.caltech.edu>

/*  This file is part of the Anti-Lamenessing Engine.

    The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with the Anti-Lamenessing Engine; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef __ale_real_h__
#define __ale_real_h__

#include "ale_fixed.h"

#define SINGLE 1
#define DOUBLE 2
#define HALF 3
#define FIXED32 4
#define FIXED16 5

#define ale_real_enable_casting()
#define ale_real_disable_casting()

#define ale_real_unexceptional_negation(VALUE) -(VALUE)

#define ale_real_from_int(INT_VALUE, MAXVAL) (((float) (INT_VALUE)) / ((float) (MAXVAL)))
#define ale_real_to_int(REAL_VALUE, MAXVAL) round((float) (REAL_VALUE) * (MAXVAL))
#define ale_real_weight_floor 0.0001

/*
 * Real-valued type used to represent the range of an image (colors, weights,
 * etc.).
 *
 * ale_real is used in computation.
 * ale_sreal is used for storage.
 */

#if ALE_COLORS == SINGLE

typedef float ale_real;
typedef float ale_sreal;

#define ale_real_ip_weight_floor 1e-10
#define ale_real_confidence_floor 0.001

#define ALE_REAL_PRECISION_STRING "SINGLE"

#elif ALE_COLORS == DOUBLE

typedef double ale_real;
typedef double ale_sreal;

#define ale_real_ip_weight_floor 1e-10
#define ale_real_confidence_floor 0.000001

#define ALE_REAL_PRECISION_STRING "DOUBLE"

#elif ALE_COLORS == HALF

/*
 * What follows is one approach to packing a floating point
 * number into 16 bits.  This implementation is very slow.
 */

#define MANTISSA_BITS (9)
#define EXP_BITS (15 - MANTISSA_BITS)
#define EXP_SPECIAL (1 << (EXP_BITS - 1))
#define EXP_MAX (EXP_SPECIAL - 1)
#define EXP_MIN (-EXP_MAX)

typedef float ale_real;

class ale_sreal {

	union {
		uint16_t bits;
		struct {
			uint16_t sign:1;
			uint16_t mant:MANTISSA_BITS;
			int16_t  exp :EXP_BITS;
		} fpr;
	} u;
public:
	ale_sreal() {
		u.bits = 0;
	}
	ale_sreal operator=(float v) {
		if (v == 0) {
			u.bits = 0;
		} else if (isnan(v)) {
			u.fpr.exp = EXP_SPECIAL;
			u.fpr.mant = 1;
		} else {

			if (v > 0)
				u.fpr.sign = 0;
			else if (v < 0) {
				u.fpr.sign = 1;
				v = -v;
			} else
				assert(0);

			/*
			 * Get the exponent.
			 */

			int log2 = (int) floor (log(v) / log(2));

			/*
			 * Test the exponent against the largest expressible
			 * exponent for ale_sreal.
			 */

			if (log2 > EXP_MAX) {
				/*
				 * Infinity
				 */

				u.fpr.exp = EXP_SPECIAL;
				u.fpr.mant = 0;

				return *this;
			}

			/*
			 * Test the exponent against the smallest expressible
			 * exponent for ale_sreal.
			 */

			if (log2 < EXP_MIN) {
				/*
				 * Zero
				 */

				u.fpr.exp = 0x0;
				u.fpr.mant = 0;

				return *this;
			}

			/*
			 * The exponent is in range, so use it.
			 */

			u.fpr.exp = log2;

			u.fpr.mant = (uint16_t) floor(v / pow(2, log2) * (1 << (MANTISSA_BITS - 1)));
		}

		return *this;
	}

	operator float() const {
		float result = 3.14159;

		if (((uint16_t) u.fpr.exp == EXP_SPECIAL) && (u.fpr.mant == 1)) {

			/*
			 * NaN
			 */

			float a = 0;
			float b = 0;

			result = a / b;

		} else if (((uint16_t) u.fpr.exp == EXP_SPECIAL) && (u.fpr.mant == 0)) {

			/*
			 * Infinity
			 */

			float a = 1;
			float b = 0;

			result = (a / b);

		} else if ((uint16_t) u.fpr.exp != EXP_SPECIAL) {

			/*
			 * Value is finite.
			 */

			result = u.fpr.mant / ((double) (1 << (MANTISSA_BITS - 1)))
			       * pow(2, u.fpr.exp);

		} else
			assert(0);

		if (u.fpr.sign)
			result = -result;

		return result;
	}

	ale_sreal operator-=(float r) {
		*this = (float) *this - (float) r;
		return *this;
	}
	ale_sreal operator/=(float r) {
		*this = (float) *this / (float) r;
		return *this;
	}
	ale_sreal operator*=(float r) {
		*this = (float) *this * (float) r;
		return *this;
	}
	ale_sreal operator+=(float r) {
		*this = (float) *this + (float) r;
		return *this;
	}
};

#undef MANTISSA_BITS
#undef EXP_BITS
#undef EXP_SPECIAL
#undef EXP_MAX
#undef EXP_MIN

#define ALE_REAL_PRECISION_STRING "HALF"

#elif ALE_COLORS == FIXED32

typedef ale_fixed<ale_fixed_32,16> ale_real;
typedef ale_fixed<ale_fixed_32,16> ale_sreal;

#undef ale_real_enable_casting
#undef ale_real_disable_casting
#define ale_real_enable_casting() ale_real::enable_casting()
#define ale_real_disable_casting() ale_real::disable_casting()

#undef ale_real_unexceptional_negation
#define ale_real_unexceptional_negation(VALUE) (VALUE).unexceptional_negation();

#undef ale_real_to_int
#undef ale_real_from_int
#define ale_real_to_int(REAL_VALUE, MAXVAL) (  (MAXVAL == 255) \
                                             ? (int) ale_fixed<ale_fixed_16_calc,8>::fixed_to_bits(REAL_VALUE) \
					     : ( (MAXVAL == 65535) \
					       ? (int) ale_fixed<ale_fixed_16_calc,16>::fixed_to_bits(REAL_VALUE)  \
					       : (int) round((float) (REAL_VALUE) * (MAXVAL)) ) )

#define ale_real_from_int(INT_VALUE, MAXVAL) (  (MAXVAL == 255) \
                                               ? (ale_real) ale_fixed<ale_fixed_16_calc,8>::bits_to_fixed(INT_VALUE) \
					       : ( (MAXVAL == 65535) \
					         ? (ale_real) ale_fixed<ale_fixed_16_calc,16>::bits_to_fixed(INT_VALUE) \
						 : (ale_real) (((float) (INT_VALUE)) / ((float) (MAXVAL))) ) )

#define ale_real_ip_weight_floor (1 / (ale_real) 100)
#define ale_real_confidence_floor (1 / (ale_real) 10)

#define ALE_REAL_PRECISION_STRING "FIXED32"

#elif ALE_COLORS == FIXED16

typedef ale_fixed<ale_fixed_16_calc,14> ale_real;
typedef ale_fixed<ale_fixed_16,12> ale_sreal;

#undef ale_real_enable_casting
#undef ale_real_disable_casting
#define ale_real_enable_casting() ale_real::enable_casting()
#define ale_real_disable_casting() ale_real::disable_casting()

#undef ale_real_unexceptional_negation
#define ale_real_unexceptional_negation(VALUE) (VALUE).unexceptional_negation();

#undef ale_real_to_int
#undef ale_real_from_int
#define ale_real_to_int(REAL_VALUE, MAXVAL) (  (MAXVAL == 255) \
                                             ? (int) ale_fixed<ale_fixed_16_calc,8>::fixed_to_bits(REAL_VALUE) \
					     : ( (MAXVAL == 65535) \
					       ? (int) ale_fixed<ale_fixed_16_calc,16>::fixed_to_bits(REAL_VALUE)  \
					       : (int) round((float) (REAL_VALUE) * (MAXVAL)) ) )

#define ale_real_from_int(INT_VALUE, MAXVAL) (  (MAXVAL == 255) \
                                               ? (ale_real) ale_fixed<ale_fixed_16_calc,8>::bits_to_fixed(INT_VALUE) \
					       : ( (MAXVAL == 65535) \
					         ? (ale_real) ale_fixed<ale_fixed_16_calc,16>::bits_to_fixed(INT_VALUE) \
						 : (ale_real) (((float) (INT_VALUE)) / ((float) (MAXVAL))) ) )

#define ale_real_ip_weight_floor (1 / (ale_real) 100)
#define ale_real_confidence_floor (1 / (ale_real) 10)

#define ALE_REAL_PRECISION_STRING "FIXED16"

#else

#warning Unknown precision in ale_real.h: Choosing PRECISION=SINGLE.

typedef float ale_real;
typedef float ale_sreal;

#define ale_real_ip_weight_floor 1e-10
#define ale_real_confidence_floor 0.001

#define ALE_REAL_PRECISION_STRING "SINGLE"

#endif

const ale_real ale_real_0 = (ale_real) 0;

#undef SINGLE
#undef DOUBLE
#undef HALF
#undef FIXED16
#undef FIXED32

#endif