4 // Copyright (c) 2000 - 2005, Intel Corporation
5 // All rights reserved.
7 // Contributed 2000 by the Intel Numerics Group, Intel Corporation
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
13 // * Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
16 // * Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
20 // * The name of Intel Corporation may not be used to endorse or promote
21 // products derived from this software without specific prior written
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
28 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
32 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
33 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 // Intel Corporation is the author of this code, and requests that all
37 // problem reports or change requests be submitted to it directly at
38 // http://www.intel.com/software/products/opensource/libraries/num.htm.
41 //==============================================================
42 // 02/02/00 Initial Version
43 // 04/04/00 Unwind support added
44 // 08/15/00 Bundle added after call to __libm_error_support to properly
45 // set [the previously overwritten] GR_Parameter_RESULT.
46 // 07/07/01 Improved speed of all paths
47 // 05/20/02 Cleaned up namespace and sf0 syntax
48 // 11/20/02 Improved speed, algorithm based on exp
49 // 03/31/05 Reformatted delimiters between data tables
52 //==============================================================
53 // double expm1(double)
55 // Overview of operation
56 //==============================================================
57 // 1. Inputs of Nan, Inf, Zero, NatVal handled with special paths
60 // Result = x, computed by x + x*x to handle appropriate flags and rounding
62 // 3. 2^-60 <= |x| < 2^-2
63 // Result determined by 13th order Taylor series polynomial
64 // expm1f(x) = x + Q2*x^2 + ... + Q13*x^13
67 // Here we know result is essentially -1 + eps, where eps only affects
68 // rounded result. Set I.
71 // Result overflows. Set I, O, and call error support
73 // 6. 2^-2 <= x < 709.7827 or -48.0 <= x < -2^-2
74 // This is the main path. The algorithm is described below:
76 // Take the input x. w is "how many log2/128 in x?"
79 // x = n log2/128 + r + delta
81 // n = 128M + index_1 + 2^4 index_2
82 // x = M log2 + (log2/128) index_1 + (log2/8) index_2 + r + delta
84 // exp(x) = 2^M 2^(index_1/128) 2^(index_2/8) exp(r) exp(delta)
86 // Get 2^(index_1/128) from table_1;
87 // Get 2^(index_2/8) from table_2;
88 // Calculate exp(r) by series by 5th order polynomial
89 // r = x - n (log2/128)_high
90 // delta = - n (log2/128)_low
91 // Calculate exp(delta) as 1 + delta
95 //==============================================================
99 // expm1(+qnan) = +qnan
100 // expm1(-qnan) = -qnan
101 // expm1(+snan) = +qnan
102 // expm1(-snan) = -qnan
104 // expm1(-inf) = -1.0
105 // expm1(+inf) = +inf
107 // Overflow and Underflow
108 //=======================
109 // expm1(x) = largest double normal when
110 // x = 709.7827 = 40862e42fefa39ef
112 // Underflow is handled as described in case 2 above.
116 //==============================================================
117 // Floating Point registers used:
119 // f9 -> f15, f32 -> f75
121 // General registers used:
124 // Predicate registers used:
128 //==============================================================
167 GR_Parameter_RESULT = r39
168 GR_Parameter_TAG = r40
203 fMIN_DBL_OFLOW_ARG = f45
204 fMAX_DBL_MINUS_1_ARG = f46
205 fMAX_DBL_NORM_ARG = f47
250 //==============================================================
255 // ************* DO NOT CHANGE ORDER OF THESE TABLES ********************
257 // double-extended 1/ln(2)
258 // 3fff b8aa 3b29 5c17 f0bb be87fed0691d3e88
259 // 3fff b8aa 3b29 5c17 f0bc
260 // For speed the significand will be loaded directly with a movl and setf.sig
261 // and the exponent will be bias+63 instead of bias+0. Thus subsequent
262 // computations need to scale appropriately.
263 // The constant 128/ln(2) is needed for the computation of w. This is also
264 // obtained by scaling the computations.
266 // Two shifting constants are loaded directly with movl and setf.d.
267 // 1. fRSHF_2TO56 = 1.1000..00 * 2^(63-7)
268 // This constant is added to x*1/ln2 to shift the integer part of
269 // x*128/ln2 into the rightmost bits of the significand.
270 // The result of this fma is fW_2TO56_RSH.
271 // 2. fRSHF = 1.1000..00 * 2^(63)
272 // This constant is subtracted from fW_2TO56_RSH * 2^(-56) to give
273 // the integer part of w, n, as a floating-point number.
274 // The result of this fms is fNfloat.
277 LOCAL_OBJECT_START(exp_Table_1)
278 data8 0x40862e42fefa39f0 // smallest dbl overflow arg
279 data8 0xc048000000000000 // approx largest arg for minus one result
280 data8 0x40862e42fefa39ef // largest dbl arg to give normal dbl result
282 data8 0xb17217f7d1cf79ab , 0x00003ff7 // ln2/128 hi
283 data8 0xc9e3b39803f2f6af , 0x00003fb7 // ln2/128 lo
285 // Table 1 is 2^(index_1/128) where
286 // index_1 goes from 0 to 15
288 data8 0x8000000000000000 , 0x00003FFF
289 data8 0x80B1ED4FD999AB6C , 0x00003FFF
290 data8 0x8164D1F3BC030773 , 0x00003FFF
291 data8 0x8218AF4373FC25EC , 0x00003FFF
292 data8 0x82CD8698AC2BA1D7 , 0x00003FFF
293 data8 0x8383594EEFB6EE37 , 0x00003FFF
294 data8 0x843A28C3ACDE4046 , 0x00003FFF
295 data8 0x84F1F656379C1A29 , 0x00003FFF
296 data8 0x85AAC367CC487B15 , 0x00003FFF
297 data8 0x8664915B923FBA04 , 0x00003FFF
298 data8 0x871F61969E8D1010 , 0x00003FFF
299 data8 0x87DB357FF698D792 , 0x00003FFF
300 data8 0x88980E8092DA8527 , 0x00003FFF
301 data8 0x8955EE03618E5FDD , 0x00003FFF
302 data8 0x8A14D575496EFD9A , 0x00003FFF
303 data8 0x8AD4C6452C728924 , 0x00003FFF
304 LOCAL_OBJECT_END(exp_Table_1)
306 // Table 2 is 2^(index_1/8) where
307 // index_2 goes from 0 to 7
308 LOCAL_OBJECT_START(exp_Table_2)
309 data8 0x8000000000000000 , 0x00003FFF
310 data8 0x8B95C1E3EA8BD6E7 , 0x00003FFF
311 data8 0x9837F0518DB8A96F , 0x00003FFF
312 data8 0xA5FED6A9B15138EA , 0x00003FFF
313 data8 0xB504F333F9DE6484 , 0x00003FFF
314 data8 0xC5672A115506DADD , 0x00003FFF
315 data8 0xD744FCCAD69D6AF4 , 0x00003FFF
316 data8 0xEAC0C6E7DD24392F , 0x00003FFF
317 LOCAL_OBJECT_END(exp_Table_2)
320 LOCAL_OBJECT_START(exp_p_table)
321 data8 0x3f8111116da21757 //P5
322 data8 0x3fa55555d787761c //P4
323 data8 0x3fc5555555555414 //P3
324 data8 0x3fdffffffffffd6a //P2
325 LOCAL_OBJECT_END(exp_p_table)
327 LOCAL_OBJECT_START(exp_Q1_table)
328 data8 0x3de6124613a86d09 // QD = 1/13!
329 data8 0x3e21eed8eff8d898 // QC = 1/12!
330 data8 0x3ec71de3a556c734 // Q9 = 1/9!
331 data8 0x3efa01a01a01a01a // Q8 = 1/8!
332 data8 0x8888888888888889,0x3ff8 // Q5 = 1/5!
333 data8 0xaaaaaaaaaaaaaaab,0x3ffc // Q3 = 1/3!
334 data8 0x0,0x0 // Pad to avoid bank conflicts
335 LOCAL_OBJECT_END(exp_Q1_table)
337 LOCAL_OBJECT_START(exp_Q2_table)
338 data8 0x3e5ae64567f544e4 // QB = 1/11!
339 data8 0x3e927e4fb7789f5c // QA = 1/10!
340 data8 0x3f2a01a01a01a01a // Q7 = 1/7!
341 data8 0x3f56c16c16c16c17 // Q6 = 1/6!
342 data8 0xaaaaaaaaaaaaaaab,0x3ffa // Q4 = 1/4!
343 data8 0x8000000000000000,0x3ffe // Q2 = 1/2!
344 LOCAL_OBJECT_END(exp_Q2_table)
348 GLOBAL_IEEE754_ENTRY(expm1)
351 getf.exp rSignexp_x = f8 // Must recompute if x unorm
352 movl rSig_inv_ln2 = 0xb8aa3b295c17f0bc // signif of 1/ln2
355 addl rAD_TB1 = @ltoff(exp_Table_1), gp
356 movl rRshf_2to56 = 0x4768000000000000 // 1.10000 2^(63+56)
360 // We do this fnorm right at the beginning to normalize
361 // any input unnormals so that SWA is not taken.
363 ld8 rAD_TB1 = [rAD_TB1]
364 fclass.m p6,p0 = f8,0x0b // Test for x=unorm
365 mov rExp_mask = 0x1ffff
368 mov rExp_bias = 0xffff
370 mov rExp_2tom56 = 0xffff-56
374 // Form two constants we need
375 // 1/ln2 * 2^63 to compute w = x * 1/ln2 * 128
376 // 1.1000..000 * 2^(63+63-7) to right shift int(w) into the significand
379 setf.sig fINV_LN2_2TO63 = rSig_inv_ln2 // form 1/ln2 * 2^63
380 fclass.m p8,p0 = f8,0x07 // Test for x=0
384 setf.d fRSHF_2TO56 = rRshf_2to56 // Form 1.100 * 2^(63+56)
385 movl rRshf = 0x43e8000000000000 // 1.10000 2^63 for rshift
390 setf.exp f2TOM56 = rExp_2tom56 // form 2^-56 for scaling Nfloat
391 fclass.m p9,p0 = f8,0x22 // Test for x=-inf
392 add rAD_TB2 = 0x140, rAD_TB1 // Point to Table 2
395 add rAD_Q1 = 0x1e0, rAD_TB1 // Point to Q table for small path
396 add rAD_Ln2_lo = 0x30, rAD_TB1 // Point to ln2_by_128_lo
397 (p6) br.cond.spnt EXPM1_UNORM // Branch if x unorm
403 ldfpd fMIN_DBL_OFLOW_ARG, fMAX_DBL_MINUS_1_ARG = [rAD_TB1],16
404 fclass.m p10,p0 = f8,0x1e1 // Test for x=+inf, NaN, NaT
405 add rAD_Q2 = 0x50, rAD_Q1 // Point to Q table for small path
410 (p8) br.ret.spnt b0 // Exit for x=0, return x
415 ldfd fMAX_DBL_NORM_ARG = [rAD_TB1],16
417 and rExp_x = rExp_mask, rSignexp_x // Biased exponent of x
420 setf.d fRSHF = rRshf // Form right shift const 1.100 * 2^63
421 (p9) fms.d.s0 f8 = f0,f0,f1 // quick exit for x=-inf
427 ldfpd fQD, fQC = [rAD_Q1], 16 // Load coeff for small path
429 sub rExp_x = rExp_x, rExp_bias // True exponent of x
432 ldfpd fQB, fQA = [rAD_Q2], 16 // Load coeff for small path
433 (p10) fma.d.s0 f8 = f8, f1, f0 // For x=+inf, NaN, NaT
434 (p10) br.ret.spnt b0 // Exit for x=+inf, NaN, NaT
439 ldfpd fQ9, fQ8 = [rAD_Q1], 16 // Load coeff for small path
440 fma.s1 fXsq = fNormX, fNormX, f0 // x*x for small path
441 cmp.gt p7, p8 = -2, rExp_x // Test |x| < 2^(-2)
444 ldfpd fQ7, fQ6 = [rAD_Q2], 16 // Load coeff for small path
451 ldfe fQ5 = [rAD_Q1], 16 // Load coeff for small path
456 ldfe fQ4 = [rAD_Q2], 16 // Load coeff for small path
457 (p7) cmp.gt.unc p6, p7 = -60, rExp_x // Test |x| < 2^(-60)
458 (p7) br.cond.spnt EXPM1_SMALL // Branch if 2^-60 <= |x| < 2^-2
462 // W = X * Inv_log2_by_128
463 // By adding 1.10...0*2^63 we shift and get round_int(W) in significand.
464 // We actually add 1.10...0*2^56 to X * Inv_log2 to do the same thing.
467 ldfe fLn2_by_128_hi = [rAD_TB1],32
468 fma.s1 fW_2TO56_RSH = fNormX, fINV_LN2_2TO63, fRSHF_2TO56
472 ldfe fLn2_by_128_lo = [rAD_Ln2_lo]
473 (p6) fma.d.s0 f8 = f8, f8, f8 // If x < 2^-60, result=x+x*x
474 (p6) br.ret.spnt b0 // Exit if x < 2^-60
478 // Divide arguments into the following categories:
479 // Certain minus one p11 - -inf < x <= MAX_DBL_MINUS_1_ARG
480 // Possible Overflow p14 - MAX_DBL_NORM_ARG < x < MIN_DBL_OFLOW_ARG
481 // Certain Overflow p15 - MIN_DBL_OFLOW_ARG <= x < +inf
483 // If the input is really a double arg, then there will never be "Possible
484 // Overflow" arguments.
487 // After that last load, rAD_TB1 points to the beginning of table 1
491 fcmp.ge.s1 p15,p14 = fNormX,fMIN_DBL_OFLOW_ARG
497 add rAD_P = 0x80, rAD_TB2
498 fcmp.le.s1 p11,p0 = fNormX,fMAX_DBL_MINUS_1_ARG
504 ldfpd fP5, fP4 = [rAD_P] ,16
505 (p14) fcmp.gt.unc.s1 p14,p0 = fNormX,fMAX_DBL_NORM_ARG
506 (p15) br.cond.spnt EXPM1_CERTAIN_OVERFLOW
510 // Nfloat = round_int(W)
511 // The signficand of fW_2TO56_RSH contains the rounded integer part of W,
512 // as a twos complement number in the lower bits (that is, it may be negative).
513 // That twos complement number (called N) is put into rN.
515 // Since fW_2TO56_RSH is scaled by 2^56, it must be multiplied by 2^-56
516 // before the shift constant 1.10000 * 2^63 is subtracted to yield fNfloat.
517 // Thus, fNfloat contains the floating point version of N
520 ldfpd fP3, fP2 = [rAD_P]
521 fms.s1 fNfloat = fW_2TO56_RSH, f2TOM56, fRSHF
522 (p11) br.cond.spnt EXPM1_CERTAIN_MINUS_ONE
527 getf.sig rN = fW_2TO56_RSH
533 // rIndex_1 has index_1
534 // rIndex_2_16 has index_2 * 16
536 // rIndex_1_16 has index_1 * 16
538 // r = x - Nfloat * ln2_by_128_hi
539 // f = 1 - Nfloat * ln2_by_128_lo
541 and rIndex_1 = 0x0f, rN
542 fnma.s1 fR = fNfloat, fLn2_by_128_hi, fNormX
546 and rIndex_2_16 = 0x70, rN
547 fnma.s1 fF = fNfloat, fLn2_by_128_lo, f1
552 // rAD_T1 has address of T1
553 // rAD_T2 has address if T2
556 add rBiased_M = rExp_bias, rM
557 add rAD_T2 = rAD_TB2, rIndex_2_16
558 shladd rAD_T1 = rIndex_1, 4, rAD_TB1
562 // Create Scale = 2^M
565 setf.exp f2M = rBiased_M
573 fmpy.s0 fTmp = fLn2_by_128_lo, fLn2_by_128_lo // Force inexact
580 fma.s1 fP54 = fR, fP5, fP4
585 fma.s1 fP32 = fR, fP3, fP2
592 fma.s1 fRsq = fR, fR, f0
599 fma.s1 fP5432 = fRsq, fP54, fP32
606 fma.s1 fS2 = fF,fT2,f0
611 fma.s1 fS1 = f2M,fT1,f0
618 fma.s1 fP = fRsq, fP5432, fR
625 fms.s1 fSm1 = fS1,fS2,f1 // S - 1.0
630 fma.s1 fS = fS1,fS2,f0
631 (p14) br.cond.spnt EXPM1_POSSIBLE_OVERFLOW
637 fma.d.s0 f8 = fS, fP, fSm1
638 br.ret.sptk b0 // Normal path exit
642 // Here if 2^-60 <= |x| <2^-2
643 // Compute 13th order polynomial
646 ldfe fQ3 = [rAD_Q1], 16
647 ldfe fQ2 = [rAD_Q2], 16
648 fma.s1 fX4 = fXsq, fXsq, f0
654 fma.s1 fQDC = fQD, fNormX, fQC
659 fma.s1 fQBA = fQB, fNormX, fQA
666 fma.s1 fQ98 = fQ9, fNormX, fQ8
671 fma.s1 fQ76= fQ7, fNormX, fQ6
678 fma.s1 fQ54 = fQ5, fNormX, fQ4
685 fma.s1 fX6 = fX4, fXsq, f0
690 fma.s1 fQ32= fQ3, fNormX, fQ2
697 fma.s1 fQDCBA = fQDC, fXsq, fQBA
702 fma.s1 fQ7654 = fQ76, fXsq, fQ54
709 fma.s1 fQDCBA98 = fQDCBA, fXsq, fQ98
714 fma.s1 fQ765432 = fQ7654, fXsq, fQ32
721 fma.s1 fQDCBA98765432 = fQDCBA98, fX6, fQ765432
728 fma.d.s0 f8 = fQDCBA98765432, fXsq, fNormX
729 br.ret.sptk b0 // Exit small branch
734 EXPM1_POSSIBLE_OVERFLOW:
736 // Here if fMAX_DBL_NORM_ARG < x < fMIN_DBL_OFLOW_ARG
737 // This cannot happen if input is a double, only if input higher precision.
738 // Overflow is a possibility, not a certainty.
740 // Recompute result using status field 2 with user's rounding mode,
741 // and wre set. If result is larger than largest double, then we have
745 mov rGt_ln = 0x103ff // Exponent for largest dbl + 1 ulp
746 fsetc.s2 0x7F,0x42 // Get user's round mode, set wre
752 setf.exp fGt_pln = rGt_ln // Create largest double + 1 ulp
753 fma.d.s2 fWre_urm_f8 = fS, fP, fSm1 // Result with wre set
760 fsetc.s2 0x7F,0x40 // Turn off wre in sf2
767 fcmp.ge.s1 p6, p0 = fWre_urm_f8, fGt_pln // Test for overflow
775 (p6) br.cond.spnt EXPM1_CERTAIN_OVERFLOW // Branch if overflow
781 fma.d.s0 f8 = fS, fP, fSm1
782 br.ret.sptk b0 // Exit if really no overflow
786 EXPM1_CERTAIN_OVERFLOW:
788 sub rTmp = rExp_mask, r0, 1
796 alloc r32=ar.pfs,1,4,4,0
797 fmerge.s FR_X = f8,f8
801 mov GR_Parameter_TAG = 41
802 fma.d.s0 FR_RESULT = fTmp, fTmp, f0 // Set I,O and +INF result
803 br.cond.sptk __libm_error_region
810 getf.exp rSignexp_x = fNormX // Must recompute if x unorm
811 fcmp.eq.s0 p6, p0 = f8, f0 // Set D flag
812 br.cond.sptk EXPM1_COMMON
816 // here if result will be -1 and inexact, x <= -48.0
817 EXPM1_CERTAIN_MINUS_ONE:
828 fms.d.s0 FR_RESULT = fTmp, fTmp, f1 // Set I, rounded -1+eps result
833 GLOBAL_IEEE754_END(expm1)
836 LOCAL_LIBM_ENTRY(__libm_error_region)
839 add GR_Parameter_Y=-32,sp // Parameter 2 value
841 .save ar.pfs,GR_SAVE_PFS
842 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
846 add sp=-64,sp // Create new stack
848 mov GR_SAVE_GP=gp // Save gp
851 stfd [GR_Parameter_Y] = FR_Y,16 // STORE Parameter 2 on stack
852 add GR_Parameter_X = 16,sp // Parameter 1 address
854 mov GR_SAVE_B0=b0 // Save b0
858 stfd [GR_Parameter_X] = FR_X // STORE Parameter 1 on stack
859 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
863 stfd [GR_Parameter_Y] = FR_RESULT // STORE Parameter 3 on stack
864 add GR_Parameter_Y = -16,GR_Parameter_Y
865 br.call.sptk b0=__libm_error_support# // Call error handling function
868 add GR_Parameter_RESULT = 48,sp
873 ldfd f8 = [GR_Parameter_RESULT] // Get return result off stack
875 add sp = 64,sp // Restore stack pointer
876 mov b0 = GR_SAVE_B0 // Restore return address
879 mov gp = GR_SAVE_GP // Restore gp
880 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
881 br.ret.sptk b0 // Return
884 LOCAL_LIBM_END(__libm_error_region)
885 .type __libm_error_support#,@function
886 .global __libm_error_support#