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.
11 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
14 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
15 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
19 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
20 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 // Intel Corporation is the author of this code, and requests that all
24 // problem reports or change requests be submitted to it directly at
25 // http://developer.intel.com/opensource.
28 //==============================================================
29 // 4/04/00 Unwind update
30 // 4/04/00 Unwind support added
31 // 8/15/00 Bundle added after call to __libm_error_support to properly
32 // set [the previously overwritten] GR_Parameter_RESULT.
33 // 8/21/00 Improvements to save 2 cycles on main path, and shorten x=0 case
34 // 12/07/00 Widen main path, shorten x=inf, nan paths
37 #include "libm_support.h"
40 //==============================================================
41 // integer registers used
52 exp_GR_Mint_p_128 = r41
58 exp_GR_min_oflow = r46
61 exp_GR_max_uflow = r49
68 exp_GR_17ones_m1 = r56
79 GR_Parameter_RESULT = r61
80 GR_Parameter_TAG = r62
87 // floating point registers used
89 EXP_MIN_SGL_OFLOW_ARG = f11
90 EXP_MAX_SGL_ZERO_ARG = f12
91 EXP_MAX_SGL_NORM_ARG = f13
92 EXP_MAX_SGL_UFLOW_ARG = f14
93 EXP_MIN_SGL_NORM_ARG = f15
137 ASM_TYPE_DIRECTIVE(exp_coeff_1_table,@object)
138 data8 0x3F56F35FDE4F8563 // p5
139 data8 0x3F2A378BEFECCFDD // p6
140 data8 0x3FE00000258C581D // p1
141 data8 0x3FC555557AE7B3D4 // p2
142 ASM_SIZE_DIRECTIVE(exp_coeff_1_table)
146 ASM_TYPE_DIRECTIVE(exp_coeff_2_table,@object)
147 data8 0x3FA5551BB6592FAE // p3
148 data8 0x3F8110E8EBFFD485 // p4
149 ASM_SIZE_DIRECTIVE(exp_coeff_2_table)
153 ASM_TYPE_DIRECTIVE(exp_T2_table,@object)
154 data8 0xa175cf9cd7d85844 , 0x00003f46 // exp(-128)
155 data8 0xdb7279415a1f9eed , 0x00003f47 // exp(-127)
156 data8 0x95213b242bd8ca5f , 0x00003f49 // exp(-126)
157 data8 0xcab03c968c989f83 , 0x00003f4a // exp(-125)
158 data8 0x89bdb674702961ad , 0x00003f4c // exp(-124)
159 data8 0xbb35a2eec278be35 , 0x00003f4d // exp(-123)
160 data8 0xfe71b17f373e7e7a , 0x00003f4e // exp(-122)
161 data8 0xace9a6ec52a39b63 , 0x00003f50 // exp(-121)
162 data8 0xeb03423fe393cf1c , 0x00003f51 // exp(-120)
163 data8 0x9fb52c5bcaef1693 , 0x00003f53 // exp(-119)
164 data8 0xd910b6377ed60bf1 , 0x00003f54 // exp(-118)
165 data8 0x9382dad8a9fdbfe4 , 0x00003f56 // exp(-117)
166 data8 0xc87d0a84dea869a3 , 0x00003f57 // exp(-116)
167 data8 0x883efb4c6d1087b0 , 0x00003f59 // exp(-115)
168 data8 0xb92d7373dce9a502 , 0x00003f5a // exp(-114)
169 data8 0xfbaeb020577fb0cb , 0x00003f5b // exp(-113)
170 ASM_SIZE_DIRECTIVE(exp_T2_table)
174 ASM_TYPE_DIRECTIVE(exp_T1_table,@object)
175 data8 0x8000000000000000 , 0x00003fff // exp(16 * 0)
176 data8 0x87975e8540010249 , 0x00004016 // exp(16 * 1)
177 data8 0x8fa1fe625b3163ec , 0x0000402d // exp(16 * 2)
178 data8 0x9826b576512a59d7 , 0x00004044 // exp(16 * 3)
179 data8 0xa12cc167acbe6902 , 0x0000405b // exp(16 * 4)
180 data8 0xaabbcdcc279f59e4 , 0x00004072 // exp(16 * 5)
181 data8 0xb4dbfaadc045d16f , 0x00004089 // exp(16 * 6)
182 data8 0xbf95e372ccdbf146 , 0x000040a0 // exp(16 * 7)
183 data8 0xcaf2a62eea10bbfb , 0x000040b7 // exp(16 * 8)
184 data8 0xd6fbeb62fddbd340 , 0x000040ce // exp(16 * 9)
185 data8 0xe3bbee32e4a440ea , 0x000040e5 // exp(16 * 10)
186 data8 0xf13d8517c34199a8 , 0x000040fc // exp(16 * 11)
187 data8 0xff8c2b166241eedd , 0x00004113 // exp(16 * 12)
188 data8 0x875a04c0b38d6129 , 0x0000412b // exp(16 * 13)
189 data8 0x8f610127db6774d7 , 0x00004142 // exp(16 * 14)
190 data8 0x97e1dd87e5c20bb6 , 0x00004159 // exp(16 * 15)
191 ASM_SIZE_DIRECTIVE(exp_T1_table)
193 // Argument Reduction
194 // exp_Mx = (int)f8 ==> The value of f8 rounded to int is placed into the
195 // significand of exp_Mx as a two's
196 // complement number.
198 // Later we want to have exp_Mx in a general register. Do this with a getf.sig
199 // and call the general register exp_GR_Mint
201 // exp_Mfloat = (float)(int)f8 ==> the two's complement number in
202 // significand of exp_Mx is turned
203 // into a floating point number.
204 // R = 1 - exp_Mfloat ==> reduced argument
206 // Core Approximation
207 // Calculate a series in R
213 // R^2(R * p6 + p5) + (R * p4 + p3)
215 // R^4(R^2(R * p6 + p5) + (R * p4 + p3)) + (R^2(R * p2 + p1))
217 // exp(R) = (1 + R) + R^4(R^2(R * p6 + p5) + (R * p4 + p3)) + (R^2(R * p2 + p1))
218 // exp(R) = 1 + R + R^2 * p1 + R^3 * p2 + R^4 * p3 + R^5 * p4 + R^6 * p5 + R^7 * p6
221 // signficand of exp_Mx is two's complement,
223 // The smallest single denormal is 2^-149 = ssdn
225 // x = log(ssdn) = -103.279
226 // But with rounding result goes to ssdn until -103.972079
227 // The largest single normal is 1.<23 1's> 2^126 ~ 2^127 = lsn
229 // x = log(lsn) = 88.7228
231 // expf overflows when x > 42b17218 = 88.7228
232 // expf returns largest single denormal when x = c2aeac50
233 // expf goes to zero when x < c2cff1b5
235 // Consider range of 8-bit two's complement, -128 ---> 127
236 // Add 128; range becomes 0 ---> 255
238 // The number (=i) in 0 ---> 255 is used as offset into two tables.
240 // i = abcd efgh = abcd * 16 + efgh = i1 * 16 + i2
242 // i1 = (exp_GR_Mint + 128) & 0xf0 (show 0xf0 as -0x10 to avoid assembler error)
243 // (The immediate in the AND is an 8-bit two's complement)
244 // i1 = i1 + start of T1 table (EXP_AD_T1)
245 // Note that the entries in T1 are double-extended numbers on 16-byte boundaries
246 // and that i1 is already shifted left by 16 after the AND.
248 // i2 must be shifted left by 4 before adding to the start of the table.
249 // i2 = ((exp_GR_Mint + 128) & 0x0f) << 4
250 // i2 = i2 + start of T2 table (EXP_AD_T2)
254 // answer = T * (R^2 * p1 + R^3 * p2 + R^4 * p3 + R^5 * p4 + R^6 * p5 + R^7 * p6) +
266 .global __ieee754_expf#
271 alloc r32 = ar.pfs,1,26,4,0
272 fcvt.fx.s1 exp_Mx = f8
273 mov exp_GR_17ones = 0x1FFFF
276 addl EXP_AD_P_1 = @ltoff(exp_coeff_1_table),gp
277 movl exp_GR_min_oflow = 0x42b17218
281 // Fnorm done to take any enabled faults
283 ld8 EXP_AD_P_1 = [EXP_AD_P_1]
284 fclass.m p6,p0 = f8, 0x07 //@zero
288 add exp_GR_max_norm = -1, exp_GR_min_oflow // 0x42b17217
289 fnorm exp_norm_f8 = f8
295 setf.s EXP_MIN_SGL_OFLOW_ARG = exp_GR_min_oflow // 0x42b17218
296 fclass.m p7,p0 = f8, 0x22 // Test for x=-inf
297 mov exp_GR_0xf0 = 0x0f0
300 setf.s EXP_MAX_SGL_NORM_ARG = exp_GR_max_norm
301 movl exp_GR_max_zero = 0xc2cff1b5
307 mov exp_GR_0x0f = 0x00f
308 movl exp_GR_max_uflow = 0xc2aeac50
312 (p6) fma.s f8 = f1,f1,f0
313 (p6) br.ret.spnt b0 // quick exit for x=0
318 setf.s EXP_MAX_SGL_ZERO_ARG = exp_GR_max_zero
319 fclass.m p8,p0 = f8, 0x21 // Test for x=+inf
320 adds exp_GR_min_norm = 1, exp_GR_max_uflow // 0xc2aeac51
323 ldfpd exp_coeff_P5,exp_coeff_P6 = [EXP_AD_P_1],16
324 (p7) fma.s f8 = f0,f0,f0
325 (p7) br.ret.spnt b0 // quick exit for x=-inf
330 ldfpd exp_coeff_P1,exp_coeff_P2 = [EXP_AD_P_1],16
331 setf.s EXP_MAX_SGL_UFLOW_ARG = exp_GR_max_uflow
332 fclass.m p9,p0 = f8, 0xc3 // Test for x=nan
337 ldfpd exp_coeff_P3,exp_coeff_P4 = [EXP_AD_P_1],16
338 setf.s EXP_MIN_SGL_NORM_ARG = exp_GR_min_norm
339 (p8) br.ret.spnt b0 // quick exit for x=+inf
343 // EXP_AD_P_1 now points to exp_T2_table
345 mov exp_GR_T2_size = 0x100
346 fcvt.xf exp_Mfloat = exp_Mx
352 getf.sig exp_GR_Mint = exp_Mx
353 (p9) fmerge.s f8 = exp_norm_f8, exp_norm_f8
354 (p9) br.ret.spnt b0 // quick exit for x=nan
360 mov EXP_AD_T2 = EXP_AD_P_1
361 add EXP_AD_T1 = exp_GR_T2_size,EXP_AD_P_1 ;;
366 adds exp_GR_Mint_p_128 = 0x80,exp_GR_Mint ;;
367 and exp_GR_Ind1 = exp_GR_Mint_p_128, exp_GR_0xf0
368 and exp_GR_Ind2 = exp_GR_Mint_p_128, exp_GR_0x0f ;;
371 // Divide arguments into the following categories:
372 // Certain Underflow/zero p11 - -inf < x <= MAX_SGL_ZERO_ARG
373 // Certain Underflow p12 - MAX_SGL_ZERO_ARG < x <= MAX_SGL_UFLOW_ARG
374 // Possible Underflow p13 - MAX_SGL_UFLOW_ARG < x < MIN_SGL_NORM_ARG
375 // Certain Safe - MIN_SGL_NORM_ARG <= x <= MAX_SGL_NORM_ARG
376 // Possible Overflow p14 - MAX_SGL_NORM_ARG < x < MIN_SGL_OFLOW_ARG
377 // Certain Overflow p15 - MIN_SGL_OFLOW_ARG <= x < +inf
379 // If the input is really a single arg, then there will never be "Possible
380 // Underflow" or "Possible Overflow" arguments.
384 add EXP_AD_M1 = exp_GR_Ind1,EXP_AD_T1
385 fcmp.ge.s1 p15,p14 = exp_norm_f8,EXP_MIN_SGL_OFLOW_ARG
389 shladd EXP_AD_M2 = exp_GR_Ind2,4,EXP_AD_T2
390 fms.s1 exp_R = f1,f8,exp_Mfloat
395 ldfe exp_T1 = [EXP_AD_M1]
396 fcmp.le.s1 p11,p12 = exp_norm_f8,EXP_MAX_SGL_ZERO_ARG
401 ldfe exp_T2 = [EXP_AD_M2]
402 (p14) fcmp.gt.s1 p14,p0 = exp_norm_f8,EXP_MAX_SGL_NORM_ARG
403 (p15) br.cond.spnt L(EXP_CERTAIN_OVERFLOW) ;;
408 (p12) fcmp.le.s1 p12,p0 = exp_norm_f8,EXP_MAX_SGL_UFLOW_ARG
409 (p11) br.cond.spnt L(EXP_CERTAIN_UNDERFLOW_ZERO)
415 (p13) fcmp.lt.s1 p13,p0 = exp_norm_f8,EXP_MIN_SGL_NORM_ARG
423 fma.s1 exp_Rsq = exp_R,exp_R,f0
428 fma.s1 exp_P3 = exp_R,exp_coeff_P2,exp_coeff_P1
435 fma.s1 exp_P1 = exp_R,exp_coeff_P6,exp_coeff_P5
440 fma.s1 exp_P2 = exp_R,exp_coeff_P4,exp_coeff_P3
448 fma.s1 exp_P7 = f1,exp_R,f1
456 fma.s1 exp_P5 = exp_Rsq,exp_P3,f0
461 fma.s1 exp_R4 = exp_Rsq,exp_Rsq,f0
468 fma.s1 exp_T = exp_T1,exp_T2,f0
473 fma.s1 exp_P4 = exp_Rsq,exp_P1,exp_P2
480 fma.s1 exp_A = exp_T,exp_P7,f0
485 fma.s1 exp_P6 = exp_R4,exp_P4,exp_P5
491 (p12) br.cond.spnt L(EXP_CERTAIN_UNDERFLOW)
492 (p13) br.cond.spnt L(EXP_POSSIBLE_UNDERFLOW)
493 (p14) br.cond.spnt L(EXP_POSSIBLE_OVERFLOW)
499 fma.s f8 = exp_T,exp_P6,exp_A
504 L(EXP_POSSIBLE_OVERFLOW):
506 // We got an answer. EXP_MAX_SGL_NORM_ARG < x < EXP_MIN_SGL_OFLOW_ARG
507 // overflow is a possibility, not a certainty
508 // Set wre in s2 and perform the last operation with s2
510 // We define an overflow when the answer with
512 // user-defined rounding mode
515 // Is the exponent 1 more than the largest single?
516 // If so, go to ERROR RETURN, else (no overflow) get the answer and
519 // Largest single is FE (biased single)
520 // FE - 7F + FFFF = 1007E
522 // Create + largest_single_plus_ulp
523 // Create - largest_single_plus_ulp
525 // Calculate answer with WRE set.
527 // Cases when answer is lsn+1 are as follows:
532 // --+----------|----------+------------
537 // exp_gt_pln contains the floating point number lsn+1.
538 // The setf.exp puts 0x1007f in the exponent and 0x800... in the significand.
540 // If the answer is >= lsn+1, we have overflowed.
541 // Then p6 is TRUE. Set the overflow tag, save input in FR_X,
542 // do the final calculation for IEEE result, and branch to error return.
545 mov exp_GR_gt_ln = 0x1007F
552 setf.exp exp_gt_pln = exp_GR_gt_ln
553 fma.s.s2 exp_wre_urm_f8 = exp_T, exp_P6, exp_A
567 fcmp.ge.unc.s1 p6, p0 = exp_wre_urm_f8, exp_gt_pln
575 (p6) br.cond.spnt L(EXP_CERTAIN_OVERFLOW) // Branch if really overflow
581 fma.s f8 = exp_T, exp_P6, exp_A
582 br.ret.sptk b0 // Exit if really no overflow
586 L(EXP_CERTAIN_OVERFLOW):
588 sub exp_GR_17ones_m1 = exp_GR_17ones, r0, 1 ;;
589 setf.exp f9 = exp_GR_17ones_m1
595 fmerge.s FR_X = f8,f8
599 mov GR_Parameter_TAG = 16
600 fma.s FR_RESULT = f9, f9, f0 // Set I,O and +INF result
601 br.cond.sptk __libm_error_region ;;
604 L(EXP_POSSIBLE_UNDERFLOW):
606 // We got an answer. EXP_MAX_SGL_UFLOW_ARG < x < EXP_MIN_SGL_NORM_ARG
607 // underflow is a possibility, not a certainty
609 // We define an underflow when the answer with
611 // is zero (tiny numbers become zero)
613 // Notice (from below) that if we have an unlimited exponent range,
614 // then there is an extra machine number E between the largest denormal and
615 // the smallest normal.
617 // So if with unbounded exponent we round to E or below, then we are
618 // tiny and underflow has occurred.
620 // But notice that you can be in a situation where we are tiny, namely
621 // rounded to E, but when the exponent is bounded we round to smallest
622 // normal. So the answer can be the smallest normal with underflow.
625 // -----+--------------------+--------------------+-----
627 // 1.1...10 2^-7f 1.1...11 2^-7f 1.0...00 2^-7e
628 // 0.1...11 2^-7e (biased, 1)
629 // largest dn smallest normal
631 // If the answer is = 0, we have underflowed.
632 // Then p6 is TRUE. Set the underflow tag, save input in FR_X,
633 // do the final calculation for IEEE result, and branch to error return.
644 fma.s.s2 exp_ftz_urm_f8 = exp_T, exp_P6, exp_A
659 fcmp.eq.unc.s1 p6, p0 = exp_ftz_urm_f8, f0
667 (p6) br.cond.spnt L(EXP_CERTAIN_UNDERFLOW) // Branch if really underflow
673 fma.s f8 = exp_T, exp_P6, exp_A
674 br.ret.sptk b0 // Exit if really no underflow
678 L(EXP_CERTAIN_UNDERFLOW):
681 fmerge.s FR_X = f8,f8
685 mov GR_Parameter_TAG = 17
686 fma.s FR_RESULT = exp_T, exp_P6, exp_A // Set I,U and tiny result
687 br.cond.sptk __libm_error_region ;;
690 L(EXP_CERTAIN_UNDERFLOW_ZERO):
692 mov exp_GR_one = 1 ;;
693 setf.exp f9 = exp_GR_one
699 fmerge.s FR_X = f8,f8
703 mov GR_Parameter_TAG = 17
704 fma.s FR_RESULT = f9, f9, f0 // Set I,U and tiny (+0.0) result
705 br.cond.sptk __libm_error_region ;;
709 ASM_SIZE_DIRECTIVE(expf)
712 .proc __libm_error_region
716 add GR_Parameter_Y=-32,sp // Parameter 2 value
718 .save ar.pfs,GR_SAVE_PFS
719 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
723 add sp=-64,sp // Create new stack
725 mov GR_SAVE_GP=gp // Save gp
728 stfs [GR_Parameter_Y] = FR_Y,16 // Store Parameter 2 on stack
729 add GR_Parameter_X = 16,sp // Parameter 1 address
731 mov GR_SAVE_B0=b0 // Save b0
735 stfs [GR_Parameter_X] = FR_X // Store Parameter 1 on stack
737 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
740 stfs [GR_Parameter_Y] = FR_RESULT // Store Parameter 3 on stack
741 add GR_Parameter_Y = -16,GR_Parameter_Y
742 br.call.sptk b0=__libm_error_support# // Call error handling function
748 add GR_Parameter_RESULT = 48,sp
752 ldfs f8 = [GR_Parameter_RESULT] // Get return result off stack
754 add sp = 64,sp // Restore stack pointer
755 mov b0 = GR_SAVE_B0 // Restore return address
758 mov gp = GR_SAVE_GP // Restore gp
759 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
760 br.ret.sptk b0 // Return
763 .endp __libm_error_region
764 ASM_SIZE_DIRECTIVE(__libm_error_region)
767 .type __libm_error_support#,@function
768 .global __libm_error_support#