2 // Copyright (c) 2000, 2001, Intel Corporation
3 // All rights reserved.
5 // Contributed 2/2/2000 by John Harrison, Cristina Iordache, Ted Kubaska,
6 // Bob Norin, Shane Story, and Ping Tak Peter Tang of the Computational
7 // Software Lab, 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 // WARRANTY DISCLAIMER
26 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
34 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 // Intel Corporation is the author of this code, and requests that all
39 // problem reports or change requests be submitted to it directly at
40 // http://developer.intel.com/opensource.
43 //====================================================================
44 // 2/02/00 Initial version
45 // 3/02/00 New Algorithm
46 // 4/04/00 Unwind support added
47 // 8/15/00 Bundle added after call to __libm_error_support to properly
48 // set [the previously overwritten] GR_Parameter_RESULT.
49 //11/28/00 Set FR_Y to f9
52 //====================================================================
53 // float fmodf(float,float);
55 // Overview of operation
56 //====================================================================
58 // where i is an integer such that, if b!=0,
59 // |i|<|a/b| and |a/b-i|<1
62 //====================================================================
63 // a). if |a|<|b|, return a
64 // b). get quotient and reciprocal overestimates accurate to
66 // c). if the exponent difference (exponent(a)-exponent(b))
67 // is less than 32, truncate quotient to integer and
68 // finish in one iteration
69 // d). if exponent(a)-exponent(b)>=32 (q2>=2^32)
70 // round quotient estimate to single precision (k=RN(q2)),
71 // calculate partial remainder (a'=a-k*b),
72 // get quotient estimate (a'*y2), and repeat from c).
75 //====================================================================
76 // b=+/-0: return NaN, call libm_error_support
77 // a=+/-Inf, a=NaN or b=NaN: return NaN
80 //====================================================================
81 // Predicate registers: p6-p11
82 // General registers: r2,r29,r32 (ar.pfs), r33-r39
83 // Floating point registers: f6-f15
85 #include "libm_support.h"
96 GR_Parameter_RESULT = r39
97 GR_Parameter_TAG = r40
112 .global __ieee754_fmodf
113 .type __ieee754_fmodf,@function
120 alloc r32=ar.pfs,1,4,4,0
135 frcpa.s1 f10,p6=f6,f7
139 // eliminate special cases
140 // Y +-NAN, +-inf, +-0? p7
143 (p0) fclass.m.unc p7,p0 = f9, 0xe7
147 // qnan snan inf norm unorm 0 -+
150 // X +-NAN, +-inf, ? p9
154 (p0) fclass.m.unc p9,p0 = f8, 0xe3
158 // |x| < |y|? Return x p8
161 (p0) fcmp.lt.unc.s1 p8,p0 = f6,f7
167 // normalize y (if |x|<|y|)
168 (p8) fma.s0 f9=f9,f1,f0
175 (p6) fma.s1 f13=f6,f10,f0
180 // (3) e0 = 1 - b * y0
181 (p6) fnma.s1 f12=f7,f10,f1
187 // normalize x (if |x|<|y|)
188 (p8) fma.s.s0 f8=f8,f1,f0
192 (p9) br.cond.spnt L(FMOD_X_NAN_INF)
193 (p7) br.cond.spnt L(FMOD_Y_NAN_INF_ZERO)
194 // if |x|<|y|, return
195 (p8) br.ret.spnt b0;;
216 (p6) fma.s1 f13=f12,f13,f13
221 // (5) e1 = e0 * e0 + 2^-34
222 (p6) fma.s1 f14=f12,f12,f11
231 // (6) y1 = y0 + e0 * y0
232 (p6) fma.s1 f10=f12,f10,f10
236 // set f12=1.25*2^{-24}
239 (p6) fma.s1 f13=f13,f14,f13
249 // (8) y2 = y1 + e1 * y1
250 (p6) fma.s1 f10=f14,f10,f10
252 cmp.ne.and p6,p10=r0,r0;;
260 fcmp.lt.unc.s1 p8,p7=f13,f15
265 // will truncate quotient to integer, if exponent<32 (in advance)
266 fcvt.fx.trunc.s1 f11=f13
271 // if exponent>32, round quotient to single precision (perform in advance)
272 fma.s.s1 f13=f13,f1,f0
278 (p8) fmerge.s f12=f8,f1
283 // normalize truncated quotient
289 // calculate remainder (assuming f13=RZ(Q))
290 (p7) fnma.s1 f14=f13,f7,f6
295 // also if exponent>32, round quotient to single precision
296 // and subtract 1 ulp: q=q-q*(1.25*2^{-24})
297 (p7) fnma.s.s1 f11=f13,f12,f13
303 // (p8) calculate remainder (82-bit format)
304 (p8) fnma.s1 f11=f13,f7,f6
309 // (p7) calculate remainder (assuming f11=RZ(Q))
310 (p7) fnma.s1 f6=f11,f7,f6
317 // Final iteration (p8): is f6 the correct remainder (quotient was not overestimated) ?
318 (p8) fcmp.lt.unc.s1 p6,p10=f11,f0
323 // get new quotient estimation: a'*y2
324 (p7) fma.s1 f13=f14,f10,f0
329 // was f14=RZ(Q) ? (then new remainder f14>=0)
330 (p7) fcmp.lt.unc.s1 p7,p9=f14,f0
335 .pred.rel "mutex",p6,p10
338 // add b to estimated remainder (to cover the case when the quotient was overestimated)
339 // also set correct sign by using f9=|b|*sgn(a), f12=sgn(a)
340 (p6) fma.s.s0 f8=f11,f12,f9
345 // calculate remainder (single precision)
346 // set correct sign of result before returning
347 (p10) fma.s.s0 f8=f11,f12,f0
348 (p8) br.ret.sptk b0;;
352 // if f13!=RZ(Q), get alternative quotient estimation: a''*y2
353 (p7) fma.s1 f13=f6,f10,f0
358 // if f14 was RZ(Q), set remainder to f14
360 br.cond.sptk L(loop24);;
380 fcmp.eq.unc.s1 p11,p0=f10,f0
387 (p11) br.cond.spnt L(FMOD_Y_ZERO);;
390 // X infinity? Return QNAN indefinite
393 (p0) fclass.m.unc p8,p9 = f8, 0x23
399 (p8) fclass.m p9,p8=f9,0xc3
404 (p8) frcpa.s0 f8,p0 = f8,f8
409 // also set Denormal flag if necessary
410 (p8) fma.s0 f9=f9,f1,f0
416 (p8) fma.s f8=f8,f1,f0
422 (p9) frcpa.s0 f8,p7=f8,f9
427 L(FMOD_Y_NAN_INF_ZERO):
432 (p0) fclass.m.unc p7,p0 = f9, 0x23
438 (p7) fma.s f8=f8,f1,f0
439 (p7) br.ret.spnt b0 ;;
445 (p0) fclass.m.unc p9,p0 = f9, 0xc3
451 (p9) fma.s f8=f9,f1,f0
452 (p9) br.ret.spnt b0 ;;
456 // Y zero? Must be zero at this point
457 // because it is the only choice left.
458 // Return QNAN indefinite
469 (p0) fclass.m.unc p9,p10 = f8, 0xc3
474 (p10) fclass.nm p9,p10 = f8, 0xff
480 (p9) frcpa f11,p7=f8,f0
486 (p10) frcpa f11,p7 = f0,f0
492 (p0) fmerge.s f10 = f8, f8
498 (p0) fma.s f8=f11,f1,f0
507 (p0) mov GR_Parameter_TAG=122
508 (p0) br.sptk __libm_error_region;;
512 ASM_SIZE_DIRECTIVE(fmodf)
513 ASM_SIZE_DIRECTIVE(__ieee754_fmodf)
515 .proc __libm_error_region
519 add GR_Parameter_Y=-32,sp // Parameter 2 value
521 .save ar.pfs,GR_SAVE_PFS
522 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
526 add sp=-64,sp // Create new stack
528 mov GR_SAVE_GP=gp // Save gp
531 stfs [GR_Parameter_Y] = FR_Y,16 // Save Parameter 2 on stack
532 add GR_Parameter_X = 16,sp // Parameter 1 address
534 mov GR_SAVE_B0=b0 // Save b0
538 stfs [GR_Parameter_X] = FR_X // Store Parameter 1 on stack
539 add GR_Parameter_RESULT = 0,GR_Parameter_Y
540 nop.b 0 // Parameter 3 address
543 stfs [GR_Parameter_Y] = FR_RESULT // Store Parameter 3 on stack
544 add GR_Parameter_Y = -16,GR_Parameter_Y
545 br.call.sptk b0=__libm_error_support#;; // Call error handling function
550 add GR_Parameter_RESULT = 48,sp
553 ldfs f8 = [GR_Parameter_RESULT] // Get return result off stack
555 add sp = 64,sp // Restore stack pointer
556 mov b0 = GR_SAVE_B0 // Restore return address
559 mov gp = GR_SAVE_GP // Restore gp
560 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
561 br.ret.sptk b0 // Return
564 .endp __libm_error_region
565 ASM_SIZE_DIRECTIVE(__libm_error_region)
567 .type __libm_error_support#,@function
568 .global __libm_error_support#