1 //===----------------------Hexagon builtin routine ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /* Double Precision Divide */
37 #define REMSUB2 r15:14
48 #define RECIPEST_SHIFT 3
51 #define DFCLASS_NORMAL 0x02
52 #define DFCLASS_NUMBER 0x0F
53 #define DFCLASS_INFINITE 0x08
54 #define DFCLASS_ZERO 0x01
55 #define DFCLASS_NONZERO (DFCLASS_NUMBER ^ DFCLASS_ZERO)
56 #define DFCLASS_NONINFINITE (DFCLASS_NUMBER ^ DFCLASS_INFINITE)
58 #define DF_MANTBITS 52
60 #define SF_MANTBITS 23
64 #define SR_ROUND_OFF 22
66 #define Q6_ALIAS(TAG) .global __qdsp_##TAG ; .set __qdsp_##TAG, __hexagon_##TAG
67 #define FAST_ALIAS(TAG) .global __hexagon_fast_##TAG ; .set __hexagon_fast_##TAG, __hexagon_##TAG
68 #define FAST2_ALIAS(TAG) .global __hexagon_fast2_##TAG ; .set __hexagon_fast2_##TAG, __hexagon_##TAG
69 #define END(TAG) .size TAG,.-TAG
72 .global __hexagon_divdf3
73 .type __hexagon_divdf3,@function
80 NORMAL = dfclass(A,#DFCLASS_NORMAL)
81 NORMAL = dfclass(B,#DFCLASS_NORMAL)
82 EXPBA = combine(BH,AH)
98 if (!NORMAL) jump .Ldiv_abnormal
99 PROD = extractu(DENOM,#SF_MANTBITS,#DF_MANTBITS-SF_MANTBITS)
103 SFDEN = or(SFONE,PRODLO)
104 EXPB = extractu(EXPB,#DF_EXPBITS,#DF_MANTBITS-32)
105 EXPA = extractu(EXPA,#DF_EXPBITS,#DF_MANTBITS-32)
106 Q_POSITIVE = cmp.gt(SIGN,#-1)
112 SFRECIP,P_TMP = sfrecipa(SFONE,SFDEN)
113 SFERROR = and(SFONE,#-2)
115 EXPA = sub(EXPA,EXPB)
120 SFERROR -= sfmpy(SFRECIP,SFDEN):lib
121 REMHI = insert(ONE,#DF_EXPBITS+1,#DF_MANTBITS-32)
122 RECIPEST = ##0x00800000 << RECIPEST_SHIFT
125 SFRECIP += sfmpy(SFRECIP,SFERROR):lib
126 DENOMHI = insert(ONE,#DF_EXPBITS+1,#DF_MANTBITS-32)
127 SFERROR = and(SFONE,#-2)
130 SFERROR -= sfmpy(SFRECIP,SFDEN):lib
135 SFRECIP += sfmpy(SFRECIP,SFERROR):lib
136 NO_OVF_UNF = cmp.gt(EXPA,QH)
137 NO_OVF_UNF = !cmp.gt(EXPA,QL)
140 RECIPEST = insert(SFRECIP,#SF_MANTBITS,#RECIPEST_SHIFT)
142 EXPA = add(EXPA,#-QADJ)
149 RECIPEST = add(RECIPEST,#((-3) << RECIPEST_SHIFT))
152 #define DIV_ITER1B(QSHIFTINSN,QSHIFT,REMSHIFT,EXTRA) \
154 PROD = mpyu(RECIPEST,REMHI); \
155 REM = asl(REM,# ## ( REMSHIFT )); \
159 REM -= mpyu(PRODHI,DENOMLO); \
160 REMSUB2 = mpyu(PRODHI,DENOMHI); \
163 Q += QSHIFTINSN(PROD, # ## ( QSHIFT )); \
164 REM -= asl(REMSUB2, # ## 32); \
169 DIV_ITER1B(ASL,14,15,)
170 DIV_ITER1B(ASR,1,15,)
171 DIV_ITER1B(ASR,16,15,)
172 DIV_ITER1B(ASR,31,15,PROD=# ( 0 );)
175 #define TMPPAIR r15:14
176 #define TMPPAIRHI r15
177 #define TMPPAIRLO r14
181 // compare or sub with carry
182 TMPPAIR = sub(REM,DENOM)
183 P_TMP = cmp.gtu(DENOM,REM)
184 // set up amt to add to q
185 if (!P_TMP.new) PRODLO = #2
189 if (!P_TMP) REM = TMPPAIR
193 P_TMP = cmp.eq(REM,TMPPAIR)
194 if (!P_TMP.new) QL = or(QL,ONE)
200 if (!Q_POSITIVE) Q = PROD
216 if (!NO_OVF_UNF) jump .Ldiv_ovf_unf
219 AH += asl(EXPA,#DF_MANTBITS-32)
225 AH += asl(EXPA,#DF_MANTBITS-32)
226 EXPB = extractu(AH,#DF_EXPBITS,#DF_MANTBITS-32)
230 EXPA = add(EXPA,EXPB)
233 P_TMP = cmp.gt(EXPA,##DF_BIAS+DF_BIAS) // overflow
234 if (P_TMP.new) jump:nt .Ldiv_ovf
237 P_TMP = cmp.gt(EXPA,#0)
238 if (P_TMP.new) jump:nt .Lpossible_unf // round up to normal possible...
241 /* We know what the infinite range exponent should be (EXPA) */
242 /* Q is 2's complement, PROD is abs(Q) */
243 /* Normalize Q, shift right, add a high bit, convert, change exponent */
245 #define FUDGE1 7 // how much to shift right
246 #define FUDGE2 4 // how many guard/round to keep at lsbs
249 EXPB = add(clb(PROD),#-1) // doesn't need to be added in since
250 EXPA = sub(#FUDGE1,EXPA) // we extract post-converted exponent
255 EXPB = min(EXPA,TMP1)
256 TMP1 = or(TMP,#0x030)
257 PROD = asl(PROD,EXPB)
261 TMPPAIR = extractu(PROD,EXPBA) // bits that will get shifted out
262 PROD = lsr(PROD,EXPB) // shift out bits
266 P_TMP = cmp.gtu(B,TMPPAIR)
267 if (!P_TMP.new) PRODLO = or(BL,PRODLO)
268 PRODHI = setbit(PRODHI,#DF_MANTBITS-32+FUDGE2)
272 P_TMP = bitsclr(PRODLO,#(1<<FUDGE2)-1)
273 if (!P_TMP.new) TMP = TMP1
277 if (Q_POSITIVE) Q = PROD
278 TMP = #-DF_BIAS-(DF_MANTBITS+FUDGE2)
284 AH += asl(TMP,#DF_MANTBITS-32)
290 /* If upper parts of Q were all F's, but abs(A) == 0x00100000_00000000, we rounded up to min_normal */
291 /* The answer is correct, but we need to raise Underflow */
293 B = extractu(A,#63,#0)
294 TMPPAIR = combine(##0x00100000,#0) // min normal
298 P_TMP = dfcmp.eq(TMPPAIR,B) // Is everything zero in the rounded value...
299 P_TMP = bitsset(PRODHI,TMP) // but a bunch of bits set in the unrounded abs(quotient)?
302 #if (__HEXAGON_ARCH__ == 60)
303 TMP = USR // If not, just return
304 if (!P_TMP) jumpr r31 // Else, we want to set Unf+Inexact
305 // Note that inexact is already set...
308 if (!P_TMP) jumpr r31 // If not, just return
309 TMP = USR // Else, we want to set Unf+Inexact
310 } // Note that inexact is already set...
325 * Raise Overflow, and choose the correct overflow value (saturated normal or infinity)
329 B = combine(##0x7fefffff,#-1)
330 AH = mux(Q_POSITIVE,#0,#-1)
333 PROD = combine(##0x7ff00000,#0)
334 QH = extractu(TMP,#2,#SR_ROUND_OFF)
343 p0 = !cmp.eq(QL,#1) // if not round-to-zero
344 p0 = !cmp.eq(QH,#2) // and not rounding the other way
345 if (p0.new) B = PROD // go to inf
346 p0 = dfcmp.eq(B,B) // get exceptions
361 P_TMP = dfclass(A,#DFCLASS_NUMBER)
362 P_TMP = dfclass(B,#DFCLASS_NUMBER)
363 Q_POSITIVE = cmp.gt(SIGN,#-1)
366 P_INF = dfclass(A,#DFCLASS_INFINITE)
367 P_INF = dfclass(B,#DFCLASS_INFINITE)
370 P_ZERO = dfclass(A,#DFCLASS_ZERO)
371 P_ZERO = dfclass(B,#DFCLASS_ZERO)
374 if (!P_TMP) jump .Ldiv_nan
375 if (P_INF) jump .Ldiv_invalid
378 if (P_ZERO) jump .Ldiv_invalid
381 P_ZERO = dfclass(A,#DFCLASS_NONZERO) // nonzero
382 P_ZERO = dfclass(B,#DFCLASS_NONINFINITE) // non-infinite
385 P_INF = dfclass(A,#DFCLASS_NONINFINITE) // non-infinite
386 P_INF = dfclass(B,#DFCLASS_NONZERO) // nonzero
389 if (!P_ZERO) jump .Ldiv_zero_result
390 if (!P_INF) jump .Ldiv_inf_result
392 /* Now we've narrowed it down to (de)normal / (de)normal */
393 /* Set up A/EXPA B/EXPB and go back */
398 P_TMP = dfclass(A,#DFCLASS_NORMAL)
399 P_TMP2 = dfclass(B,#DFCLASS_NORMAL)
403 EXPBA = combine(BH,AH)
404 AH = insert(TMP,#DF_EXPBITS+1,#DF_MANTBITS-32) // clear out hidden bit, sign bit
405 BH = insert(TMP,#DF_EXPBITS+1,#DF_MANTBITS-32) // clear out hidden bit, sign bit
408 if (P_TMP) AH = or(AH,TMP) // if normal, add back in hidden bit
409 if (P_TMP2) BH = or(BH,TMP) // if normal, add back in hidden bit
412 QH = add(clb(A),#-DF_EXPBITS)
413 QL = add(clb(B),#-DF_EXPBITS)
417 EXPA = extractu(EXPA,#DF_EXPBITS,#DF_MANTBITS-32)
418 EXPB = extractu(EXPB,#DF_EXPBITS,#DF_MANTBITS-32)
423 if (!P_TMP) EXPA = sub(TMP,QH)
424 if (!P_TMP2) EXPB = sub(TMP,QL)
425 } // recreate values needed by resume coke
427 PROD = extractu(B,#SF_MANTBITS,#DF_MANTBITS-SF_MANTBITS)
430 SFDEN = or(SFONE,PRODLO)
431 jump .Ldenorm_continue
445 p2 = dfclass(B,#DFCLASS_ZERO)
446 p2 = dfclass(A,#DFCLASS_NONINFINITE)
454 TMP = or(TMP,#0x04) // DBZ
461 B = combine(##0x7ff00000,#0)
462 p0 = dfcmp.uo(B,B) // take possible exception
470 p0 = dfclass(A,#0x10)
471 p1 = dfclass(B,#0x10)
476 QH = convert_df2sf(A) // get possible invalid exceptions
477 QL = convert_df2sf(B)
489 A = convert_sf2df(TMP) // get invalid, get DF qNaN
492 END(__hexagon_divdf3)