4 // Copyright (c) 2000 - 2003, Intel Corporation
5 // All rights reserved.
7 // Contributed 2000 by the Intel Numerics Group, 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://www.intel.com/software/products/opensource/libraries/num.htm.
41 //====================================================================
42 // 02/02/00 Initial version
43 // 03/02/00 New Algorithm
44 // 04/04/00 Unwind support added
45 // 08/15/00 Bundle added after call to __libm_error_support to properly
46 // set [the previously overwritten] GR_Parameter_RESULT.
47 // 11/28/00 Set FR_Y to f9
48 // 03/11/02 Fixed flags for fmodf(qnan,zero)
49 // 05/20/02 Cleaned up namespace and sf0 syntax
50 // 02/10/03 Reordered header: .section, .global, .proc, .align
51 // 04/28/03 Fix: fmod(sNaN,0) no longer sets errno
54 //====================================================================
55 // float fmodf(float,float);
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 //====================================================================
78 // b=+/-0: return NaN, call libm_error_support
79 // a=+/-Inf, a=NaN or b=NaN: return NaN
82 //====================================================================
83 // Predicate registers: p6-p11
84 // General registers: r2,r29,r32 (ar.pfs), r33-r39
85 // Floating point registers: f6-f15
94 GR_Parameter_RESULT = r39
95 GR_Parameter_TAG = r40
103 GLOBAL_IEEE754_ENTRY(fmodf)
109 alloc r32=ar.pfs,1,4,4,0
124 frcpa.s1 f10,p6=f6,f7
128 // eliminate special cases
129 // Y +-NAN, +-inf, +-0? p7
132 fclass.m.unc p7,p0 = f9, 0xe7
136 // qnan snan inf norm unorm 0 -+
139 // X +-NAN, +-inf, ? p9
143 fclass.m.unc p9,p0 = f8, 0xe3
147 // |x| < |y|? Return x p8
150 fcmp.lt.unc.s1 p8,p0 = f6,f7
156 // normalize y (if |x|<|y|)
157 (p8) fma.s0 f9=f9,f1,f0
164 (p6) fma.s1 f13=f6,f10,f0
169 // (3) e0 = 1 - b * y0
170 (p6) fnma.s1 f12=f7,f10,f1
176 // normalize x (if |x|<|y|)
177 (p8) fma.s.s0 f8=f8,f1,f0
181 (p9) br.cond.spnt FMOD_X_NAN_INF
182 (p7) br.cond.spnt FMOD_Y_NAN_INF_ZERO
183 // if |x|<|y|, return
184 (p8) br.ret.spnt b0;;
205 (p6) fma.s1 f13=f12,f13,f13
210 // (5) e1 = e0 * e0 + 2^-34
211 (p6) fma.s1 f14=f12,f12,f11
220 // (6) y1 = y0 + e0 * y0
221 (p6) fma.s1 f10=f12,f10,f10
225 // set f12=1.25*2^{-24}
228 (p6) fma.s1 f13=f13,f14,f13
238 // (8) y2 = y1 + e1 * y1
239 (p6) fma.s1 f10=f14,f10,f10
241 cmp.ne.and p6,p10=r0,r0;;
249 fcmp.lt.unc.s1 p8,p7=f13,f15
254 // will truncate quotient to integer, if exponent<32 (in advance)
255 fcvt.fx.trunc.s1 f11=f13
260 // if exponent>32, round quotient to single precision (perform in advance)
261 fma.s.s1 f13=f13,f1,f0
267 (p8) fmerge.s f12=f8,f1
272 // normalize truncated quotient
278 // calculate remainder (assuming f13=RZ(Q))
279 (p7) fnma.s1 f14=f13,f7,f6
284 // also if exponent>32, round quotient to single precision
285 // and subtract 1 ulp: q=q-q*(1.25*2^{-24})
286 (p7) fnma.s.s1 f11=f13,f12,f13
292 // (p8) calculate remainder (82-bit format)
293 (p8) fnma.s1 f11=f13,f7,f6
298 // (p7) calculate remainder (assuming f11=RZ(Q))
299 (p7) fnma.s1 f6=f11,f7,f6
306 // Final iteration (p8): is f6 the correct remainder (quotient was not overestimated) ?
307 (p8) fcmp.lt.unc.s1 p6,p10=f11,f0
312 // get new quotient estimation: a'*y2
313 (p7) fma.s1 f13=f14,f10,f0
318 // was f14=RZ(Q) ? (then new remainder f14>=0)
319 (p7) fcmp.lt.unc.s1 p7,p9=f14,f0
324 .pred.rel "mutex",p6,p10
327 // add b to estimated remainder (to cover the case when the quotient was overestimated)
328 // also set correct sign by using f9=|b|*sgn(a), f12=sgn(a)
329 (p6) fma.s.s0 f8=f11,f12,f9
334 // calculate remainder (single precision)
335 // set correct sign of result before returning
336 (p10) fma.s.s0 f8=f11,f12,f0
337 (p8) br.ret.sptk b0;;
341 // if f13!=RZ(Q), get alternative quotient estimation: a''*y2
342 (p7) fma.s1 f13=f6,f10,f0
347 // if f14 was RZ(Q), set remainder to f14
349 br.cond.sptk loop24;;
364 fclass.m p10,p0=f8,0xc3 // Test x=nan
380 (p10) fclass.m p10,p0=f9,0x07 // Test x=nan, and y=zero
385 fcmp.eq.unc.s1 p11,p0=f10,f0
386 (p10) br.ret.spnt b0;; // Exit with result=x if x=nan and y=zero
392 (p11) br.cond.spnt FMOD_Y_ZERO;;
395 // X infinity? Return QNAN indefinite
398 fclass.m.unc p8,p9 = f8, 0x23
404 (p8) fclass.m p9,p8=f9,0xc3
409 (p8) frcpa.s0 f8,p0 = f8,f8
414 // also set Denormal flag if necessary
415 (p8) fma.s0 f9=f9,f1,f0
421 (p8) fma.s.s0 f8=f8,f1,f0
427 (p9) frcpa.s0 f8,p7=f8,f9
437 fclass.m.unc p7,p0 = f9, 0x23
443 (p7) fma.s.s0 f8=f8,f1,f0
444 (p7) br.ret.spnt b0 ;;
450 fclass.m.unc p9,p0 = f9, 0xc3
456 (p9) fma.s.s0 f8=f9,f1,f0
457 (p9) br.ret.spnt b0 ;;
461 // Y zero? Must be zero at this point
462 // because it is the only choice left.
463 // Return QNAN indefinite
468 frcpa.s0 f12,p0=f0,f0
474 fclass.m.unc p9,p10 = f8, 0xc3
479 (p10) fclass.nm p9,p10 = f8, 0xff
485 (p9) frcpa.s0 f11,p7=f8,f0
491 (p10) frcpa.s0 f11,p7 = f0,f0
497 fmerge.s f10 = f8, f8
503 fma.s.s0 f8=f11,f1,f0
512 mov GR_Parameter_TAG=122
513 br.sptk __libm_error_region;;
516 GLOBAL_IEEE754_END(fmodf)
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#