3 // Copyright (C) 2000, 2001, Intel Corporation
4 // All rights reserved.
6 // Contributed 2/2/2000 by John Harrison, Cristina Iordache, Ted Kubaska,
7 // Bob Norin, Shane Story, and Ping Tak Peter Tang of the Computational
8 // Software Lab, Intel Corporation.
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
14 // * Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
17 // * Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
21 // * The name of Intel Corporation may not be used to endorse or promote
22 // products derived from this software without specific prior written
25 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
33 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 // Intel Corporation is the author of this code, and requests that all
38 // problem reports or change requests be submitted to it directly at
39 // http://developer.intel.com/opensource.
42 //====================================================================
43 // 2/02/00 Initial version
44 // 3/02/00 New Algorithm
45 // 4/04/00 Unwind support added
46 // 8/15/00 Bundle added after call to __libm_error_support to properly
47 // set [the previously overwritten] GR_Parameter_RESULT.
48 //11/28/00 Set FR_Y to f9
51 //====================================================================
52 // double fmod(double,double);
54 // Overview of operation
55 //====================================================================
57 // where i is an integer such that, if b!=0,
58 // |i|<|a/b| and |a/b-i|<1
61 //====================================================================
62 // a). if |a|<|b|, return a
63 // b). get quotient and reciprocal overestimates accurate to
65 // c). if the exponent difference (exponent(a)-exponent(b))
66 // is less than 32, truncate quotient to integer and
67 // finish in one iteration
68 // d). if exponent(a)-exponent(b)>=32 (q2>=2^32)
69 // round quotient estimate to single precision (k=RN(q2)),
70 // calculate partial remainder (a'=a-k*b),
71 // get quotient estimate (a'*y2), and repeat from c).
74 //====================================================================
75 // b=+/-0: return NaN, call libm_error_support
76 // a=+/-Inf, a=NaN or b=NaN: return NaN
79 //====================================================================
80 // Predicate registers: p6-p11
81 // General registers: r2,r29,r32 (ar.pfs), r33-r39
82 // Floating point registers: f6-f15
84 #include "libm_support.h"
96 GR_Parameter_RESULT = r39
97 GR_Parameter_TAG = r40
111 .global __ieee754_fmod
112 .type __ieee754_fmod,@function
119 alloc r32=ar.pfs,1,4,4,0
134 frcpa.s1 f10,p6=f6,f7
138 // Y +-NAN, +-inf, +-0? p7
141 (p0) fclass.m.unc p7,p0 = f9, 0xe7
145 // qnan snan inf norm unorm 0 -+
148 // X +-NAN, +-inf, ? p9
152 (p0) fclass.m.unc p9,p0 = f8, 0xe3
156 // |x| < |y|? Return x p8
159 (p0) fcmp.lt.unc.s1 p8,p0 = f6,f7
165 // normalize y (if |x|<|y|)
166 (p8) fma.s0 f9=f9,f1,f0
173 (p6) fma.s1 f13=f6,f10,f0
178 // (3) e0 = 1 - b * y0
179 (p6) fnma.s1 f12=f7,f10,f1
185 // normalize x (if |x|<|y|)
186 (p8) fma.d.s0 f8=f8,f1,f0
190 (p9) br.cond.spnt L(FMOD_X_NAN_INF)
191 (p7) br.cond.spnt L(FMOD_Y_NAN_INF_ZERO)
192 // if |x|<|y|, return
193 (p8) br.ret.spnt b0;;
213 (p6) fma.s1 f13=f12,f13,f13
218 // (5) e1 = e0 * e0 + 2^-34
219 (p6) fma.s1 f14=f12,f12,f11
228 // (6) y1 = y0 + e0 * y0
229 (p6) fma.s1 f10=f12,f10,f10
233 // set f12=1.25*2^{-24}
236 (p6) fma.s1 f13=f13,f14,f13
246 // (8) y2 = y1 + e1 * y1
247 (p6) fma.s1 f10=f14,f10,f10
249 cmp.ne.and p6,p10=r0,r0;;
257 fcmp.lt.unc.s1 p8,p7=f13,f15
262 // will truncate quotient to integer, if exponent<32 (in advance)
263 fcvt.fx.trunc.s1 f11=f13
268 // if exponent>32, round quotient to single precision (perform in advance)
269 fma.s.s1 f13=f13,f1,f0
275 (p8) fmerge.s f12=f8,f1
280 // normalize truncated quotient
286 // calculate remainder (assuming f13=RZ(Q))
287 (p7) fnma.s1 f14=f13,f7,f6
292 // also if exponent>32, round quotient to single precision
293 // and subtract 1 ulp: q=q-q*(1.25*2^{-24})
294 (p7) fnma.s.s1 f11=f13,f12,f13
300 // (p8) calculate remainder (82-bit format)
301 (p8) fnma.s1 f11=f13,f7,f6
306 // (p7) calculate remainder (assuming f11=RZ(Q))
307 (p7) fnma.s1 f6=f11,f7,f6
314 // Final iteration (p8): is f6 the correct remainder (quotient was not overestimated) ?
315 (p8) fcmp.lt.unc.s1 p6,p10=f11,f0
320 // get new quotient estimation: a'*y2
321 (p7) fma.s1 f13=f14,f10,f0
326 // was f14=RZ(Q) ? (then new remainder f14>=0)
327 (p7) fcmp.lt.unc.s1 p7,p9=f14,f0
332 .pred.rel "mutex",p6,p10
335 // add b to estimated remainder (to cover the case when the quotient was overestimated)
336 // also set correct sign by using f9=|b|*sgn(a), f12=sgn(a)
337 (p6) fma.d.s0 f8=f11,f12,f9
342 // calculate remainder (single precision)
343 // set correct sign of result before returning
344 (p10) fma.d.s0 f8=f11,f12,f0
345 (p8) br.ret.sptk b0;;
349 // if f13!=RZ(Q), get alternative quotient estimation: a''*y2
350 (p7) fma.s1 f13=f6,f10,f0
355 // if f14 was RZ(Q), set remainder to f14
357 br.cond.sptk L(loop53);;
372 fcmp.eq.unc.s1 p11,p0=f10,f0
379 (p11) br.cond.spnt L(FMOD_Y_ZERO);;
382 // X infinity? Return QNAN indefinite
385 (p0) fclass.m.unc p8,p9 = f8, 0x23
391 (p8) fclass.m p9,p8=f9,0xc3
396 (p8) frcpa.s0 f8,p0 = f8,f8
401 // also set Denormal flag if necessary
402 (p8) fma.s0 f9=f9,f1,f0
408 (p8) fma.d f8=f8,f1,f0
414 (p9) frcpa.s0 f8,p7=f8,f9
419 L(FMOD_Y_NAN_INF_ZERO):
424 (p0) fclass.m.unc p7,p0 = f9, 0x23
430 (p7) fma.d f8=f8,f1,f0
431 (p7) br.ret.spnt b0 ;;
437 (p0) fclass.m.unc p9,p0 = f9, 0xc3
443 (p9) fma.d f8=f9,f1,f0
444 (p9) br.ret.spnt b0 ;;
448 // Y zero? Must be zero at this point
449 // because it is the only choice left.
450 // Return QNAN indefinite
461 (p0) fclass.m.unc p9,p10 = f8, 0xc3
466 (p10) fclass.nm p9,p10 = f8, 0xff
472 (p9) frcpa f11,p7=f8,f0
478 (p10) frcpa f11,p7 = f9,f9
479 (p0) mov GR_Parameter_TAG = 121 ;;
484 (p0) fmerge.s f10 = f8, f8
490 (p0) fma.d f8=f11,f1,f0
491 (p0) br.sptk __libm_error_region;;
495 ASM_SIZE_DIRECTIVE(fmod)
496 ASM_SIZE_DIRECTIVE(__ieee754_fmod)
498 .proc __libm_error_region
502 add GR_Parameter_Y=-32,sp // Parameter 2 value
504 .save ar.pfs,GR_SAVE_PFS
505 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
509 add sp=-64,sp // Create new stack
511 mov GR_SAVE_GP=gp // Save gp
514 stfd [GR_Parameter_Y] = FR_Y,16 // Save Parameter 2 on stack
515 add GR_Parameter_X = 16,sp // Parameter 1 address
517 mov GR_SAVE_B0=b0 // Save b0
521 stfd [GR_Parameter_X] = FR_X // Store Parameter 1 on stack
522 add GR_Parameter_RESULT = 0,GR_Parameter_Y
523 nop.b 0 // Parameter 3 address
526 stfd [GR_Parameter_Y] = FR_RESULT // Store Parameter 3 on stack
527 add GR_Parameter_Y = -16,GR_Parameter_Y
528 br.call.sptk b0=__libm_error_support# // Call error handling function
533 add GR_Parameter_RESULT = 48,sp
536 ldfd f8 = [GR_Parameter_RESULT] // Get return result off stack
538 add sp = 64,sp // Restore stack pointer
539 mov b0 = GR_SAVE_B0 // Restore return address
542 mov gp = GR_SAVE_GP // Restore gp
543 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
544 br.ret.sptk b0 // Return
547 .endp __libm_error_region
548 ASM_SIZE_DIRECTIVE(__libm_error_region)
550 .type __libm_error_support#,@function
551 .global __libm_error_support#