4 // Copyright (c) 2000 - 2004, Intel Corporation
5 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
15 // * Redistributions in binary form must reproduce the above copyright
16 // notice, this list of conditions and the following disclaimer in the
17 // documentation and/or other materials provided with the distribution.
19 // * The name of Intel Corporation may not be used to endorse or promote
20 // products derived from this software without specific prior written
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
27 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
31 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
32 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 // Intel Corporation is the author of this code, and requests that all
36 // problem reports or change requests be submitted to it directly at
37 // http://www.intel.com/software/products/opensource/libraries/num.htm.
40 //====================================================================
41 // 02/02/00 Initial version
42 // 03/02/00 New Algorithm
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 // 11/28/00 Set FR_Y to f9
47 // 03/11/02 Fixed flags for fmodl(qnan, zero)
48 // 05/20/02 Cleaned up namespace and sf0 syntax
49 // 02/10/03 Reordered header:.section,.global,.proc,.align
50 // 04/28/03 Fix: fmod(sNaN, 0) no longer sets errno
51 // 11/23/04 Reformatted routine and improved speed
54 //====================================================================
55 // long double fmodl(long double, long double);
57 // Overview of operation
58 //====================================================================
60 // where i is an integer such that, if b!= 0,
61 // |i|<|a/b| and |a/b-i|<1
64 //====================================================================
65 // a). if |a|<|b|, return a
66 // b). get quotient and reciprocal overestimates accurate to
68 // c). if the exponent difference (exponent(a)-exponent(b))
69 // is less than 32, truncate quotient to integer and
70 // finish in one iteration
71 // d). if exponent(a)-exponent(b)>= 32 (q2>= 2^32)
72 // round quotient estimate to single precision (k= RN(q2)),
73 // calculate partial remainder (a'= a-k*b),
74 // get quotient estimate (a'*y2), and repeat from c).
77 //====================================================================
96 GR_Parameter_RESULT = r39
97 GR_Parameter_TAG = r40
129 GLOBAL_IEEE754_ENTRY(fmodl)
135 getf.sig GR_SIG_B = f9
137 fmerge.s FR_ABS_A = f0, f8
138 mov GR_SMALLBIASEXP = 0x0ffdd
143 fmerge.s FR_ABS_B = f0, f9
149 setf.exp FR_SMALLBIAS = GR_SMALLBIASEXP
151 frcpa.s1 FR_Y_INV0, p6 = FR_ABS_A, FR_ABS_B
158 movl GR_ROUNDCONST = 0x33a00000
162 // eliminate special cases
167 cmp.eq p7, p10 = GR_SIG_B, r0
171 // set p7 if b +/-NAN, +/-inf, +/-0
174 (p10) fclass.m p7, p10 = f9, 0xe7
180 mov GR_2P32 = 0x1001f
182 (p6) fma.s1 FR_Q0 = FR_ABS_A, FR_Y_INV0, f0
187 // (3) e0 = 1 - b * y0
188 (p6) fnma.s1 FR_E0 = FR_ABS_B, FR_Y_INV0, f1
193 // set p9 if a +/-NAN, +/-inf
196 fclass.m.unc p9, p11 = f8, 0xe3
199 // |a| < |b|? Return a, p8=1
202 (p10) fcmp.lt.unc.s1 p8, p0 = FR_ABS_A, FR_ABS_B
207 // set p7 if b +/-NAN, +/-inf, +/-0
211 (p10) fclass.nm p7, p0 = f9, 0xff
216 // set p9 if a is +/-NaN, +/-Inf
219 (p11) fclass.nm p9, p0 = f8, 0xff
224 // b denormal ? set D flag (if |a|<|b|)
225 (p8) fnma.s0 FR_DFLAG = f9, f1, f9
232 setf.exp FR_2P32 = GR_2P32
234 (p6) fma.s1 FR_Q1 = FR_E0, FR_Q0, FR_Q0
239 // (5) e1 = e0 * e0 + 2^-34
240 (p6) fma.s1 FR_E1 = FR_E0, FR_E0, FR_SMALLBIAS
247 // normalize a (if |a|<|b|)
248 (p8) fma.s0 f8 = f8, f1, f0
252 (p9) br.cond.spnt FMOD_A_NAN_INF
253 (p7) br.cond.spnt FMOD_B_NAN_INF_ZERO
254 // if |a|<|b|, return
262 // (6) y1 = y0 + e0 * y0
263 (p6) fma.s1 FR_Y_INV1 = FR_E0, FR_Y_INV0, FR_Y_INV0
270 // a denormal ? set D flag
271 // b denormal ? set D flag
272 fcmp.eq.s0 p12,p0 = FR_ABS_A, FR_ABS_B
276 // set FR_ROUNDCONST = 1.25*2^{-24}
277 setf.s FR_ROUNDCONST = GR_ROUNDCONST
279 (p6) fma.s1 FR_Q = FR_Q1, FR_E1, FR_Q1
286 fmerge.s FR_B_SGN_A = f8, f9
291 // (8) y2 = y1 + e1 * y1
292 (p6) fma.s1 FR_Y_INV = FR_E1, FR_Y_INV1, FR_Y_INV1
293 // set p6 = 0, p10 = 0
294 cmp.ne.and p6, p10 = r0, r0
298 // will compute integer quotient bits (24 bits per iteration)
304 fcmp.lt.unc.s1 p8, p7 = FR_Q, FR_2P32
309 // will truncate quotient to integer, if exponent<32 (in advance)
310 fcvt.fx.trunc.s1 FR_QINT = FR_Q
317 // if exponent>32 round quotient to single precision (perform in advance)
318 fma.s.s1 FR_QRND24 = FR_Q, f1, f0
325 // set FR_ROUNDCONST = sgn(a)
326 (p8) fmerge.s FR_ROUNDCONST = f8, f1
331 // normalize truncated quotient
332 (p8) fcvt.xf FR_QRND24 = FR_QINT
339 // calculate remainder (assuming FR_QRND24 = RZ(Q))
340 (p7) fnma.s1 FR_E1 = FR_QRND24, FR_ABS_B, FR_ABS_A
345 // also if exponent>32, round quotient to single precision
346 // and subtract 1 ulp: q = q-q*(1.25*2^{-24})
347 (p7) fnma.s.s1 FR_QINT_Z = FR_QRND24, FR_ROUNDCONST, FR_QRND24
354 // (p8) calculate remainder (82-bit format)
355 (p8) fnma.s1 FR_QREM = FR_QRND24, FR_ABS_B, FR_ABS_A
360 // (p7) calculate remainder (assuming FR_QINT_Z = RZ(Q))
361 (p7) fnma.s1 FR_ABS_A = FR_QINT_Z, FR_ABS_B, FR_ABS_A
368 // Final iteration (p8): is FR_ABS_A the correct remainder
369 // (quotient was not overestimated) ?
370 (p8) fcmp.lt.unc.s1 p6, p10 = FR_QREM, f0
377 // get new quotient estimation: a'*y2
378 (p7) fma.s1 FR_Q = FR_E1, FR_Y_INV, f0
383 // was FR_Q = RZ(Q) ? (then new remainder FR_E1> = 0)
384 (p7) fcmp.lt.unc.s1 p7, p9 = FR_E1, f0
389 .pred.rel "mutex", p6, p10
392 // add b to estimated remainder (to cover the case when the quotient was
394 // also set correct sign by using
395 // FR_B_SGN_A = |b|*sgn(a), FR_ROUNDCONST = sgn(a)
396 (p6) fma.s0 f8 = FR_QREM, FR_ROUNDCONST, FR_B_SGN_A
401 // set correct sign of result before returning: FR_ROUNDCONST = sgn(a)
402 (p10) fma.s0 f8 = FR_QREM, FR_ROUNDCONST, f0
409 // if f13! = RZ(Q), get alternative quotient estimation: a''*y2
410 (p7) fma.s1 FR_Q = FR_ABS_A, FR_Y_INV, f0
415 // if FR_E1 was RZ(Q), set remainder to FR_E1
416 (p9) fma.s1 FR_ABS_A = FR_E1, f1, f0
426 fclass.m p10, p0 = f8, 0xc3 // Test a = nan
431 fma.s1 FR_NORM_B = f9, f1, f0
438 fma.s0 f8 = f8, f1, f0
443 (p10) fclass.m p10, p0 = f9, 0x07 // Test x = nan, and y = zero
450 fcmp.eq.unc.s1 p11, p0 = FR_NORM_B, f0
451 (p10) br.ret.spnt b0 // Exit with result = a if a = nan and b = zero
459 (p11) br.cond.spnt FMOD_B_ZERO
463 // a= infinity? Return QNAN indefinite
466 cmp.ne p7, p0 = r0, r0
467 fclass.m.unc p8, p9 = f8, 0x23
475 (p8) fclass.m p9, p8 = f9, 0xc3
480 // b not pseudo-zero ? (GR_SIG_B holds significand)
483 (p8) cmp.ne p7, p0 = GR_SIG_B, r0
490 (p8) frcpa.s0 f8, p0 = f8, f8
495 // also set Denormal flag if necessary
496 (p7) fnma.s0 f9 = f9, f1, f9
503 (p8) fma.s0 f8 = f8, f1, f0
510 (p9) frcpa.s0 f8, p7 = f8, f9
519 fclass.m.unc p7, p0 = f9, 0x23
526 (p7) fma.s0 f8 = f8, f1, f0
534 fclass.m.unc p9, p10 = f9, 0xc3
541 (p10) fclass.nm p9, p0 = f9, 0xff
548 (p9) fma.s0 f8 = f9, f1, f0
554 // Y zero? Must be zero at this point
555 // because it is the only choice left.
556 // Return QNAN indefinite
561 frcpa.s0 FR_TMP, p0 = f0, f0
569 fclass.m.unc p9, p10 = f8, 0xc3
575 alloc GR_ARPFS = ar.pfs, 1, 4, 4, 0
576 (p10) fclass.nm p9, p10 = f8, 0xff
583 (p9) frcpa.s0 FR_TMP2, p7 = f8, f0
590 (p10) frcpa.s0 FR_TMP2, p7 = f9, f9
591 mov GR_Parameter_TAG = 120
597 fmerge.s FR_X = f8, f8
602 fma.s0 f8 = FR_TMP2, f1, f0
603 br.sptk __libm_error_region
607 GLOBAL_IEEE754_END(fmodl)
608 libm_alias_ldouble_other (__fmod, fmod)
610 LOCAL_LIBM_ENTRY(__libm_error_region)
613 add GR_Parameter_Y = -32, sp // Parameter 2 value
615 .save ar.pfs, GR_SAVE_PFS
616 mov GR_SAVE_PFS = ar.pfs // Save ar.pfs
620 add sp = -64, sp // Create new stack
622 mov GR_SAVE_GP = gp // Save gp
627 stfe [ GR_Parameter_Y ] = FR_Y, 16 // Save Parameter 2 on stack
628 add GR_Parameter_X = 16, sp // Parameter 1 address
630 mov GR_SAVE_B0 = b0 // Save b0
636 stfe [ GR_Parameter_X ] = FR_X // Store Parameter 1 on stack
637 add GR_Parameter_RESULT = 0, GR_Parameter_Y
638 nop.b 0 // Parameter 3 address
641 stfe [ GR_Parameter_Y ] = FR_RESULT // Store Parameter 3 on stack
642 add GR_Parameter_Y = -16, GR_Parameter_Y
643 br.call.sptk b0 = __libm_error_support# // Call error handling function
650 add GR_Parameter_RESULT = 48, sp
655 ldfe f8 = [ GR_Parameter_RESULT ] // Get return result off stack
657 add sp = 64, sp // Restore stack pointer
658 mov b0 = GR_SAVE_B0 // Restore return address
663 mov gp = GR_SAVE_GP // Restore gp
664 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
665 br.ret.sptk b0 // Return
669 LOCAL_LIBM_END(__libm_error_region)
671 .type __libm_error_support#, @function
672 .global __libm_error_support#