Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb6e1d5

Browse files
authored
Merge pull request numpy#11184 from eric-wieser/dragon4-bithelpers
MAINT: Add bitmask helper functions
2 parents a5f7d64 + 3d21112 commit bb6e1d5

1 file changed

Lines changed: 40 additions & 28 deletions

File tree

numpy/core/src/multiarray/dragon4.c

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@
4242
#define DEBUG_ASSERT(stmnt) do {} while(0)
4343
#endif
4444

45+
static inline npy_uint64
46+
bitmask_u64(npy_uint32 n)
47+
{
48+
return ~(~((npy_uint64)0) << n);
49+
}
50+
51+
static inline npy_uint32
52+
bitmask_u32(npy_uint32 n)
53+
{
54+
return ~(~((npy_uint32)0) << n);
55+
}
56+
4557
/*
4658
* Get the log base 2 of a 32-bit unsigned integer.
4759
* http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
@@ -139,13 +151,13 @@ BigInt_Copy(BigInt *dst, const BigInt *src)
139151
static void
140152
BigInt_Set_uint64(BigInt *i, npy_uint64 val)
141153
{
142-
if (val > 0xFFFFFFFF) {
143-
i->blocks[0] = val & 0xFFFFFFFF;
144-
i->blocks[1] = (val >> 32) & 0xFFFFFFFF;
154+
if (val > bitmask_u64(32)) {
155+
i->blocks[0] = val & bitmask_u64(32);
156+
i->blocks[1] = (val >> 32) & bitmask_u64(32);
145157
i->length = 2;
146158
}
147159
else if (val != 0) {
148-
i->blocks[0] = val & 0xFFFFFFFF;
160+
i->blocks[0] = val & bitmask_u64(32);
149161
i->length = 1;
150162
}
151163
else {
@@ -228,7 +240,7 @@ BigInt_Add(BigInt *result, const BigInt *lhs, const BigInt *rhs)
228240
npy_uint64 sum = carry + (npy_uint64)(*largeCur) +
229241
(npy_uint64)(*smallCur);
230242
carry = sum >> 32;
231-
*resultCur = sum & 0xFFFFFFFF;
243+
*resultCur = sum & bitmask_u64(32);
232244
++largeCur;
233245
++smallCur;
234246
++resultCur;
@@ -238,7 +250,7 @@ BigInt_Add(BigInt *result, const BigInt *lhs, const BigInt *rhs)
238250
while (largeCur != largeEnd) {
239251
npy_uint64 sum = carry + (npy_uint64)(*largeCur);
240252
carry = sum >> 32;
241-
(*resultCur) = sum & 0xFFFFFFFF;
253+
(*resultCur) = sum & bitmask_u64(32);
242254
++largeCur;
243255
++resultCur;
244256
}
@@ -307,13 +319,13 @@ BigInt_Multiply(BigInt *result, const BigInt *lhs, const BigInt *rhs)
307319
npy_uint64 product = (*resultCur) +
308320
(*largeCur)*(npy_uint64)multiplier + carry;
309321
carry = product >> 32;
310-
*resultCur = product & 0xFFFFFFFF;
322+
*resultCur = product & bitmask_u64(32);
311323
++largeCur;
312324
++resultCur;
313325
} while(largeCur != large->blocks + large->length);
314326

315327
DEBUG_ASSERT(resultCur < result->blocks + maxResultLen);
316-
*resultCur = (npy_uint32)(carry & 0xFFFFFFFF);
328+
*resultCur = (npy_uint32)(carry & bitmask_u64(32));
317329
}
318330
}
319331

@@ -337,7 +349,7 @@ BigInt_Multiply_int(BigInt *result, const BigInt *lhs, npy_uint32 rhs)
337349
const npy_uint32 *pLhsEnd = lhs->blocks + lhs->length;
338350
for ( ; pLhsCur != pLhsEnd; ++pLhsCur, ++resultCur) {
339351
npy_uint64 product = (npy_uint64)(*pLhsCur) * rhs + carry;
340-
*resultCur = (npy_uint32)(product & 0xFFFFFFFF);
352+
*resultCur = (npy_uint32)(product & bitmask_u64(32));
341353
carry = product >> 32;
342354
}
343355

@@ -414,7 +426,7 @@ BigInt_Multiply10(BigInt *result)
414426
npy_uint32 *end = result->blocks + result->length;
415427
for ( ; cur != end; ++cur) {
416428
npy_uint64 product = (npy_uint64)(*cur) * 10ull + carry;
417-
(*cur) = (npy_uint32)(product & 0xFFFFFFFF);
429+
(*cur) = (npy_uint32)(product & bitmask_u64(32));
418430
carry = product >> 32;
419431
}
420432

@@ -654,7 +666,7 @@ BigInt_Pow10(BigInt *result, npy_uint32 exponent)
654666
* initialize the result by looking up a 32-bit power of 10 corresponding to
655667
* the first 3 bits
656668
*/
657-
smallExponent = exponent & 0x7;
669+
smallExponent = exponent & bitmask_u32(3);
658670
BigInt_Set_uint32(curTemp, g_PowerOf10_U32[smallExponent]);
659671

660672
/* remove the low bits that we used for the 32-bit lookup table */
@@ -704,7 +716,7 @@ BigInt_MultiplyPow10(BigInt *result, const BigInt *in, npy_uint32 exponent)
704716
* initialize the result by looking up a 32-bit power of 10 corresponding to
705717
* the first 3 bits
706718
*/
707-
smallExponent = exponent & 0x7;
719+
smallExponent = exponent & bitmask_u32(3);
708720
if (smallExponent != 0) {
709721
BigInt_Multiply_int(curTemp, in, g_PowerOf10_U32[smallExponent]);
710722
}
@@ -788,7 +800,7 @@ BigInt_DivideWithRemainder_MaxQuotient9(BigInt *dividend, const BigInt *divisor)
788800
*/
789801
DEBUG_ASSERT(!divisor->length == 0 &&
790802
divisor->blocks[divisor->length-1] >= 8 &&
791-
divisor->blocks[divisor->length-1] < 0xFFFFFFFF &&
803+
divisor->blocks[divisor->length-1] < bitmask_u64(32) &&
792804
dividend->length <= divisor->length);
793805

794806
/*
@@ -825,10 +837,10 @@ BigInt_DivideWithRemainder_MaxQuotient9(BigInt *dividend, const BigInt *divisor)
825837
carry = product >> 32;
826838

827839
difference = (npy_uint64)*dividendCur
828-
- (product & 0xFFFFFFFF) - borrow;
840+
- (product & bitmask_u64(32)) - borrow;
829841
borrow = (difference >> 32) & 1;
830842

831-
*dividendCur = difference & 0xFFFFFFFF;
843+
*dividendCur = difference & bitmask_u64(32);
832844

833845
++divisorCur;
834846
++dividendCur;
@@ -860,7 +872,7 @@ BigInt_DivideWithRemainder_MaxQuotient9(BigInt *dividend, const BigInt *divisor)
860872
- (npy_uint64)*divisorCur - borrow;
861873
borrow = (difference >> 32) & 1;
862874

863-
*dividendCur = difference & 0xFFFFFFFF;
875+
*dividendCur = difference & bitmask_u64(32);
864876

865877
++divisorCur;
866878
++dividendCur;
@@ -1442,8 +1454,8 @@ typedef union FloatUnion16
14421454
} FloatUnion16;
14431455

14441456
npy_bool IsNegative_F16(FloatUnion16 *v) { return (v->integer >> 15) != 0; }
1445-
npy_uint32 GetExponent_F16(FloatUnion16 *v) { return (v->integer >> 10) & 0x1F;}
1446-
npy_uint32 GetMantissa_F16(FloatUnion16 *v) { return v->integer & 0x3FF; }
1457+
npy_uint32 GetExponent_F16(FloatUnion16 *v) { return (v->integer >> 10) & bitmask_u32(5);}
1458+
npy_uint32 GetMantissa_F16(FloatUnion16 *v) { return v->integer & bitmask_u32(10); }
14471459

14481460

14491461
/*
@@ -1459,8 +1471,8 @@ typedef union FloatUnion32
14591471
} FloatUnion32;
14601472

14611473
npy_bool IsNegative_F32(FloatUnion32 *v) { return (v->integer >> 31) != 0; }
1462-
npy_uint32 GetExponent_F32(FloatUnion32 *v) { return (v->integer >> 23) & 0xFF;}
1463-
npy_uint32 GetMantissa_F32(FloatUnion32 *v) { return v->integer & 0x7FFFFF; }
1474+
npy_uint32 GetExponent_F32(FloatUnion32 *v) { return (v->integer >> 23) & bitmask_u32(8);}
1475+
npy_uint32 GetMantissa_F32(FloatUnion32 *v) { return v->integer & bitmask_u32(23); }
14641476

14651477
/*
14661478
* Helper union to decompose a 64-bit IEEE float.
@@ -1474,8 +1486,8 @@ typedef union FloatUnion64
14741486
npy_uint64 integer;
14751487
} FloatUnion64;
14761488
npy_bool IsNegative_F64(FloatUnion64 *v) { return (v->integer >> 63) != 0; }
1477-
npy_uint32 GetExponent_F64(FloatUnion64 *v) { return (v->integer >> 52) & 0x7FF; }
1478-
npy_uint64 GetMantissa_F64(FloatUnion64 *v) { return v->integer & 0xFFFFFFFFFFFFFull; }
1489+
npy_uint32 GetExponent_F64(FloatUnion64 *v) { return (v->integer >> 52) & bitmask_u32(11); }
1490+
npy_uint64 GetMantissa_F64(FloatUnion64 *v) { return v->integer & bitmask_u64(52); }
14791491

14801492
/*
14811493
* Helper unions and datatype to decompose a 80-bit IEEE float
@@ -1496,9 +1508,9 @@ typedef struct FloatVal128 {
14961508
npy_bool IsNegative_F128(FloatVal128 *v) {
14971509
return ((v->integer[1] >> 15) & 0x1) != 0;
14981510
}
1499-
npy_uint32 GetExponent_F128(FloatVal128 *v) { return v->integer[1] & 0x7FFF; }
1511+
npy_uint32 GetExponent_F128(FloatVal128 *v) { return v->integer[1] & bitmask_u32(15); }
15001512
npy_uint64 GetMantissa_F128(FloatVal128 *v) {
1501-
return v->integer[0] & 0x7FFFFFFFFFFFFFFFull;
1513+
return v->integer[0] & bitmask_u64(63);
15021514
}
15031515

15041516
/*
@@ -2151,7 +2163,7 @@ Dragon4_PrintFloat16(char *buffer, npy_uint32 bufferSize, npy_uint16 value,
21512163
}
21522164

21532165
/* if this is a special value */
2154-
if (floatExponent == 0x1F) {
2166+
if (floatExponent == bitmask_u32(5)) {
21552167
return PrintInfNan(buffer, bufferSize, floatMantissa, 3, signbit);
21562168
}
21572169
/* else this is a number */
@@ -2248,7 +2260,7 @@ Dragon4_PrintFloat32(char *buffer, npy_uint32 bufferSize, npy_float32 value,
22482260
}
22492261

22502262
/* if this is a special value */
2251-
if (floatExponent == 0xFF) {
2263+
if (floatExponent == bitmask_u32(8)) {
22522264
return PrintInfNan(buffer, bufferSize, floatMantissa, 6, signbit);
22532265
}
22542266
/* else this is a number */
@@ -2346,7 +2358,7 @@ Dragon4_PrintFloat64(char *buffer, npy_uint32 bufferSize, npy_float64 value,
23462358
}
23472359

23482360
/* if this is a special value */
2349-
if (floatExponent == 0x7FF) {
2361+
if (floatExponent == bitmask_u32(11)) {
23502362
return PrintInfNan(buffer, bufferSize, floatMantissa, 13, signbit);
23512363
}
23522364
/* else this is a number */
@@ -2442,7 +2454,7 @@ Dragon4_PrintFloat128(char *buffer, npy_uint32 bufferSize, FloatVal128 value,
24422454
}
24432455

24442456
/* if this is a special value */
2445-
if (floatExponent == 0x7FFF) {
2457+
if (floatExponent == bitmask_u32(15)) {
24462458
return PrintInfNan(buffer, bufferSize, floatMantissa, 16, signbit);
24472459
}
24482460
/* else this is a number */

0 commit comments

Comments
 (0)