4 // Copyright (c) 2000 - 2003, 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 fmod(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
53 //====================================================================
54 // double fmod(double,double);
56 // Overview of operation
57 //====================================================================
59 // where i is an integer such that, if b!=0,
60 // |i|<|a/b| and |a/b-i|<1
63 //====================================================================
64 // a). if |a|<|b|, return a
65 // b). get quotient and reciprocal overestimates accurate to
67 // c). if the exponent difference (exponent(a)-exponent(b))
68 // is less than 32, truncate quotient to integer and
69 // finish in one iteration
70 // d). if exponent(a)-exponent(b)>=32 (q2>=2^32)
71 // round quotient estimate to single precision (k=RN(q2)),
72 // calculate partial remainder (a'=a-k*b),
73 // get quotient estimate (a'*y2), and repeat from c).
76 //====================================================================
77 // b=+/-0: return NaN, call libm_error_support
78 // a=+/-Inf, a=NaN or b=NaN: return NaN
81 //====================================================================
82 // Predicate registers: p6-p11
83 // General registers: r2,r29,r32 (ar.pfs), r33-r39
84 // Floating point registers: f6-f15
93 GR_Parameter_RESULT = r39
94 GR_Parameter_TAG = r40
102 GLOBAL_IEEE754_ENTRY(fmod)
108 alloc r32=ar.pfs,1,4,4,0
123 frcpa.s1 f10,p6=f6,f7
127 // Y +-NAN, +-inf, +-0? p7
130 fclass.m.unc p7,p0 = f9, 0xe7
134 // qnan snan inf norm unorm 0 -+
137 // X +-NAN, +-inf, ? p9
141 fclass.m.unc p9,p0 = f8, 0xe3
145 // |x| < |y|? Return x p8
148 fcmp.lt.unc.s1 p8,p0 = f6,f7
154 // normalize y (if |x|<|y|)
155 (p8) fma.s0 f9=f9,f1,f0
162 (p6) fma.s1 f13=f6,f10,f0
167 // (3) e0 = 1 - b * y0
168 (p6) fnma.s1 f12=f7,f10,f1
174 // normalize x (if |x|<|y|)
175 (p8) fma.d.s0 f8=f8,f1,f0
179 (p9) br.cond.spnt FMOD_X_NAN_INF
180 (p7) br.cond.spnt FMOD_Y_NAN_INF_ZERO
181 // if |x|<|y|, return
182 (p8) br.ret.spnt b0;;
202 (p6) fma.s1 f13=f12,f13,f13
207 // (5) e1 = e0 * e0 + 2^-34
208 (p6) fma.s1 f14=f12,f12,f11
217 // (6) y1 = y0 + e0 * y0
218 (p6) fma.s1 f10=f12,f10,f10
222 // set f12=1.25*2^{-24}
225 (p6) fma.s1 f13=f13,f14,f13
235 // (8) y2 = y1 + e1 * y1
236 (p6) fma.s1 f10=f14,f10,f10
238 cmp.ne.and p6,p10=r0,r0;;
246 fcmp.lt.unc.s1 p8,p7=f13,f15
251 // will truncate quotient to integer, if exponent<32 (in advance)
252 fcvt.fx.trunc.s1 f11=f13
257 // if exponent>32, round quotient to single precision (perform in advance)
258 fma.s.s1 f13=f13,f1,f0
264 (p8) fmerge.s f12=f8,f1
269 // normalize truncated quotient
275 // calculate remainder (assuming f13=RZ(Q))
276 (p7) fnma.s1 f14=f13,f7,f6
281 // also if exponent>32, round quotient to single precision
282 // and subtract 1 ulp: q=q-q*(1.25*2^{-24})
283 (p7) fnma.s.s1 f11=f13,f12,f13
289 // (p8) calculate remainder (82-bit format)
290 (p8) fnma.s1 f11=f13,f7,f6
295 // (p7) calculate remainder (assuming f11=RZ(Q))
296 (p7) fnma.s1 f6=f11,f7,f6
303 // Final iteration (p8): is f6 the correct remainder (quotient was not overestimated) ?
304 (p8) fcmp.lt.unc.s1 p6,p10=f11,f0
309 // get new quotient estimation: a'*y2
310 (p7) fma.s1 f13=f14,f10,f0
315 // was f14=RZ(Q) ? (then new remainder f14>=0)
316 (p7) fcmp.lt.unc.s1 p7,p9=f14,f0
321 .pred.rel "mutex",p6,p10
324 // add b to estimated remainder (to cover the case when the quotient was overestimated)
325 // also set correct sign by using f9=|b|*sgn(a), f12=sgn(a)
326 (p6) fma.d.s0 f8=f11,f12,f9
331 // calculate remainder (single precision)
332 // set correct sign of result before returning
333 (p10) fma.d.s0 f8=f11,f12,f0
334 (p8) br.ret.sptk b0;;
338 // if f13!=RZ(Q), get alternative quotient estimation: a''*y2
339 (p7) fma.s1 f13=f6,f10,f0
344 // if f14 was RZ(Q), set remainder to f14
346 br.cond.sptk loop53;;
356 fclass.m p10,p0=f8,0xc3 // Test x=nan
372 (p10) fclass.m p10,p0=f9,0x07 // Test x=nan, and y=zero
378 fcmp.eq.unc.s1 p11,p0=f10,f0
379 (p10) br.ret.spnt b0;; // Exit with result=x if x=nan and y=zero
385 (p11) br.cond.spnt FMOD_Y_ZERO;;
388 // X infinity? Return QNAN indefinite
391 fclass.m.unc p8,p9 = f8, 0x23
397 (p8) fclass.m p9,p8=f9,0xc3
402 (p8) frcpa.s0 f8,p0 = f8,f8
407 // also set Denormal flag if necessary
408 (p8) fma.s0 f9=f9,f1,f0
414 (p8) fma.d.s0 f8=f8,f1,f0
420 (p9) frcpa.s0 f8,p7=f8,f9
430 fclass.m.unc p7,p0 = f9, 0x23
436 (p7) fma.d.s0 f8=f8,f1,f0
437 (p7) br.ret.spnt b0 ;;
443 fclass.m.unc p9,p0 = f9, 0xc3
449 (p9) fma.d.s0 f8=f9,f1,f0
450 (p9) br.ret.spnt b0 ;;
454 // Y zero? Must be zero at this point
455 // because it is the only choice left.
456 // Return QNAN indefinite
461 frcpa.s0 f12,p0=f0,f0
467 fclass.m.unc p9,p10 = f8, 0xc3
472 (p10) fclass.nm p9,p10 = f8, 0xff
478 (p9) frcpa.s0 f11,p7=f8,f0
484 (p10) frcpa.s0 f11,p7 = f9,f9
485 mov GR_Parameter_TAG = 121 ;;
490 fmerge.s f10 = f8, f8
496 fma.d.s0 f8=f11,f1,f0
497 br.sptk __libm_error_region;;
500 GLOBAL_IEEE754_END(fmod)
501 libm_alias_double_other (__fmod, fmod)
503 LOCAL_LIBM_ENTRY(__libm_error_region)
506 add GR_Parameter_Y=-32,sp // Parameter 2 value
508 .save ar.pfs,GR_SAVE_PFS
509 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
513 add sp=-64,sp // Create new stack
515 mov GR_SAVE_GP=gp // Save gp
518 stfd [GR_Parameter_Y] = FR_Y,16 // Save Parameter 2 on stack
519 add GR_Parameter_X = 16,sp // Parameter 1 address
521 mov GR_SAVE_B0=b0 // Save b0
525 stfd [GR_Parameter_X] = FR_X // Store Parameter 1 on stack
526 add GR_Parameter_RESULT = 0,GR_Parameter_Y
527 nop.b 0 // Parameter 3 address
530 stfd [GR_Parameter_Y] = FR_RESULT // Store Parameter 3 on stack
531 add GR_Parameter_Y = -16,GR_Parameter_Y
532 br.call.sptk b0=__libm_error_support# // Call error handling function
537 add GR_Parameter_RESULT = 48,sp
540 ldfd f8 = [GR_Parameter_RESULT] // Get return result off stack
542 add sp = 64,sp // Restore stack pointer
543 mov b0 = GR_SAVE_B0 // Restore return address
546 mov gp = GR_SAVE_GP // Restore gp
547 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
548 br.ret.sptk b0 // Return
551 LOCAL_LIBM_END(__libm_error_region)
554 .type __libm_error_support#,@function
555 .global __libm_error_support#