arXiv:2008.02894v1 [astro-ph.IM] 6 Aug 2020

h ODCcnetin concept CORDIC the oeti hrcmn,w td eeteie osml ignor for simply hoping to still decision, ( idea direction convergence the the in here change study scale the we iteration shortcoming, each in this come a requires step this ready MNRAS mzechmeister/ke/master//js/cordic.html 1 ⋆⋆ mult sine, cosine, tion) (e.g. functions elementary by many invented compute was Ro ) (Coordinate Digital CORDIC algorithm. tation CORDIC-like a use to posed ovn elrseuto ihCRI obeiterations double CORDIC with equation Kepler’s Solving ⋆ Fin adjustments. appropriate some given purposeful indeed where cetd22 uut6 eevd22 uut5 noiia f original in 5; August 2020 Received 6. August 2020 Accepted elrseuto K)i udmna nmn ed fastro of fields many anomaly mean in relates fundamental It is physics. (KE) equation Kepler’s INTRODUCTION 1 h equation the o trtos alysmto,tbeitroain ri ( or interpolation, table method, Halley’s iterations, ton hntelcto rvlct fa betms epredicted, be must compute object to an requires of then velocity or location the then E E c ntttfrAtohsk er-uutUiestt Fri Georg-August-Universität, Astrophysik, für Institut owl 1993 Colwell .Zechmeister M. ( − epoiea niedm at demo online an provide We oeaalbeat available Code 00TeAuthors The 2020 M -al [email protected] E-mail: e 1 .Frisac,i ri tig bevn times observing fitting, orbit in instance, For ). In npatc eotnne osleteivreteproblem the inverse the solve to need often we practice In aymtoshv enpooe osleK,sc sNew- as such KE, solve to proposed been have methods Many sin n ed nyadtosadbthfs ewl reyrevie briefly will We bitshifts. and additions only needs and M Ze18 000 E ( t ) = = , h oaindrcin r e cuaey oee,al- However, accurately. set are directions rotation the , M 1 τ – et 3 Sect. .In ). ( ?? P t E ihtime with ) (2020) https://github.com/mzechmeister/ke/ Zechmeister .W ildmntaeta hsapoc is approach this that demonstrate will We ). et 2 Sect. E ⋆⋆ . t n ria period orbital and . ( M 2018 qaincoet adaeadalw oslei ihcepa words: cheap Key with it shift-an solve new to allows The and too. hardware times case, to terms close hyperbolic cosine equation the and for sine adopted its be and can anomaly ne eccentric self-correc algorithm the are The ers iterations. iterations subsequent CORDIC in rotations double wrong that decided find is We direction scale. rotation The factor. init the correction prescale equat scale We Kepler’s multiplication. initial solve one and and eq shortcoming tions, Kepler’s major mu solve still this but to overcome division, idea any we require the not developed does which we algorithm, work, previous a In ABSTRACT n ceti anomaly eccentric and hereafter , https://raw.githack.com/ Volder P . . dihHn-lt ,307Gtign Germany Göttingen, 37077 1, edrich-Hund-Platz Ze18 ( eeta ehnc ehd:numerical methods: – mechanics celestial 1959 t r ie and given are vreseries nverse ,w pro- we ), r 00My11. May 2020 orm . oover- To . n can and ) correct a ly we ally, iplica- which E rpit1 uut22 oplduigMRSL MNRAS using Compiled 2020 August 10 Preprint via (1) w e - - aea er g.Therefore, ago. simp with years particular as mult ware in while expensive, , more for usually are perform cations to easy very are Additions θ form the of angles only allowed and z z coordinates polar in as well as re Carte the in resentation complex or representation lar rdc in product ( algorithm the of ihangles with where z coordinates Cartesian in expressed be can number complex A ELEMENTARY FOR ALGORITHM CORDIC 2 ( implementation an discuss n = = = i = P r r x FUNCTIONS θ Y exp + hnw ersn hsnumber this represent we When σ n n iy n x = α = cos i n ϕ z r q (4) Eq. = cos θ θ n n hs r ipeadtosi h xoeti h po- the in exponent the in additions simple are these , r Y ϕ Y n n and et 6 Sect. efcoe h emcos term the factored He . (1 (cos + y i = tan θ ). n a etrwt h cetiiyadthe and eccentricity the with vector ial r + sin ihu orcinfrtechanging the for correction without θ i n d 5 oeieain n deliv- and iterations more 75% eds et 5 Sect. tpiain nec trto.Here iteration. each in ltiplications sin (5) ) ϕ Volder dsml adaecomponents. hardware simple nd -d loih rnsKepler’s brings algorithm d-add . θ h cetiiy h algorithm The eccentricity. the igadcmest possible compensate and ting n o sn nybthfs addi- bitshifts, only using ion ) . ainwt CORDIC-like a with uation n vlaeteperformance the evaluate and ) ( z 1959 yasqec frotations of sequence a by ogtt ipiythe simplify to sought ) θ n ⋆ A T E tl l v3.0 file style X inrep- sian ehard- le ipli- (6) (4) (2) (3) 2 M. Zechmeister π with 1 tan α = and σ 1, 1 (7) n 2n 1 n ∈{− } − 3−π for n 1. So the first angle is α = 45◦ and the next rotation angles 4 ≥ 1 αn are almost halved in each iteration and the rotation can be clock- or counter-clockwise (positive or negative). Now, Eq. (5) can be π written as − 2

σ z = rK 1+i n (8) N Y  2n  −π n 4 single CORDIC iterations double CORDIC iterations where the term eccentric anomaly E [rad] (E - e sin E = M) 1 1 KN = cos αn = = (9) 0 Y Y + 2 Y √1 + 4 n π π 3π n n 1 tan αn n − 0 − − − π p 4 2 4 is called scale correction. The factor KN can be pre-computed, be- mean anomaly M [rad] cause the cos-function is symmetric and therefore independent of σn and the absolute values of the rotation angles θn = αn are pre- Figure 1. CORDIC on KE (for e = 1) with single iterations (red, 2 | | defined (Kc K 0.607 253) . shift sequence kn = 0, 1, 2, 3, 4,...) and with double iterations (blue, kn = ≡ ∞ ≈ Due to the angle choice in Eq. (7), the remaining product term 1, 1, 2, 2, 3, 3,...). can be computed efficiently. This is easier to explain when explic- itly writing an adjacent rotation for the real and imaginary part of z as Walther (1971) generalised the CORDIC algorithm with a lin- ear and hyperbolic mode allowing to compute multiplication, di- σn+1yn xn+1 = xn m (10) vision and the functions exp, ln, atanh, and square root. Table A1 − 2kn+1 σn+1 xn gives an overview of the different modes and Table A2 lists the yn+1 = yn + , (11) 2kn+1 required input and the corresponding output to obtain various el- ementary functions. This diversity demonstrates the capability of where the coordinate parameter m is 1 for the circular case ( 1 for this simple algorithm. the hyperbolic and 0 for the linear case). − However, it must be noted that the hyperbolic mode needs spe- The multiplication with σn is just a negation in case σn = 1. 3 n − cific iterations to be done twice for The multiplication by an integer power of two (2− ) is also very easy for a computer. It is a simple bit shift in binary system; very 3m+2 1 k 4, 13, 40, 121, ..., K, 3K + 1 = − . (13) similar in a decimal system a division by ten is just a left shift n ∈ 2 of the decimal point. Therefore, all multiplications are eliminated. This compensates the accumulating problem that subsequent ro- Only the one multiplication rKN in Eq. (8) remains; and in case tation angles are a little smaller than half, 2αn+1 < αn (while the r = 1 even this multiplication can be saved (Walther 1971); the start circular modes has here some redundancy 2αn+1 > αn, and the lin- vector is initialised with (x0, y0) = (KN , 0). ear mode is exact 2αn+1 = αn). With this sequence the convergence With these basic equations, the CORDIC algorithm can com- is overall. From now on, we use the variable name n for iteration pute the sine and cosine function. Given an input angle ϕ, we can number and kn for the shift sequence. approach it in each iteration with the condition

+1 θn <ϕ σn+1 =  P (12)  1 else. 3 CORDIC DOUBLE ITERATIONS FOR KEPLER’S −  EQUATION The Cartesian representation is propagated simultaneously with the same rotation directions σn via Eqn. 10 and 11. So when To apply CORDIC to Kepler’s equation, we proposed in Ze18 the θ ϕ, then x cos ϕ and y sin ϕ. Figure A1 illustrates this modified condition for the rotation direction4 n → n → n → process.P +1 En e sin En < M The convergence range can be derived when performing only σn+1 =  − (14) N (n 1)  1 else. positive rotations resulting in = θn = n atan 2− − 1.7433 =  n 1 | | → − 99.88◦ for N . An initialP rotation withP 90◦, which needs no  →∞ For readability and with respect to Eq. (1), we renamed θn with scale correction, can extend the range to 189.88◦. En and ϕ with M compared to Sect. 2. P It is also possible to calculate atan(x, y). So given x and y, the The decision in Eq. (14) is exact within the working precision. angle ϕ of this vector is wanted. In this mode, called vectoring, the However, the term sin En is not accessible in true CORDIC, be- component yn is driven towards zero. cause of the scale change. And a simultaneous compensation would require a multiplication in every iteration. When the start vector

2 n k The q-Pochhammer symbol is defined as (a; q)n = (1 aq ). Thus k=0 − − 2 ∞ k Q 1 3 the product series Kc = = (1+4− ) is the special case ( 1; ) The sequence is related to http://oeis.org/A003462. k 0 − 4 ∞ ≈ 2.71182. Likewise, in hyperbolicQ mode there occurs ∞ (1 4 k) = 4 For reasons of uniformity with code implementation, we list the positive k=1 − − ( 1 ; 1 ) 0.68854, which is also a special case of the EulerQ product. case first compared to Ze18. 4 4 ∞ ≈

MNRAS 000, 1–?? (2020) Solving KE with CORDIC double iterations 3

M the subsequent rotations. In case of single rotations, all subsequent E4 rotations α 2α can recover α . But the small redundancy E E5 n+1 n n+1 n M3 ≈ E3 in single rotationsP (in m = 1) is generally insufficient to compensate E2 3 y 3 M - 5 E yet the initiating departure. Double rotations, however, introduce 3

E sin M redundancy, which is the key to overcome the convergence prob- e 4 - M 3 E1 2 E lem, but also the price to be paid for the approximation. 3 E x1,y1 sin

3 y e 4 HYPERBOLIC MODE M 1 Analogous to Ze18, we study whether our double iteration algo- E0 M0 rithm is also applicable to the hyperbolic Kepler’s equation (HKE)

Figure 2. Evolution of the vector x , y (colour-coded arrows), when drop- n n M = e sinh H H, (18) ping the scaling term cos αn and using double rotation. The example is for − e = 0.9 and M = 2.08 0.9 sin 2.08 1.294 (thus E = 2.08). With appropri- − ≈ where e 1. Replacing the trigonometric terms by the hyperbolic ate prescaling by KN , the vector approaches length e (grey arc) after n = N analogues,≥ Eqn. 7 and 17 become (black arrow) and converges towards the E. The discrepancy between exact intermediate mean anomaly Mn = En e sin En (crosses) and approximated 1 − tanh αn = (19) mean anomaly En yn (open circles) is colour-coded with dotted arcs on the 2kn − grey unit circle. Iteration n = 1 and n = 2 (or three and four) have the same +1 yn Hn

Table 1. Examples for bit operations (arithmetic shift, xor, and) in two’s 0.1

complement with 8 bits. 3.1 1.65

[rad] e = 0.9 input operation output decimal expression n 0.05

E E(M)

1.6 E 00000001 <<2 00000100 1 22 = 4 16 × M + y 3.05 11111111 11111100 1 22 = 4 16

− × − 0 00001011 >>2 00000010 11 / 4 = 2 11110101 11111101 11 / 4 = 3

− − 0.005 0.005 0.005

01111111 ^( 1) 10000000 127 ( 1) = 128 E(M) −

− ∧ − −

01111111 ^0 01111111 127 0 = 127 n ∧ E 11110101 & 11110101 = ( 1) 11 & ( 1) 11 -0.005 -0.005 -0.005 − − − − 0 0.005 0.01 0.72 0.76 2.95 3 3.05 3.1 mean anomaly M [rad] 1.8 1019 numbers. The location of the virtual “binary point” de- · Figure 3. Top: CORDIC output after 16 double iterations (k16 = 8, 29 frac- pends on the convention for the mapping function. tional bits) for eccentricity e = 1 and three mean anomaly regions. Com- Most systems operate with two’s-complement arithmetic. That pared to the exact solution E (black), E16 is a step function (blue), while means, for signed integers the leading bit is preserved to distinguish M +y16 (red) has slopes. Bottom: The absolute residuals are smaller than 7 positive and negative numbers. The bit is set for negative numbers. 2− = 0.0078125. In particular, 1 is represented by setting all bits (Table 1, second row). There is− one more negative number than positive numbers Alternatively, one can also start with one scale-free pre- (e.g. 128 vs. 127 in 8 bit systems). − rotation with α = 90 = τ in exchange for two rotations with α = A multiplication or a division by a power of two (2n) is done 1 ◦ 4 1 α = 45 (k = k = 0) quickly by a bitshift to the left or right, respectively. (Similarly, in 2 ◦ 1 2 n decimal system a division by 10 is just a left shift of the decimal +1 E0 e sin E0 0 and therefore requires two subtractions (and − n n resented in floating-point format as specified in IEEE 754 (IEEE one comparison). It can be advantageous to reformulate this as 2008). Here the first bit is the sign bit, followed by the exponent tn +yn > 0 with tn = M En and t0 = M. This saves one subtraction bits and finally the mantissa. (and one variable i.e. memory− access) in each iteration and is possi- One possibility to apply CORDIC is to simply convert the ble, because M is a fixed input and En is needed only in the compar- float numbers to fixed-point representation and to continue with ison during the iterations. This accumulation is a common practice Sect. 5.1. This concept is realised in Code B.1 and in Code B.2. in CORDIC algorithms, where for e = 0 (so yn = 0) the condition If the algorithm should be still carried out with floating-point tn > 0 remains. At the end the eccentric anomaly can be recovered format, one can consider the following circumstances. A multipli- with EN = M +yN . cation with 1 is just a flip of the sign bit. A division by 2k is just Figure 3 shows that the output M +y differs a bit from E , − N N a subtraction of k from the exponent. However, one has to catch but both are within the nominal limits. Positive and negative resid- the possibility of an exponent underflow. Also, the addition of two uals appear balanced with no visible bias for both cases; a property floating point numbers requires an internal normalisation of the ex- related to the two-sided design of the proposed CORDIC algorithm ponent. Hence addition is not much faster than multiplication and (see also Fig. 3 of Ze18). Contrary, one-sided algorithms will be both usually slower than fix-point addition. biased. Such a variant was outlined in Ze18 (Eq. (13)).

5.3 Shift and angle sequence 6 ACCURACY AND PERFORMANCE STUDY When we chose as shift sequence k = 0, 0, 1, 1, 2, 2,... for the cir- n 6.1 Accuracy of the fix-point algorithm cular mode, then double iterations cover a range of E < N α | | n=1 n ≈ 199.88◦, that means about twice as wide as in single rotations.P The We forward calculated with Eq. (1) 1 000 (M(E), E) pairs, with M 2 26 τ scale factor becomes Kdbl K = 0.368 76. sampled log-uniformly over [10− , 2 ]. Here E might be seen as ≈ ∞ MNRAS 000, 1–?? (2020) Solving KE with CORDIC double iterations 5

-6 e = 1.0 3

-E| 10

53 e = 0.999 999 999 9 2.5 10-8 e = 0.5 -16 = |E

δ 2´10 /E 2 -10 -61 √ 3 2 10 2 ´ 6/M -61 2 1.5 10-12

runtime t [ms] 1 10-14 CORDIC-dbl flt Newton + math.h 0.5 CORDIC-dbl int Newton CORDIC-like 10-16 absolute deviation 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 -6 e = 1.0 -E| 10 eccentricity e

53 e = 0.999 999 999 9 10-8 e = 0.5 -16 Figure 6. Execution time as function of eccentricity for various algorithms. = |E

δ 2´10 /E M E -10 -61 √ 3 2 For solid curves, was distributed uniformly, for dashed curves . 10 2 ´ 6/M

-14 -12 10 -12 ner. The errors decrease quite quickly with eccentricity. The in- -10 -14 crease of error for M ' 0.1 should be related to the simplified con- 1 - 10 10 e = 1 - 10 1 - 10 version between floating-point and fix-point, which was used for 0.99999999 0.999999 0.9999 10-16 0.99 the preparation of the look-up table and the input data. The man- absolute deviation tissa of 64 bit floating-point numbers holds only 53 bits. 10-25 10-20 10-15 10-10 10-5 0.01 1 Finally, we remark the need for some spare bits. This is inves- mean anomaly M [rad] tigated here with a short bit system. We divided the range from 0 to 4rad into 212 = 4096 mean anomalies having thus 10 fractional Figure 4. Accuracy for the fix-point (top) and floating-point algorithm (bot- 10 17 bits (2− ). Then we limited our fix-point algorithm also to 10 frac- tom). For visibility, zero deviations (δ = 0) were lifted to δ = 2 10− . · tional bits (cf. L 12 in Code B.1) and iterated until the last bit, i.e. kN = 10 (N = 17). Still, the residuals in Fig. 5 are overall limited to 6 1 2− (with a slight bias towards more negative deviations) for this short bit algorithm. So sometimes the last four iterations yield no 2-6 0.8 improvement. Therefore, some trailing bits ( log2 kN ) are advis- ff ∼ 2-8 able to bu er accumulating truncation errors.

0.6 -10 - E [rad]

2 10 -2-10 6.2 Accuracy of the floating-point algorithm 0.4

eccentricity e -8 Overall, the error behaviour of the floating-point algorithm with -2 0.2 double iteration (Fig. 4) is very similar to the CORDIC-like al- -2-6 residuals E gorithm from Ze18 (Fig. 6), who already explained the functional forms arising for the different eccentricities. Compared to the fix- 0 0 0.5 1 1.5 2 2.5 3 point algorithm, the floating-point algorithms have a better accu- mean anomaly M [rad] racy in the corner, where they profit from the higher precision to represent small numbers. On the other hand, the fix-point version Figure 5. Residual map for a fix-point algorithm with 10 fractional bits and 5 has better accuracy at about M & 10− . = = iterated to the last bit (kN k17 10). The colour-coding is log-symmetric. The algorithm used for Fig. 4 employed the shift sequence starting with kn = 0, 0, 1, 1, 2, 2,... and without accumulation. For small mean anomalies, the rotation will go back and forth to zero the true value. Then we injected M into our algorithms to solve the in the first iterations, thus keeping properties of the one-sided al- inverse problem E(M). The top panel of Fig. 4 shows the depen- gorithms in Ze18. However, the two optimisations suggested in dency of the accuracy as function of M and e for Code B.1. The Sect. 5.3 and Sect. 5.4 will worsen the accuracy to a level com- accuracy becomes critical in the so called corner of KE at M = 0 parable to the fix-point algorithm, because for floating-point the for e = 1. Here the function behaves likes a cubic root E √3 6M ≃ order of additions does matter (while in fix-point format this does and the derivative becomes infinite. When using 61 bits for the not affect the precision). Both cases lead to the additions of small 61 19 binary fraction (Sect. 5.1), the step size is 2− = 4.3 10− rad. · numbers to big numbers, and thus floating-point numbers cannot This is the resolution for M. The value of the eccentric anomaly utilise the better representation of small numbers. 3 19 6 is ∆E = E(M = ∆M) = √6 4.3 10− = 1.4 10− . This point marks about the largest error and· is indicated· in the· figure. 1 The general error relation is dE = 1 e cos E dM, which follows 6.3 Performance − from Eq. (1). For e = 1, it becomes dE 1 3 6 dM and since ∆M ≃ 3 q M2 We have shown that Kepler’s equation can indeed be solved with 2/3 is constant, the errors declines as M− . The residuals matches the shift-and-add algorithm CORDIC using minor modifications. those theoretical limits and thus validates∝ our implementation. Thus, the pressing question is: how fast is the algorithm? We remind, that Fig. 4 is an extreme magnification of the cor- We implemented the algorithms (as well as the comparison

MNRAS 000, 1–?? (2020) 6 M. Zechmeister solvers) in plain C and measured with the Python’s timeit feature input and the direction decision of the various modes the execution times to solve Eq. (1) 10 000 times and for 21 ec- rotating t > 0 t = ϕ (31) centricities between 0 and 0.999 999. The maximum shift index in n 0 the CORDIC versions was kN = 28 corresponding to a precision of vectoring yn > 0 t0 = 0 (32) 28 9 − 2− = 3.7 10− . The mean anomalies M were distributed uniformly arcsine ϕ+y > 0 t = 0 (33) · − n 0 between 0...180◦ (case U(M)). In a second test, mean anomalies KE tn +yn > 0 t0 = M (34) were generated, whose eccentric anomalies E were distributed uni- HKE t y > 0 t = M, (35) formly (case U(E)). This gives some more weight to small mean − n − n 0 − anomalies and shall probe whether there is a dependency on M, which includes the arcsine mode for completeness. The Keplerian since some solvers are slower in the corner. modes appear as mixture of rotating and vectoring, and we call it Figure 6 shows the results. The CORDIC double rotation al- “keplering”. 5 gorithms performed Ndbl = 56 iterations . The runtime is indepen- Rotation and vectoring handle the different coordinate systems dent of eccentricity and the mean anomaly. The fix-point version via the parameter m, which appears in Eq. (10), but not in Eqn. 31– is about 2.2 times faster than the floating-point version. Thus there 33. Now, to unify the Keplerian modes, we can suggest to introduce is a noticeable benefit. However, it is also “only” a factor of two, m in Eqn. 34 and 35 as since the floating-point code needs two multiplications in each it- erations. This shows that multiplications have been optimised over GKE m(tn +yn) > 0 t0 = mM. (36) the last years in current computer processing unit (CPU), crowded For the linear mode with m = 0, this unification appears point- out CORDIC algorithms from those architectures. less, as the outcome will not depend on M at all. Well, if we asso- For comparison, we show the CORDIC-like version from ciate the linear Keplerian mode with the case of radial trajectories, Ze18, which is a floating-point algorithm. We used a one-sided it would indeed complete the picture. KE and HKE solve for a time variant, which is faster for small E. This may partly explain the per- dependent auxiliary angle, but in radial cases the angle is fixed and U formance increase for the case (E). Moreover, this version used time independent. an if-branch, thus branch prediction may affect the results, too. In Another possibility for unification are base angles mαn (thus any case, since the CORDIC-like version needs only N = 29 itera- negative for HKE) along with the condition tn +myn > 0 and t0 = M. tions, it is faster than its double iteration companion. Yet, the new Then for m = 0, all base angle would be zero. fix-point version is even 1.5 times faster. Finally, we see that the speed of the fix-point version com- pares well with Newton’s method, which used the start guess E0 = 7.2 Barker’s equation M +0.85e. The cosine and sine functions were called from the stan- We also considered that a linear mode extension might be as- dard math.h library. Since the source of their implementation can be hard to track down, a self-programmed sine and cosine func- sociated with parabolic orbits. This special case is handled with tions were tested as well. The runtime increases with eccentricity. Barker’s equation (Colwell 1993), which is given by At e = 1, the U(E) case take a bit longer than the U(M) case, which 1 M = D + D3, (37) indicates slower convergence in the corner (at M 0). 3 ≈ The performed tests can only serve as an orientation. There where D is an auxiliary variable, similar to the eccentric anomaly are many aspects, which can alter the outcome. Furthermore, the E and H, and related to true the anomaly by algorithm in Ze18 yields as output also the terms cos E and sin E, ν which will be needed for subsequent computation. Our CORDIC tan = D. (38) algorithm delivers e cos E and e sin E. In case division by e appears 2 disadvantageous in particular for small eccentricities, one can re- Equation (37) is a cubic equation, whose explicit solution is compute the terms from E or alternatively propagate them in paral- often given as lel with the algorithm. 1 D = B (39) − B

3 = + √ 2 + = 3 with B qW W 1(Meire 1985) and W 2 M. Computing this 7 FURTHER DISCUSSION in a CORDIC framework requires five operations: division, hy- potenuse, as well as exp, div, and ln for the cubic root (3M are 7.1 Unifying the Keplerian CORDIC modes simply three additions). The equivalent solution with hyperbolicus functions CORDIC has three coordinate systems: linear, circular, and hyper- bolic. We have extended here the circular and hyperbolic mode to asinh 3M D = 2 sinh 2 (40) the elliptic and hyperbolic case of Kepler’s equations. Thus, the 3 question is nearby, whether there is an analog extension for the lin- seems to be less known (Sect. C). It requires at most five CORDIC ear mode. To address this, we summarise the main difference in the operations: sinh, div, and, for the arcsine hyperbolicus (Eq. (A3)), mul, cathetus, and atanh. When using the double iteration variant for the arcsine hyperbolicus, then the total costs are about four 5 CORDIC cycles. A further reduction could be done by optimising For simplicity, we just truncated the shift sequence of the kN = 61 version, the division by three (Sect. A2). which performs double iterations until k 26, thus Ndbl = 2 27+(28 26) = ≤ · 14 − 28 56. But already for k > 14, scale corrections are smaller than 4− = 2− . Therefore, Barker’s equation can be solved with a few nested Thus N = 2 15+(28 14) = 44 would be sufficient, too, promising a 21 % CORDIC operations, but we don’t see a possibility to tackle it more dbl · − shorter runtime. directly with CORDIC.

MNRAS 000, 1–?? (2020) Solving KE with CORDIC double iterations 7

8 SUMMARY In this work, we presented to our knowledge for the first time a shift-and-add algorithm to solve KE. The features of the algorithm are usage of most basic computer operations (addition, bitshift, xor)• small code size and short lookup table • independent of math libraries • adjustable precision • runtime independent of mean anomaly M and e. • We require only two minor modifications to the normal CORDIC algorithm, which are the modified direction decisions (Eq. (17)) 1 and repeating each iteration once (double iterations for k 2 kN ). The modifications constitute a new CORDIC mode, which≤ we call Keplerian mode or keplering. It is mixture of rotating and vectoring and handles the eccentric and hyperbolic case of Kepler’s equation. From the perspective of CORDIC, solving Kepler’s equation appears twice as expensive as the computation of a sine function or about as expensive as the arcsine function, which both are parame- ter less, while KE has the parameter e. Albeit we could eliminate all multiplications from the iteration loop, this is hardly honoured by current desktop computers, which Figure A1. Examples of CORDIC rotations for circular (blue, m = 1), lin- nowadays have sophisticated multiplier units. Thus CORDIC algo- ear (green, m = 0), and hyperbolic mode (red, m = 1). The target angle ϕ − k rithms have been displaced from computer . While our (dashed line) is approached through rotations with αn = atan 2− . The vec- CORDIC KE solver is well competitive with Newton’s method, tors change their length with each iteration. its full potential would become available on architectures, which favour CORDIC methods, such as old or cheap devices. Also, a wider revival of CORDIC might be possible in the future. Volder J. E., 1959, IRE Transactions on electronic computers, EC-8, 330 Walther J. S., 1971, in Proceedings of the May 18-20, 1971, Spring Joint Computer Conference. AFIPS ’71 (Spring). ACM, New York, NY, USA, pp 379–385, doi:10.1145/1478786.1478840, http://doi.acm. ACKNOWLEDGEMENTS org/10.1145/1478786.1478840 I thank Hanno Rein for refereeing the paper, Trifon Trifonov for Zechmeister M., 2018, A&A, 619, A128 manuscript reading, and Albert Chan for helpful discussion about Barker’s equation (Eq. (40), Sect. C). This work is supported by the Deutsche Forschungsgemeinschaft under DFG RE 1664/12-1 and Research Unit FOR2544 “Blue Planets around Red Stars”, project APPENDIX A: NOTES ON THE CORDIC METHOD no. RE 1664/14-1. Table A1 summarises the general output of CORDIC for the three coordinate systems m and the two operation modes. From this one can derive elementary functions using specific inputs as given in DATA AVAILABILITY Table A2. For instance, the exp-function is obtained with x0 = Kh No new data were generated or analysed in support of this research. and y0 = Kh in hyperbolic vectoring (which is even simpler than adding the output of x0 = Kh and y0 = 0: exp x = cosh x+sinh x). The logarithm is obtained from the atanh-function via the identity6 REFERENCES a a b Baykov V., 1972, PhD thesis, Leningrad State Univ. of Electrical Eng., ln = 2 atanh − . http://baykov.de/CORDIC1972.htm b a + b

Colwell P., 1993, Solving Kepler’s equation over three centuries. Willmann- So to compute ln a, one has to set b = 1, thus x0 = a+1 and y0 = Bell, Richmond, VA a 1. The final multiplication with two is again a bitshift. The same Fukushima T., 1997, Celestial Mechanics and Dynamical Astronomy, 68, − K2 = + h 121 mode provides simultaneously the square root √a. With x0 a 4 K2 IEEE 2008, IEEE Std 754-2008, pp 1–58 h 7 and y0 = a 4 , the output is directly available in xn. The square Lang T., Antelo E., 2000, Journal of VLSI signal processing systems for root is independent− of t, so this channel can be omitted. signal, image and video technology, 25, 19 For completeness, we list further important functions that can Meire R., 1985, Journal of the British Astronomical Association, 95, 113 Muller J., 2006, The CORDIC Algorithm. Birkhäuser Boston, Boston, MA, pp 133–156, doi:10.1007/0-8176-4408-3_7, https://doi.org/ 10.1007/0-8176-4408-3_7 6 a exp ln √a/b expln √a/b √a/b √b/a a b tanh ln = − = − = ˘ q b exp ln √a/b + expln √a/b √a/b+ √b/a a−+b Payne M. H., Hanek R. N., 1983, SIGNUM Newsl., 18, 18âA¸S19 2 7 Kh 1 1 Takagi N., Asada T., Yajima S., 1991, IEEE Transactions on Computers, = 0.364512. Usually, x0 = a+ and y0 = a are proposed to get √a. 4 4 − 4 40, 989 But this requires a post-multiplication with 1 . Kh

MNRAS 000, 1–?? (2020) 8 M. Zechmeister

Table A1. General input and output triples for various CORDIC modes (Walther 1971).

type m αn αn kn Km, rotating (t 0) vectoring (y 0) P ∞ → →

x x x x linear 0 2 kn 2 0...N 1 y y+xt y 0 − ⇒ ⇒ t 0 t t+y/x

2 2 Kc x x cos t y sin t Kc x x +y kn − p circular 1 atan 2 1.743 286 0...N 0.607 253 Kcy x sin t+y cos t Kcy 0 − ⇒ ⇒ t 0 t t+atan(y, x)

K x x cosh t+y sinh t K x x2 y2 h h − hyperbolic 1 atanh 2 kn 1.118 173 1...N* 1.207 497 K y x sinh t+y cosh t K y 0p − − h ⇒ h ⇒ t 0 t t+atanh(y, x)

(*) Notes. Specific shift values kn must be repeated, see Eq. (13).

Table A2. Input and output triples for some elementary CORDIC function 1.8 CORDIC arcsine m (-1 circular, 0 linear, 1 hyperbolic). 1.6 asin(x) k = 0,1,2,3,... (single rotation) 1.4 n k = 0,0,1,1,2,2,... m x0 y0 t0 xN yN tN n ⇒ 1.2 kn = 0,1,1,2,2,... 1 Kc 0 ϕ t 0 cos ϕ sin ϕ 0 1 k = 1,1,2,2,... → n 1 Kh 0 ϕ t 0 cosh ϕ sinh ϕ 0 0.8 − → 1 K K ϕ t 0 exp ϕ exp ϕ 0 − h h → 0.6 1 1 b 0 y 0 1 √1+b2 0 atan b → Kh 0.4 1 1 b 0 y 0 1 √1 b2 0 atanh b − → Kh − 0.2 1 a+b a b 0 y 0 2 √ab 0 1 ln a − − → Kh 2 b 0 1 a+1 a 1 0 y 0 2 √a 0 1 ln a − − → Kh 2 0 0.2 0.4 0.6 0.8 1 K2 K2 1 a+ h a h 0 y 0 √a 0 1 ln 4a 4 4 2 K2 − − → h Figure A2. Output of the arcsine mode. Single rotations (red) have overall 0 a 0 ϕ t 0 a ϕ a 0 → · convergence problems. The three double rotation sequences (green, cyan, 0 a b 0 y 0 a 0 b/a → blue) converge in a limited range. Full convergence is achieved by three chained CORDIC operations or double iterations with quadrant check or scale correction (black, barely visible in right corner). be derived with additional subsequent CORDIC calls sin x sinh x tan x = tanh x = (A1) the problem with double iterations, meaning each iterations is exe- x x cos cosh cuted twice. However, just using the sequence k = 0, 0, 1, 1,... con- cos x cosh x n cot x = coth x = (A2) verges only for x < 0.73, because after the third rotation the vector sin x sinh x cannot recover| from| an excursion into an adjacent quadrant. The asin x = atan(x, √1 x2) asinh x = atanh(x, √x2 1) (A3) sequences k = 0, 1, 1, 2, 2,... or k = 1, 1, 2, 2,... have a larger con- − − n n acos x = atan( √1 x2, x) acosh x = atanh(x, √x2 +1). (A4) vergence range (related to a smaller total scale corrections). But − only an additional quadrant check (xn > 0) in each iteration gives For instance, tan x requires one division as a second call, full convergence including the arcsine-corner. We remark that Ke- whereas sin x and cos x come simultaneously from circular rota- pler’s equation is bijective and thus does not suffer from quadrant tion. The arcsine is discussed in the next section. confusion. Takagi et al. (1991) developed an alternative method, which is also called double-CORDIC iteration and employs an auxiliary A1 The arcsine function variable, which is scale corrected on the fly. Lang & Antelo (2000) From Eq. (A3) and Table A1, it can be seen that asin x can be also uses an on the fly correction, but do not require double rota- computed with hyperbolic vectoring, which provides the cathetus tions. √1 x2, and circular vectoring, which executes the atan function us- In summary, there are different ways to compute the arcsine. − ing two arguments and saves the division. Note, the cathetus needs They may require modifications and the total costs correspond to a multiplicative scale correction (but see Sect. A2), implying three about 2–3 normal CORDIC cycles. CORDIC calls in total. A direct way to compute the arcsine is to change the direction A2 Multiplication and division with a constant decision as in Eq. (33) and to drive the yn component of the vec- tor towards the input argument. However, as explained in Muller The standard CORDIC algorithm requires a scale correction. The (2006), the missing scale correction can lead to wrong decision and scale correction does not matter in the output tN , where the factor erroneous output as illustrated in Fig. A2. Baykov (1972) solved cancels out in the ratio y/x in all modes. But a pre- or post-scaling

MNRAS 000, 1–?? (2020) Solving KE with CORDIC double iterations 9

Code B.1: Fix-point algorithm with CORDIC double iteration for Code B.2: Fix-point algorithm with CORDIC double iteration for Kepler’s equation. hyperbolic Kepler’s equation.

1 from math import atan, tau 1 from math import atanh, frexp, log 2 2 ln2 = log(2) 3 kmax = 53 # largest shift value 3 4 R = 1 << 61 # binary point 4 kmax = 53 # largest shift value 5 5 R = 1 << 61 # binary point 6 ak = [] # atan lookup table 6 7 K = 1 # scale correction factor 7 ahk = [] # atanh lookup table 8 for k in range(kmax+1): 8 Kh = 1 # scale correction factor 9 ak += [(atan(2**-k), k)] 9 for k in range(1, kmax+1): 10 if 2*k <= kmax: 10 ahk += [(atanh(2**-k), k)] 11 K /= 1 + 4**-k 11 if 2*k <= kmax: 12 ak += ak[-1:] 12 Kh /= 1 - 4**-k 13 13 ahk += ahk[-1:] 14 KR = K * R 14 15 i64_ak = [(round(a*R), k) for a,k in ak] 15 KhR = Kh * R 16 16 i64_ahk = [(round(a*R), k) for a,k in ahk] 17 def i64_Ecs(M, e): 17 18 t = M - tau*round(M/tau) 18 def i64_Hcs(M, e): 19 t = round(t * R) 19 m = max(0, frexp(M/e)[1]) 20 x, y = round(KR*e), 0 20 eK = round(e * KhR) >> 1 21 for a,k in i64_ak: 21 x = (eK<>m) 22 s = t+y >> 63 22 y = (eK<>m) 23 t-=s^s+a 23 if M < 0: m, y = -m, -y 24 x, y = x - (s^s+(y>>k)),\ 24 t=M+m*ln2 25 y + (s^s+(x>>k)) 25 t = -round(t * R) 26 return M+y/R, x/R, y/R 26 for a,k in i64_ahk: 27 s = -t-y >> 63 28 t-=s^s+a 29 x, y = x + (s^s+(y>>k)),\ 30 y + (s^s+(x>>k)) is needed, when using the output xN or yN in the modes m = 1 or 1. 31 return y/R-M, x/R, y/R − CORDIC provides multiplication and division, and their exe- cution requires a full CORDIC cycle. However, if the multiplicator is known in advance and often needed, the efforts can be reduced. location of the binary point (Sect. 5.1)8 defined in line L 4. The Let’s consider first a division by 3, which occurs in Eq. (40). lookup table (L 6, L 9) stores the tuples of the basis angles and the 1 The binary representation of is 0.0101010101 ..., thus its multi- 1 k 3 2 shift sequence (α = atan , k ). If the correction term 4 in Eq. (9) 2 4 6 8 n 2kn n plication can be done as (2− +2− +2− +2− +...). The correspond- kN is numerical yet larger than 2 (L 10), i.e. k <= kN /2, the required ing shift-and-add algorithm (essentially a binary multiplier) can be scale correction is accumulated (L 11) and the last table entry is seen as a CORDIC simplification, which needs only half iterations, repeated (L 12). Then the table and the scale factor are converted because the iterations with odd shift values can be omitted, since to fix-point (64 bit integers, L 14–L 15). The input of algorithm the linear mode is scale free. Moreover, it does not need direction are floating-point numbers. Thus after a simple range reduction in decisions and the t channel, because the multiplicator is encoded in L 18 (note that there are more accurate range reduction algorithms, the shift sequence. The bit shifts can be done in parallel. Payne & Hanek 1983) and pre-scaling with K, the start vector is Multiplications with other important constants, in partic- converted to fix-point (L 19–L 20). The multiplication e K (L 20) ular the scale correction K 0.100 110 110 111 010 and K ∗ c ≈ 2 h ≈ is the only true multiplication (but see Sect. A2). (The multipli- 1.001 101 010 001 1112, could be implemented in a similar way. cation with R and integer conversion can be done with mantissa extraction and bit shift). The CORDIC iterations start in L 21. The comparison of Eq. (17) is done by an addition and a subsequent arithmetic right bit shift, which extracts the sign bit (L 22). The variable s is either 0 APPENDIX B: CODE ILLUSTRATION or -1. The s^(s+x) (alternatively (x^s)-s) modi- In the following, we document a fix-point implementation of the fies the sign of x in a branchless fashion. In case of s=0 (σn = 1), the variable x is not modified. In case of s=-1 (σ = 1), the result is an CORDIC double iteration. The pure Python code shall illustrate the n − functionality and low complexity. Further variants (floating-point) arithmetic negation (-x, see also Table 1). This can be directly im- and other programming languages, in particular more performant, plemented in hardware (-substractor). Line L 23 accumulates low-level C, are maintained online⋆. Code B.1 implements the algorithm described in Sect. 3. In this snippet, the largest shift is hardcoded in line L 3 and thus sets 8 Because Python integers have arbitrary precision, calculations beyond 64 the number of iterations. The precision depends furthermore on the bits would be possible by increasing this number.

MNRAS 000, 1–?? (2020) 10 M. Zechmeister the angles as described in Sect. 5.4. Finally, the fix-point numbers are converted back to floating-point (L 26). As an example, calling the function with i64_Ecs(2-sin(2), 1) should return the triple (2.0, -0.41614683654714246, 0.9092974268256817). The code for the hyperbolic Kepler equation (Sect. 4, Code B.2) is very similar. It requires the hyperbolic arctangent (L 10). The range extension is done in L 19–L 22.

APPENDIX C: BARKER’S EQUATION The equivalence of Eqn. 39 and 40 follows from

1 ln B ln B B = e e− = 2 sinh ln B (C1) − B − 3 asinh W = 2 sinh ln W + √W2 +1 = 2 sinh . (C2) q 3 The last step employed the identity (e.g. Fukushima 1997, Eq. (73)) asinh x = ln x+ √x2 +1 , (C3)   which itself can be verified with the substitution x = sinh t t = ln sinh t + sinh2 t+1 = ln sinh t + cosh2 t (C4)  p   p  = ln (sinh t +cosh t) = ln et = t. (C5)

MNRAS 000, 1–?? (2020)