2 * Copyright (c) 2020 Mark Owen https://www.quinapalus.com .
4 * Raspberry Pi (Trading) Ltd (Licensor) hereby grants to you a non-exclusive license to use the software solely on a
5 * Raspberry Pi Pico device. No other use is permitted under the terms of this license.
7 * This software is also available from the copyright owner under GPLv2 licence.
9 * THIS SOFTWARE IS PROVIDED BY THE LICENSOR AND COPYRIGHT OWNER "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
10 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE LICENSOR OR COPYRIGHT OWNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
12 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
13 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
14 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18#include "asm_helper.S"
24.macro double_section name
25// todo separate flag for shims?
27.section RAM_SECTION_NAME(\name), "ax"
29.section SECTION_NAME(\name), "ax"
33double_section double_table_shim_on_use_helper
34regular_func double_table_shim_on_use_helper
38 // sanity check to make sure we weren't called by non (shimmable_) table_tail_call macro
49 uxtb r1, r1 // r1 holds table offset
66#if PICO_DOUBLE_SUPPORT_ROM_V1 && PICO_RP2040_B0_SUPPORTED
67// Note that the V1 ROM has no double support, so this is basically the identical
68// library, and shim inter-function calls do not bother to redirect back thru the
72.equ IOPORT ,0xd0000000
73.equ DIV_UDIVIDEND,0x00000060
74.equ DIV_UDIVISOR ,0x00000064
75.equ DIV_QUOTIENT ,0x00000070
76.equ DIV_CSR ,0x00000078
79@ rx:ry means the concatenation of rx and ry with rx having the less significant bits
104@ IEEE double in ra:rb ->
105@ mantissa in ra:rb 12Q52 (53 significant bits) with implied 1 set
109.macro mdunpack ra,rb,re,rs,rt
110 lsrs \re,\rb,#20 @ extract sign and exponent
113 subs \rb,\rs @ clear sign and exponent in mantissa; insert implied 1
114 lsrs \rs,\re,#11 @ sign
116 lsrs \re,#21 @ exponent
117 beq l\@_1 @ zero exponent?
120 beq l\@_2 @ exponent != 0x7ff? then done
130@ IEEE double in ra:rb ->
131@ signed mantissa in ra:rb 12Q52 (53 significant bits) with implied 1
134@ +zero, +denormal -> exponent=-0x80000
135@ -zero, -denormal -> exponent=-0x80000
136@ +Inf, +NaN -> exponent=+0x77f000
137@ -Inf, -NaN -> exponent=+0x77e000
138.macro mdunpacks ra,rb,re,rt0,rt1
139 lsrs \re,\rb,#20 @ extract sign and exponent
140 lsrs \rt1,\rb,#31 @ sign only
143 subs \rb,\rt0 @ clear sign and exponent in mantissa; insert implied 1
145 bcc l\@_1 @ skip on positive
146 mvns \rb,\rb @ negate mantissa
152 beq l\@_2 @ zero exponent?
155 beq l\@_3 @ exponent != 0x7ff? then done
159 lsls \rt1,#1 @ +ve: 0 -ve: 2
160 adds \rb,\rt1,#1 @ +ve: 1 -ve: 3
161 lsls \rb,#30 @ create +/-1 mantissa
168double_section WRAPPER_FUNC_NAME(__aeabi_dsub)
170# frsub first because it is the only one that needs alignment
171regular_func drsub_shim
177regular_func dsub_shim
181 eors r3,r4 @ flip sign on second argument
182 b da_entry @ continue in dadd
185double_section dadd_shim
186regular_func dadd_shim
189 mdunpacks r0,r1,r4,r6,r7
190 mdunpacks r2,r3,r5,r6,r7
191 subs r7,r5,r4 @ ye-xe
192 subs r6,r4,r5 @ xe-ye
194@ here xe>=ye: need to shift y down r6 places
195 mov r12,r4 @ save exponent
197 bge da_xrgty @ xe rather greater than ye?
200 lsls r4,r4,r7 @ rounding bit + sticky bits
211@ here unnormalised signed result (possibly 0) is in r0:r1 with exponent r12, rounding + sticky bits in r4
212@ Note that if a large normalisation shift is required then the arguments were close in magnitude and so we
213@ cannot have not gone via the xrgty/yrgtx paths. There will therefore always be enough high bits in r4
214@ to provide a correct continuation of the exact result.
215@ now pack result back up
216 lsrs r3,r1,#31 @ get sign bit
217 beq 1f @ skip on positive
218 mvns r1,r1 @ negate mantissa
225 mov r2,r12 @ get exponent
227 bne da_0 @ shift down required?
229 bne da_1 @ normalised?
231 beq da_5 @ could mantissa be zero?
236 subs r2,#1 @ adjust exponent
240 lsls r4,#1 @ check rounding bit
243 adds r0,#1 @ round up
247 cmp r4,#0 @ sticky bits zero?
249 lsrs r0,#1 @ round to even
254 adds r4,r2,#2 @ check if exponent is overflowing
257 lsls r2,#20 @ pack exponent and sign
264@ here exponent overflow: return signed infinity
270@ here exponent underflow: return signed zero
277@ here mantissa could be zero
282@ inputs must have been of identical magnitude and opposite sign, so return +0
286@ here a shift down by one place is required for normalisation
287 adds r2,#1 @ adjust exponent
288 lsls r6,r0,#31 @ save rounding bit
297da_xrgty: @ xe>ye and shift>=32 places
299 bge da_xmgty @ xe much greater than ye?
304 lsls r4,r4,r7 @ these would be shifted off the bottom of the sticky bits
313 asrs r3,r2,#31 @ propagate sign bit
317@ here ye>xe: need to shift x down r7 places
318 mov r12,r5 @ save exponent
320 bge da_yrgtx @ ye rather greater than xe?
323 lsls r4,r4,r6 @ rounding bit + sticky bits
334 bge da_ymgtx @ ye much greater than xe?
339 lsls r4,r4,r6 @ these would be shifted off the bottom of the sticky bits
348 asrs r1,r0,#31 @ propagate sign bit
351da_ymgtx: @ result is just y
354da_xmgty: @ result is just x
355 movs r4,#0 @ clear sticky bits
361@ needs five temporary registers
362@ can have rt3==rx, in which case rx trashed
363@ can have rt4==ry, in which case ry trashed
366@ can have rzl,rzh==rt3,rt4
367.macro mul32_32_64 rx,ry,rzl,rzh,rt0,rt1,rt2,rt3,rt4
372 muls \rt0,\rt1 @ xlyl=L
373 lsrs \rt2,\rx,#16 @ xh
374 muls \rt1,\rt2 @ xhyl=M0
375 lsrs \rt4,\ry,#16 @ yh
376 muls \rt2,\rt4 @ xhyh=H
378 muls \rt3,\rt4 @ xlyh=M1
379 adds \rt1,\rt3 @ M0+M1=M
380 bcc l\@_1 @ addition of the two cross terms can overflow, so add carry into H
382 lsls \rt3,#16 @ 0x10000
387 lsls \rzl,\rt1,#16 @ ML
388 lsrs \rzh,\rt1,#16 @ MH
393@ SUMULL: x signed, y unsigned
394@ in table below ¯ means signed variable
395@ needs five temporary registers
396@ can have rt3==rx, in which case rx trashed
397@ can have rt4==ry, in which case ry trashed
400@ can have rzl,rzh==rt3,rt4
401.macro muls32_32_64 rx,ry,rzl,rzh,rt0,rt1,rt2,rt3,rt4
406 muls \rt0,\rt1 @ xlyl=L
407 asrs \rt2,\rx,#16 @ ¯xh
408 muls \rt1,\rt2 @ ¯xhyl=M0
409 lsrs \rt4,\ry,#16 @ yh
410 muls \rt2,\rt4 @ ¯xhyh=H
412 muls \rt3,\rt4 @ xlyh=M1
413 asrs \rt4,\rt1,#31 @ M0sx (M1 sign extension is zero)
414 adds \rt1,\rt3 @ M0+M1=M
416 adcs \rt4,\rt3 @ ¯Msx
417 lsls \rt4,#16 @ ¯Msx<<16
422 lsls \rzl,\rt1,#16 @ M~
423 lsrs \rzh,\rt1,#16 @ M~
428@ SSMULL: x signed, y signed
429@ in table below ¯ means signed variable
430@ needs five temporary registers
431@ can have rt3==rx, in which case rx trashed
432@ can have rt4==ry, in which case ry trashed
435@ can have rzl,rzh==rt3,rt4
436.macro muls32_s32_64 rx,ry,rzl,rzh,rt0,rt1,rt2,rt3,rt4
441 muls \rt0,\rt1 @ xlyl=L
442 asrs \rt2,\rx,#16 @ ¯xh
443 muls \rt1,\rt2 @ ¯xhyl=M0
444 asrs \rt4,\ry,#16 @ ¯yh
445 muls \rt2,\rt4 @ ¯xhyh=H
447 muls \rt3,\rt4 @ ¯xlyh=M1
448 adds \rt1,\rt3 @ ¯M0+M1=M
449 asrs \rt3,\rt1,#31 @ Msx
451 mvns \rt3,\rt3 @ ¯Msx flip sign extension bits if overflow
453 lsls \rt3,#16 @ ¯Msx<<16
458 lsls \rzl,\rt1,#16 @ M~
459 lsrs \rzh,\rt1,#16 @ M~
464@ can have rt2==rx, in which case rx trashed
467.macro square32_64 rx,rzl,rzh,rt0,rt1,rt2
470 muls \rt0,\rt0 @ xlxl=L
472 lsrs \rt2,\rx,#16 @ xh
473 muls \rt1,\rt2 @ xlxh=M
474 muls \rt2,\rt2 @ xhxh=H
475 lsls \rzl,\rt1,#17 @ ML
476 lsrs \rzh,\rt1,#15 @ MH
481double_section dmul_shim
482 regular_func dmul_shim
484 mdunpack r0,r1,r4,r6,r5
486 mdunpack r2,r3,r4,r7,r5
487 eors r7,r6 @ sign of result
488 add r4,r12 @ exponent of result
491@ accumulate full product in r12:r5:r6:r7
492 mul32_32_64 r0,r2, r0,r5, r4,r6,r7,r0,r5 @ XL*YL
493 mov r12,r0 @ save LL bits
495 mul32_32_64 r1,r3, r6,r7, r0,r2,r4,r6,r7 @ XH*YH
498 mul32_32_64 r0,r3, r0,r3, r1,r2,r4,r0,r3 @ XL*YH
505 mul32_32_64 r1,r2, r1,r2, r0,r3,r4, r1,r2 @ XH*YL
511@ here r5:r6:r7 holds the product [1..4) in Q(104-32)=Q72, with extra LSBs in r12
512 pop {r3,r4} @ exponent in r3, sign in r4
519 lsls r5,#11 @ now r5:r0:r1 Q83=Q(51+32), extra LSBs in r12
521 bne 1f @ skip if in range [2..4)
522 adds r5,r5 @ shift up so always [2..4) Q83, i.e. [1..2) Q84=Q(52+32)
525 subs r3,#1 @ correct exponent
528 subs r3,r6 @ correct for exponent bias
531 bhs dm_0 @ exponent over- or underflow
532 lsls r5,#1 @ rounding bit to carry
533 bcc 1f @ result is correctly rounded
536 adcs r1,r6 @ round up
537 mov r6,r12 @ remaining sticky bits
539 bne 1f @ some sticky bits set?
541 lsls r0,#1 @ round to even
550@ here for exponent over- or underflow
553 adds r3,#1 @ would-be zero exponent?
556 bne 1f @ all-ones mantissa?
567@ here for exponent overflow
576@ Approach to division y/x is as follows.
578@ First generate u1, an approximation to 1/x to about 29 bits. Multiply this by the top
579@ 32 bits of y to generate a0, a first approximation to the result (good to 28 bits or so).
580@ Calculate the exact remainder r0=y-a0*x, which will be about 0. Calculate a correction
581@ d0=r0*u1, and then write a1=a0+d0. If near a rounding boundary, compute the exact
582@ remainder r1=y-a1*x (which can be done using r0 as a basis) to determine whether to
585@ The calculation of 1/x is as given in dreciptest.c. That code verifies exhaustively
586@ that | u1*x-1 | < 10*2^-32.
593@ u0=(q15~)"(0xffffffffU/(unsigned int)roundq(x/x_ulp))/powq(2,16)"(x0); // q15 approximation to 1/x; "~" denotes rounding rather than truncation
595@ u1=(q30)u0-(q30~)(u0*v);
605@ Use Greek letters to represent the errors introduced by rounding and truncation.
608@ = y - [ u₁ ( y - α ) - β ] x where 0 ≤ α < 2^-31, 0 ≤ β < 2^-30
609@ = y ( 1 - u₁x ) + ( u₁α + β ) x
613@ | r₀ / x | < 2 * 10*2^-32 + 2^-31 + 2^-30
619@ = r₀ - u₁ ( r₀ - γ ) x where 0 ≤ γ < 2^-57
620@ = r₀ ( 1 - u₁x ) + u₁γx
624@ | r₁ / x | < 26*2^-32 * 10*2^-32 + 2^-57
628@ Empirically it seems to be nearly twice as good as this.
630@ To determine correctly whether the exact remainder calculation can be skipped we need a result
631@ accurate to < 0.25ulp. In the case where x>y the quotient will be shifted up one place for normalisation
632@ and so 1ulp is 2^-53 and so the calculation above suffices.
634double_section ddiv_shim
635 regular_func ddiv_shim
637ddiv0: @ entry point from dtan
638 mdunpack r2,r3,r4,r7,r6 @ unpack divisor
646 str r6,[r5,#DIV_UDIVIDEND]
647 lsrs r6,r3,#4 @ x0=(q16)x
648 str r6,[r5,#DIV_UDIVISOR]
649@ if there are not enough cycles from now to the read of the quotient for
650@ the divider to do its stuff we need a busy-wait here
654@ unpack dividend by hand to save on register use
657 mov r12,r6 @ result sign in r12b0; r12b1 trashed
659 lsrs r7,r1,#21 @ exponent
660 beq 1f @ zero exponent?
663 beq 2f @ exponent != 0x7ff? then done
667 subs r7,#64 @ less drastic fiddling of exponents to get 0/0, Inf/Inf correct
672 add r12,r12,r6 @ (signed) exponent in r12[31..8]
673 subs r7,#1 @ implied 1
680 ldr r6,[r5,#DIV_QUOTIENT]
686@ this is not beautiful; could be replaced by better code that uses knowledge of divisor range
690 lsrs r1,r3,#4 @ x0=(q16)x
691 bl __aeabi_uidiv @ !!! this could (but apparently does not) trash R12
701@ r6 u0, first approximation to 1/x Q15
702@ r12: result sign, exponent
706 orrs r5,r4 @ x1=(q30)x
707 muls r5,r6 @ u0*x1 Q45
708 asrs r5,#15 @ v=u0*x1-1 Q30
709 muls r5,r6 @ u0*v Q45
712 asrs r5,#1 @ round u0*v to Q30
719@ r6 u1, second approximation to 1/x Q30
720@ r12: result sign, exponent
725 orrs r4,r5 @ y0=(q31)y
726 mul32_32_64 r4,r6, r4,r5, r2,r3,r7,r4,r5 @ y0*u1 Q61
728 adcs r5,r5 @ a0=(q30)(y0*u1)
732@ r5 a0, first approximation to y/x Q30
733@ r6 u1, second approximation to 1/x Q30
734@ r12 result sign, exponent
737 mul32_32_64 r2,r5, r2,r3, r1,r4,r7,r2,r3 @ xL*a0
740 adds r3,r4 @ r2:r3 now x*a0 Q82
743 orrs r2,r1 @ r2 now x*a0 Q57; r7:r2 is x*a0 Q89
744 lsls r4,r0,#5 @ y Q57
745 subs r0,r4,r2 @ r0x=y-x*a0 Q57 (signed)
749@ r5 a0, first approximation to y/x Q30
752@ r12 result sign, exponent
754 muls32_32_64 r0,r6, r7,r6, r1,r2,r3, r7,r6 @ r7:r6 r0x*u1 Q87
757 lsls r3,r6,#7 @ r3:r5 a1 Q62 (but bottom 7 bits are zero so 55 bits of precision after binary point)
758@ here we could recover another 7 bits of precision (but not accuracy) from the top of r7
759@ but these bits are thrown away in the rounding and conversion to Q52 below
762@ r3:r5 a1 Q62 candidate quotient [0.5,2) or so
764@ r12 result sign, exponent
767 adds r3,#128 @ for initial rounding to Q53
771@ here candidate quotient a1 is in range [0.5,1)
772@ so 30 significant bits in r5
774 lsls r4,#1 @ y now Q58
775 lsrs r1,r5,#9 @ to Q52
777 lsrs r3,#9 @ 0.5ulp-significance bit in carry: if this is 1 we may need to correct result
782@ here candidate quotient a1 is in range [1,2)
783@ so 31 significant bits in r5
786 add r12,r12,r2 @ fix exponent; r3:r5 now effectively Q61
787 adds r3,#128 @ complete rounding to Q53
791 lsrs r3,#10 @ 0.5ulp-significance bit in carry: if this is 1 we may need to correct result
797@ r0:r1 rounded result Q53 [0.5,1) or Q52 [1,2), but may not be correctly rounded-to-nearest
799@ r12 result sign, exponent
803 adcs r1,r1,r1 @ z Q53 with 1 in LSB
804 lsls r4,#16 @ Q105-32=Q73
805 ldr r2,[r13,#0] @ xL Q52
806 ldr r3,[r13,#4] @ xH Q20
809 muls r5,r2 @ zH*xL Q73
811 muls r3,r0 @ zL*xH Q73
813 mul32_32_64 r2,r0, r2,r3, r5,r6,r7,r2,r3 @ xL*zL
814 negs r2,r2 @ borrow from low half?
815 sbcs r4,r3 @ y-xz Q73 (remainder bits 52..73)
820 movs r2,#0 @ round up
824 lsrs r0,#1 @ shift back down to Q52
831 lsls r7,r2,#31 @ result sign
832 asrs r2,#2 @ result exponent
837 bhs dd_3 @ over- or underflow?
839 adds r1,r2 @ pack exponent
841 adds r1,r7 @ pack sign
856.section SECTION_NAME(dsqrt_shim)
858Approach to square root x=sqrt(y) is as follows.
860First generate a3, an approximation to 1/sqrt(y) to about 30 bits. Multiply this by y
861to give a4~sqrt(y) to about 28 bits and a remainder r4=y-a4^2. Then, because
862d sqrt(y) / dy = 1 / (2 sqrt(y)) let d4=r4*a3/2 and then the value a5=a4+d4 is
863a better approximation to sqrt(y). If this is near a rounding boundary we
864compute an exact remainder y-a5*a5 to decide whether to round up or down.
866The calculation of a3 and a4 is as given in dsqrttest.c. That code verifies exhaustively
867that | 1 - a3a4 | < 10*2^-32, | r4 | < 40*2^-32 and | r4/y | < 20*2^-32.
869More precisely, with "y" representing y truncated to 30 binary places:
871u=(q3)y; // 24-entry table
872a0=(q8~)"1/sqrtq(x+x_ulp/2)"(u); // first approximation from table
873p0=(q16)(a0*a0) * (q16)y;
875dy0=(q15)(r0*a0); // Newton-Raphson correction term
876a1=(q16)a0-dy0/2; // good to ~9 bits
878p1=(q19)(a1*a1)*(q19)y;
880dy1=(q15~)(r1*a1); // second Newton-Raphson correction
881a2x=(q16)a1-dy1/2; // good to ~16 bits
882a2=a2x-a2x/1t16; // prevent overflow of a2*a2 in 32 bits
884p2=(a2*a2)*(q30)y; // Q62
886dy2=(q30)(r2*a2); // Q52->Q30
887a3=(q31)a2-dy2/2; // good to about 30 bits
888a4=(q30)(a3*(q30)y+1t-31); // good to about 28 bits
897 = y - a₄² - a₃a₄r₄ - 1/4 a₃²r₄²
898 = r₄ - a₃a₄r₄ - 1/4 a₃²r₄²
900 | r₅ | < | r₄ | | 1 - a₃a₄ | + 1/4 r₄²
902 a₅ = √y √( 1 - r₅/y )
903 = √y ( 1 - 1/2 r₅/y + ... )
905So to first order (second order being very tiny)
911 | √y - a₅ | < 1/2 ( | r₄/y | | 1 - a₃a₄ | + 1/4 r₄²/y )
913From dsqrttest.c (conservatively):
915 < 1/2 ( 20*2^-32 * 10*2^-32 + 1/4 * 40*2^-32*20*2^-32 )
916 = 1/2 ( 200 + 200 ) * 2^-64
919Empirically we see about 1ulp worst-case error including rounding at Q57.
921To determine correctly whether the exact remainder calculation can be skipped we need a result
922accurate to < 0.25ulp at Q52, or 2^-54.
932 lsls r1,#31 @ preserve sign bit
933 lsrs r2,#21 @ extract exponent
934 beq dq_4 @ -0? return it
935 asrs r1,#11 @ make -Inf
940 lsls r1,#20 @ return +Inf
947regular_func dsqrt_shim
950 lsrs r2,#21 @ extract exponent
954 bhs dq_2 @ catches 0 and +Inf
957 subs r1,r4 @ insert implied 1
959 bcc 1f @ even exponent? skip
960 adds r0,r0,r0 @ odd exponent: shift up mantissa
966 mov r12,r2 @ save result exponent
969@ r0:r1 y mantissa Q52 [1,4)
971.equ drsqrtapp_minus_8, (drsqrtapp-8)
972 adr r4,drsqrtapp_minus_8 @ first eight table entries are never accessed because of the mantissa's leading 1
973 lsrs r2,r1,#17 @ y Q3
974 ldrb r2,[r4,r2] @ initial approximation to reciprocal square root a0 Q8
975 lsrs r3,r1,#4 @ first Newton-Raphson iteration
977 muls r3,r2 @ i32 p0=a0*a0*(y>>14); // Q32
978 asrs r3,r3,#12 @ i32 r0=p0>>12; // Q20
980 asrs r3,#13 @ i32 dy0=(r0*a0)>>13; // Q15
982 subs r2,r3 @ i32 a1=(a0<<8)-dy0; // Q16
988 muls r3,r4 @ i32 p1=((a1*a1)>>11)*(y>>11); // Q19*Q19=Q38
989 asrs r3,#15 @ i32 r1=p1>>15; // Q23
993 asrs r3,#1 @ i32 dy1=(r1*a1+(1<<23))>>24; // Q23*Q16=Q39; Q15
994 subs r2,r3 @ i32 a2=a1-dy1; // Q16
996 subs r2,r3 @ if(a2>=0x10000) a2=0xffff; to prevent overflow of a2*a2
1000@ r2 a2 ~ 1/sqrt(y) Q16
1001@ r12 result exponent
1008 mul32_32_64 r1,r3, r4,r3, r5,r6,r7,r4,r3 @ i64 p2=(ui64)(a2*a2)*(ui64)y; // Q62 r4:r3
1012 adds r4,#0x20 @ i32 r2=(p2>>26)+0x20; // Q36 r4
1019 asrs r4,#6 @ i32 dy2=((i64)r2*(i64)a2)>>22; // Q36*Q16=Q52; Q30
1026@ r2 a3 ~ 1/sqrt(y) Q31
1027@ r12 result exponent
1029 mul32_32_64 r2,r1, r3,r4, r5,r6,r7,r3,r4
1034 adcs r3,r4 @ ui32 a4=((ui64)a3*(ui64)y+(1U<<31))>>31; // Q30
1039@ r2 a3 Q31 ~ 1/sqrt(y)
1040@ r3 a4 Q30 ~ sqrt(y)
1041@ r12 result exponent
1043 square32_64 r3, r4,r5, r6,r5,r7
1047 sbcs r7,r5 @ r4=(q60)y-a4*a4
1049@ by exhaustive testing, r4 = fffffffc0e134fdc .. 00000003c2bf539c Q60
1053 adcs r6,r5 @ r4 Q57 with rounding
1054 muls32_32_64 r6,r2, r6,r2, r4,r5,r7,r6,r2 @ d4=a3*r4/2 Q89
1055@ r4+d4 is correct to 1ULP at Q57, tested on ~9bn cases including all extreme values of r4 for each possible y Q30
1058 asrs r2,#5 @ d4 Q52, rounded to Q53 with spare bit in carry
1063@ r2 d4 Q52, rounded to Q53
1064@ C flag contains d4_b53
1069 lsrs r5,r3,#10 @ a4 Q52
1076 add r1,r12 @ pack exponent
1082@ round(sqrt(2^22./[68:8:252]))
1084.byte 0xf8,0xeb,0xdf,0xd6,0xcd,0xc5,0xbe,0xb8
1085.byte 0xb2,0xad,0xa8,0xa4,0xa0,0x9c,0x99,0x95
1086.byte 0x92,0x8f,0x8d,0x8a,0x88,0x85,0x83,0x81
1089@ here we are near a rounding boundary, C is set
1090 adcs r2,r2,r2 @ d4 Q53+1ulp
1092 lsls r4,r3,#23 @ r4:r5 a4 Q53
1095 adcs r5,r1 @ r4:r5 a5=a4+d4 Q53+1ulp
1098 square32_64 r4,r1,r2,r6,r2,r7
1100 adds r2,r3 @ r1:r2 a5^2 Q106
1104 sbcs r0,r2 @ remainder y-a5^2
1105 bmi 1f @ y<a5^2: no need to increment a5
1108 adcs r5,r3 @ bump a5 if over rounding boundary
1117@ "scientific" functions start here
1119@ double-length CORDIC rotation step
1122@ r6 32-i (complementary shift)
1126@ r12 coefficient pointer
1128@ an option in rotation mode would be to compute the sequence of σ values
1129@ in one pass, rotate the initial vector by the residual ω and then run a
1130@ second pass to compute the final x and y. This would relieve pressure
1131@ on registers and hence possibly be faster. The same trick does not work
1132@ in vectoring mode (but perhaps one could work to single precision in
1133@ a first pass and then double precision in a second pass?).
1135double_section dcordic_vec_step
1136 regular_func dcordic_vec_step
1145double_section dcordic_rot_step
1146 regular_func dcordic_rot_step
1165 orrs r2,r4 @ r2:r3 y>>i, rounding in carry
1169 adcs r3,r5 @ r2:r3 x+(y>>i)
1177 orrs r4,r3 @ r4:r5 x>>i, rounding in carry
1181 sbcs r3,r5 @ r2:r3 y-(x>>i)
1200 orrs r2,r4 @ r2:r3 x>>i, rounding in carry
1202 mov r5,r11 @ r4:r5 y
1204 adcs r3,r5 @ r2:r3 y+(x>>i)
1212 orrs r4,r3 @ r4:r5 y>>i, rounding in carry
1216 sbcs r3,r5 @ r2:r3 x-(y>>i)
1221@ convert packed double in r0:r1 to signed/unsigned 32/64-bit integer/fixed-point value in r0:r1 [with r2 places after point], with rounding towards -Inf
1222@ fixed-point versions only work with reasonable values in r2 because of the way dunpacks works
1224double_section double2int_shim
1225 regular_func double2int_shim
1226 movs r2,#0 @ and fall through
1227regular_func double2fix_shim
1230 bl double2fix64_shim
1234double_section double2uint_shim
1235 regular_func double2uint_shim
1236 movs r2,#0 @ and fall through
1237regular_func double2ufix_shim
1240 bl double2ufix64_shim
1244double_section double2int64_shim
1245 regular_func double2int64_shim
1246 movs r2,#0 @ and fall through
1247regular_func double2fix64_shim
1253 bne 1f @ sign extension bits fail to match sign of result?
1259 eors r1,r1,r0 @ generate extreme fixed-point values
1262double_section double2uint64_shim
1263 regular_func double2uint64_shim
1264 movs r2,#0 @ and fall through
1265regular_func double2ufix64_shim
1266 asrs r3,r1,#20 @ negative? return 0
1270@ convert double in r0:r1 to signed fixed point in r0:r1:r3, r2 places after point, rounding towards -Inf
1271@ result clamped so that r3 can only be 0 or -1
1281 movs r1,#0 @ -0 -> +0
1292.weak d2fix_a // weak because it exists in float code too
1296@ r0:r1 two's complement mantissa
1297@ r2 unbaised exponent
1298@ r3 mantissa sign extension bits
1299 add r2,r12 @ exponent plus offset for required binary point position
1300 subs r2,#52 @ required shift
1301 bmi 1f @ shift down?
1302@ here a shift up by r2 places
1303 cmp r2,#12 @ will clamp?
1309 adds r2,#32 @ complementary shift
1315 mvns r1,r3 @ overflow: clamp to extreme fixed-point values
1318@ here a shift down by -r2 places
1320 bmi 1f @ long shift?
1324 adds r2,#32 @ complementary shift
1330@ here a long shift down
1332 asrs r1,#31 @ shift down 32 places
1334 bmi 1f @ very long shift?
1340 movs r0,r3 @ result very near zero: use sign extension bits
1344double_section double2float_shim
1345 regular_func double2float_shim
1347 lsrs r2,#21 @ exponent
1349 subs r2,r3 @ fix exponent bias
1350 ble 1f @ underflow or zero
1352 bge 2f @ overflow or infinity
1353 lsls r2,#23 @ position exponent of result
1356 orrs r2,r3 @ insert sign
1357 lsls r3,r0,#3 @ rounding bits
1361 orrs r0,r1 @ assemble mantissa
1362 orrs r0,r2 @ insert exponent and sign
1364 bcc 3f @ no rounding
1365 beq 4f @ all sticky bits 0?
1371 lsrs r3,r0,#1 @ odd? then round up
1375 beq 6f @ check case where value is just less than smallest normal
1381 lsls r2,r1,#12 @ 20 1:s at top of mantissa?
1385 lsrs r2,r0,#29 @ and 3 more 1:s?
1388 movs r2,#1 @ return smallest normal with correct sign
1393 lsrs r0,r1,#31 @ return signed infinity
1399double_section x2double_shims
1400@ convert signed/unsigned 32/64-bit integer/fixed-point value in r0:r1 [with r2 places after point] to packed double in r0:r1, with rounding
1403regular_func uint2double_shim
1404 movs r1,#0 @ and fall through
1405regular_func ufix2double_shim
1408 b ufix642double_shim
1411regular_func int2double_shim
1412 movs r1,#0 @ and fall through
1413regular_func fix2double_shim
1415 asrs r1,r0,#31 @ sign extend
1419regular_func uint642double_shim
1420 movs r2,#0 @ and fall through
1421regular_func ufix642double_shim
1426regular_func int642double_shim
1427 movs r2,#0 @ and fall through
1428regular_func fix642double_shim
1429 asrs r3,r1,#31 @ sign bit across all bits
1437 subs r2,r4,r2 @ form biased exponent
1439@ r0:r1 unnormalised mantissa
1440@ r2 -Q (will become exponent)
1441@ r3 sign across all bits
1443 bne 1f @ short normalising shift?
1445 beq 2f @ zero? return it
1447 subs r2,#32 @ fix exponent
1450 bne 3f @ will need shift down (and rounding?)
1451 bcs 4f @ normalised already?
1454 adds r0,r0 @ shift up
1461 bhs 6f @ over/underflow? return signed zero/infinity
1463 lsls r2,#20 @ pack and return
14696: @ return signed zero/infinity according to unclamped exponent in r2
1477@ here we need to shift down to normalise and possibly round
1478 bmi 1f @ already normalised to Q63?
1481 adds r0,r0 @ shift up
1485@ here we have a 1 in b63 of r0:r1
1486 adds r2,#11 @ correct exponent for subsequent shift down
1487 lsls r4,r0,#21 @ save bits for rounding
1493 beq 1f @ sticky bits are zero?
1500 bcc 4b @ sticky bits are zero but not on rounding boundary
1501 lsrs r4,r0,#1 @ increment if odd (force round to even)
1507double_section dunpacks
1508 regular_func dunpacks
1509 mdunpacks r0,r1,r2,r3,r4
1511 subs r2,r3 @ exponent without offset
1514@ r0:r1 signed mantissa Q52
1515@ r2 unbiased exponent < 10 (i.e., |x|<2^10)
1517@ - divisor reciprocal approximation r=1/d Q15
1518@ - divisor d Q62 0..20
1519@ - divisor d Q62 21..41
1520@ - divisor d Q62 42..62
1522@ r0:r1 reduced result y Q62, -0.6 d < y < 0.6 d (better in practice)
1523@ r2 quotient q (number of reductions)
1524@ if exponent >=10, returns r0:r1=0, r2=1024*mantissa sign
1525@ designed to work for 0.5<d<2, in particular d=ln2 (~0.7) and d=π/2 (~1.6)
1526double_section dreduce
1527 regular_func dreduce
1529 bmi 1f @ |x|<0.25, too small to need adjustment
1538 adds r5,#7 @ 22-e = 32-(e+10)
1543 orrs r1,r6 @ r0:r1 x Q62
1548 adcs r2,r3 @ rx Q0 rounded = q; for e.g. r=1.5 |q|<1.5*2^10
1549 muls r5,r2 @ qd in pieces: L Q62
1558 adds r7,r5 @ r6:r7 qd Q62
1560 sbcs r1,r7 @ remainder Q62
1563 movs r2,#12 @ overflow: clamp to +/-1024
1575 lsls r0,#8 @ r0:r1 Q60, to be shifted down -r2 places
1577 adds r2,#32 @ shift down in r3, complementary shift in r2
1578 bmi 1f @ long shift?
1585 movs r2,#0 @ rounding
1591 movs r0,r1 @ down 32 places
1596 movs r0,#0 @ very long shift? return 0
1601double_section dtan_shim
1602 regular_func dtan_shim
1613 b ddiv0 @ compute sin θ/cos θ
1615double_section dcos_shim
1616 regular_func dcos_shim
1623double_section dsin_shim
1624 regular_func dsin_shim
1633double_section dsincos_shim
1635 @ Note that this function returns in r0-r3
1636 regular_func dsincos_shim
1650double_section dtrig_guts
1652@ unpack double θ in r0:r1, range reduce and calculate ε, cos α and sin α such that
1653@ θ=α+ε and |ε|≤2^-32
1655@ r0:r1 ε (residual ω, where θ=α+ε) Q62, |ε|≤2^-32 (so fits in r0)
1667 ldr r5,=0x9df04dbb @ this value compensates for the non-unity scaling of the CORDIC rotations
1705@ r0:r1 ε (residual ω, where θ=α+ε) Q62, |ε|≤2^-32 (so fits in r0)
1708@ and we wish to calculate cos θ=cos(α+ε)~cos α - ε sin α
1712@ adds r1,r2 @ rounding improves accuracy very slightly
1713 muls32_s32_64 r0,r1, r2,r3, r4,r5,r6,r2,r3
1714@ r2:r3 ε sin α Q(62+62-32)=Q92
1721 sbcs r0,r2 @ include rounding
1728@ r0:r1 ε (residual ω, where θ=α+ε) Q62, |ε|≤2^-32 (so fits in r0)
1731@ and we wish to calculate sin θ=sin(α+ε)~sin α + ε cos α
1733 muls32_s32_64 r0,r1, r2,r3, r4,r5,r6,r2,r3
1734@ r2:r3 ε cos α Q(62+62-32)=Q92
1741 adcs r0,r2 @ include rounding
1749.word 0x0000517d @ 2/π Q15
1750.word 0x0014611A @ π/2 Q62=6487ED5110B4611A split into 21-bit pieces
1756regular_func datan2_shim
1765 cmp r4,r5 @ or Inf/NaN?
1775 cmp r4,r5 @ or Inf/NaN?
1782 movs r6,#0 @ quadrant offset
1783 lsls r5,#11 @ constant 0x80000000
1785 bpl 1f @ skip if x positive
1789 bmi 1f @ quadrant offset=+2 if y was positive
1790 negs r6,r6 @ quadrant offset=-2 if y was negative
1792@ now in quadrant 0 or 3
1793 adds r7,r1,r5 @ r7=-r1
1795@ y>=0: in quadrant 0
1797 ble 2f @ y<~x so 0≤θ<~π/4: skip
1799 eors r1,r5 @ negate x
1800 b 3f @ and exchange x and y = rotate by -π/2
1803 bge 2f @ -y<~x so -π/4<~θ≤0: skip
1805 eors r3,r5 @ negate y and ...
1807 movs r7,r0 @ exchange x and y
1815@ r6 has quadrant offset
1820 beq 10f @ x==0 going into division?
1824 bne 1f @ x==Inf going into division?
1827 adds r4,#1 @ y also ±Inf?
1829 subs r1,#1 @ make them both just finite
1841 bl double2fix64_shim
1862@ r0:r1 atan(y/x) Q62
1863@ r8:r9 x residual Q62
1864@ r10:r11 y residual Q62
1867 subs r2,#12 @ this makes atan(0)==0
1868@ the following is basically a division residual y/x ~ atan(residual y/x)
1874 movs r3,r3 @ preserve carry
1899 ldr r4,=0x885A308D @ π/2 Q61
1902 mvns r4,r4 @ negative quadrant offset
1906 bne 2f @ skip if quadrant offset is ±1
1914 bl fix642double_shim
1922.word 0x61bb4f69, 0x1dac6705 @ atan 2^-1 Q62
1923.word 0x96406eb1, 0x0fadbafc @ atan 2^-2 Q62
1924.word 0xab0bdb72, 0x07f56ea6 @ atan 2^-3 Q62
1925.word 0xe59fbd39, 0x03feab76 @ atan 2^-4 Q62
1926.word 0xba97624b, 0x01ffd55b @ atan 2^-5 Q62
1927.word 0xdddb94d6, 0x00fffaaa @ atan 2^-6 Q62
1928.word 0x56eeea5d, 0x007fff55 @ atan 2^-7 Q62
1929.word 0xaab7776e, 0x003fffea @ atan 2^-8 Q62
1930.word 0x5555bbbc, 0x001ffffd @ atan 2^-9 Q62
1931.word 0xaaaaadde, 0x000fffff @ atan 2^-10 Q62
1932.word 0xf555556f, 0x0007ffff @ atan 2^-11 Q62
1933.word 0xfeaaaaab, 0x0003ffff @ atan 2^-12 Q62
1934.word 0xffd55555, 0x0001ffff @ atan 2^-13 Q62
1935.word 0xfffaaaab, 0x0000ffff @ atan 2^-14 Q62
1936.word 0xffff5555, 0x00007fff @ atan 2^-15 Q62
1937.word 0xffffeaab, 0x00003fff @ atan 2^-16 Q62
1938.word 0xfffffd55, 0x00001fff @ atan 2^-17 Q62
1939.word 0xffffffab, 0x00000fff @ atan 2^-18 Q62
1940.word 0xfffffff5, 0x000007ff @ atan 2^-19 Q62
1941.word 0xffffffff, 0x000003ff @ atan 2^-20 Q62
1942.word 0x00000000, 0x00000200 @ atan 2^-21 Q62 @ consider optimising these
1943.word 0x00000000, 0x00000100 @ atan 2^-22 Q62
1944.word 0x00000000, 0x00000080 @ atan 2^-23 Q62
1945.word 0x00000000, 0x00000040 @ atan 2^-24 Q62
1946.word 0x00000000, 0x00000020 @ atan 2^-25 Q62
1947.word 0x00000000, 0x00000010 @ atan 2^-26 Q62
1948.word 0x00000000, 0x00000008 @ atan 2^-27 Q62
1949.word 0x00000000, 0x00000004 @ atan 2^-28 Q62
1950.word 0x00000000, 0x00000002 @ atan 2^-29 Q62
1951.word 0x00000000, 0x00000001 @ atan 2^-30 Q62
1952.word 0x80000000, 0x00000000 @ atan 2^-31 Q62
1953.word 0x40000000, 0x00000000 @ atan 2^-32 Q62
1955double_section dexp_guts
1956regular_func dexp_shim
1964 ldr r5,=0x2C5C85FD @ ln2 Q62
1974 lsls r3,#30 @ x=1 Q62
1984 adds r6,#32 @ complementary shift
1990 lsrs r6,r7 @ rounding bit in carry
1993 adcs r3,r5 @ x+=x>>i
1997 adds r0,r4 @ restore argument
2006@ r0:r1 ε (residual x, where x=a+ε) Q62, |ε|≤2^-32 (so fits in r0)
2008@ and we wish to calculate exp x=exp a exp ε~(exp a)(1+ε)
2009 muls32_32_64 r0,r3, r4,r1, r5,r6,r7,r4,r1
2010@ r4:r1 ε exp a Q(62+62-32)=Q92
2021 bl fix642double_shim @ in principle we can pack faster than this because we know the exponent
2027regular_func dln_shim
2032 beq 5f @ ... or =0? return -Inf
2034 beq 6f @ Inf/NaN? return +Inf
2041@ r0:r1 m Q61 = m/2 Q62 0.5≤m/2<1
2047 movs r3,#0 @ y=0 Q62
2051 adds r6,#32 @ complementary shift
2058 orrs r4,r6 @ x>>i, rounding bit in carry
2060 adcs r5,r1 @ x+(x>>i)
2063 bne 1f @ x+(x>>i)>1?
2065 movs r1,r5 @ x+=x>>i
2078@ r0:r1 residual x, nearly 1 Q62
2079@ r2:r3 y ~ ln m/2 = ln m - ln2 Q62
2080@ result is y + ln2 + ln x ~ y + ln2 + (x-1)
2088@ r2:r3 ln m/2 = ln m - ln2 Q62
2089@ r7 unbiased exponent
2090.equ dreddata1_plus_4, (dreddata1+4)
2091 adr r4,dreddata1_plus_4
2107@ r0:r1:r4 exponent*ln2 Q62
2112@ r0:r1:r4 result Q62
2117 beq 2f @ r4 a sign extension of r1?
2118 lsrs r0,#4 @ no: shift down 4 places and try again
2128 bl fix642double_shim
2145.word 0x0000B8AA @ 1/ln2 Q15
2146.word 0x0013DE6B @ ln2 Q62 Q62=2C5C85FDF473DE6B split into 21-bit pieces
2151.word 0xbf984bf3, 0x19f323ec @ log 1+2^-1 Q62
2152.word 0xcd4d10d6, 0x0e47fbe3 @ log 1+2^-2 Q62
2153.word 0x8abcb97a, 0x0789c1db @ log 1+2^-3 Q62
2154.word 0x022c54cc, 0x03e14618 @ log 1+2^-4 Q62
2155.word 0xe7833005, 0x01f829b0 @ log 1+2^-5 Q62
2156.word 0x87e01f1e, 0x00fe0545 @ log 1+2^-6 Q62
2157.word 0xac419e24, 0x007f80a9 @ log 1+2^-7 Q62
2158.word 0x45621781, 0x003fe015 @ log 1+2^-8 Q62
2159.word 0xa9ab10e6, 0x001ff802 @ log 1+2^-9 Q62
2160.word 0x55455888, 0x000ffe00 @ log 1+2^-10 Q62
2161.word 0x0aa9aac4, 0x0007ff80 @ log 1+2^-11 Q62
2162.word 0x01554556, 0x0003ffe0 @ log 1+2^-12 Q62
2163.word 0x002aa9ab, 0x0001fff8 @ log 1+2^-13 Q62
2164.word 0x00055545, 0x0000fffe @ log 1+2^-14 Q62
2165.word 0x8000aaaa, 0x00007fff @ log 1+2^-15 Q62
2166.word 0xe0001555, 0x00003fff @ log 1+2^-16 Q62
2167.word 0xf80002ab, 0x00001fff @ log 1+2^-17 Q62
2168.word 0xfe000055, 0x00000fff @ log 1+2^-18 Q62
2169.word 0xff80000b, 0x000007ff @ log 1+2^-19 Q62
2170.word 0xffe00001, 0x000003ff @ log 1+2^-20 Q62
2171.word 0xfff80000, 0x000001ff @ log 1+2^-21 Q62
2172.word 0xfffe0000, 0x000000ff @ log 1+2^-22 Q62
2173.word 0xffff8000, 0x0000007f @ log 1+2^-23 Q62
2174.word 0xffffe000, 0x0000003f @ log 1+2^-24 Q62
2175.word 0xfffff800, 0x0000001f @ log 1+2^-25 Q62
2176.word 0xfffffe00, 0x0000000f @ log 1+2^-26 Q62
2177.word 0xffffff80, 0x00000007 @ log 1+2^-27 Q62
2178.word 0xffffffe0, 0x00000003 @ log 1+2^-28 Q62
2179.word 0xfffffff8, 0x00000001 @ log 1+2^-29 Q62
2180.word 0xfffffffe, 0x00000000 @ log 1+2^-30 Q62
2181.word 0x80000000, 0x00000000 @ log 1+2^-31 Q62
2182.word 0x40000000, 0x00000000 @ log 1+2^-32 Q62