forked from serious-steve/rnnlm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastexp.h
1608 lines (1338 loc) · 57.1 KB
/
fastexp.h
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
/*=====================================================================*
* Copyright (C) 2012 Paul Mineiro *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with *
* or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the *
* above copyright notice, this list of conditions and *
* the following disclaimer. *
* *
* * 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. *
* *
* * Neither the name of Paul Mineiro nor the names *
* of other 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 OWNER *
* OR CONTRIBUTORS 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. *
* *
* Contact: Paul Mineiro <[email protected]> *
*=====================================================================*/
#ifndef __CAST_H_
#ifdef __cplusplus
#define cast_uint32_t static_cast<uint32_t>
#else
#define cast_uint32_t (uint32_t)
#endif
#endif // __CAST_H_
/*=====================================================================*
* Copyright (C) 2011 Paul Mineiro *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with *
* or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the *
* above copyright notice, this list of conditions and *
* the following disclaimer. *
* *
* * 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. *
* *
* * Neither the name of Paul Mineiro nor the names *
* of other 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 OWNER *
* OR CONTRIBUTORS 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. *
* *
* Contact: Paul Mineiro <[email protected]> *
*=====================================================================*/
#ifndef __SSE_H_
#define __SSE_H_
#ifdef __SSE2__
#include <emmintrin.h>
#ifdef __cplusplus
namespace {
#endif // __cplusplus
typedef __m128 v4sf;
typedef __m128i v4si;
#define v4si_to_v4sf _mm_cvtepi32_ps
#define v4sf_to_v4si _mm_cvttps_epi32
#define v4sfl(x) ((const v4sf) { (x), (x), (x), (x) })
#define v2dil(x) ((const v4si) { (x), (x) })
#define v4sil(x) v2dil((((unsigned long long) (x)) << 32) | (x))
typedef union { v4sf f; float array[4]; } v4sfindexer;
#define v4sf_index(_findx, _findi) \
({ \
v4sfindexer _findvx = { _findx } ; \
_findvx.array[_findi]; \
})
typedef union { v4si i; int array[4]; } v4siindexer;
#define v4si_index(_iindx, _iindi) \
({ \
v4siindexer _iindvx = { _iindx } ; \
_iindvx.array[_iindi]; \
})
typedef union { v4sf f; v4si i; } v4sfv4sipun;
#define v4sf_fabs(x) \
({ \
v4sfv4sipun vx; \
vx.f = x; \
vx.i &= v4sil (0x7FFFFFFF); \
vx.f; \
})
#ifdef __cplusplus
} // end namespace
#endif // __cplusplus
#endif // __SSE2__
#endif // __SSE_H_
/*=====================================================================*
* Copyright (C) 2011 Paul Mineiro *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with *
* or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the *
* above copyright notice, this list of conditions and *
* the following disclaimer. *
* *
* * 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. *
* *
* * Neither the name of Paul Mineiro nor the names *
* of other 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 OWNER *
* OR CONTRIBUTORS 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. *
* *
* Contact: Paul Mineiro <[email protected]> *
*=====================================================================*/
#ifndef __FAST_EXP_H_
#define __FAST_EXP_H_
#include <stdint.h>
// Underflow of exponential is common practice in numerical routines,
// so handle it here.
static inline float
fastpow2 (float p)
{
float offset = (p < 0) ? 1.0f : 0.0f;
float clipp = (p < -126) ? -126.0f : p;
int w = clipp;
float z = clipp - w + offset;
union { uint32_t i; float f; } v = { cast_uint32_t ( (1 << 23) * (clipp + 121.2740575f + 27.7280233f / (4.84252568f - z) - 1.49012907f * z) ) };
return v.f;
}
static inline float
fastexp (float p)
{
return fastpow2 (1.442695040f * p);
}
static inline float
fasterpow2 (float p)
{
float clipp = (p < -126) ? -126.0f : p;
union { uint32_t i; float f; } v = { cast_uint32_t ( (1 << 23) * (clipp + 126.94269504f) ) };
return v.f;
}
static inline float
fasterexp (float p)
{
return fasterpow2 (1.442695040f * p);
}
#ifdef __SSE2__
static inline v4sf
vfastpow2 (const v4sf p)
{
v4sf ltzero = _mm_cmplt_ps (p, v4sfl (0.0f));
v4sf offset = _mm_and_ps (ltzero, v4sfl (1.0f));
v4sf lt126 = _mm_cmplt_ps (p, v4sfl (-126.0f));
v4sf clipp = _mm_or_ps (_mm_andnot_ps (lt126, p), _mm_and_ps (lt126, v4sfl (-126.0f)));
v4si w = v4sf_to_v4si (clipp);
v4sf z = clipp - v4si_to_v4sf (w) + offset;
const v4sf c_121_2740838 = v4sfl (121.2740575f);
const v4sf c_27_7280233 = v4sfl (27.7280233f);
const v4sf c_4_84252568 = v4sfl (4.84252568f);
const v4sf c_1_49012907 = v4sfl (1.49012907f);
union { v4si i; v4sf f; } v = {
v4sf_to_v4si (
v4sfl (1 << 23) *
(clipp + c_121_2740838 + c_27_7280233 / (c_4_84252568 - z) - c_1_49012907 * z)
)
};
return v.f;
}
static inline v4sf
vfastexp (const v4sf p)
{
const v4sf c_invlog_2 = v4sfl (1.442695040f);
return vfastpow2 (c_invlog_2 * p);
}
static inline v4sf
vfasterpow2 (const v4sf p)
{
const v4sf c_126_94269504 = v4sfl (126.94269504f);
v4sf lt126 = _mm_cmplt_ps (p, v4sfl (-126.0f));
v4sf clipp = _mm_or_ps (_mm_andnot_ps (lt126, p), _mm_and_ps (lt126, v4sfl (-126.0f)));
union { v4si i; v4sf f; } v = { v4sf_to_v4si (v4sfl (1 << 23) * (clipp + c_126_94269504)) };
return v.f;
}
static inline v4sf
vfasterexp (const v4sf p)
{
const v4sf c_invlog_2 = v4sfl (1.442695040f);
return vfasterpow2 (c_invlog_2 * p);
}
#endif //__SSE2__
#endif // __FAST_EXP_H_
/*=====================================================================*
* Copyright (C) 2011 Paul Mineiro *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with *
* or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the *
* above copyright notice, this list of conditions and *
* the following disclaimer. *
* *
* * 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. *
* *
* * Neither the name of Paul Mineiro nor the names *
* of other 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 OWNER *
* OR CONTRIBUTORS 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. *
* *
* Contact: Paul Mineiro <[email protected]> *
*=====================================================================*/
#ifndef __FAST_LOG_H_
#define __FAST_LOG_H_
#include <stdint.h>
static inline float
fastlog2 (float x)
{
union { float f; uint32_t i; } vx = { x };
union { uint32_t i; float f; } mx = { (vx.i & 0x007FFFFF) | 0x3f000000 };
float y = vx.i;
y *= 1.1920928955078125e-7f;
return y - 124.22551499f
- 1.498030302f * mx.f
- 1.72587999f / (0.3520887068f + mx.f);
}
static inline float
fastlog (float x)
{
return 0.69314718f * fastlog2 (x);
}
static inline float
fasterlog2 (float x)
{
union { float f; uint32_t i; } vx = { x };
float y = vx.i;
y *= 1.1920928955078125e-7f;
return y - 126.94269504f;
}
static inline float
fasterlog (float x)
{
// return 0.69314718f * fasterlog2 (x);
union { float f; uint32_t i; } vx = { x };
float y = vx.i;
y *= 8.2629582881927490e-8f;
return y - 87.989971088f;
}
#ifdef __SSE2__
static inline v4sf
vfastlog2 (v4sf x)
{
union { v4sf f; v4si i; } vx = { x };
union { v4si i; v4sf f; } mx; mx.i = (vx.i & v4sil (0x007FFFFF)) | v4sil (0x3f000000);
v4sf y = v4si_to_v4sf (vx.i);
y *= v4sfl (1.1920928955078125e-7f);
const v4sf c_124_22551499 = v4sfl (124.22551499f);
const v4sf c_1_498030302 = v4sfl (1.498030302f);
const v4sf c_1_725877999 = v4sfl (1.72587999f);
const v4sf c_0_3520087068 = v4sfl (0.3520887068f);
return y - c_124_22551499
- c_1_498030302 * mx.f
- c_1_725877999 / (c_0_3520087068 + mx.f);
}
static inline v4sf
vfastlog (v4sf x)
{
const v4sf c_0_69314718 = v4sfl (0.69314718f);
return c_0_69314718 * vfastlog2 (x);
}
static inline v4sf
vfasterlog2 (v4sf x)
{
union { v4sf f; v4si i; } vx = { x };
v4sf y = v4si_to_v4sf (vx.i);
y *= v4sfl (1.1920928955078125e-7f);
const v4sf c_126_94269504 = v4sfl (126.94269504f);
return y - c_126_94269504;
}
static inline v4sf
vfasterlog (v4sf x)
{
// const v4sf c_0_69314718 = v4sfl (0.69314718f);
//
// return c_0_69314718 * vfasterlog2 (x);
union { v4sf f; v4si i; } vx = { x };
v4sf y = v4si_to_v4sf (vx.i);
y *= v4sfl (8.2629582881927490e-8f);
const v4sf c_87_989971088 = v4sfl (87.989971088f);
return y - c_87_989971088;
}
#endif // __SSE2__
#endif // __FAST_LOG_H_
/*=====================================================================*
* Copyright (C) 2011 Paul Mineiro *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with *
* or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the *
* above copyright notice, this list of conditions and *
* the following disclaimer. *
* *
* * 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. *
* *
* * Neither the name of Paul Mineiro nor the names *
* of other 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 OWNER *
* OR CONTRIBUTORS 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. *
* *
* Contact: Paul Mineiro <[email protected]> *
*=====================================================================*/
#ifndef __FAST_ERF_H_
#define __FAST_ERF_H_
#include <math.h>
#include <stdint.h>
// fasterfc: not actually faster than erfcf(3) on newer machines!
// ... although vectorized version is interesting
// and fastererfc is very fast
static inline float
fasterfc (float x)
{
static const float k = 3.3509633149424609f;
static const float a = 0.07219054755431126f;
static const float b = 15.418191568719577f;
static const float c = 5.609846028328545f;
union { float f; uint32_t i; } vc = { c * x };
float xsq = x * x;
float xquad = xsq * xsq;
vc.i |= 0x80000000;
return 2.0f / (1.0f + fastpow2 (k * x)) - a * x * (b * xquad - 1.0f) * fasterpow2 (vc.f);
}
static inline float
fastererfc (float x)
{
static const float k = 3.3509633149424609f;
return 2.0f / (1.0f + fasterpow2 (k * x));
}
// fasterf: not actually faster than erff(3) on newer machines!
// ... although vectorized version is interesting
// and fastererf is very fast
static inline float
fasterf (float x)
{
return 1.0f - fasterfc (x);
}
static inline float
fastererf (float x)
{
return 1.0f - fastererfc (x);
}
static inline float
fastinverseerf (float x)
{
static const float invk = 0.30004578719350504f;
static const float a = 0.020287853348211326f;
static const float b = 0.07236892874789555f;
static const float c = 0.9913030456864257f;
static const float d = 0.8059775923760193f;
float xsq = x * x;
return invk * fastlog2 ((1.0f + x) / (1.0f - x))
+ x * (a - b * xsq) / (c - d * xsq);
}
static inline float
fasterinverseerf (float x)
{
static const float invk = 0.30004578719350504f;
return invk * fasterlog2 ((1.0f + x) / (1.0f - x));
}
#ifdef __SSE2__
static inline v4sf
vfasterfc (v4sf x)
{
const v4sf k = v4sfl (3.3509633149424609f);
const v4sf a = v4sfl (0.07219054755431126f);
const v4sf b = v4sfl (15.418191568719577f);
const v4sf c = v4sfl (5.609846028328545f);
union { v4sf f; v4si i; } vc; vc.f = c * x;
vc.i |= v4sil (0x80000000);
v4sf xsq = x * x;
v4sf xquad = xsq * xsq;
return v4sfl (2.0f) / (v4sfl (1.0f) + vfastpow2 (k * x)) - a * x * (b * xquad - v4sfl (1.0f)) * vfasterpow2 (vc.f);
}
static inline v4sf
vfastererfc (const v4sf x)
{
const v4sf k = v4sfl (3.3509633149424609f);
return v4sfl (2.0f) / (v4sfl (1.0f) + vfasterpow2 (k * x));
}
static inline v4sf
vfasterf (v4sf x)
{
return v4sfl (1.0f) - vfasterfc (x);
}
static inline v4sf
vfastererf (const v4sf x)
{
return v4sfl (1.0f) - vfastererfc (x);
}
static inline v4sf
vfastinverseerf (v4sf x)
{
const v4sf invk = v4sfl (0.30004578719350504f);
const v4sf a = v4sfl (0.020287853348211326f);
const v4sf b = v4sfl (0.07236892874789555f);
const v4sf c = v4sfl (0.9913030456864257f);
const v4sf d = v4sfl (0.8059775923760193f);
v4sf xsq = x * x;
return invk * vfastlog2 ((v4sfl (1.0f) + x) / (v4sfl (1.0f) - x))
+ x * (a - b * xsq) / (c - d * xsq);
}
static inline v4sf
vfasterinverseerf (v4sf x)
{
const v4sf invk = v4sfl (0.30004578719350504f);
return invk * vfasterlog2 ((v4sfl (1.0f) + x) / (v4sfl (1.0f) - x));
}
#endif //__SSE2__
#endif // __FAST_ERF_H_
/*=====================================================================*
* Copyright (C) 2011 Paul Mineiro *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with *
* or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the *
* above copyright notice, this list of conditions and *
* the following disclaimer. *
* *
* * 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. *
* *
* * Neither the name of Paul Mineiro nor the names *
* of other 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 OWNER *
* OR CONTRIBUTORS 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. *
* *
* Contact: Paul Mineiro <[email protected]> *
*=====================================================================*/
#ifndef __FAST_GAMMA_H_
#define __FAST_GAMMA_H_
#include <stdint.h>
/* gamma/digamma functions only work for positive inputs */
static inline float
fastlgamma (float x)
{
float logterm = fastlog (x * (1.0f + x) * (2.0f + x));
float xp3 = 3.0f + x;
return - 2.081061466f
- x
+ 0.0833333f / xp3
- logterm
+ (2.5f + x) * fastlog (xp3);
}
static inline float
fasterlgamma (float x)
{
return - 0.0810614667f
- x
- fasterlog (x)
+ (0.5f + x) * fasterlog (1.0f + x);
}
static inline float
fastdigamma (float x)
{
float twopx = 2.0f + x;
float logterm = fastlog (twopx);
return (-48.0f + x * (-157.0f + x * (-127.0f - 30.0f * x))) /
(12.0f * x * (1.0f + x) * twopx * twopx)
+ logterm;
}
static inline float
fasterdigamma (float x)
{
float onepx = 1.0f + x;
return -1.0f / x - 1.0f / (2 * onepx) + fasterlog (onepx);
}
#ifdef __SSE2__
static inline v4sf
vfastlgamma (v4sf x)
{
const v4sf c_1_0 = v4sfl (1.0f);
const v4sf c_2_0 = v4sfl (2.0f);
const v4sf c_3_0 = v4sfl (3.0f);
const v4sf c_2_081061466 = v4sfl (2.081061466f);
const v4sf c_0_0833333 = v4sfl (0.0833333f);
const v4sf c_2_5 = v4sfl (2.5f);
v4sf logterm = vfastlog (x * (c_1_0 + x) * (c_2_0 + x));
v4sf xp3 = c_3_0 + x;
return - c_2_081061466
- x
+ c_0_0833333 / xp3
- logterm
+ (c_2_5 + x) * vfastlog (xp3);
}
static inline v4sf
vfasterlgamma (v4sf x)
{
const v4sf c_0_0810614667 = v4sfl (0.0810614667f);
const v4sf c_0_5 = v4sfl (0.5f);
const v4sf c_1 = v4sfl (1.0f);
return - c_0_0810614667
- x
- vfasterlog (x)
+ (c_0_5 + x) * vfasterlog (c_1 + x);
}
static inline v4sf
vfastdigamma (v4sf x)
{
v4sf twopx = v4sfl (2.0f) + x;
v4sf logterm = vfastlog (twopx);
return (v4sfl (-48.0f) + x * (v4sfl (-157.0f) + x * (v4sfl (-127.0f) - v4sfl (30.0f) * x))) /
(v4sfl (12.0f) * x * (v4sfl (1.0f) + x) * twopx * twopx)
+ logterm;
}
static inline v4sf
vfasterdigamma (v4sf x)
{
const v4sf c_1_0 = v4sfl (1.0f);
const v4sf c_2_0 = v4sfl (2.0f);
v4sf onepx = c_1_0 + x;
return -c_1_0 / x - c_1_0 / (c_2_0 * onepx) + vfasterlog (onepx);
}
#endif //__SSE2__
#endif // __FAST_GAMMA_H_
/*=====================================================================*
* Copyright (C) 2011 Paul Mineiro *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with *
* or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the *
* above copyright notice, this list of conditions and *
* the following disclaimer. *
* *
* * 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. *
* *
* * Neither the name of Paul Mineiro nor the names *
* of other 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 OWNER *
* OR CONTRIBUTORS 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. *
* *
* Contact: Paul Mineiro <[email protected]> *
*=====================================================================*/
#ifndef __FAST_HYPERBOLIC_H_
#define __FAST_HYPERBOLIC_H_
#include <stdint.h>
static inline float
fastsinh (float p)
{
return 0.5f * (fastexp (p) - fastexp (-p));
}
static inline float
fastersinh (float p)
{
return 0.5f * (fasterexp (p) - fasterexp (-p));
}
static inline float
fastcosh (float p)
{
return 0.5f * (fastexp (p) + fastexp (-p));
}
static inline float
fastercosh (float p)
{
return 0.5f * (fasterexp (p) + fasterexp (-p));
}
static inline float
fasttanh (float p)
{
return -1.0f + 2.0f / (1.0f + fastexp (-2.0f * p));
}
static inline float
fastertanh (float p)
{
return -1.0f + 2.0f / (1.0f + fasterexp (-2.0f * p));
}
#ifdef __SSE2__
static inline v4sf
vfastsinh (const v4sf p)
{
const v4sf c_0_5 = v4sfl (0.5f);
return c_0_5 * (vfastexp (p) - vfastexp (-p));
}
static inline v4sf
vfastersinh (const v4sf p)
{
const v4sf c_0_5 = v4sfl (0.5f);
return c_0_5 * (vfasterexp (p) - vfasterexp (-p));
}
static inline v4sf
vfastcosh (const v4sf p)
{
const v4sf c_0_5 = v4sfl (0.5f);
return c_0_5 * (vfastexp (p) + vfastexp (-p));
}
static inline v4sf
vfastercosh (const v4sf p)
{
const v4sf c_0_5 = v4sfl (0.5f);
return c_0_5 * (vfasterexp (p) + vfasterexp (-p));
}
static inline v4sf
vfasttanh (const v4sf p)
{
const v4sf c_1 = v4sfl (1.0f);
const v4sf c_2 = v4sfl (2.0f);
return -c_1 + c_2 / (c_1 + vfastexp (-c_2 * p));
}
static inline v4sf
vfastertanh (const v4sf p)
{
const v4sf c_1 = v4sfl (1.0f);
const v4sf c_2 = v4sfl (2.0f);
return -c_1 + c_2 / (c_1 + vfasterexp (-c_2 * p));
}
#endif //__SSE2__
#endif // __FAST_HYPERBOLIC_H_
/*=====================================================================*
* Copyright (C) 2011 Paul Mineiro *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with *
* or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the *
* above copyright notice, this list of conditions and *
* the following disclaimer. *
* *
* * 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. *
* *
* * Neither the name of Paul Mineiro nor the names *
* of other 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 OWNER *
* OR CONTRIBUTORS 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. *
* *
* Contact: Paul Mineiro <[email protected]> *
*=====================================================================*/
#ifndef __FAST_LAMBERT_W_H_
#define __FAST_LAMBERT_W_H_
#include <stdint.h>
// these functions compute the upper branch aka W_0
static inline float
fastlambertw (float x)
{
static const float threshold = 2.26445f;
float c = (x < threshold) ? 1.546865557f : 1.0f;
float d = (x < threshold) ? 2.250366841f : 0.0f;
float a = (x < threshold) ? -0.737769969f : 0.0f;
float logterm = fastlog (c * x + d);
float loglogterm = fastlog (logterm);
float minusw = -a - logterm + loglogterm - loglogterm / logterm;
float expminusw = fastexp (minusw);
float xexpminusw = x * expminusw;
float pexpminusw = xexpminusw - minusw;
return (2.0f * xexpminusw - minusw * (4.0f * xexpminusw - minusw * pexpminusw)) /
(2.0f + pexpminusw * (2.0f - minusw));
}
static inline float
fasterlambertw (float x)
{
static const float threshold = 2.26445f;
float c = (x < threshold) ? 1.546865557f : 1.0f;
float d = (x < threshold) ? 2.250366841f : 0.0f;
float a = (x < threshold) ? -0.737769969f : 0.0f;
float logterm = fasterlog (c * x + d);
float loglogterm = fasterlog (logterm);
float w = a + logterm - loglogterm + loglogterm / logterm;
float expw = fasterexp (-w);
return (w * w + expw * x) / (1.0f + w);
}
static inline float
fastlambertwexpx (float x)
{
static const float k = 1.1765631309f;
static const float a = 0.94537622168f;
float logarg = fmaxf (x, k);
float powarg = (x < k) ? a * (x - k) : 0;
float logterm = fastlog (logarg);
float powterm = fasterpow2 (powarg); // don't need accuracy here
float w = powterm * (logarg - logterm + logterm / logarg);
float logw = fastlog (w);
float p = x - logw;
return w * (2.0f + p + w * (3.0f + 2.0f * p)) /
(2.0f - p + w * (5.0f + 2.0f * w));
}
static inline float
fasterlambertwexpx (float x)
{
static const float k = 1.1765631309f;
static const float a = 0.94537622168f;
float logarg = fmaxf (x, k);
float powarg = (x < k) ? a * (x - k) : 0;
float logterm = fasterlog (logarg);
float powterm = fasterpow2 (powarg);
float w = powterm * (logarg - logterm + logterm / logarg);
float logw = fasterlog (w);