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
|
/*
* Revision v1.24
*
* Credits
*
* Maintained, conceived, written, and fiddled with by:
*
* Andrew E. Mileski <andrewm@isoar.ca>
*
* Other source code contributors:
*
* Kevin Rockel
* Kevin Michael Woley
* Mark Anthony Lisher
* Nicolas Pitre
* Anonymous
*
* License
*
* Licensed under the terms of the MIT license:
*
* Copyright (c) 2000,2002,2006,2012,2016 Andrew E. Mileski <andrewm@isoar.ca>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The copyright notice, and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/* See header for full details */
#include "math-sll.h"
/*
* Local prototypes
*/
static sll _sllcos(sll x);
static sll _sllsin(sll x);
static sll _sllexp(sll x);
/*
* Unpack IEEE 754 floating point double format into fixed point sll format
*
* Description
*
* IEEE 754 specifies the binary64 type ("double" in C) as having:
*
* 1 bit sign
* 11 bit exponent
* 53 bit significand
*
* The first bit of the significand is an implied 1 which is not stored.
* The decimal would be to the right of that implied 1, or to the left of
* the stored significand.
*
* The exponent is unsigned, and biased with an offset of 1023.
*
* The IEEE 754 standard does not specify endianess, but the endian used is
* traditionally the same endian that the processor uses.
*/
sll dbl2sll(double dbl)
{
union {
double d;
unsigned u[2];
ull ull;
sll sll;
} in, retval;
register unsigned exp;
/* Move into memory as args might be passed in regs */
in.d = dbl;
#if defined(BROKEN_IEEE754_DOUBLE)
exp = in.u[0];
in.u[0] = in.u[1];
in.u[1] = exp;
#endif /* defined(BROKEN_IEEE754_DOUBLE) */
/* Leading 1 is assumed by IEEE */
retval.u[1] = 0x40000000;
/* Unpack the mantissa into the unsigned long */
retval.u[1] |= (in.u[1] << 10) & 0x3ffffc00;
retval.u[1] |= (in.u[0] >> 22) & 0x000003ff;
retval.u[0] = in.u[0] << 10;
/* Extract the exponent and align the decimals */
exp = (in.u[1] >> 20) & 0x7ff;
if (exp)
/* IEEE 754 decimal begins at right of bit position 30 */
retval.ull >>= (1023 + 30) - exp;
else
return 0L;
/* Negate if negative flag set */
if (in.u[1] & 0x80000000)
retval.sll = _sllneg(retval.sll);
return retval.sll;
}
/*
* Pack fixed point sll format into IEEE 754 floating point double format
*
* Description
*
* IEEE 754 specifies the binary64 type ("double" in C) as having:
*
* 1 bit sign
* 11 bit exponent
* 53 bit significand
*
* The first bit of the significand is an implied 1 which is not stored.
* The decimal would be to the right of that implied 1, or to the left of
* the stored significand.
*
* The exponent is unsigned, and biased with an offset of 1023.
*
* The IEEE 754 standard does not specify endianess, but the endian used is
* traditionally the same endian that the processor uses.
*/
double sll2dbl(sll s)
{
union {
double d;
unsigned u[2];
ull ull;
sll sll;
} in, retval;
register unsigned exp;
register unsigned flag;
if (s == 0)
return 0.0;
/* Move into memory as args might be passed in regs */
in.sll = s;
/* Handle the negative flag */
if (in.sll < 1) {
flag = 0x80000000;
in.ull = _sllneg(in.sll);
} else
flag = 0x00000000;
/*
* Normalize
*
* IEEE 754 decimal-point begins at right of bit position 30
*/
for (exp = (1023 + 30); in.ull && (in.u[1] & 0x80000000) == 0; exp--) {
in.ull <<= 1;
}
in.ull <<= 1;
exp++;
in.ull >>= 12;
retval.ull = in.ull;
retval.u[1] |= flag | (exp << 20);
#if defined(BROKEN_IEEE754_DOUBLE)
exp = retval.u[0];
retval.u[0] = retval.u[1];
retval.u[1] = exp;
#endif /* defined(BROKEN_IEEE754_DOUBLE) */
return retval.d;
}
/*
* Multiply two sll values
*
* Description
*
* When multiplying two 64 bit sll numbers, the result is 128 bits, but there
* is only room for a 64 bit result with sll!
*
* The 128 bit result has 64 bits on either side of the decimal, so 32 bits
* of overflow to the left of the decimal, and 32 bits of underflow to the
* right of the decmial.
*
* 32.32 * 32.32 = 64.64 = overflow(32) + 32.32 + underflow(32)
*
* However, a "long long" multiply has 64 bits of overflow to the left of the
* decimal, resulting in the entire integer part being lost!
*
* 32.32 * 32.32 = 64.64 = overflow(64) + .64
*
* Hence a custom multiply routine is required, to preserve the parts
* of the result that sll needs.
*
* Consider two sll numbers, x and y:
*
* Let x = x_hi * 2^0 + x_lo * 2^(-32)
* Let y = y_hi * 2^0 + y_lo * 2^(-32)
*
* Where:
*
* *_hi is the signed 32 bit integer part to the left of the decimal
* *_lo is the unsigned 32 bit fractional part to the right of the decimal
*
* x * y = (x_hi * 2^0 + x_lo * 2^(-32))
* * (y_hi * 2^0 + y_lo * 2^(-32))
*
* Expanding the terms, we get:
*
* = x_hi * y_hi * 2^0 + x_hi * y_lo * 2^(-32)
* + x_lo * y_hi * 2^(-32) + x_lo * y_lo * 2^(-64)
*
* Grouping by powers of 2, we get:
*
* (x_hi * y_hi) * 2^0
* We only need the low 32 bits of this term, as the rest is overflow
*
* (x_hi * y_lo + x_lo * y_hi) * 2^-32
* We need all bits of this term
*
* x_lo * y_lo * 2^-64
* We only need the high 32 bits of this term, as the rest is underflow
*/
sll sllmul(sll x, sll y)
{
register unsigned int x_lo;
register signed int x_hi;
register unsigned int y_lo;
register signed int y_hi;
x_hi = (signed int) ((ull) x >> 32); // Discard lower 32 bits
x_lo = (unsigned int) x; // Discard upper 32 bits
y_hi = (signed int) ((ull) y >> 32); // Discard lower 32 bits
y_lo = (unsigned int) y; // Discard upper 32 bits
return (sll) (
((ull) (x_hi * y_hi) << 32)
+ ((ull) x_hi * y_lo + x_lo * (ull) y_hi)
+ (((ull) x_lo * y_lo) >> 32)
);
}
/*
* Calculate cos x where -pi/4 <= x <= pi/4
*
* Description
*
* cos x = 1 - x^2 / 2! + x^4 / 4! - ... + x^(2N) / (2N)!
* Note that (pi/4)^12 / 12! < 2^-32 which is the smallest possible number.
*
* cos x = t0 + t1 + t2 + t3 + t4 + t5 + t6
*
* Consider only the factorials:
* f0 = 0! = 1
* f1 = 2! = 2 * 1 * f0 = 2 * f0
* f2 = 4! = 4 * 3 * f1 = 12 * f1
* f3 = 6! = 6 * 5 * f2 = 30 * f2
* f4 = 8! = 8 * 7 * f3 = 56 * f3
* f5 = 10! = 10 * 9 * f4 = 90 * f4
* f6 = 12! = 12 * 11 * f5 = 132 * f5
*
* Now consider each term of the series:
* t0 = 1
* t1 = -t0 * x^2 / f1 = -t0 * x^2 * CONST_1_2
* t2 = -t1 * x^2 / f2 = -t1 * x^2 * CONST_1_12
* t3 = -t2 * x^2 / f3 = -t2 * x^2 * CONST_1_30
* t4 = -t3 * x^2 / f4 = -t3 * x^2 * CONST_1_56
* t5 = -t4 * x^2 / f5 = -t4 * x^2 * CONST_1_90
* t6 = -t5 * x^2 / f6 = -t5 * x^2 * CONST_1_132
*/
sll _sllcos(sll x)
{
sll retval;
sll x2;
x2 = sllmul(x, x);
retval = _sllsub(CONST_1, sllmul(x2, CONST_1_132));
retval = _sllsub(CONST_1, sllmul(sllmul(x2, retval), CONST_1_90));
retval = _sllsub(CONST_1, sllmul(sllmul(x2, retval), CONST_1_56));
retval = _sllsub(CONST_1, sllmul(sllmul(x2, retval), CONST_1_30));
retval = _sllsub(CONST_1, sllmul(sllmul(x2, retval), CONST_1_12));
retval = _sllsub(CONST_1, slldiv2(sllmul(x2, retval)));
return retval;
}
/*
* Calculate sin x where -pi/4 <= x <= pi/4
*
* Description
*
* sin x = x - x^3 / 3! + x^5 / 5! - ... + x^(2N+1) / (2N+1)!
* Note that (pi/4)^13 / 13! < 2^-32 which is the smallest possible number.
*
* sin x = t0 + t1 + t2 + t3 + t4 + t5 + t6
*
* Consider only the factorials:
* f0 = 0! = 1
* f1 = 3! = 3 * 2 * f0 = 6 * f0
* f2 = 5! = 5 * 4 * f1 = 20 * f1
* f3 = 7! = 7 * 6 * f2 = 42 * f2
* f4 = 9! = 9 * 8 * f3 = 72 * f3
* f5 = 11! = 11 * 10 * f4 = 110 * f4
* f6 = 13! = 13 * 12 * f5 = 156 * f5
*
* Now consider each term of the series:
* t0 = 1
* t1 = -t0 * x^2 / 6 = -t0 * x^2 * CONST_1_6
* t2 = -t1 * x^2 / 20 = -t1 * x^2 * CONST_1_20
* t3 = -t2 * x^2 / 42 = -t2 * x^2 * CONST_1_42
* t4 = -t3 * x^2 / 72 = -t3 * x^2 * CONST_1_72
* t5 = -t4 * x^2 / 110 = -t4 * x^2 * CONST_1_110
* t6 = -t5 * x^2 / 156 = -t5 * x^2 * CONST_1_156
*/
sll _sllsin(sll x)
{
sll retval;
sll x2;
x2 = sllmul(x, x);
retval = _sllsub(x, sllmul(x2, CONST_1_156));
retval = _sllsub(x, sllmul(sllmul(x2, retval), CONST_1_110));
retval = _sllsub(x, sllmul(sllmul(x2, retval), CONST_1_72));
retval = _sllsub(x, sllmul(sllmul(x2, retval), CONST_1_42));
retval = _sllsub(x, sllmul(sllmul(x2, retval), CONST_1_20));
retval = _sllsub(x, sllmul(sllmul(x2, retval), CONST_1_6));
return retval;
}
/*
* Calculate cos x for any value of x, by quadrant
*/
sll sllcos(sll x)
{
int i;
sll retval;
/* Calculate cos (x - i * pi/2), where -pi/4 <= x - i * pi/2 <= pi/4 */
i = _sll2int(_slladd(sllmul(x, CONST_2_PI), CONST_1_2));
x = _sllsub(x, sllmul(_int2sll(i), CONST_PI_2));
/* Locate the quadrant */
switch (i & 3) {
default:
case 0:
retval = _sllcos(x);
break;
case 1:
retval = sllneg(_sllsin(x));
break;
case 2:
retval = sllneg(_sllcos(x));
break;
case 3:
retval = _sllsin(x);
break;
}
return retval;
}
/*
* Calculate sin x for any value of x, by quadrant
*/
sll sllsin(sll x)
{
int i;
sll retval;
/* Calculate sin (x - n * pi/2), where -pi/4 <= x - i * pi/2 <= pi/4 */
i = _sll2int(_slladd(sllmul(x, CONST_2_PI), CONST_1_2));
x = _sllsub(x, sllmul(_int2sll(i), CONST_PI_2));
/* Locate the quadrant */
switch (i & 3) {
default:
case 0:
retval = _sllsin(x);
break;
case 1:
retval = _sllcos(x);
break;
case 2:
retval = sllneg(_sllsin(x));
break;
case 3:
retval = sllneg(_sllcos(x));
break;
}
return retval;
}
/*
* Calculate tan x for any value of x, by quadrant
*/
sll slltan(sll x)
{
int i;
sll retval;
i = _sll2int(_slladd(sllmul(x, CONST_2_PI), CONST_1_2));
x = _sllsub(x, sllmul(_int2sll(i), CONST_PI_2));
/* Locate the quadrant */
switch (i & 3) {
default:
case 0:
case 2:
retval = slldiv(_sllsin(x), _sllcos(x));
break;
case 1:
case 3:
retval = _sllneg(slldiv(_sllcos(x), _sllsin(x)));
break;
}
return retval;
}
/*
*
* Calculate asin x, where |x| <= 1
*
* Description
*
* asin x = SUM[n=0,) C(2 * n, n) * x^(2 * n + 1) / (4^n * (2 * n + 1)), |x| <= 1
*
* where C(n, r) = nCr = n! / (r! * (n - r)!)
*
* Using a two term approximation:
* [1] a = x + x^3 / 6
*
* Results in:
* asin x = a + D
* where D is the difference from the exact result
*
* Letting D = asin d results in:
* [2] asin x = a + asin d
*
* Re-arranging:
* asin x - a = asin d
*
* Applying sin to both sides:
* sin (asin x - a) = sin asin d
* sin (asin x - a) = d
* d = sin (asin x - a)
*
* Applying the standard identity:
* sin (u - v) = sin u * cos v - cos u * sin v
*
* Results in:
* d = sin asin x * cos a - cos asin x * sin a
* d = x * cos a - cos asin x * sin a
*
* Applying the standard identity:
* cos asin u = (1 - u^2)^(1 / 2)
*
* Results in:
* [3] d = x * cos a - (1 - x^2)^(1 / 2) * sin a
*
* Putting the pieces together:
* [1] a = x + x^3 / 6
* [3] d = x * cos a - (1 - x^2)^(1 / 2) * sin a
* [2] asin x = a + asin d
*
* The worst case is x = 1.0 which converges after 2 iterations.
*/
sll sllasin(sll x)
{
int left_side;
sll a;
sll retval;
/* asin -x = -asin x */
if ((left_side = x < 0))
x = _sllneg(x);
/* Out-of-range */
if (x > CONST_1)
return 0;
/* Initial approximate */
a = sllmul(x, _slladd(CONST_1, sllmul(x, sllmul(x, CONST_1_6))));
retval = a;
/* First iteration */
x = _sllsub(sllmul(x, sllcos(a)), sllmul(sllsqrt(_sllsub(CONST_1, sllmul(x, x))), sllsin(a)));
a = sllmul(x, _slladd(CONST_1, sllmul(x, sllmul(x, CONST_1_6))));
retval = _slladd(retval, a);
/* Second iteration */
x = _sllsub(sllmul(x, sllcos(a)), sllmul(sllsqrt(_sllsub(CONST_1, sllmul(x, x))), sllsin(a)));
a = sllmul(x, _slladd(CONST_1, sllmul(x, sllmul(x, CONST_1_6))));
retval = _slladd(retval, a);
/* Negate result if necessary */
return (left_side ? _sllneg(retval): retval);
}
/*
* Calculate atan x
*
* Description
*
* atan x = SUM[n=0,) (-1)^n * x^(2 * n + 1) / (2 * n + 1), |x| <= 1
*
* Using a two term approximation:
* [1] a = x - x^3 / 3
*
* Results in:
* atan x = a + D
* where D is the difference from the exact result
*
* Letting D = atan d results in:
* [2] atan x = a + atan d
*
* Re-arranging:
* atan x - a = atan d
*
* Applying tan to both sides:
* tan (atan x - a) = tan atan d
* tan (atan x - a) = d
* d = tan (atan x - a)
*
* Applying the standard identity:
* tan (u - v) = (tan u - tan v) / (1 + tan u * tan v)
*
* Results in:
* d = tan (atan x - a) = (tan atan x - tan a) / (1 + tan atan x * tan a)
* d = tan (atan x - a) = (x - tan a) / (1 + x * tan a)
*
* Let:
* [3] t = tan a
*
* Results in:
* [4] d = (x - t) / (1 + x * t)
*
* So putting the pieces together:
* [1] a = x - x^3 / 3
* [3] t = tan a
* [4] d = (x - t) / (1 + x * t)
* [2] atan x = a + atan d
* atan x = a + atan ((x - t) / (1 + x * t))
*
* The worst case is x = 1.0 which converges after 2 iterations.
*/
sll sllatan(sll x)
{
int side;
sll a;
sll t;
sll retval;
if (x < CONST_1) {
/* Left: if (x < -1) then atan x = pi / 2 + atan 1 / x */
side = -1;
x = sllinv(x);
} else if (x > CONST_1) {
/* Right: if (x > 1) then atan x = pi / 2 - atan 1 / x */
side = 1;
x = sllinv(x);
} else {
/* Middle: -1 <= x <= 1 */
side = 0;
}
/* Initial approximate */
a = sllmul(x, _sllsub(CONST_1, sllmul(x, sllmul(x, CONST_1_3))));
retval = a;
/* First iteration */
t = _slldiv(_sllsin(a), _sllcos(a));
x = _slldiv(_sllsub(x, t), _slladd(CONST_1, sllmul(x, t)));
a = sllmul(x, _sllsub(CONST_1, sllmul(x, sllmul(x, CONST_1_3))));
retval = _slladd(retval, a);
/* Second iteration */
t = _slldiv(_sllsin(a), _sllcos(a));
x = _slldiv(_sllsub(x, t), _slladd(CONST_1, sllmul(x, t)));
a = sllmul(x, _sllsub(CONST_1, sllmul(x, sllmul(x, CONST_1_3))));
retval = _slladd(retval, a);
if (side == -1) {
/* Left: if (x < -1) then atan x = pi / 2 + atan 1 / x */
retval = _slladd(CONST_PI_2, retval);
} else if (side == 1) {
/* Right: if (x > 1) then atan x = pi / 2 - atan 1 / x */
retval = _sllsub(CONST_PI_2, retval);
}
return retval;
}
/*
* Calculate e^x where -0.5 <= x <= 0.5
*
* Description:
* e^x = x^0 / 0! + x^1 / 1! + ... + x^N / N!
* Note that 0.5^11 / 11! < 2^-32 which is the smallest possible number.
*/
sll _sllexp(sll x)
{
sll retval;
retval = CONST_1;
retval = _slladd(CONST_1, sllmul(retval, sllmul(x, CONST_1_11)));
retval = _slladd(CONST_1, sllmul(retval, sllmul(x, CONST_1_10)));
retval = _slladd(CONST_1, sllmul(retval, sllmul(x, CONST_1_9)));
retval = _slladd(CONST_1, sllmul(retval, slldiv2n(x, 3)));
retval = _slladd(CONST_1, sllmul(retval, sllmul(x, CONST_1_7)));
retval = _slladd(CONST_1, sllmul(retval, sllmul(x, CONST_1_6)));
retval = _slladd(CONST_1, sllmul(retval, sllmul(x, CONST_1_5)));
retval = _slladd(CONST_1, sllmul(retval, slldiv4(x)));
retval = _slladd(CONST_1, sllmul(retval, sllmul(x, CONST_1_3)));
retval = _slladd(CONST_1, sllmul(retval, slldiv2(x)));
retval = _slladd(CONST_1, sllmul(retval, x));
return retval;
}
/*
* Calculate e^x for any value of x
*/
sll sllexp(sll x)
{
int i;
sll e;
sll retval;
e = CONST_E;
/* -0.5 <= x <= 0.5 */
i = _sll2int(_slladd(x, CONST_1_2));
retval = _sllexp(_sllsub(x, _int2sll(i)));
/* i >= 0 */
if (i < 0) {
i = -i;
e = CONST_1_E;
}
/* Scale the result */
for (; i; i >>= 1) {
if (i & 1)
retval = sllmul(retval, e);
e = sllmul(e, e);
}
return retval;
}
/*
* Calculate natural logarithm using Netwton-Raphson method
*/
sll slllog(sll x)
{
sll x1;
sll ln;
ln = 0;
/* Scale: e^(-1/2) <= x <= e^(1/2) */
while (x < CONST_1_SQRTE) {
ln = _sllsub(ln, CONST_1);
x = sllmul(x, CONST_E);
}
while (x > CONST_SQRTE) {
ln = _slladd(ln, CONST_1);
x = sllmul(x, CONST_1_E);
}
/* First iteration */
x1 = sllmul(_sllsub(x, CONST_1), slldiv2(_sllsub(x, CONST_3)));
ln = _sllsub(ln, x1);
x = sllmul(x, _sllexp(x1));
/* Second iteration */
x1 = sllmul(_sllsub(x, CONST_1), slldiv2(_sllsub(x, CONST_3)));
ln = _sllsub(ln, x1);
x = sllmul(x, _sllexp(x1));
/* Third iteration */
x1 = sllmul(_sllsub(x, CONST_1), slldiv2(_sllsub(x, CONST_3)));
ln = _sllsub(ln, x1);
return ln;
}
/*
* Calculate the inverse for non-zero values
*/
sll sllinv(sll x)
{
int sgn;
sll u;
ull s;
/* Use positive numbers, or the approximation won't work */
if (x < CONST_0) {
x = _sllneg(x);
sgn = 1;
} else {
sgn = 0;
}
/* Starting-point (gets shifted right to become positive) */
s = -1;
/* An approximation - must be larger than the actual value */
for (u = x; u; u = ((ull) u) >> 1)
s >>= 1;
/* Newton's Method */
u = sllmul(s, _sllsub(CONST_2, sllmul(x, s)));
u = sllmul(u, _sllsub(CONST_2, sllmul(x, u)));
u = sllmul(u, _sllsub(CONST_2, sllmul(x, u)));
u = sllmul(u, _sllsub(CONST_2, sllmul(x, u)));
u = sllmul(u, _sllsub(CONST_2, sllmul(x, u)));
u = sllmul(u, _sllsub(CONST_2, sllmul(x, u)));
return ((sgn) ? _sllneg(u): u);
}
/*
* Calculate x^y
*
* Description
*
* The standard identity:
* ln x^y = y * log x
*
* Raising e to the power of either sides:
* e^(ln x^y) = e^(y * log x)
*
* Which simplifies to:
* x^y = e^(y * ln x)
*/
sll sllpow(sll x, sll y)
{
if (y == CONST_0)
return CONST_1;
if (y == CONST_1)
return x;
if (y == CONST_2)
return sllmul(x, x);
return sllexp(sllmul(y, slllog(x)));
}
/*
* Calculate the square-root
*
* Description
*
* Consider a parabola centered on the y-axis:
* y = a * x^2 + b
*
* Has zeros (y = 0) located at:
* a * x^2 + b = 0
* a * x^2 = -b
* x^2 = -b / a
* x = +- (-b / a)^(1 / 2)
*
* Letting a = 1 and b = -X results in:
* y = x^2 - X
* x = +- X^(1 / 2)
*
* Which is a convenient result, since we want to find the square root of X,
* and we can * use Newton's Method to find the zeros of any f(x):
* xn = x - f(x) / f'(x)
*
* Applying Newton's Method to our parabola:
* f(x) = x^2 - X
* xn = x - (x^2 - X) / (2 * x)
* xn = x - (x - X / x) / 2
*
* To make this converge quickly, we scale X so that:
* X = 4^N * z
*
* Taking the roots of both sides
* X^(1 / 2) = (4^n * z)^(1 / 2)
* X^(1 / 2) = 2^n * z^(1 / 2)
*
* Letting N = 2^n results in:
* x^(1 / 2) = N * z^(1 / 2)
*
* We want this to converge to the positive root, so we must start at a point
* 0 < start <= x^(1 / 2)
* or
* x^(1/2) <= start <= infinity
*
* Since:
* (1/2)^(1/2) = 0.707
* 2^(1/2) = 1.414
*
* A good choice is 1 which lies in the middle, and takes 4 iterations to
* converge from either extreme.
*/
sll sllsqrt(sll x)
{
sll n;
sll xn;
/* Quick solutions for the simple cases */
if (x <= CONST_0 || x == CONST_1)
return x;
/* Start with a scaling factor of 1 */
n = CONST_1;
/* Scale x so that 0.5 <= x < 2 */
while (x >= CONST_2) {
x = slldiv4(x);
n = sllmul2(n);
}
while (x < CONST_1_2) {
x = sllmul4(x);
n = slldiv2(n);
}
/* Simple solution if x = 4^n */
if (x == CONST_1)
return n;
/* The starting point */
xn = CONST_1;
/* Four iterations will be enough */
xn = _sllsub(xn, slldiv2(_sllsub(xn, slldiv(x, xn))));
xn = _sllsub(xn, slldiv2(_sllsub(xn, slldiv(x, xn))));
xn = _sllsub(xn, slldiv2(_sllsub(xn, slldiv(x, xn))));
xn = _sllsub(xn, slldiv2(_sllsub(xn, slldiv(x, xn))));
/* Scale the result */
return sllmul(n, xn);
}
sll slld2dsqrt(sll num)
{
if (num <= CONST_0 || num == CONST_1)
{
return num;
}
sll result = 0;
sll bit;
// Many numbers will be less than 15, so
// this gives a good balance between time spent
// in if vs. time spent in the while loop
// when searching for the starting value.
if (num & 0xFFFFFF0000000000LL)
bit = 0x4000000000000000LL; //(sll)1 << 62;
else
bit = 0x0000004000000000LL; //(sll)1 << 38;
while (bit > num) bit >>= 2;
while (bit)
{
if (num >= result + bit)
{
num -= result + bit;
result = (result >> 1) + bit;
}
else
{
result = (result >> 1);
}
bit >>= 2;
}
// Then process it again to get the lowest 8 bits.
// 尾数定点数大于等于1
if (num > CONST_P9999)
{
// The remainder 'num' is too large to be shifted left
// by 32, so we have to add 0.5 to result manually and
// adjust 'num' accordingly. a是原本的数
// why 0.5 is enough? because: result^2 <= a < (result+1)^2
// num = a - (result + 0.5)^2
// = num + result^2 - (result + 0.5)^2
// = num - result - 0.25
num -= result;
// 注意,严格来说,这里要左移16位才缩小到结果,但是为了后续小数不用再放大,这里提前做了放大
num = (num << 32) - CONST_1_4;
result = (result << 32) + CONST_1_2;
}
else
{
num <<= 32;
result <<= 32;
}
bit = 0x0000000040000000LL; //(sll)1 << 30;
while (bit)
{
if (num >= result + bit)
{
num -= result + bit;
result = (result >> 1) + bit;
}
else
{
result = (result >> 1);
}
bit >>= 2;
}
return result;
}
|