-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed_point_compile_tests.cpp
More file actions
595 lines (477 loc) · 11.7 KB
/
fixed_point_compile_tests.cpp
File metadata and controls
595 lines (477 loc) · 11.7 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
--------------------------------------------------------------------------------
Fixed point C++ template class compile-time tests
Copyright (c) 2012, Oleg Endo
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the names of any
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
Example how to compile this file:
g++ -std=c++11 -O2 fixed_point_compile_tests.cpp
--------------------------------------------------------------------------------
*/
//#define USE_TEST_NAMESPACE
#ifdef USE_TEST_NAMESPACE
#define __FIXED_POINT_BEGIN_NAMESPACE__ namespace test { namespace math {
#define __FIXED_POINT_END_NAMESPACE__ } }
#define __FIXED_POINT_USE_NAMESPACE__ test::math::
#endif
#include "fixed_point.hpp"
#ifdef USE_TEST_NAMESPACE
typedef test::math::fixed_point<int32_t, 16, 16> fxpt_16_16;
typedef test::math::fixed_point<int32_t, 8, 24> fxpt_8_24;
typedef test::math::fixed_point<int64_t, 32, 32> fxpt_32_32;
typedef test::math::fixed_point<int64_t, 31, 33> fxpt_31_33;
typedef test::math::fixed_point<int32_t, 32, 0> fxpt_32_0;
typedef test::math::fixed_point<int32_t, 9, 23> fxpt_9_23;
typedef test::math::fixed_point<int8_t, 4, 4> fxpt_4_4;
typedef test::math::fixed_point<int16_t, 8, 8> fxpt_8_8;
typedef test::math::fixed_point<uint32_t, 16, 16> fxptu_16_16;
#else
typedef fixed_point<int32_t, 16, 16> fxpt_16_16;
typedef fixed_point<int32_t, 8, 24> fxpt_8_24;
typedef fixed_point<int64_t, 32, 32> fxpt_32_32;
typedef fixed_point<int64_t, 31, 33> fxpt_31_33;
typedef fixed_point<int32_t, 32, 0> fxpt_32_0;
typedef fixed_point<int32_t, 9, 23> fxpt_9_23;
typedef fixed_point<int8_t, 4, 4> fxpt_4_4;
typedef fixed_point<int16_t, 8, 8> fxpt_8_8;
typedef fixed_point<uint32_t, 16, 16> fxptu_16_16;
#endif
fxpt_16_16 test_00 (fxpt_32_32 a)
{
return (fxpt_16_16)a; // explicit conversion
// return fxpt_16_16 (a);
}
fxpt_16_16 test_00_1 (fxpt_16_16 a, fxpt_16_16 b)
{
fxpt_16_16 r;
r = a * b; // implicit conversion from widened
return r;
}
fxpt_32_32 test_00_2 (fxpt_16_16 a, fxpt_16_16 b)
{
return (fxpt_32_32)(a * b); // explicit conversion from widened
}
int32_t test_01 (fxpt_16_16 a)
{
return (int32_t)a;
}
int32_t test_02 (fxpt_8_24 a)
{
return (int32_t)a;
}
fxpt_16_16 test_03 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a * b + c * d;
}
fxpt_8_24 test_04 (fxpt_16_16 a)
{
return (fxpt_8_24)a;
}
fxpt_16_16 test_05 (fxpt_16_16 a)
{
return std::abs (a);
}
fxptu_16_16 test_06 (fxptu_16_16 a)
{
return std::abs (a); // results in a no operation
}
fxpt_16_16 test_07 (fxpt_16_16 a, fxpt_16_16 b)
{
return std::min (a, b);
}
fxpt_16_16 test_08 (fxpt_16_16 a, fxpt_16_16 b)
{
return std::max (a, b);
}
fxpt_16_16 test_14 (void)
{
return 1;
}
fxpt_16_16 test_15 (fxpt_16_16 a, fxpt_8_24 b)
{
return a + (fxpt_16_16)b;
}
fxpt_31_33 test_16 (fxpt_32_32 a, fxpt_32_32 b)
{
return (fxpt_31_33)(a + b); // cast to a different type is explicit
}
fxpt_32_0 test_17 (fxpt_16_16 a)
{
return (fxpt_32_0)a;
}
fxpt_4_4 test_17_1 (fxpt_16_16 a)
{
return (fxpt_4_4)a;
}
fxpt_4_4 test_17_2 (fxpt_4_4 a, fxpt_4_4 b)
{
return a + b;
}
fxpt_8_8 test_17_3 (fxpt_8_8 a, fxpt_8_8 b)
{
return a * b;
}
fxpt_4_4 test_17_4 (fxpt_4_4 a, fxpt_4_4 b)
{
return a * b; // minimum possible base_type, its widened_type and the lack of its narrowed_type
}
void test_18 (fxpt_16_16& a, int b)
{
a *= b;
}
void test_19 (fxpt_16_16& a, float b)
{
a *= b;
}
void test_19_1 (fxpt_16_16& a, fxpt_16_16 b, fxpt_16_16 c)
{
a *= b * c;
}
fxpt_16_16 test_23 (float a)
{
return 5.0f; // implcit conversion ctor
}
fxptu_16_16 test_24 (fxpt_16_16 a)
{
return (fxptu_16_16)a; // explicit cast required
}
fxpt_32_32 test_25 (fxpt_32_32 a, fxpt_32_32 b)
{
// return a * b; // NG: would require 128 bit integer
// -> compile time error
return 0;
}
void test_26 (fxpt_16_16& a)
{
a ++;
}
fxpt_16_16 test_26_1 (fxpt_16_16 a)
{
return ++ a;
}
void test_27 (float& a)
{
++ a;
}
fxpt_16_16 test_28 (fxpt_16_16 a, int b)
{
return a * b;
}
fxpt_16_16 test_29 (fxpt_16_16 a, int b)
{
return b * a;
}
fxpt_16_16 test_30 (fxpt_16_16 a, bool b)
{
return b * a;
}
fxpt_16_16 test_31 (fxpt_16_16 a, int b)
{
return a / b;
}
fxpt_16_16 test_32 (fxpt_16_16 a, int b)
{
return a / 256; // generates shift code
}
int32_t test_33 (fxpt_16_16 a, int32_t b, int32_t c)
{
if (a) // converted to bool even though the conversion op is explicit
return b;
else
return c;
}
bool test_34_0 (fxpt_16_16 a)
{
return (bool)a; // cast must be written due to explicit conversion op
}
fxpt_16_16 test_34_1 (bool a)
{
return a; // implicit conversion ctor from bool
}
fxpt_16_16 test_35 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a * b * c * d;
}
fxpt_16_16 test_36 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return (a * b) * (c * d);
}
fxpt_16_16 test_37 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16& d)
{
d *= a * b;
return 0;
}
void test_38 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16& d)
{
d += a * b;
}
fxpt_16_16 test_38_1 (fxpt_16_16 a, fxpt_16_16 b, float c, float d)
{
return a + c;
}
fxpt_16_16 test_39 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16& d)
{
return a * b + c;
}
fxpt_16_16 test_40 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16& d)
{
return c + a * b;
}
fxpt_16_16 test_41 (fxpt_16_16 a, fxpt_16_16 b, int c, int d)
{
return -(a * b);
}
void test_42 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16& c, int d)
{
c -= a * b;
}
fxpt_16_16 test_43 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return (a * b) - (c * d);
}
fxpt_16_16 test_44 (fxpt_16_16 a, fxpt_16_16 b, int c, fxpt_16_16 d)
{
return (a * b) + c;
}
fxpt_16_16 test_45 (fxpt_16_16 a, fxpt_16_16 b, int c, fxpt_16_16 d)
{
return (a * b) - c;
}
fxpt_16_16 test_46 (fxpt_16_16 a, fxpt_16_16 b, int c, fxpt_16_16 d)
{
return (a * b) - c + d;
}
fxpt_16_16 test_47 (fxpt_16_16 a, fxpt_16_16 b, int c, fxpt_16_16 d, int e)
{
return ((a * b) - c + d) * e;
}
fxpt_16_16 test_48 (fxpt_16_16 a, fxpt_16_16 b, int c, fxpt_16_16 d)
{
return (a * b) / c; // widened / int
}
fxpt_16_16 test_49 (fxpt_16_16 a, fxpt_16_16 b, int c, fxpt_16_16 d)
{
return a / b; // fixed / fixed
}
fxpt_16_16 test_50 (fxpt_16_16 a, fxpt_16_16 b, int c, fxpt_16_16 d)
{
return (a * b) / d; // widened / fixed
}
fxpt_16_16 test_51 (fxpt_16_16 a, fxpt_16_16 b, int c, fxpt_16_16 d)
{
return d / (a * b); // fixed / widened
}
void test_51 (fxpt_16_16 a, fxpt_16_16 b, int c, fxpt_16_16& d)
{
d /= a * b; // fixed / widened
}
fxpt_16_16 test_52 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16& d)
{
return (a * b) / (c * d); // widened / widened
}
fxpt_16_16 test_53 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16& d)
{
return a * b / c * d; // (mul (div (mul a b) c) d)
}
fxpt_16_16 test_54 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16& d)
{
return a * b / c + d;
}
fxpt_9_23 test_55 (fxpt_8_24 a, fxpt_16_16 b, fxpt_31_33 c)
{
return (fxpt_9_23)a;
}
fxpt_9_23 test_56 (fxpt_8_24 a, fxpt_16_16 b, fxpt_31_33 c)
{
// return a + b;
return (fxpt_9_23)a + (fxpt_9_23)b; // needs excplicit cast
// could do without, but then precision control becomes
// difficult and the automagic might do the wrong thing.
}
fxpt_16_16 test_57 (void)
{
// implicit
fxpt_16_16 r = 12;
r = 18;
return 4;
}
fxpt_16_16 test_58 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16& c, int d)
{
auto r = a * b;
r = c; // implicit conversion to widened
return d;
}
fxpt_16_16 test_59 (fxpt_16_16 a, float b)
{
return a + b;
}
fxpt_16_16 test_60 (fxpt_16_16 a, float b)
{
return a * b;
}
fxpt_16_16 test_61 (float a, fxpt_16_16 b)
{
return a * b;
}
fxpt_16_16 test_62 (fxpt_16_16 a)
{
return 5.0f + a;
}
fxpt_16_16 test_63 (fxpt_16_16 a)
{
return a + 5.0f;
}
fxpt_16_16 test_64 (fxpt_16_16 a)
{
return a * 5.0f;
}
fxpt_16_16 test_65 (fxpt_16_16 a)
{
return 5.0f * a;
}
void test_65_1 (fxpt_16_16& a, float b)
{
a *= b;
}
void test_65_2 (fxpt_16_16& a, float b)
{
a *= 6.0f;
}
void test_65_3 (fxpt_16_16& a, float b)
{
a /= b;
}
void test_65_4 (fxpt_16_16& a, float b)
{
a /= 0.3f;
}
void test_65_5 (fxpt_16_16& a, fxpt_16_16 b, float c)
{
auto r = b * c;
a /= b;
}
void test_65_5 (fxpt_16_16& a, fxpt_16_16 b, float c, float d)
{
auto r = b * c;
r /= d;
a = r;
}
fxpt_16_16 test_66 (fxpt_16_16 a, fxpt_16_16 b, float c)
{
return a * (b * c);
}
fxpt_16_16 test_67 (fxpt_16_16 a, fxpt_16_16 b, float c)
{
return (a * b) * c;
}
fxpt_16_16 test_68 (fxpt_16_16 a, fxpt_16_16 b, float c)
{
return a * b * c;
}
fxpt_16_16 test_69 (fxpt_16_16 a, fxpt_16_16 b, float c, int d)
{
return a * b * c + d;
}
fxpt_16_16 test_70 (fxpt_16_16 a, fxpt_16_16 b, float c, int d)
{
return a * b * c * d;
}
bool test_71 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a == b;
}
bool test_72 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a != b;
}
bool test_73 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a == b;
}
bool test_74 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a < b;
}
bool test_75 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a <= b;
}
bool test_76 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a > b;
}
bool test_77 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a >= b;
}
bool test_78 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return !a;
}
bool test_79 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a * b == c;
}
bool test_80 (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
return a * b + c == d;
}
void test_82 (fxpt_32_32& a, fxpt_32_32 b)
{
// a *= b; // NG: would require 128 bit intermediate
// -> compile time error
}
const fxpt_16_16& test_83 (void)
{
static const fxpt_16_16 x = 5.5;
return x;
}
const fxpt_16_16& test_83_1 (void)
{
static const fxpt_16_16 x = 5.5f;
return x;
}
constexpr inline int32_t f (int32_t a, int32_t b)
{
return a + b;
}
constexpr inline float g (float a, float b)
{
return a + b;
}
constexpr fxpt_16_16 h (fxpt_16_16 a, fxpt_16_16 b)
{
return a + b;
}
static constexpr int32_t global_x = f (5, 6);
static constexpr float global_y = g (5.0f, 6.0f);
static constexpr fxpt_16_16 global_z = h (5, 6);
fxpt_16_16 test_84 (fxpt_16_16 a, fxpt_16_16 b)
{
return std::copysign (a, b);
}
int main (void)
{
return 0;
}