Changeset 98498 in vbox
- Timestamp:
- Feb 7, 2023 10:13:45 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/uint64.h
r98103 r98498 56 56 * 57 57 * @returns true if they are, false if they aren't. 58 * @param pValue The input and output value.59 */ 60 DECLINLINE(bool) RTUInt64IsZero(P RTUINT64U pValue)58 * @param pValue The value to examine. 59 */ 60 DECLINLINE(bool) RTUInt64IsZero(PCRTUINT64U pValue) 61 61 { 62 62 #if ARCH_BITS >= 32 … … 73 73 74 74 /** 75 * Checks if the sign-bit is set. 76 * 77 * @returns Sign bit. 78 * @param pValue The value to examine. 79 */ 80 DECLINLINE(bool) RTUInt64IsSigned(PCRTUINT64U pValue) 81 { 82 #if ARCH_BITS >= 32 83 return RT_BOOL(pValue->DWords.dw1 & RT_BIT_32(31)); 84 #else 85 return RT_BOOL(pValue->Words.w3 & ((uint16_t)1 << 15)); 86 #endif 87 } 88 89 90 /** 75 91 * Set a 128-bit unsigned integer value to zero. 76 92 * … … 165 181 pResult->s.Hi = pValue1->s.Hi - pValue2->s.Hi; 166 182 if (pResult->s.Lo > pValue1->s.Lo) 183 pResult->s.Hi--; 184 return pResult; 185 } 186 187 188 /** 189 * Calculates the negated number (special case of subtraction) 190 * 191 * @returns pResult 192 * @param pResult The result variable. 193 * @param pValue The value to negate. 194 */ 195 DECLINLINE(PRTUINT64U) RTUInt64Neg(PRTUINT64U pResult, PCRTUINT64U pValue) 196 { 197 pResult->s.Lo = UINT32_C(0) - pValue->s.Lo; 198 pResult->s.Hi = UINT32_C(0) - pValue->s.Hi; 199 if (pResult->s.Lo > UINT32_C(0)) 167 200 pResult->s.Hi--; 168 201 return pResult; … … 311 344 312 345 DECLINLINE(PRTUINT64U) RTUInt64DivRem(PRTUINT64U pQuotient, PRTUINT64U pRemainder, PCRTUINT64U pValue1, PCRTUINT64U pValue2); 346 DECLINLINE(PRTUINT64U) RTUInt64DivRemSigned(PRTUINT64U pQuotient, PRTUINT64U pRemainder, PCRTUINT64U pValue1, PCRTUINT64U pValue2); 313 347 314 348 /** … … 339 373 RTUINT64U Ignored; 340 374 RTUInt64DivRem(&Ignored, pResult, pValue1, pValue2); 375 return pResult; 376 } 377 378 379 /** 380 * Divides a 64-bit signed integer value by another. 381 * 382 * @returns pResult 383 * @param pResult The result variable. 384 * @param pValue1 The dividend value. 385 * @param pValue2 The divisor value. 386 */ 387 DECLINLINE(PRTUINT64U) RTUInt64DivSigned(PRTUINT64U pResult, PCRTUINT64U pValue1, PCRTUINT64U pValue2) 388 { 389 RTUINT64U Ignored; 390 return RTUInt64DivRemSigned(pResult, &Ignored, pValue1, pValue2); 391 } 392 393 394 /** 395 * Divides a 64-bit unsigned integer value by another, returning the remainder. 396 * 397 * @returns pResult 398 * @param pResult The result variable (remainder). 399 * @param pValue1 The dividend value. 400 * @param pValue2 The divisor value. 401 */ 402 DECLINLINE(PRTUINT64U) RTUInt64ModSigned(PRTUINT64U pResult, PCRTUINT64U pValue1, PCRTUINT64U pValue2) 403 { 404 RTUINT64U Ignored; 405 RTUInt64DivRemSigned(&Ignored, pResult, pValue1, pValue2); 341 406 return pResult; 342 407 } … … 619 684 620 685 /** 686 * Negates a 64-bit unsigned integer value, returning the result in place. 687 * 688 * @returns pValueResult. 689 * @param pValueResult The value and result. 690 */ 691 DECLINLINE(PRTUINT64U) RTUInt64AssignNeg(PRTUINT64U pValueResult) 692 { 693 pValueResult->s.Lo = UINT32_C(0) - pValueResult->s.Lo; 694 pValueResult->s.Hi = UINT32_C(0) - pValueResult->s.Hi; 695 if (pValueResult->s.Lo > UINT32_C(0)) 696 pValueResult->s.Hi--; 697 return pValueResult; 698 } 699 700 701 /** 621 702 * Multiplies two 64-bit unsigned integer values, storing the result in the 622 703 * first. … … 812 893 { 813 894 /* (left shift) */ 814 cBits &= 31;895 cBits &= 63; 815 896 if (cBits >= 32) 816 897 { … … 829 910 /* (right shift) */ 830 911 cBits = -cBits; 831 cBits &= 31;912 cBits &= 63; 832 913 if (cBits >= 32) 833 914 { … … 1336 1417 1337 1418 1419 /** 1420 * Divides a 64-bit signed integer value by another, returning both quotient and 1421 * remainder. 1422 * 1423 * @returns pQuotient, NULL if pValue2 is 0. 1424 * @param pQuotient Where to return the quotient. 1425 * @param pRemainder Where to return the remainder. 1426 * @param pValue1 The dividend value. 1427 * @param pValue2 The divisor value. 1428 */ 1429 DECLINLINE(PRTUINT64U) RTUInt64DivRemSigned(PRTUINT64U pQuotient, PRTUINT64U pRemainder, PCRTUINT64U pValue1, PCRTUINT64U pValue2) 1430 { 1431 /* 1432 * If both values are positive, we can do a straight unsigned division. 1433 */ 1434 if (!RTUInt64IsSigned(pValue1)) 1435 { 1436 if (!RTUInt64IsSigned(pValue2)) 1437 return RTUInt64DivRem(pQuotient, pRemainder, pValue1, pValue2); 1438 1439 /* 1440 * Negative divisor, Positive dividend: 1441 * negate the divisor, do unsigned division, and negate the quotient. 1442 */ 1443 { 1444 RTUINT64U NegDivisor; 1445 RTUInt64DivRem(pQuotient, pRemainder, pValue1, RTUInt64Neg(&NegDivisor, pValue2)); 1446 RTUInt64AssignNeg(pQuotient); 1447 } 1448 } 1449 else 1450 { 1451 /* Negate the dividend to get a positive value. */ 1452 RTUINT64U NegDividend; 1453 RTUInt64Neg(&NegDividend, pValue1); 1454 1455 if (!RTUInt64IsSigned(pValue2)) 1456 { 1457 /* 1458 * Negative dividend, positive divisor: 1459 * Negate the dividend (above), do unsigned division, and negate both quotient and remainder 1460 */ 1461 RTUInt64DivRem(pQuotient, pRemainder, &NegDividend, pValue2); 1462 RTUInt64AssignNeg(pQuotient); 1463 } 1464 else 1465 { 1466 /* 1467 * Negative dividend, negative divisor: 1468 * Negate both dividend (above) and divisor, do unsigned division, and negate the remainder. 1469 */ 1470 RTUINT64U NegDivisor; 1471 RTUInt64DivRem(pQuotient, pRemainder, &NegDividend, RTUInt64Neg(&NegDivisor, pValue2)); 1472 } 1473 RTUInt64AssignNeg(pRemainder); 1474 } 1475 return pQuotient; 1476 } 1477 1478 1338 1479 /** @} */ 1339 1480
Note:
See TracChangeset
for help on using the changeset viewer.