-
Notifications
You must be signed in to change notification settings - Fork 81
/
reference_math.cpp
5538 lines (4630 loc) · 175 KB
/
reference_math.cpp
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************
//
// OpenCL Conformance Tests
//
// Copyright: (c) 2008-2013 by Apple Inc. All Rights Reserved.
//
******************************************************************/
#if defined( _MSC_VER )
# pragma warning( push )
# pragma warning( disable : 4056 ) /* overflow in floating-point constant arithmetic */
# pragma warning( disable : 4101 ) /* unreferenced local variable */
# pragma warning( disable : 4146 ) /* unary minus operator applied to unsigned type, result still unsigned */
# pragma warning( disable : 4244 ) /* conversion from 'double' to 'float', possible loss of data */
# pragma warning( disable : 4723 ) /* potential divide by 0 */
# pragma warning( disable : 4756 ) /* overflow in constant arithmetic */
#endif
//
// Todo
// reference_log1pl
// reference_exp2
//
#include "compat.h"
#include "reference_math.h"
#include <limits.h>
#if !defined(_WIN32)
#include <cstring>
#endif
#include "Utility.h"
#if defined( __SSE__ ) || (defined( _MSC_VER ) && (defined(_M_IX86) || defined(_M_X64)))
#include <xmmintrin.h>
#endif
#if defined( __SSE2__ ) || (defined( _MSC_VER ) && (defined(_M_IX86) || defined(_M_X64)))
#include <emmintrin.h>
#endif
#if defined(__ANDROID__)
#define sqrtl(X) sqrt(X)
#define expm1l(X) expm1(X)
#define log1pl(X) log1p(X)
#define sinl(X) sin(X)
#define asinl(X) asin(X)
#define cosl(X) cos(X)
#define expl(X) exp(X)
#define tanl(X) tan(X)
#define atan2l(X,Y) atan2(X,Y)
#define atanl(X) atan(X)
#define tanhl(X) tanh(X)
#define coshl(X) cosh(X)
#define sinhl(X) sinh(X)
float modff_android( float value, float *iptr )
{
*iptr = truncf( value );
return copysignf( isinf( value ) ? 0.0 : value - *iptr, value );
}
#endif
#ifndef M_PI_4
#define M_PI_4 (M_PI/4)
#endif
#define EVALUATE( x ) x
#define CONCATENATE(x, y) x ## EVALUATE(y)
// Declare Classification macros for non-C99 platforms
#ifndef isinf
#define isinf(x) \
[](auto y) { \
if constexpr (sizeof(y) == sizeof(float)) \
return fabsf(y) == INFINITY; \
else if constexpr (sizeof(y) == sizeof(double)) \
return fabs(y) == INFINITY; \
else \
return fabsl(y) == INFINITY; \
}(x)
#endif
#ifndef isfinite
#define isfinite(x) \
[](auto y) { \
if constexpr (sizeof(y) == sizeof(float)) \
return fabsf(y) < INFINITY; \
else if constexpr (sizeof(y) == sizeof(double)) \
return fabs(y) < INFINITY; \
else \
return fabsl(y) < INFINITY; \
}(x)
#endif
#ifndef isnan
#define isnan(_a) ( (_a) != (_a) )
#endif
#ifdef __MINGW32__
#undef isnormal
#endif
#ifndef isnormal
#define isnormal(x) \
[](auto y) { \
if constexpr (sizeof(y) == sizeof(float)) \
return fabsf(y) < INFINITY && fabsf(y) >= FLT_MIN; \
else if constexpr (sizeof(y) == sizeof(double)) \
return fabs(y) < INFINITY && fabs(y) >= DBL_MIN; \
else \
return fabsl(y) < INFINITY && fabsl(y) >= LDBL_MIN; \
}(x)
#endif
#ifndef islessgreater
// Note: Non-C99 conformant. This will trigger floating point exceptions. We don't care about that here.
#define islessgreater( _x, _y ) ( (_x) < (_y) || (_x) > (_y) )
#endif
#if !defined ( _MSC_VER )
#pragma STDC FP_CONTRACT OFF
#endif
static void __log2_ep(double *hi, double *lo, double x);
typedef union
{
uint64_t i;
double d;
}uint64d_t;
static const uint64d_t _CL_NAN = { 0x7ff8000000000000ULL };
#define cl_make_nan() _CL_NAN.d
static double reduce1( double x );
static double reduce1( double x )
{
if( fabs(x) >= HEX_DBL( +, 1, 0, +, 53 ) )
{
if( fabs(x) == INFINITY )
return cl_make_nan();
return 0.0; //we patch up the sign for sinPi and cosPi later, since they need different signs
}
// Find the nearest multiple of 2
const double r = copysign( HEX_DBL( +, 1, 0, +, 53 ), x );
double z = x + r;
z -= r;
// subtract it from x. Value is now in the range -1 <= x <= 1
return x - z;
}
/*
static double reduceHalf( double x );
static double reduceHalf( double x )
{
if( fabs(x) >= HEX_DBL( +, 1, 0, +, 52 ) )
{
if( fabs(x) == INFINITY )
return cl_make_nan();
return 0.0; //we patch up the sign for sinPi and cosPi later, since they need different signs
}
// Find the nearest multiple of 1
const double r = copysign( HEX_DBL( +, 1, 0, +, 52 ), x );
double z = x + r;
z -= r;
// subtract it from x. Value is now in the range -0.5 <= x <= 0.5
return x - z;
}
*/
double reference_acospi( double x) { return reference_acos( x ) / M_PI; }
double reference_asinpi( double x) { return reference_asin( x ) / M_PI; }
double reference_atanpi( double x) { return reference_atan( x ) / M_PI; }
double reference_atan2pi( double y, double x ) { return reference_atan2( y, x) / M_PI; }
double reference_cospi( double x)
{
if( reference_fabs(x) >= HEX_DBL( +, 1, 0, +, 52 ) )
{
if( reference_fabs(x) == INFINITY )
return cl_make_nan();
//Note this probably fails for odd values between 0x1.0p52 and 0x1.0p53.
//However, when starting with single precision inputs, there will be no odd values.
return 1.0;
}
x = reduce1(x+0.5);
// reduce to [-0.5, 0.5]
if( x < -0.5 )
x = -1 - x;
else if ( x > 0.5 )
x = 1 - x;
// cosPi zeros are all +0
if( x == 0.0 )
return 0.0;
return reference_sin( x * M_PI );
}
double reference_relaxed_divide( double x, double y ) { return (float)(((float) x ) / ( (float) y )); }
double reference_divide( double x, double y ) { return x / y; }
// Add a + b. If the result modulo overflowed, write 1 to *carry, otherwise 0
static inline cl_ulong add_carry( cl_ulong a, cl_ulong b, cl_ulong *carry )
{
cl_ulong result = a + b;
*carry = result < a;
return result;
}
// Subtract a - b. If the result modulo overflowed, write 1 to *carry, otherwise 0
static inline cl_ulong sub_carry( cl_ulong a, cl_ulong b, cl_ulong *carry )
{
cl_ulong result = a - b;
*carry = result > a;
return result;
}
static float fallback_frexpf( float x, int *iptr )
{
cl_uint u, v;
float fu, fv;
std::memcpy( &u, &x, sizeof(u));
cl_uint exponent = u & 0x7f800000U;
cl_uint mantissa = u & ~0x7f800000U;
// add 1 to the exponent
exponent += 0x00800000U;
if( (cl_int) exponent < (cl_int) 0x01000000 )
{ // subnormal, NaN, Inf
mantissa |= 0x3f000000U;
v = mantissa & 0xff800000U;
u = mantissa;
std::memcpy( &fv, &v, sizeof(v));
std::memcpy( &fu, &u, sizeof(u));
fu -= fv;
std::memcpy( &v, &fv, sizeof(v));
std::memcpy( &u, &fu, sizeof(u));
exponent = u & 0x7f800000U;
mantissa = u & ~0x7f800000U;
*iptr = (exponent >> 23) + (-126 + 1 -126);
u = mantissa | 0x3f000000U;
std::memcpy( &fu, &u, sizeof(u));
return fu;
}
*iptr = (exponent >> 23) - 127;
u = mantissa | 0x3f000000U;
std::memcpy( &fu, &u, sizeof(u));
return fu;
}
static inline int extractf( float, cl_uint * );
static inline int extractf( float x, cl_uint *mant )
{
static float (*frexppf)(float, int*) = NULL;
int e;
// verify that frexp works properly
if( NULL == frexppf )
{
if( 0.5f == frexpf( HEX_FLT( +, 1, 0, -, 130 ), &e ) && e == -129 )
frexppf = frexpf;
else
frexppf = fallback_frexpf;
}
*mant = (cl_uint) (HEX_FLT( +, 1, 0, +, 32 ) * fabsf( frexppf( x, &e )));
return e - 1;
}
// Shift right by shift bits. Any bits lost on the right side are bitwise OR'd together and ORd into the LSB of the result
static inline void shift_right_sticky_64( cl_ulong *p, int shift );
static inline void shift_right_sticky_64( cl_ulong *p, int shift )
{
cl_ulong sticky = 0;
cl_ulong r = *p;
// C doesn't handle shifts greater than the size of the variable dependably
if( shift >= 64 )
{
sticky |= (0 != r);
r = 0;
}
else
{
sticky |= (0 != (r << (64-shift)));
r >>= shift;
}
*p = r | sticky;
}
// Add two 64 bit mantissas. Bits that are below the LSB of the result are OR'd into the LSB of the result
static inline void add64( cl_ulong *p, cl_ulong c, int *exponent );
static inline void add64( cl_ulong *p, cl_ulong c, int *exponent )
{
cl_ulong carry;
c = add_carry(c, *p, &carry);
if( carry )
{
carry = c & 1; // set aside sticky bit
c >>= 1; // right shift to deal with overflow
c |= carry | 0x8000000000000000ULL; // or in carry bit, and sticky bit. The latter is to prevent rounding from believing we are exact half way case
*exponent = *exponent + 1; // adjust exponent
}
*p = c;
}
// IEEE-754 round to nearest, ties to even rounding
static float round_to_nearest_even_float( cl_ulong p, int exponent );
static float round_to_nearest_even_float( cl_ulong p, int exponent )
{
union{ cl_uint u; cl_float d;} u;
// If mantissa is zero, return 0.0f
if (p == 0) return 0.0f;
// edges
if( exponent > 127 )
{
volatile float r = exponent * CL_FLT_MAX; // signal overflow
// attempt to fool the compiler into not optimizing the above line away
if( r > CL_FLT_MAX )
return INFINITY;
return r;
}
if( exponent == -150 && p > 0x8000000000000000ULL)
return HEX_FLT( +, 1, 0, -, 149 );
if( exponent <= -150 ) return 0.0f;
//Figure out which bits go where
int shift = 8 + 32;
if( exponent < -126 )
{
shift -= 126 + exponent; // subnormal: shift is not 52
exponent = -127; // set exponent to 0
}
else
p &= 0x7fffffffffffffffULL; // normal: leading bit is implicit. Remove it.
// Assemble the double (round toward zero)
u.u = (cl_uint)(p >> shift) | ((cl_uint) (exponent + 127) << 23);
// put a representation of the residual bits into hi
p <<= (64-shift);
//round to nearest, ties to even based on the unused portion of p
if( p < 0x8000000000000000ULL ) return u.d;
if( p == 0x8000000000000000ULL ) u.u += u.u & 1U;
else u.u++;
return u.d;
}
static float round_to_nearest_even_float_ftz( cl_ulong p, int exponent );
static float round_to_nearest_even_float_ftz( cl_ulong p, int exponent )
{
extern int gCheckTininessBeforeRounding;
union{ cl_uint u; cl_float d;} u;
int shift = 8 + 32;
// If mantissa is zero, return 0.0f
if (p == 0) return 0.0f;
// edges
if( exponent > 127 )
{
volatile float r = exponent * CL_FLT_MAX; // signal overflow
// attempt to fool the compiler into not optimizing the above line away
if( r > CL_FLT_MAX )
return INFINITY;
return r;
}
// Deal with FTZ for gCheckTininessBeforeRounding
if( exponent < (gCheckTininessBeforeRounding - 127) )
return 0.0f;
if( exponent == -127 ) // only happens for machines that check tininess after rounding
p = (p&1) | (p>>1);
else
p &= 0x7fffffffffffffffULL; // normal: leading bit is implicit. Remove it.
cl_ulong q = p;
// Assemble the double (round toward zero)
u.u = (cl_uint)(q >> shift) | ((cl_uint) (exponent + 127) << 23);
// put a representation of the residual bits into hi
q <<= (64-shift);
//round to nearest, ties to even based on the unused portion of p
if( q > 0x8000000000000000ULL )
u.u++;
else if( q == 0x8000000000000000ULL )
u.u += u.u & 1U;
// Deal with FTZ for ! gCheckTininessBeforeRounding
if( 0 == (u.u & 0x7f800000U ) )
return 0.0f;
return u.d;
}
// IEEE-754 round toward zero.
static float round_toward_zero_float( cl_ulong p, int exponent );
static float round_toward_zero_float( cl_ulong p, int exponent )
{
union{ cl_uint u; cl_float d;} u;
// If mantissa is zero, return 0.0f
if (p == 0) return 0.0f;
// edges
if( exponent > 127 )
{
volatile float r = exponent * CL_FLT_MAX; // signal overflow
// attempt to fool the compiler into not optimizing the above line away
if( r > CL_FLT_MAX )
return CL_FLT_MAX;
return r;
}
if( exponent <= -149 )
return 0.0f;
//Figure out which bits go where
int shift = 8 + 32;
if( exponent < -126 )
{
shift -= 126 + exponent; // subnormal: shift is not 52
exponent = -127; // set exponent to 0
}
else
p &= 0x7fffffffffffffffULL; // normal: leading bit is implicit. Remove it.
// Assemble the double (round toward zero)
u.u = (cl_uint)(p >> shift) | ((cl_uint) (exponent + 127) << 23);
return u.d;
}
static float round_toward_zero_float_ftz( cl_ulong p, int exponent );
static float round_toward_zero_float_ftz( cl_ulong p, int exponent )
{
extern int gCheckTininessBeforeRounding;
union{ cl_uint u; cl_float d;} u;
int shift = 8 + 32;
// If mantissa is zero, return 0.0f
if (p == 0) return 0.0f;
// edges
if( exponent > 127 )
{
volatile float r = exponent * CL_FLT_MAX; // signal overflow
// attempt to fool the compiler into not optimizing the above line away
if( r > CL_FLT_MAX )
return CL_FLT_MAX;
return r;
}
// Deal with FTZ for gCheckTininessBeforeRounding
if( exponent < -126 )
return 0.0f;
cl_ulong q = p &= 0x7fffffffffffffffULL; // normal: leading bit is implicit. Remove it.
// Assemble the double (round toward zero)
u.u = (cl_uint)(q >> shift) | ((cl_uint) (exponent + 127) << 23);
// put a representation of the residual bits into hi
q <<= (64-shift);
return u.d;
}
// Subtract two significands.
static inline void sub64( cl_ulong *c, cl_ulong p, cl_uint *signC, int *expC );
static inline void sub64( cl_ulong *c, cl_ulong p, cl_uint *signC, int *expC )
{
cl_ulong carry;
p = sub_carry( *c, p, &carry );
if( carry )
{
*signC ^= 0x80000000U;
p = -p;
}
// normalize
if( p )
{
int shift = 32;
cl_ulong test = 1ULL << 32;
while( 0 == (p & 0x8000000000000000ULL))
{
if( p < test )
{
p <<= shift;
*expC = *expC - shift;
}
shift >>= 1;
test <<= shift;
}
}
else
{
// zero result.
*expC = -200;
*signC = 0; // IEEE rules say a - a = +0 for all rounding modes except -inf
}
*c = p;
}
float reference_fma( float a, float b, float c, int shouldFlush )
{
static const cl_uint kMSB = 0x80000000U;
// Make bits accessible
union{ cl_uint u; cl_float d; } ua; ua.d = a;
union{ cl_uint u; cl_float d; } ub; ub.d = b;
union{ cl_uint u; cl_float d; } uc; uc.d = c;
// deal with Nans, infinities and zeros
if( isnan( a ) || isnan( b ) || isnan(c) ||
isinf( a ) || isinf( b ) || isinf(c) ||
0 == ( ua.u & ~kMSB) || // a == 0, defeat host FTZ behavior
0 == ( ub.u & ~kMSB) || // b == 0, defeat host FTZ behavior
0 == ( uc.u & ~kMSB) ) // c == 0, defeat host FTZ behavior
{
FPU_mode_type oldMode;
RoundingMode oldRoundMode = kRoundToNearestEven;
if( isinf( c ) && !isinf(a) && !isinf(b) )
return (c + a) + b;
if (gIsInRTZMode)
oldRoundMode = set_round(kRoundTowardZero, kfloat);
memset( &oldMode, 0, sizeof( oldMode ) );
if( shouldFlush )
ForceFTZ( &oldMode );
a = (float) reference_multiply( a, b ); // some risk that the compiler will insert a non-compliant fma here on some platforms.
a = (float) reference_add( a, c ); // We use STDC FP_CONTRACT OFF above to attempt to defeat that.
if( shouldFlush )
RestoreFPState( &oldMode );
if( gIsInRTZMode )
set_round(oldRoundMode, kfloat);
return a;
}
// extract exponent and mantissa
// exponent is a standard unbiased signed integer
// mantissa is a cl_uint, with leading non-zero bit positioned at the MSB
cl_uint mantA, mantB, mantC;
int expA = extractf( a, &mantA );
int expB = extractf( b, &mantB );
int expC = extractf( c, &mantC );
cl_uint signC = uc.u & kMSB; // We'll need the sign bit of C later to decide if we are adding or subtracting
// exact product of A and B
int exponent = expA + expB;
cl_uint sign = (ua.u ^ ub.u) & kMSB;
cl_ulong product = (cl_ulong) mantA * (cl_ulong) mantB;
// renormalize -- 1.m * 1.n yields a number between 1.0 and 3.99999..
// The MSB might not be set. If so, fix that. Otherwise, reflect the fact that we got another power of two from the multiplication
if( 0 == (0x8000000000000000ULL & product) )
product <<= 1;
else
exponent++; // 2**31 * 2**31 gives 2**62. If the MSB was set, then our exponent increased.
//infinite precision add
cl_ulong addend = (cl_ulong) mantC << 32;
if( exponent >= expC )
{
// Shift C relative to the product so that their exponents match
if( exponent > expC )
shift_right_sticky_64( &addend, exponent - expC );
// Add
if( sign ^ signC )
sub64( &product, addend, &sign, &exponent );
else
add64( &product, addend, &exponent );
}
else
{
// Shift the product relative to C so that their exponents match
shift_right_sticky_64( &product, expC - exponent );
// add
if( sign ^ signC )
sub64( &addend, product, &signC, &expC );
else
add64( &addend, product, &expC );
product = addend;
exponent = expC;
sign = signC;
}
// round to IEEE result -- we do not do flushing to zero here. That part is handled manually in ternary.c.
if (gIsInRTZMode)
{
if( shouldFlush )
ua.d = round_toward_zero_float_ftz( product, exponent);
else
ua.d = round_toward_zero_float( product, exponent);
}
else
{
if( shouldFlush )
ua.d = round_to_nearest_even_float_ftz( product, exponent);
else
ua.d = round_to_nearest_even_float( product, exponent);
}
// Set the sign
ua.u |= sign;
return ua.d;
}
double reference_relaxed_exp10( double x)
{
return reference_exp10(x);
}
double reference_exp10( double x) { return reference_exp2( x * HEX_DBL( +, 1, a934f0979a371, +, 1 ) ); }
int reference_ilogb( double x )
{
extern int gDeviceILogb0, gDeviceILogbNaN;
union { cl_double f; cl_ulong u;} u;
u.f = (float) x;
cl_int exponent = (cl_int) (u.u >> 52) & 0x7ff;
if( exponent == 0x7ff )
{
if( u.u & 0x000fffffffffffffULL )
return gDeviceILogbNaN;
return CL_INT_MAX;
}
if( exponent == 0 )
{ // deal with denormals
u.f = x * HEX_DBL( +, 1, 0, +, 64 );
exponent = (cl_int) (u.u >> 52) & 0x7ff;
if( exponent == 0 )
return gDeviceILogb0;
return exponent - (1023 + 64);
}
return exponent - 1023;
}
double reference_nan( cl_uint x )
{
union{ cl_uint u; cl_float f; }u;
u.u = x | 0x7fc00000U;
return (double) u.f;
}
double reference_maxmag( double x, double y )
{
double fabsx = fabs(x);
double fabsy = fabs(y);
if( fabsx < fabsy )
return y;
if( fabsy < fabsx )
return x;
return reference_fmax( x, y );
}
double reference_minmag( double x, double y )
{
double fabsx = fabs(x);
double fabsy = fabs(y);
if( fabsx > fabsy )
return y;
if( fabsy > fabsx )
return x;
return reference_fmin( x, y );
}
//double my_nextafter( double x, double y ){ return (double) nextafterf( (float) x, (float) y ); }
double reference_relaxed_mad( double a, double b, double c)
{
return ((float) a )* ((float) b) + (float) c;
}
double reference_mad( double a, double b, double c )
{
return a * b + c;
}
double reference_recip( double x) { return 1.0 / x; }
double reference_rootn( double x, int i )
{
//rootn ( x, 0 ) returns a NaN.
if( 0 == i )
return cl_make_nan();
//rootn ( x, n ) returns a NaN for x < 0 and n is even.
if( x < 0 && 0 == (i&1) )
return cl_make_nan();
if( x == 0.0 )
{
switch( i & 0x80000001 )
{
//rootn ( +-0, n ) is +0 for even n > 0.
case 0:
return 0.0f;
//rootn ( +-0, n ) is +-0 for odd n > 0.
case 1:
return x;
//rootn ( +-0, n ) is +inf for even n < 0.
case 0x80000000:
return INFINITY;
//rootn ( +-0, n ) is +-inf for odd n < 0.
case 0x80000001:
return copysign( (double)INFINITY, x);
}
}
double sign = x;
x = reference_fabs(x);
x = reference_exp2( reference_log2(x) / (double) i );
return reference_copysignd( x, sign );
}
double reference_rsqrt( double x) { return 1.0 / reference_sqrt(x); }
//double reference_sincos( double x, double *c ){ *c = cos(x); return sin(x); }
double reference_sinpi( double x)
{
double r = reduce1(x);
// reduce to [-0.5, 0.5]
if( r < -0.5 )
r = -1 - r;
else if ( r > 0.5 )
r = 1 - r;
// sinPi zeros have the same sign as x
if( r == 0.0 )
return reference_copysignd(0.0, x);
return reference_sin( r * M_PI );
}
double reference_tanpi( double x)
{
// set aside the sign (allows us to preserve sign of -0)
double sign = reference_copysignd( 1.0, x);
double z = reference_fabs(x);
// if big and even -- caution: only works if x only has single precision
if( z >= HEX_DBL( +, 1, 0, +, 24 ) )
{
if( z == INFINITY )
return x - x; // nan
return reference_copysignd( 0.0, x); // tanpi ( n ) is copysign( 0.0, n) for even integers n.
}
// reduce to the range [ -0.5, 0.5 ]
double nearest = reference_rint( z ); // round to nearest even places n + 0.5 values in the right place for us
int i = (int) nearest; // test above against 0x1.0p24 avoids overflow here
z -= nearest;
//correction for odd integer x for the right sign of zero
if( (i&1) && z == 0.0 )
sign = -sign;
// track changes to the sign
sign *= reference_copysignd(1.0, z); // really should just be an xor
z = reference_fabs(z); // remove the sign again
// reduce once more
// If we don't do this, rounding error in z * M_PI will cause us not to return infinities properly
if( z > 0.25 )
{
z = 0.5 - z;
return sign / reference_tan( z * M_PI ); // use system tan to get the right result
}
//
return sign * reference_tan( z * M_PI ); // use system tan to get the right result
}
double reference_pown( double x, int i) { return reference_pow( x, (double) i ); }
double reference_powr( double x, double y )
{
//powr ( x, y ) returns NaN for x < 0.
if( x < 0.0 )
return cl_make_nan();
//powr ( x, NaN ) returns the NaN for x >= 0.
//powr ( NaN, y ) returns the NaN.
if( isnan(x) || isnan(y) )
return x + y; // Note: behavior different here than for pow(1,NaN), pow(NaN, 0)
if( x == 1.0 )
{
//powr ( +1, +-inf ) returns NaN.
if( reference_fabs(y) == INFINITY )
return cl_make_nan();
//powr ( +1, y ) is 1 for finite y. (NaN handled above)
return 1.0;
}
if( y == 0.0 )
{
//powr ( +inf, +-0 ) returns NaN.
//powr ( +-0, +-0 ) returns NaN.
if( x == 0.0 || x == INFINITY )
return cl_make_nan();
//powr ( x, +-0 ) is 1 for finite x > 0. (x <= 0, NaN, INF already handled above)
return 1.0;
}
if( x == 0.0 )
{
//powr ( +-0, -inf) is +inf.
//powr ( +-0, y ) is +inf for finite y < 0.
if( y < 0.0 )
return INFINITY;
//powr ( +-0, y ) is +0 for y > 0. (NaN, y==0 handled above)
return 0.0;
}
// x = +inf
if( isinf(x) )
{
if( y < 0 )
return 0;
return INFINITY;
}
double fabsx = reference_fabs(x);
double fabsy = reference_fabs(y);
//y = +-inf cases
if( isinf(fabsy) )
{
if( y < 0 )
{
if( fabsx < 1 )
return INFINITY;
return 0;
}
if( fabsx < 1 )
return 0;
return INFINITY;
}
double hi, lo;
__log2_ep(&hi, &lo, x);
double prod = y * hi;
double result = reference_exp2(prod);
return result;
}
double reference_fract( double x, double *ip )
{
float i;
#if defined(__ANDROID__)
float f = modff_android((float) x, &i );
#else
float f = modff((float) x, &i );
#endif
if( f < 0.0 )
{
f = 1.0f + f;
i -= 1.0f;
if( f == 1.0f )
f = HEX_FLT( +, 1, fffffe, -, 1 );
}
*ip = i;
return f;
}
//double my_fdim( double x, double y){ return fdimf( (float) x, (float) y ); }
double reference_add( double x, double y )
{
volatile float a = (float) x;
volatile float b = (float) y;
#if defined( __SSE__ ) || (defined( _MSC_VER ) && (defined(_M_IX86) || defined(_M_X64)))
// defeat x87
__m128 va = _mm_set_ss( (float) a );
__m128 vb = _mm_set_ss( (float) b );
va = _mm_add_ss( va, vb );
_mm_store_ss( (float*) &a, va );
#elif defined(__PPC__)
// Most Power host CPUs do not support the non-IEEE mode (NI) which flushes denorm's to zero.
// As such, the reference add with FTZ must be emulated in sw.
if (fpu_control & _FPU_MASK_NI) {
union{ cl_uint u; cl_float d; } ua; ua.d = a;
union{ cl_uint u; cl_float d; } ub; ub.d = b;
cl_uint mantA, mantB;
cl_ulong addendA, addendB, sum;
int expA = extractf( a, &mantA );
int expB = extractf( b, &mantB );
cl_uint signA = ua.u & 0x80000000U;
cl_uint signB = ub.u & 0x80000000U;
// Force matching exponents if an operand is 0
if (a == 0.0f) {
expA = expB;
} else if (b == 0.0f) {
expB = expA;
}
addendA = (cl_ulong)mantA << 32;
addendB = (cl_ulong)mantB << 32;
if (expA >= expB) {
// Shift B relative to the A so that their exponents match
if( expA > expB )
shift_right_sticky_64( &addendB, expA - expB );
// add
if( signA ^ signB )
sub64( &addendA, addendB, &signA, &expA );
else
add64( &addendA, addendB, &expA );
} else {
// Shift the A relative to B so that their exponents match
shift_right_sticky_64( &addendA, expB - expA );
// add
if( signA ^ signB )
sub64( &addendB, addendA, &signB, &expB );
else
add64( &addendB, addendA, &expB );
addendA = addendB;
expA = expB;
signA = signB;