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
// 07/21/00 Fixed quotient=2^{24*m+23}*1.q1...q23 1 bug
46
// 08/15/00 Bundle added after call to __libm_error_support to properly
47
// set [the previously overwritten] GR_Parameter_RESULT.
48
// 11/29/00 Set FR_Y to f9
49
// 05/20/02 Cleaned up namespace and sf0 syntax
50
// 02/10/03 Reordered header: .section, .global, .proc, .align
53
//====================================================================
54
// long double remainderl(long double,long double);
56
// Overview of operation
57
//====================================================================
58
// remainder(a,b)=a-i*b,
59
// where i is an integer such that, if b!=0 and a is finite,
60
// |a/b-i|<=1/2. If |a/b-i|=1/2, i is even.
63
//====================================================================
64
// a). eliminate special cases
65
// b). if |a/b|<0.25 (first quotient estimate), return a
66
// c). use single precision divide algorithm to get quotient q
67
// rounded to 24 bits of precision
68
// d). calculate partial remainders (using both q and q-ulp);
69
// select one and RZ(a/b) based on the sign of |a|-|b|*q
70
// e). if the exponent difference (exponent(a)-exponent(b))
71
// is less than 24 (quotient estimate<2^{24}-2), use RZ(a/b)
72
// and sticky bits to round to integer; exit loop and
73
// calculate final remainder
74
// f). if exponent(a)-exponent(b)>=24, select new value of a as
75
// the partial remainder calculated using RZ(a/b);
79
//====================================================================
80
// a=+/- Inf, or b=+/-0: return NaN, call libm_error_support
81
// a=NaN or b=NaN: return NaN
84
//====================================================================
85
// Predicate registers: p6-p14
86
// General registers: r2,r3,r28,r29,r32 (ar.pfs), r33-r39
87
// Floating point registers: f6-f15,f32
97
GR_Parameter_RESULT = r39
98
GR_Parameter_TAG = r40
106
GLOBAL_IEEE754_ENTRY(remainderl)
112
alloc r32=ar.pfs,1,4,4,0
134
cmp.eq p11,p10=r29,r0;;
137
// Y +-NAN, +-inf, +-0? p11
140
(p10) fclass.m p11,p10 = f9, 0xe7
143
// qnan snan inf norm unorm 0 -+
146
// X +-NAN, +-inf, ? p9
149
fclass.m.unc p9,p8 = f8, 0xe3
160
cmp.eq.unc p7,p0=r0,r0
163
frcpa.s1 f10,p6=f13,f14
166
// Y +-NAN, +-inf, +-0? p11
170
(p10) fclass.nm p11,p0 = f9, 0xff
174
// qnan snan inf norm unorm 0 -+
177
// X +-NAN, +-inf, ? p9
181
(p8) fclass.nm p9,p0 = f8, 0xff
186
(p9) br.cond.spnt FREM_X_NAN_INF
187
(p11) br.cond.spnt FREM_Y_NAN_INF_ZERO
191
// set D flag if a (f8) is denormal
200
// q0 = a * y0 in f15
201
(p6) fma.s1 f12=f13,f10,f0
206
// e0 = 1 - b * y0 in f7
207
(p6) fnma.s1 f7=f14,f10,f1
218
(p6) fma.s1 f15=f12,f7,f12
224
// e1 = e0 * e0 + E in f7
225
(p6) fma.s1 f7=f7,f7,f32
229
(p7) getf.exp r29=f12
238
// q2 = q1 + e1 * q1 in f11
239
(p6) fma.s.s1 f11=f7,f15,f15
244
// q2 = q1 + e1 * q1 in f6
245
(p6) fma.s1 f6=f7,f15,f15
252
// q<1/4 ? (i.e. expon< -2)
253
(p7) cmp.gt p7,p0=r28,r29
260
// if |a/b|<1/4, set D flag before returning
261
(p7) fma.s0 f9=f9,f0,f8
266
// can be combined with bundle above if sign of 0 or
267
// FTZ enabled are not important
268
(p7) fmerge.s f8=f8,f9
269
// return if |a|<4*|b| (estimated quotient < 1/4)
270
(p7) br.ret.spnt b0;;
275
// set f8 to current a value | sign
281
// last step ? (q<2^{23})
282
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
299
cmp.eq p11,p14=r2,r28
303
.pred.rel "mutex",p11,p14
306
// if exp_q=2^23, then r=a-b*2^{23}
307
(p11) fnma.s1 f13=f12,f14,f13
313
(p14) fnma.s1 f13=f14,f15,f13
318
// r>0 iff q=RZ(a/b) and inexact
319
fcmp.gt.unc.s1 p8,p0=f6,f0
323
// r<0 iff q'=RZ(a/b) and inexact
324
(p14) fcmp.lt.unc.s1 p9,p10=f6,f0
328
.pred.rel "mutex",p8,p9
331
// (p8) Q=q+(last iteration ? sticky bits:0)
332
// i.e. Q=q+q*x (x=2^{-32} or 0)
333
(p8) fma.s1 f11=f11,f7,f11
337
// (p9) Q=q'+(last iteration ? sticky bits:0)
338
// i.e. Q=q'+q'*x (x=2^{-32} or 0)
339
(p9) fma.s1 f11=f15,f7,f15
345
// (p9) set r=r2 (new a, if not last iteration)
348
(p12) br.cond.sptk remloop24;;
377
// This can be removed if sign of 0 is not important
378
// get remainder using sf1
379
fnma.s1 f12=f9,f11,f8
391
// This can be removed if sign of 0 is not important
392
fcmp.eq.unc.s1 p8,p0=f12,f0
397
// if f8=0, set sign correctly
398
// This can be removed if sign of 0 is not important
399
(p8) fmerge.s f8=f7,f8
416
fcmp.eq.unc.s1 p11,p0=f10,f0
423
(p11) br.cond.spnt FREM_Y_ZERO;;
426
// X infinity? Return QNAN indefinite
429
fclass.m.unc p8,p0 = f8, 0x23
432
// X infinity? Return QNAN indefinite
435
fclass.m.unc p11,p0 = f8, 0x23
441
(p8) fclass.m.unc p0,p8=f9,0xc3
446
// also set Denormal flag if necessary
447
(p8) fnma.s0 f9=f9,f1,f9
452
(p8) frcpa.s0 f8,p7 = f8,f8
463
(p8) fma.s0 f8=f8,f1,f0
470
(p11) br.cond.spnt EXP_ERROR_RETURN;;
483
fclass.m.unc p7,p0 = f9, 0x23
489
(p7) fma.s0 f8=f8,f1,f0
490
(p7) br.ret.spnt b0 ;;
496
fclass.m.unc p9,p10 = f9, 0xc3
501
(p10) fclass.nm p9,p0 = f9, 0xff
507
(p9) fma.s0 f8=f9,f1,f0
508
(p9) br.ret.spnt b0 ;;
512
// Y zero? Must be zero at this point
513
// because it is the only choice left.
514
// Return QNAN indefinite
519
fclass.m.unc p9,p10 = f8, 0xc3
524
(p10) fclass.nm p9,p10 = f8, 0xff
530
(p9) frcpa.s0 f11,p7=f8,f0
535
(p10) frcpa.s0 f11,p7 = f0,f0
541
fmerge.s f10 = f8, f8
554
mov GR_Parameter_TAG = 123
556
br.sptk __libm_error_region;;
559
GLOBAL_IEEE754_END(remainderl)
561
LOCAL_LIBM_ENTRY(__libm_error_region)
564
add GR_Parameter_Y=-32,sp // Parameter 2 value
566
.save ar.pfs,GR_SAVE_PFS
567
mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
571
add sp=-64,sp // Create new stack
573
mov GR_SAVE_GP=gp // Save gp
576
stfe [GR_Parameter_Y] = FR_Y,16 // Save Parameter 2 on stack
577
add GR_Parameter_X = 16,sp // Parameter 1 address
579
mov GR_SAVE_B0=b0 // Save b0
583
stfe [GR_Parameter_X] = FR_X // Store Parameter 1 on stack
584
add GR_Parameter_RESULT = 0,GR_Parameter_Y
585
nop.b 0 // Parameter 3 address
588
stfe [GR_Parameter_Y] = FR_RESULT // Store Parameter 3 on stack
589
add GR_Parameter_Y = -16,GR_Parameter_Y
590
br.call.sptk b0=__libm_error_support# // Call error handling function
595
add GR_Parameter_RESULT = 48,sp
598
ldfe f8 = [GR_Parameter_RESULT] // Get return result off stack
600
add sp = 64,sp // Restore stack pointer
601
mov b0 = GR_SAVE_B0 // Restore return address
604
mov gp = GR_SAVE_GP // Restore gp
605
mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
606
br.ret.sptk b0 // Return
609
LOCAL_LIBM_END(__libm_error_region)
612
.type __libm_error_support#,@function
613
.global __libm_error_support#