1 //===----------------------Hexagon builtin routine ------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Double Precision Divide
36 #define REMSUB2 r15:14
47 #define RECIPEST_SHIFT 3
50 #define DFCLASS_NORMAL 0x02
51 #define DFCLASS_NUMBER 0x0F
52 #define DFCLASS_INFINITE 0x08
53 #define DFCLASS_ZERO 0x01
54 #define DFCLASS_NONZERO (DFCLASS_NUMBER ^ DFCLASS_ZERO)
55 #define DFCLASS_NONINFINITE (DFCLASS_NUMBER ^ DFCLASS_INFINITE)
57 #define DF_MANTBITS 52
59 #define SF_MANTBITS 23
63 #define SR_ROUND_OFF 22
65 #define Q6_ALIAS(TAG) .global __qdsp_##TAG ; .set __qdsp_##TAG, __hexagon_##TAG
66 #define FAST_ALIAS(TAG) .global __hexagon_fast_##TAG ; .set __hexagon_fast_##TAG, __hexagon_##TAG
67 #define FAST2_ALIAS(TAG) .global __hexagon_fast2_##TAG ; .set __hexagon_fast2_##TAG, __hexagon_##TAG
68 #define END(TAG) .size TAG,.-TAG
71 .global __hexagon_divdf3
72 .type __hexagon_divdf3,@function
79 NORMAL = dfclass(A,#DFCLASS_NORMAL)
80 NORMAL = dfclass(B,#DFCLASS_NORMAL)
81 EXPBA = combine(BH,AH)
97 if (!NORMAL) jump .Ldiv_abnormal
98 PROD = extractu(DENOM,#SF_MANTBITS,#DF_MANTBITS-SF_MANTBITS)
102 SFDEN = or(SFONE,PRODLO)
103 EXPB = extractu(EXPB,#DF_EXPBITS,#DF_MANTBITS-32)
104 EXPA = extractu(EXPA,#DF_EXPBITS,#DF_MANTBITS-32)
105 Q_POSITIVE = cmp.gt(SIGN,#-1)
111 SFRECIP,P_TMP = sfrecipa(SFONE,SFDEN)
112 SFERROR = and(SFONE,#-2)
114 EXPA = sub(EXPA,EXPB)
119 SFERROR -= sfmpy(SFRECIP,SFDEN):lib
120 REMHI = insert(ONE,#DF_EXPBITS+1,#DF_MANTBITS-32)
121 RECIPEST = ##0x00800000 << RECIPEST_SHIFT
124 SFRECIP += sfmpy(SFRECIP,SFERROR):lib
125 DENOMHI = insert(ONE,#DF_EXPBITS+1,#DF_MANTBITS-32)
126 SFERROR = and(SFONE,#-2)
129 SFERROR -= sfmpy(SFRECIP,SFDEN):lib
134 SFRECIP += sfmpy(SFRECIP,SFERROR):lib
135 NO_OVF_UNF = cmp.gt(EXPA,QH)
136 NO_OVF_UNF = !cmp.gt(EXPA,QL)
139 RECIPEST = insert(SFRECIP,#SF_MANTBITS,#RECIPEST_SHIFT)
141 EXPA = add(EXPA,#-QADJ)
148 RECIPEST = add(RECIPEST,#((-3) << RECIPEST_SHIFT))
151 #define DIV_ITER1B(QSHIFTINSN,QSHIFT,REMSHIFT,EXTRA) \
153 PROD = mpyu(RECIPEST,REMHI); \
154 REM = asl(REM,# ## ( REMSHIFT )); \
158 REM -= mpyu(PRODHI,DENOMLO); \
159 REMSUB2 = mpyu(PRODHI,DENOMHI); \
162 Q += QSHIFTINSN(PROD, # ## ( QSHIFT )); \
163 REM -= asl(REMSUB2, # ## 32); \
168 DIV_ITER1B(ASL,14,15,)
169 DIV_ITER1B(ASR,1,15,)
170 DIV_ITER1B(ASR,16,15,)
171 DIV_ITER1B(ASR,31,15,PROD=# ( 0 );)
174 #define TMPPAIR r15:14
175 #define TMPPAIRHI r15
176 #define TMPPAIRLO r14
180 // compare or sub with carry
181 TMPPAIR = sub(REM,DENOM)
182 P_TMP = cmp.gtu(DENOM,REM)
183 // set up amt to add to q
184 if (!P_TMP.new) PRODLO = #2
188 if (!P_TMP) REM = TMPPAIR
192 P_TMP = cmp.eq(REM,TMPPAIR)
193 if (!P_TMP.new) QL = or(QL,ONE)
199 if (!Q_POSITIVE) Q = PROD
215 if (!NO_OVF_UNF) jump .Ldiv_ovf_unf
218 AH += asl(EXPA,#DF_MANTBITS-32)
224 AH += asl(EXPA,#DF_MANTBITS-32)
225 EXPB = extractu(AH,#DF_EXPBITS,#DF_MANTBITS-32)
229 EXPA = add(EXPA,EXPB)
232 P_TMP = cmp.gt(EXPA,##DF_BIAS+DF_BIAS) // overflow
233 if (P_TMP.new) jump:nt .Ldiv_ovf
236 P_TMP = cmp.gt(EXPA,#0)
237 if (P_TMP.new) jump:nt .Lpossible_unf // round up to normal possible...
240 // We know what the infinite range exponent should be (EXPA)
241 // Q is 2's complement, PROD is abs(Q)
242 // Normalize Q, shift right, add a high bit, convert, change exponent
244 #define FUDGE1 7 // how much to shift right
245 #define FUDGE2 4 // how many guard/round to keep at lsbs
248 EXPB = add(clb(PROD),#-1) // doesn't need to be added in since
249 EXPA = sub(#FUDGE1,EXPA) // we extract post-converted exponent
254 EXPB = min(EXPA,TMP1)
255 TMP1 = or(TMP,#0x030)
256 PROD = asl(PROD,EXPB)
260 TMPPAIR = extractu(PROD,EXPBA) // bits that will get shifted out
261 PROD = lsr(PROD,EXPB) // shift out bits
265 P_TMP = cmp.gtu(B,TMPPAIR)
266 if (!P_TMP.new) PRODLO = or(BL,PRODLO)
267 PRODHI = setbit(PRODHI,#DF_MANTBITS-32+FUDGE2)
271 P_TMP = bitsclr(PRODLO,#(1<<FUDGE2)-1)
272 if (!P_TMP.new) TMP = TMP1
276 if (Q_POSITIVE) Q = PROD
277 TMP = #-DF_BIAS-(DF_MANTBITS+FUDGE2)
283 AH += asl(TMP,#DF_MANTBITS-32)
289 // If upper parts of Q were all F's, but abs(A) == 0x00100000_00000000, we rounded up to min_normal
290 // The answer is correct, but we need to raise Underflow
292 B = extractu(A,#63,#0)
293 TMPPAIR = combine(##0x00100000,#0) // min normal
297 P_TMP = dfcmp.eq(TMPPAIR,B) // Is everything zero in the rounded value...
298 P_TMP = bitsset(PRODHI,TMP) // but a bunch of bits set in the unrounded abs(quotient)?
301 #if (__HEXAGON_ARCH__ == 60)
302 TMP = USR // If not, just return
303 if (!P_TMP) jumpr r31 // Else, we want to set Unf+Inexact
304 // Note that inexact is already set...
307 if (!P_TMP) jumpr r31 // If not, just return
308 TMP = USR // Else, we want to set Unf+Inexact
309 } // Note that inexact is already set...
324 // Raise Overflow, and choose the correct overflow value (saturated normal or infinity)
328 B = combine(##0x7fefffff,#-1)
329 AH = mux(Q_POSITIVE,#0,#-1)
332 PROD = combine(##0x7ff00000,#0)
333 QH = extractu(TMP,#2,#SR_ROUND_OFF)
342 p0 = !cmp.eq(QL,#1) // if not round-to-zero
343 p0 = !cmp.eq(QH,#2) // and not rounding the other way
344 if (p0.new) B = PROD // go to inf
345 p0 = dfcmp.eq(B,B) // get exceptions
360 P_TMP = dfclass(A,#DFCLASS_NUMBER)
361 P_TMP = dfclass(B,#DFCLASS_NUMBER)
362 Q_POSITIVE = cmp.gt(SIGN,#-1)
365 P_INF = dfclass(A,#DFCLASS_INFINITE)
366 P_INF = dfclass(B,#DFCLASS_INFINITE)
369 P_ZERO = dfclass(A,#DFCLASS_ZERO)
370 P_ZERO = dfclass(B,#DFCLASS_ZERO)
373 if (!P_TMP) jump .Ldiv_nan
374 if (P_INF) jump .Ldiv_invalid
377 if (P_ZERO) jump .Ldiv_invalid
380 P_ZERO = dfclass(A,#DFCLASS_NONZERO) // nonzero
381 P_ZERO = dfclass(B,#DFCLASS_NONINFINITE) // non-infinite
384 P_INF = dfclass(A,#DFCLASS_NONINFINITE) // non-infinite
385 P_INF = dfclass(B,#DFCLASS_NONZERO) // nonzero
388 if (!P_ZERO) jump .Ldiv_zero_result
389 if (!P_INF) jump .Ldiv_inf_result
391 // Now we've narrowed it down to (de)normal / (de)normal
392 // Set up A/EXPA B/EXPB and go back
397 P_TMP = dfclass(A,#DFCLASS_NORMAL)
398 P_TMP2 = dfclass(B,#DFCLASS_NORMAL)
402 EXPBA = combine(BH,AH)
403 AH = insert(TMP,#DF_EXPBITS+1,#DF_MANTBITS-32) // clear out hidden bit, sign bit
404 BH = insert(TMP,#DF_EXPBITS+1,#DF_MANTBITS-32) // clear out hidden bit, sign bit
407 if (P_TMP) AH = or(AH,TMP) // if normal, add back in hidden bit
408 if (P_TMP2) BH = or(BH,TMP) // if normal, add back in hidden bit
411 QH = add(clb(A),#-DF_EXPBITS)
412 QL = add(clb(B),#-DF_EXPBITS)
416 EXPA = extractu(EXPA,#DF_EXPBITS,#DF_MANTBITS-32)
417 EXPB = extractu(EXPB,#DF_EXPBITS,#DF_MANTBITS-32)
422 if (!P_TMP) EXPA = sub(TMP,QH)
423 if (!P_TMP2) EXPB = sub(TMP,QL)
424 } // recreate values needed by resume coke
426 PROD = extractu(B,#SF_MANTBITS,#DF_MANTBITS-SF_MANTBITS)
429 SFDEN = or(SFONE,PRODLO)
430 jump .Ldenorm_continue
444 p2 = dfclass(B,#DFCLASS_ZERO)
445 p2 = dfclass(A,#DFCLASS_NONINFINITE)
453 TMP = or(TMP,#0x04) // DBZ
460 B = combine(##0x7ff00000,#0)
461 p0 = dfcmp.uo(B,B) // take possible exception
469 p0 = dfclass(A,#0x10)
470 p1 = dfclass(B,#0x10)
475 QH = convert_df2sf(A) // get possible invalid exceptions
476 QL = convert_df2sf(B)
488 A = convert_sf2df(TMP) // get invalid, get DF qNaN
491 END(__hexagon_divdf3)