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 // 07/21/00 Fixed quotient=2^{24*m+23} bug
45 // 08/15/00 Bundle added after call to __libm_error_support to properly
46 // set [the previously overwritten] GR_Parameter_RESULT.
47 // 11/29/00 Set FR_Y to f9
48 // 05/20/02 Cleaned up namespace and sf0 syntax
49 // 02/10/03 Reordered header: .section, .global, .proc, .align
52 //====================================================================
53 // float remainderf(float,float);
55 // Overview of operation
56 //====================================================================
57 // remainder(a,b)=a-i*b,
58 // where i is an integer such that, if b!=0 and a is finite,
59 // |a/b-i|<=1/2. If |a/b-i|=1/2, i is even.
62 //====================================================================
63 // a). eliminate special cases
64 // b). if |a/b|<0.25 (first quotient estimate), return a
65 // c). use single precision divide algorithm to get quotient q
66 // rounded to 24 bits of precision
67 // d). calculate partial remainders (using both q and q-ulp);
68 // select one and RZ(a/b) based on the sign of |a|-|b|*q
69 // e). if the exponent difference (exponent(a)-exponent(b))
70 // is less than 24 (quotient estimate<2^{24}-2), use RZ(a/b)
71 // and sticky bits to round to integer; exit loop and
72 // calculate final remainder
73 // f). if exponent(a)-exponent(b)>=24, select new value of a as
74 // the partial remainder calculated using RZ(a/b);
78 //====================================================================
79 // a=+/- Inf, or b=+/-0: return NaN, call libm_error_support
80 // a=NaN or b=NaN: return NaN
83 //====================================================================
84 // Predicate registers: p6-p12
85 // General registers: r2,r3,r28,r29,r32 (ar.pfs), r33-r39
86 // Floating point registers: f6-f15
96 GR_Parameter_RESULT = r39
97 GR_Parameter_TAG = r40
105 GLOBAL_IEEE754_ENTRY(remainderf)
111 alloc r32=ar.pfs,1,4,4,0
128 // Y +-NAN, +-inf, +-0? p11
131 fclass.m.unc p11,p0 = f9, 0xe7
134 // qnan snan inf norm unorm 0 -+
137 // X +-NAN, +-inf, ? p9
140 fclass.m.unc p9,p0 = f8, 0xe3
151 cmp.eq.unc p7,p0=r0,r0
154 frcpa.s1 f10,p6=f13,f14
158 (p9) br.cond.spnt FREM_X_NAN_INF
159 (p11) br.cond.spnt FREM_Y_NAN_INF_ZERO
163 // set D flag if a (f8) is denormal
174 // q0 = a * y0 in f15
175 (p6) fma.s1 f15=f13,f10,f0
181 // e0 = 1 - b * y0 in f7
182 (p6) fnma.s1 f7=f14,f10,f1
193 // q1 = q0 + e0 * q0 in f6
194 (p6) fma.s1 f6=f7,f15,f15
200 // e1 = e0 * e0 in f7
201 (p6) fma.s1 f7=f7,f7,f0
205 (p7) getf.exp r29=f15
214 // q2 = q1 + e1 * q1 in f6
215 (p6) fma.s1 f6=f7,f6,f6
221 // e2 = e1 * e1 in f7
222 (p6) fma.s1 f7=f7,f7,f0
227 // q<1/4 ? (i.e. expon< -2)
228 (p7) cmp.gt.unc p7,p0=r28,r29
237 // if |a/b|<1/4, set D flag before returning
238 (p7) fma.s.s0 f9=f9,f0,f8
243 // can be combined with bundle above if sign of 0 or
244 // FTZ enabled are not important
245 (p7) fmerge.s f8=f8,f9
246 // return if |a|<4*|b| (estimated quotient < 1/4)
247 (p7) br.ret.spnt b0;;
251 // set f8 to current a value | sign
253 // r2=2^{-24}+2^{-48} (double prec.)
262 // q3 = q2 + e2 * q2 in f6
263 (p6) fma.d.s1 f6=f7,f6,f6
270 (p6) fma.s.s1 f11=f6,f1,f0
276 // last step ? (q3<2^{24}-2 --> q<2^{24})
277 fcmp.lt.unc.s1 p0,p12=f6,f12
288 fnma.s1 f6=f14,f11,f13
293 // q'=q-q*(1.25*2^{-24}) (q'=q-ulp)
294 fnma.s.s1 f15=f11,f15,f11
301 fnma.s1 f13=f14,f15,f13
306 // r>0 iff q=RZ(a/b) and inexact
307 fcmp.gt.unc.s1 p8,p0=f6,f0
312 // r<0 iff q'=RZ(a/b) and inexact
313 fcmp.lt.unc.s1 p9,p10=f6,f0
316 .pred.rel "mutex",p8,p9
319 // (p8) Q=q+(last iteration ? sticky bits:0)
320 // i.e. Q=q+q*x (x=2^{-32} or 0)
321 (p8) fma.s1 f11=f11,f12,f11
326 // (p9) Q=q'+(last iteration ? sticky bits:0)
327 // i.e. Q=q'+q'*x (x=2^{-24} or 0: if expon. difference=23, want to round back to q)
328 (p9) fma.s1 f11=f15,f7,f15
334 // (p9) set r=r2 (new a, if not last iteration)
337 (p12) br.cond.sptk remloop24;;
367 // This can be removed if sign of 0 is not important
368 // get remainder using sf1
369 fnma.s.s1 f12=f9,f11,f8
375 fnma.s.s0 f8=f9,f11,f8
384 // This can be removed if sign of 0 is not important
385 fcmp.eq.unc.s1 p8,p0=f12,f0
390 // if f8=0, set sign correctly
391 // This can be removed if sign of 0 is not important
392 (p8) fmerge.s f8=f7,f8
408 fcmp.eq.unc.s1 p11,p0=f10,f0
415 (p11) br.cond.spnt FREM_Y_ZERO;;
418 // X infinity? Return QNAN indefinite
421 fclass.m.unc p8,p0 = f8, 0x23
424 // X infinity? Return QNAN indefinite
427 fclass.m.unc p11,p0 = f8, 0x23
433 (p8) fclass.m.unc p0,p8=f9,0xc3
438 // also set Denormal flag if necessary
439 (p8) fma.s0 f9=f9,f1,f0
444 (p8) frcpa.s0 f8,p7 = f8,f8
455 (p8) fma.s.s0 f8=f8,f1,f0
462 (p11) br.cond.spnt EXP_ERROR_RETURN;;
476 fclass.m.unc p7,p0 = f9, 0x23
482 (p7) fma.s.s0 f8=f8,f1,f0
483 (p7) br.ret.spnt b0 ;;
489 fclass.m.unc p9,p0 = f9, 0xc3
495 (p9) fma.s.s0 f8=f9,f1,f0
496 (p9) br.ret.spnt b0 ;;
500 // Y zero? Must be zero at this point
501 // because it is the only choice left.
502 // Return QNAN indefinite
507 fclass.m.unc p9,p10 = f8, 0xc3
512 (p10) fclass.nm p9,p10 = f8, 0xff
518 (p9) frcpa.s0 f11,p7=f8,f0
524 (p10) frcpa.s0 f11,p7 = f0,f0
530 fmerge.s f10 = f8, f8
536 fma.s.s0 f8=f11,f1,f0
544 mov GR_Parameter_TAG = 125
546 br.sptk __libm_error_region;;
549 GLOBAL_IEEE754_END(remainderf)
550 libm_alias_float_other (__remainder, remainder)
551 weak_alias (__remainderf, dremf)
555 LOCAL_LIBM_ENTRY(__libm_error_region)
558 add GR_Parameter_Y=-32,sp // Parameter 2 value
560 .save ar.pfs,GR_SAVE_PFS
561 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
565 add sp=-64,sp // Create new stack
567 mov GR_SAVE_GP=gp // Save gp
570 stfs [GR_Parameter_Y] = FR_Y,16 // Save Parameter 2 on stack
571 add GR_Parameter_X = 16,sp // Parameter 1 address
573 mov GR_SAVE_B0=b0 // Save b0
577 stfs [GR_Parameter_X] = FR_X // Store Parameter 1 on stack
578 add GR_Parameter_RESULT = 0,GR_Parameter_Y
579 nop.b 0 // Parameter 3 address
582 stfs [GR_Parameter_Y] = FR_RESULT // Store Parameter 3 on stack
583 add GR_Parameter_Y = -16,GR_Parameter_Y
584 br.call.sptk b0=__libm_error_support#;; // Call error handling function
589 add GR_Parameter_RESULT = 48,sp
592 ldfs f8 = [GR_Parameter_RESULT] // Get return result off stack
594 add sp = 64,sp // Restore stack pointer
595 mov b0 = GR_SAVE_B0 // Restore return address
598 mov gp = GR_SAVE_GP // Restore gp
599 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
600 br.ret.sptk b0 // Return
603 LOCAL_LIBM_END(__libm_error_region)
606 .type __libm_error_support#,@function
607 .global __libm_error_support#