3 // Copyright (C) 2000, 2001, Intel Corporation
4 // All rights reserved.
6 // Contributed 2/2/2000 by John Harrison, Ted Kubaska, Bob Norin, Shane Story,
7 // and Ping Tak Peter Tang of the Computational Software Lab, 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://developer.intel.com/opensource.
41 //==============================================================
42 // 4/04/00 Unwind update
43 // 4/04/00 Unwind support added
44 // 8/15/00 Bundle added after call to __libm_error_support to properly
45 // set [the previously overwritten] GR_Parameter_RESULT.
46 // 8/21/00 Improvements to save 2 cycles on main path, and shorten x=0 case
47 // 12/07/00 Widen main path, shorten x=inf, nan paths
50 #include "libm_support.h"
53 //==============================================================
54 // integer registers used
65 exp_GR_Mint_p_128 = r41
71 exp_GR_min_oflow = r46
74 exp_GR_max_uflow = r49
81 exp_GR_17ones_m1 = r56
92 GR_Parameter_RESULT = r61
93 GR_Parameter_TAG = r62
100 // floating point registers used
102 EXP_MIN_SGL_OFLOW_ARG = f11
103 EXP_MAX_SGL_ZERO_ARG = f12
104 EXP_MAX_SGL_NORM_ARG = f13
105 EXP_MAX_SGL_UFLOW_ARG = f14
106 EXP_MIN_SGL_NORM_ARG = f15
150 ASM_TYPE_DIRECTIVE(exp_coeff_1_table,@object)
151 data8 0x3F56F35FDE4F8563 // p5
152 data8 0x3F2A378BEFECCFDD // p6
153 data8 0x3FE00000258C581D // p1
154 data8 0x3FC555557AE7B3D4 // p2
155 ASM_SIZE_DIRECTIVE(exp_coeff_1_table)
159 ASM_TYPE_DIRECTIVE(exp_coeff_2_table,@object)
160 data8 0x3FA5551BB6592FAE // p3
161 data8 0x3F8110E8EBFFD485 // p4
162 ASM_SIZE_DIRECTIVE(exp_coeff_2_table)
166 ASM_TYPE_DIRECTIVE(exp_T2_table,@object)
167 data8 0xa175cf9cd7d85844 , 0x00003f46 // exp(-128)
168 data8 0xdb7279415a1f9eed , 0x00003f47 // exp(-127)
169 data8 0x95213b242bd8ca5f , 0x00003f49 // exp(-126)
170 data8 0xcab03c968c989f83 , 0x00003f4a // exp(-125)
171 data8 0x89bdb674702961ad , 0x00003f4c // exp(-124)
172 data8 0xbb35a2eec278be35 , 0x00003f4d // exp(-123)
173 data8 0xfe71b17f373e7e7a , 0x00003f4e // exp(-122)
174 data8 0xace9a6ec52a39b63 , 0x00003f50 // exp(-121)
175 data8 0xeb03423fe393cf1c , 0x00003f51 // exp(-120)
176 data8 0x9fb52c5bcaef1693 , 0x00003f53 // exp(-119)
177 data8 0xd910b6377ed60bf1 , 0x00003f54 // exp(-118)
178 data8 0x9382dad8a9fdbfe4 , 0x00003f56 // exp(-117)
179 data8 0xc87d0a84dea869a3 , 0x00003f57 // exp(-116)
180 data8 0x883efb4c6d1087b0 , 0x00003f59 // exp(-115)
181 data8 0xb92d7373dce9a502 , 0x00003f5a // exp(-114)
182 data8 0xfbaeb020577fb0cb , 0x00003f5b // exp(-113)
183 ASM_SIZE_DIRECTIVE(exp_T2_table)
187 ASM_TYPE_DIRECTIVE(exp_T1_table,@object)
188 data8 0x8000000000000000 , 0x00003fff // exp(16 * 0)
189 data8 0x87975e8540010249 , 0x00004016 // exp(16 * 1)
190 data8 0x8fa1fe625b3163ec , 0x0000402d // exp(16 * 2)
191 data8 0x9826b576512a59d7 , 0x00004044 // exp(16 * 3)
192 data8 0xa12cc167acbe6902 , 0x0000405b // exp(16 * 4)
193 data8 0xaabbcdcc279f59e4 , 0x00004072 // exp(16 * 5)
194 data8 0xb4dbfaadc045d16f , 0x00004089 // exp(16 * 6)
195 data8 0xbf95e372ccdbf146 , 0x000040a0 // exp(16 * 7)
196 data8 0xcaf2a62eea10bbfb , 0x000040b7 // exp(16 * 8)
197 data8 0xd6fbeb62fddbd340 , 0x000040ce // exp(16 * 9)
198 data8 0xe3bbee32e4a440ea , 0x000040e5 // exp(16 * 10)
199 data8 0xf13d8517c34199a8 , 0x000040fc // exp(16 * 11)
200 data8 0xff8c2b166241eedd , 0x00004113 // exp(16 * 12)
201 data8 0x875a04c0b38d6129 , 0x0000412b // exp(16 * 13)
202 data8 0x8f610127db6774d7 , 0x00004142 // exp(16 * 14)
203 data8 0x97e1dd87e5c20bb6 , 0x00004159 // exp(16 * 15)
204 ASM_SIZE_DIRECTIVE(exp_T1_table)
206 // Argument Reduction
207 // exp_Mx = (int)f8 ==> The value of f8 rounded to int is placed into the
208 // significand of exp_Mx as a two's
209 // complement number.
211 // Later we want to have exp_Mx in a general register. Do this with a getf.sig
212 // and call the general register exp_GR_Mint
214 // exp_Mfloat = (float)(int)f8 ==> the two's complement number in
215 // significand of exp_Mx is turned
216 // into a floating point number.
217 // R = 1 - exp_Mfloat ==> reduced argument
219 // Core Approximation
220 // Calculate a series in R
226 // R^2(R * p6 + p5) + (R * p4 + p3)
228 // R^4(R^2(R * p6 + p5) + (R * p4 + p3)) + (R^2(R * p2 + p1))
230 // exp(R) = (1 + R) + R^4(R^2(R * p6 + p5) + (R * p4 + p3)) + (R^2(R * p2 + p1))
231 // exp(R) = 1 + R + R^2 * p1 + R^3 * p2 + R^4 * p3 + R^5 * p4 + R^6 * p5 + R^7 * p6
234 // signficand of exp_Mx is two's complement,
236 // The smallest single denormal is 2^-149 = ssdn
238 // x = log(ssdn) = -103.279
239 // But with rounding result goes to ssdn until -103.972079
240 // The largest single normal is 1.<23 1's> 2^126 ~ 2^127 = lsn
242 // x = log(lsn) = 88.7228
244 // expf overflows when x > 42b17218 = 88.7228
245 // expf returns largest single denormal when x = c2aeac50
246 // expf goes to zero when x < c2cff1b5
248 // Consider range of 8-bit two's complement, -128 ---> 127
249 // Add 128; range becomes 0 ---> 255
251 // The number (=i) in 0 ---> 255 is used as offset into two tables.
253 // i = abcd efgh = abcd * 16 + efgh = i1 * 16 + i2
255 // i1 = (exp_GR_Mint + 128) & 0xf0 (show 0xf0 as -0x10 to avoid assembler error)
256 // (The immediate in the AND is an 8-bit two's complement)
257 // i1 = i1 + start of T1 table (EXP_AD_T1)
258 // Note that the entries in T1 are double-extended numbers on 16-byte boundaries
259 // and that i1 is already shifted left by 16 after the AND.
261 // i2 must be shifted left by 4 before adding to the start of the table.
262 // i2 = ((exp_GR_Mint + 128) & 0x0f) << 4
263 // i2 = i2 + start of T2 table (EXP_AD_T2)
267 // answer = T * (R^2 * p1 + R^3 * p2 + R^4 * p3 + R^5 * p4 + R^6 * p5 + R^7 * p6) +
279 .global __ieee754_expf#
284 alloc r32 = ar.pfs,1,26,4,0
285 fcvt.fx.s1 exp_Mx = f8
286 mov exp_GR_17ones = 0x1FFFF
289 addl EXP_AD_P_1 = @ltoff(exp_coeff_1_table),gp
290 movl exp_GR_min_oflow = 0x42b17218
294 // Fnorm done to take any enabled faults
296 ld8 EXP_AD_P_1 = [EXP_AD_P_1]
297 fclass.m p6,p0 = f8, 0x07 //@zero
301 add exp_GR_max_norm = -1, exp_GR_min_oflow // 0x42b17217
302 fnorm exp_norm_f8 = f8
308 setf.s EXP_MIN_SGL_OFLOW_ARG = exp_GR_min_oflow // 0x42b17218
309 fclass.m p7,p0 = f8, 0x22 // Test for x=-inf
310 mov exp_GR_0xf0 = 0x0f0
313 setf.s EXP_MAX_SGL_NORM_ARG = exp_GR_max_norm
314 movl exp_GR_max_zero = 0xc2cff1b5
320 mov exp_GR_0x0f = 0x00f
321 movl exp_GR_max_uflow = 0xc2aeac50
325 (p6) fma.s f8 = f1,f1,f0
326 (p6) br.ret.spnt b0 // quick exit for x=0
331 setf.s EXP_MAX_SGL_ZERO_ARG = exp_GR_max_zero
332 fclass.m p8,p0 = f8, 0x21 // Test for x=+inf
333 adds exp_GR_min_norm = 1, exp_GR_max_uflow // 0xc2aeac51
336 ldfpd exp_coeff_P5,exp_coeff_P6 = [EXP_AD_P_1],16
337 (p7) fma.s f8 = f0,f0,f0
338 (p7) br.ret.spnt b0 // quick exit for x=-inf
343 ldfpd exp_coeff_P1,exp_coeff_P2 = [EXP_AD_P_1],16
344 setf.s EXP_MAX_SGL_UFLOW_ARG = exp_GR_max_uflow
345 fclass.m p9,p0 = f8, 0xc3 // Test for x=nan
350 ldfpd exp_coeff_P3,exp_coeff_P4 = [EXP_AD_P_1],16
351 setf.s EXP_MIN_SGL_NORM_ARG = exp_GR_min_norm
352 (p8) br.ret.spnt b0 // quick exit for x=+inf
356 // EXP_AD_P_1 now points to exp_T2_table
358 mov exp_GR_T2_size = 0x100
359 fcvt.xf exp_Mfloat = exp_Mx
365 getf.sig exp_GR_Mint = exp_Mx
366 (p9) fmerge.s f8 = exp_norm_f8, exp_norm_f8
367 (p9) br.ret.spnt b0 // quick exit for x=nan
373 mov EXP_AD_T2 = EXP_AD_P_1
374 add EXP_AD_T1 = exp_GR_T2_size,EXP_AD_P_1 ;;
379 adds exp_GR_Mint_p_128 = 0x80,exp_GR_Mint ;;
380 and exp_GR_Ind1 = exp_GR_Mint_p_128, exp_GR_0xf0
381 and exp_GR_Ind2 = exp_GR_Mint_p_128, exp_GR_0x0f ;;
384 // Divide arguments into the following categories:
385 // Certain Underflow/zero p11 - -inf < x <= MAX_SGL_ZERO_ARG
386 // Certain Underflow p12 - MAX_SGL_ZERO_ARG < x <= MAX_SGL_UFLOW_ARG
387 // Possible Underflow p13 - MAX_SGL_UFLOW_ARG < x < MIN_SGL_NORM_ARG
388 // Certain Safe - MIN_SGL_NORM_ARG <= x <= MAX_SGL_NORM_ARG
389 // Possible Overflow p14 - MAX_SGL_NORM_ARG < x < MIN_SGL_OFLOW_ARG
390 // Certain Overflow p15 - MIN_SGL_OFLOW_ARG <= x < +inf
392 // If the input is really a single arg, then there will never be "Possible
393 // Underflow" or "Possible Overflow" arguments.
397 add EXP_AD_M1 = exp_GR_Ind1,EXP_AD_T1
398 fcmp.ge.s1 p15,p14 = exp_norm_f8,EXP_MIN_SGL_OFLOW_ARG
402 shladd EXP_AD_M2 = exp_GR_Ind2,4,EXP_AD_T2
403 fms.s1 exp_R = f1,f8,exp_Mfloat
408 ldfe exp_T1 = [EXP_AD_M1]
409 fcmp.le.s1 p11,p12 = exp_norm_f8,EXP_MAX_SGL_ZERO_ARG
414 ldfe exp_T2 = [EXP_AD_M2]
415 (p14) fcmp.gt.s1 p14,p0 = exp_norm_f8,EXP_MAX_SGL_NORM_ARG
416 (p15) br.cond.spnt L(EXP_CERTAIN_OVERFLOW) ;;
421 (p12) fcmp.le.s1 p12,p0 = exp_norm_f8,EXP_MAX_SGL_UFLOW_ARG
422 (p11) br.cond.spnt L(EXP_CERTAIN_UNDERFLOW_ZERO)
428 (p13) fcmp.lt.s1 p13,p0 = exp_norm_f8,EXP_MIN_SGL_NORM_ARG
436 fma.s1 exp_Rsq = exp_R,exp_R,f0
441 fma.s1 exp_P3 = exp_R,exp_coeff_P2,exp_coeff_P1
448 fma.s1 exp_P1 = exp_R,exp_coeff_P6,exp_coeff_P5
453 fma.s1 exp_P2 = exp_R,exp_coeff_P4,exp_coeff_P3
461 fma.s1 exp_P7 = f1,exp_R,f1
469 fma.s1 exp_P5 = exp_Rsq,exp_P3,f0
474 fma.s1 exp_R4 = exp_Rsq,exp_Rsq,f0
481 fma.s1 exp_T = exp_T1,exp_T2,f0
486 fma.s1 exp_P4 = exp_Rsq,exp_P1,exp_P2
493 fma.s1 exp_A = exp_T,exp_P7,f0
498 fma.s1 exp_P6 = exp_R4,exp_P4,exp_P5
504 (p12) br.cond.spnt L(EXP_CERTAIN_UNDERFLOW)
505 (p13) br.cond.spnt L(EXP_POSSIBLE_UNDERFLOW)
506 (p14) br.cond.spnt L(EXP_POSSIBLE_OVERFLOW)
512 fma.s f8 = exp_T,exp_P6,exp_A
517 L(EXP_POSSIBLE_OVERFLOW):
519 // We got an answer. EXP_MAX_SGL_NORM_ARG < x < EXP_MIN_SGL_OFLOW_ARG
520 // overflow is a possibility, not a certainty
521 // Set wre in s2 and perform the last operation with s2
523 // We define an overflow when the answer with
525 // user-defined rounding mode
528 // Is the exponent 1 more than the largest single?
529 // If so, go to ERROR RETURN, else (no overflow) get the answer and
532 // Largest single is FE (biased single)
533 // FE - 7F + FFFF = 1007E
535 // Create + largest_single_plus_ulp
536 // Create - largest_single_plus_ulp
538 // Calculate answer with WRE set.
540 // Cases when answer is lsn+1 are as follows:
545 // --+----------|----------+------------
550 // exp_gt_pln contains the floating point number lsn+1.
551 // The setf.exp puts 0x1007f in the exponent and 0x800... in the significand.
553 // If the answer is >= lsn+1, we have overflowed.
554 // Then p6 is TRUE. Set the overflow tag, save input in FR_X,
555 // do the final calculation for IEEE result, and branch to error return.
558 mov exp_GR_gt_ln = 0x1007F
565 setf.exp exp_gt_pln = exp_GR_gt_ln
566 fma.s.s2 exp_wre_urm_f8 = exp_T, exp_P6, exp_A
580 fcmp.ge.unc.s1 p6, p0 = exp_wre_urm_f8, exp_gt_pln
588 (p6) br.cond.spnt L(EXP_CERTAIN_OVERFLOW) // Branch if really overflow
594 fma.s f8 = exp_T, exp_P6, exp_A
595 br.ret.sptk b0 // Exit if really no overflow
599 L(EXP_CERTAIN_OVERFLOW):
601 sub exp_GR_17ones_m1 = exp_GR_17ones, r0, 1 ;;
602 setf.exp f9 = exp_GR_17ones_m1
608 fmerge.s FR_X = f8,f8
612 mov GR_Parameter_TAG = 16
613 fma.s FR_RESULT = f9, f9, f0 // Set I,O and +INF result
614 br.cond.sptk __libm_error_region ;;
617 L(EXP_POSSIBLE_UNDERFLOW):
619 // We got an answer. EXP_MAX_SGL_UFLOW_ARG < x < EXP_MIN_SGL_NORM_ARG
620 // underflow is a possibility, not a certainty
622 // We define an underflow when the answer with
624 // is zero (tiny numbers become zero)
626 // Notice (from below) that if we have an unlimited exponent range,
627 // then there is an extra machine number E between the largest denormal and
628 // the smallest normal.
630 // So if with unbounded exponent we round to E or below, then we are
631 // tiny and underflow has occurred.
633 // But notice that you can be in a situation where we are tiny, namely
634 // rounded to E, but when the exponent is bounded we round to smallest
635 // normal. So the answer can be the smallest normal with underflow.
638 // -----+--------------------+--------------------+-----
640 // 1.1...10 2^-7f 1.1...11 2^-7f 1.0...00 2^-7e
641 // 0.1...11 2^-7e (biased, 1)
642 // largest dn smallest normal
644 // If the answer is = 0, we have underflowed.
645 // Then p6 is TRUE. Set the underflow tag, save input in FR_X,
646 // do the final calculation for IEEE result, and branch to error return.
657 fma.s.s2 exp_ftz_urm_f8 = exp_T, exp_P6, exp_A
672 fcmp.eq.unc.s1 p6, p0 = exp_ftz_urm_f8, f0
680 (p6) br.cond.spnt L(EXP_CERTAIN_UNDERFLOW) // Branch if really underflow
686 fma.s f8 = exp_T, exp_P6, exp_A
687 br.ret.sptk b0 // Exit if really no underflow
691 L(EXP_CERTAIN_UNDERFLOW):
694 fmerge.s FR_X = f8,f8
698 mov GR_Parameter_TAG = 17
699 fma.s FR_RESULT = exp_T, exp_P6, exp_A // Set I,U and tiny result
700 br.cond.sptk __libm_error_region ;;
703 L(EXP_CERTAIN_UNDERFLOW_ZERO):
705 mov exp_GR_one = 1 ;;
706 setf.exp f9 = exp_GR_one
712 fmerge.s FR_X = f8,f8
716 mov GR_Parameter_TAG = 17
717 fma.s FR_RESULT = f9, f9, f0 // Set I,U and tiny (+0.0) result
718 br.cond.sptk __libm_error_region ;;
722 ASM_SIZE_DIRECTIVE(expf)
725 .proc __libm_error_region
729 add GR_Parameter_Y=-32,sp // Parameter 2 value
731 .save ar.pfs,GR_SAVE_PFS
732 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
736 add sp=-64,sp // Create new stack
738 mov GR_SAVE_GP=gp // Save gp
741 stfs [GR_Parameter_Y] = FR_Y,16 // Store Parameter 2 on stack
742 add GR_Parameter_X = 16,sp // Parameter 1 address
744 mov GR_SAVE_B0=b0 // Save b0
748 stfs [GR_Parameter_X] = FR_X // Store Parameter 1 on stack
750 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
753 stfs [GR_Parameter_Y] = FR_RESULT // Store Parameter 3 on stack
754 add GR_Parameter_Y = -16,GR_Parameter_Y
755 br.call.sptk b0=__libm_error_support# // Call error handling function
761 add GR_Parameter_RESULT = 48,sp
765 ldfs f8 = [GR_Parameter_RESULT] // Get return result off stack
767 add sp = 64,sp // Restore stack pointer
768 mov b0 = GR_SAVE_B0 // Restore return address
771 mov gp = GR_SAVE_GP // Restore gp
772 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
773 br.ret.sptk b0 // Return
776 .endp __libm_error_region
777 ASM_SIZE_DIRECTIVE(__libm_error_region)
780 .type __libm_error_support#,@function
781 .global __libm_error_support#