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 fmodf(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 // float fmodf(float,float);
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(fmodf)
108 alloc r32=ar.pfs,1,4,4,0
123 frcpa.s1 f10,p6=f6,f7
127 // eliminate special cases
128 // Y +-NAN, +-inf, +-0? p7
131 fclass.m.unc p7,p0 = f9, 0xe7
135 // qnan snan inf norm unorm 0 -+
138 // X +-NAN, +-inf, ? p9
142 fclass.m.unc p9,p0 = f8, 0xe3
146 // |x| < |y|? Return x p8
149 fcmp.lt.unc.s1 p8,p0 = f6,f7
155 // normalize y (if |x|<|y|)
156 (p8) fma.s0 f9=f9,f1,f0
163 (p6) fma.s1 f13=f6,f10,f0
168 // (3) e0 = 1 - b * y0
169 (p6) fnma.s1 f12=f7,f10,f1
175 // normalize x (if |x|<|y|)
176 (p8) fma.s.s0 f8=f8,f1,f0
180 (p9) br.cond.spnt FMOD_X_NAN_INF
181 (p7) br.cond.spnt FMOD_Y_NAN_INF_ZERO
182 // if |x|<|y|, return
183 (p8) br.ret.spnt b0;;
204 (p6) fma.s1 f13=f12,f13,f13
209 // (5) e1 = e0 * e0 + 2^-34
210 (p6) fma.s1 f14=f12,f12,f11
219 // (6) y1 = y0 + e0 * y0
220 (p6) fma.s1 f10=f12,f10,f10
224 // set f12=1.25*2^{-24}
227 (p6) fma.s1 f13=f13,f14,f13
237 // (8) y2 = y1 + e1 * y1
238 (p6) fma.s1 f10=f14,f10,f10
240 cmp.ne.and p6,p10=r0,r0;;
248 fcmp.lt.unc.s1 p8,p7=f13,f15
253 // will truncate quotient to integer, if exponent<32 (in advance)
254 fcvt.fx.trunc.s1 f11=f13
259 // if exponent>32, round quotient to single precision (perform in advance)
260 fma.s.s1 f13=f13,f1,f0
266 (p8) fmerge.s f12=f8,f1
271 // normalize truncated quotient
277 // calculate remainder (assuming f13=RZ(Q))
278 (p7) fnma.s1 f14=f13,f7,f6
283 // also if exponent>32, round quotient to single precision
284 // and subtract 1 ulp: q=q-q*(1.25*2^{-24})
285 (p7) fnma.s.s1 f11=f13,f12,f13
291 // (p8) calculate remainder (82-bit format)
292 (p8) fnma.s1 f11=f13,f7,f6
297 // (p7) calculate remainder (assuming f11=RZ(Q))
298 (p7) fnma.s1 f6=f11,f7,f6
305 // Final iteration (p8): is f6 the correct remainder (quotient was not overestimated) ?
306 (p8) fcmp.lt.unc.s1 p6,p10=f11,f0
311 // get new quotient estimation: a'*y2
312 (p7) fma.s1 f13=f14,f10,f0
317 // was f14=RZ(Q) ? (then new remainder f14>=0)
318 (p7) fcmp.lt.unc.s1 p7,p9=f14,f0
323 .pred.rel "mutex",p6,p10
326 // add b to estimated remainder (to cover the case when the quotient was overestimated)
327 // also set correct sign by using f9=|b|*sgn(a), f12=sgn(a)
328 (p6) fma.s.s0 f8=f11,f12,f9
333 // calculate remainder (single precision)
334 // set correct sign of result before returning
335 (p10) fma.s.s0 f8=f11,f12,f0
336 (p8) br.ret.sptk b0;;
340 // if f13!=RZ(Q), get alternative quotient estimation: a''*y2
341 (p7) fma.s1 f13=f6,f10,f0
346 // if f14 was RZ(Q), set remainder to f14
348 br.cond.sptk loop24;;
363 fclass.m p10,p0=f8,0xc3 // Test x=nan
379 (p10) fclass.m p10,p0=f9,0x07 // Test x=nan, and y=zero
384 fcmp.eq.unc.s1 p11,p0=f10,f0
385 (p10) br.ret.spnt b0;; // Exit with result=x if x=nan and y=zero
391 (p11) br.cond.spnt FMOD_Y_ZERO;;
394 // X infinity? Return QNAN indefinite
397 fclass.m.unc p8,p9 = f8, 0x23
403 (p8) fclass.m p9,p8=f9,0xc3
408 (p8) frcpa.s0 f8,p0 = f8,f8
413 // also set Denormal flag if necessary
414 (p8) fma.s0 f9=f9,f1,f0
420 (p8) fma.s.s0 f8=f8,f1,f0
426 (p9) frcpa.s0 f8,p7=f8,f9
436 fclass.m.unc p7,p0 = f9, 0x23
442 (p7) fma.s.s0 f8=f8,f1,f0
443 (p7) br.ret.spnt b0 ;;
449 fclass.m.unc p9,p0 = f9, 0xc3
455 (p9) fma.s.s0 f8=f9,f1,f0
456 (p9) br.ret.spnt b0 ;;
460 // Y zero? Must be zero at this point
461 // because it is the only choice left.
462 // Return QNAN indefinite
467 frcpa.s0 f12,p0=f0,f0
473 fclass.m.unc p9,p10 = f8, 0xc3
478 (p10) fclass.nm p9,p10 = f8, 0xff
484 (p9) frcpa.s0 f11,p7=f8,f0
490 (p10) frcpa.s0 f11,p7 = f0,f0
496 fmerge.s f10 = f8, f8
502 fma.s.s0 f8=f11,f1,f0
511 mov GR_Parameter_TAG=122
512 br.sptk __libm_error_region;;
515 GLOBAL_IEEE754_END(fmodf)
516 libm_alias_float_other (__fmod, fmod)
518 LOCAL_LIBM_ENTRY(__libm_error_region)
521 add GR_Parameter_Y=-32,sp // Parameter 2 value
523 .save ar.pfs,GR_SAVE_PFS
524 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
528 add sp=-64,sp // Create new stack
530 mov GR_SAVE_GP=gp // Save gp
533 stfs [GR_Parameter_Y] = FR_Y,16 // Save Parameter 2 on stack
534 add GR_Parameter_X = 16,sp // Parameter 1 address
536 mov GR_SAVE_B0=b0 // Save b0
540 stfs [GR_Parameter_X] = FR_X // Store Parameter 1 on stack
541 add GR_Parameter_RESULT = 0,GR_Parameter_Y
542 nop.b 0 // Parameter 3 address
545 stfs [GR_Parameter_Y] = FR_RESULT // Store Parameter 3 on stack
546 add GR_Parameter_Y = -16,GR_Parameter_Y
547 br.call.sptk b0=__libm_error_support#;; // Call error handling function
552 add GR_Parameter_RESULT = 48,sp
555 ldfs f8 = [GR_Parameter_RESULT] // Get return result off stack
557 add sp = 64,sp // Restore stack pointer
558 mov b0 = GR_SAVE_B0 // Restore return address
561 mov gp = GR_SAVE_GP // Restore gp
562 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
563 br.ret.sptk b0 // Return
566 LOCAL_LIBM_END(__libm_error_region)
568 .type __libm_error_support#,@function
569 .global __libm_error_support#