Skip to content

Commit d052c5d

Browse files
Alvov1claude
andcommitted
fix: resolve -Wconversion -Wsign-conversion warnings
Replace long long loop counters with std::size_t using the i-- > 0 idiom for safe reverse iteration. Fix signed/unsigned arithmetic in Aesi integral constructors and operators. Use 1ULL for bit-shift stamp calculations to avoid UB when shift amount equals type width. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 61442e0 commit d052c5d

8 files changed

Lines changed: 72 additions & 74 deletions

File tree

include/AesiMultiprecision/Aesi.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ class Aesi final {
100100
gpu constexpr Aesi(Integral value) noexcept {
101101
using enum Sign;
102102
if(value < 0) {
103-
value *= -1;
104-
base = Base(static_cast<unsigned long long>(value));
103+
base = Base(static_cast<unsigned long long>(-value));
105104
sign = Negative;
106105
} else if(value > 0) {
107106
base = Base(static_cast<unsigned long long>(value));
@@ -431,7 +430,7 @@ class Aesi final {
431430
} else {
432431
if(factor < 0) {
433432
multiplication.inverse();
434-
factor *= -1;
433+
factor = static_cast<Integral>(-factor);
435434
}
436435
multiplication.base *= static_cast<unsigned long long>(factor);
437436
}
@@ -497,7 +496,7 @@ class Aesi final {
497496
} else {
498497
if(divisor < 0) {
499498
division.inverse();
500-
divisor *= -1;
499+
divisor = static_cast<Integral>(-divisor);
501500
}
502501
division.base /= static_cast<unsigned long long>(divisor);
503502
if(division.base.isZero()) division.sign = Zero;
@@ -567,7 +566,7 @@ class Aesi final {
567566
} else {
568567
if(modulo < 0) {
569568
modulation.inverse();
570-
modulo *= -1;
569+
modulo = static_cast<Integral>(-modulo);
571570
}
572571
modulation.base %= static_cast<unsigned long long>(modulo);
573572
}
@@ -660,7 +659,7 @@ class Aesi final {
660659
}
661660
} else if(integral < 0) {
662661
if(sign == Negative)
663-
switch(base.compareTo(static_cast<unsigned long long>(integral * -1))) {
662+
switch(base.compareTo(static_cast<unsigned long long>(-integral))) {
664663
using enum Comparison;
665664
case greater:
666665
return less;

include/AesiMultiprecision/Aeu.h

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,11 @@ class Aeu final {
166166
template <typename Integral> requires (std::is_unsigned_v<Integral>)
167167
gpu constexpr Aeu(Integral value) noexcept {
168168
if(value != 0) {
169-
uint64_t tValue = (value < 0 ? static_cast<uint64_t>(value * -1) : static_cast<uint64_t>(value));
169+
uint64_t tValue = static_cast<uint64_t>(value);
170170
for (std::size_t i = 0; i < blocksNumber; ++i) {
171171
blocks[i] = static_cast<block>(tValue % blockBase);
172172
tValue /= blockBase;
173173
}
174-
if(value < 0)
175-
makeComplement(blocks);
176174
} else
177175
blocks = {};
178176
}
@@ -665,11 +663,12 @@ class Aeu final {
665663
if(bitShift >= bitness || bitShift == 0) return value;
666664

667665
const std::size_t quotient = bitShift / blockBitLength, remainder = bitShift % blockBitLength;
668-
const block stamp = (1UL << (blockBitLength - remainder)) - 1;
666+
const block stamp = static_cast<block>((1ULL << (blockBitLength - remainder)) - 1);
669667

670-
for (long long i = static_cast<long long>(blocksNumber) - 1; i >= static_cast<long long>(quotient + (remainder ? 1 : 0)); --i)
668+
const std::size_t minI = quotient + (remainder ? 1U : 0U);
669+
for (std::size_t i = blocksNumber; i-- > minI;)
671670
value.blocks[i] = (value.blocks[i - quotient] & stamp) << remainder
672-
| ((value.blocks[i - quotient - (remainder ? 1 : 0)] & ~stamp) >> (blockBitLength - remainder) % blockBitLength);
671+
| ((value.blocks[i - quotient - (remainder ? 1U : 0U)] & ~stamp) >> (blockBitLength - remainder) % blockBitLength);
673672

674673
value.blocks[quotient] = (value.blocks[0] & stamp) << remainder;
675674

@@ -702,7 +701,7 @@ class Aeu final {
702701
if(bitShift >= bitness || bitShift == 0) return value;
703702

704703
const std::size_t quotient = bitShift / blockBitLength, remainder = bitShift % blockBitLength;
705-
const block stamp = (1UL << remainder) - 1;
704+
const block stamp = static_cast<block>((1ULL << remainder) - 1);
706705

707706
for(std::size_t i = 0; i < blocksNumber - (quotient + (remainder ? 1 : 0)); ++i)
708707
value.blocks[i] = ((value.blocks[i + quotient + (remainder ? 1 : 0)] & stamp) << (blockBitLength - remainder) % blockBitLength) | (value.blocks[i + quotient] & ~stamp) >> remainder;
@@ -807,7 +806,7 @@ class Aeu final {
807806
using enum Comparison;
808807

809808
const auto lowerBlockBorder = (blocksNumber < other.totalBlocksNumber() ? blocksNumber : other.totalBlocksNumber());
810-
for(long long i = lowerBlockBorder - 1; i >= 0; --i) {
809+
for(std::size_t i = lowerBlockBorder; i-- > 0;) {
811810
const block thisBlock = blocks[i], otherBlock = other.getBlock(i);
812811
if(thisBlock != otherBlock)
813812
return (thisBlock > otherBlock ? greater : less);
@@ -816,11 +815,11 @@ class Aeu final {
816815
if constexpr (otherBitness != blocksNumber * blockBitLength) {
817816
using enum Comparison;
818817
if (other.totalBlocksNumber() > blocksNumber) {
819-
for (long long i = static_cast<long long>(other.totalBlocksNumber()) - 1; i > static_cast<long long>(lowerBlockBorder) - 1; --i)
818+
for (std::size_t i = other.totalBlocksNumber(); i-- > lowerBlockBorder;)
820819
if (other.getBlock(i) != 0)
821820
return less;
822821
} else if (blocksNumber > other.totalBlocksNumber()) {
823-
for (long long i = static_cast<long long>(blocksNumber) - 1; i > static_cast<long long>(lowerBlockBorder) - 1; --i)
822+
for (std::size_t i = blocksNumber; i-- > lowerBlockBorder;)
824823
if (blocks[i] != 0)
825824
return greater;
826825
}
@@ -950,7 +949,7 @@ class Aeu final {
950949
#endif
951950
const std::size_t blockNumber = index / sizeof(block), byteInBlock = index % sizeof(block), shift = byteInBlock * bitsInByte;
952951
assert(blockNumber < blocksNumber && byteInBlock < sizeof(block));
953-
return (blocks[blockNumber] & (0xffU << shift)) >> shift;
952+
return static_cast<byte>((blocks[blockNumber] & (0xffU << shift)) >> shift);
954953
}
955954

956955
/**
@@ -990,7 +989,7 @@ class Aeu final {
990989
for(; lastBlock > 0 && blocks[lastBlock] == 0; --lastBlock)
991990
;
992991

993-
for(int8_t byteN = sizeof(block) - 1; byteN >= 0; --byteN) {
992+
for(std::size_t byteN = sizeof(block); byteN-- > 0;) {
994993
if((blocks[lastBlock] & (0xffU << (byteN * bitsInByte))) >> (byteN * bitsInByte))
995994
return lastBlock * sizeof(block) + byteN + 1;
996995
}
@@ -1007,11 +1006,11 @@ class Aeu final {
10071006
for(; lastBlock > 0 && blocks[lastBlock] == 0; --lastBlock)
10081007
;
10091008

1010-
for(int8_t byteN = sizeof(block) - 1; byteN >= 0; --byteN) {
1009+
for(std::size_t byteN = sizeof(block); byteN-- > 0;) {
10111010
const auto byte = (blocks[lastBlock] & (0xffU << (byteN * bitsInByte))) >> (byteN * bitsInByte);
10121011
if(!byte) continue;
10131012

1014-
for(int8_t bitN = bitsInByte - 1; bitN >= 0; --bitN) {
1013+
for(std::size_t bitN = bitsInByte; bitN-- > 0;) {
10151014
if((byte & (0x1u << bitN)) >> bitN)
10161015
return (lastBlock * sizeof(block) + byteN) * bitsInByte + bitN + 1;
10171016
}
@@ -1047,7 +1046,7 @@ class Aeu final {
10471046
*/
10481047
[[nodiscard]]
10491048
gpu constexpr auto filledBlocksNumber() const noexcept -> std::size_t {
1050-
for(long long i = blocksNumber - 1; i >= 0; --i)
1049+
for(std::size_t i = blocksNumber; i-- > 0;)
10511050
if(blocks[i]) return i + 1;
10521051
return 0;
10531052
}
@@ -1092,7 +1091,7 @@ class Aeu final {
10921091

10931092
if(ratio == Comparison::greater) {
10941093
const auto bitsUsed = number.filledBlocksNumber() * blockBitLength;
1095-
for(long long i = bitsUsed - 1; i >= 0; --i) {
1094+
for(std::size_t i = bitsUsed; i-- > 0;) {
10961095
remainder <<= 1u;
10971096
remainder.setBit(0, number.getBit(i));
10981097

@@ -1315,18 +1314,18 @@ class Aeu final {
13151314
}
13161315

13171316
if constexpr (base == 16) {
1318-
long long iter = blocks.size() - 1;
1319-
for (; blocks[iter] == 0 && iter >= 0; --iter)
1317+
std::size_t iter = blocks.size() - 1;
1318+
for (; iter > 0 && blocks[iter] == 0; --iter)
13201319
;
13211320

13221321
if constexpr (std::is_same_v<Char, char>) {
1323-
position += snprintf(buffer + position, bufferSize - position, (hexUppercase ? "%X" : "%x"), blocks[iter--]);
1324-
for (; iter >= 0; --iter)
1325-
position += snprintf(buffer + position, bufferSize - position, (hexUppercase ? "%08X" : "%08x"), blocks[iter]);
1322+
position += static_cast<std::size_t>(snprintf(buffer + position, bufferSize - position, (hexUppercase ? "%X" : "%x"), blocks[iter]));
1323+
for (; iter-- > 0;)
1324+
position += static_cast<std::size_t>(snprintf(buffer + position, bufferSize - position, (hexUppercase ? "%08X" : "%08x"), blocks[iter]));
13261325
} else {
1327-
position += swprintf(buffer + position, bufferSize - position, (hexUppercase ? L"%X" : L"%x"), blocks[iter--]);
1328-
for (; iter >= 0; --iter)
1329-
position += swprintf(buffer + position, bufferSize - position, (hexUppercase ? L"%08X" : L"%08x"), blocks[iter]);
1326+
position += static_cast<std::size_t>(swprintf(buffer + position, bufferSize - position, (hexUppercase ? L"%X" : L"%x"), blocks[iter]));
1327+
for (; iter-- > 0;)
1328+
position += static_cast<std::size_t>(swprintf(buffer + position, bufferSize - position, (hexUppercase ? L"%08X" : L"%08x"), blocks[iter]));
13301329
}
13311330
} else {
13321331
const auto startPosition = position;
@@ -1384,12 +1383,12 @@ class Aeu final {
13841383
return os << '0';
13851384

13861385
if(base == 16) {
1387-
long long iter = number.blocks.size() - 1;
1388-
for(; number.blocks[iter] == 0 && iter >= 0; --iter)
1386+
std::size_t iter = number.blocks.size() - 1;
1387+
for(; iter > 0 && number.blocks[iter] == 0; --iter)
13891388
;
13901389

1391-
os << number.blocks[iter--];
1392-
for (; iter >= 0; --iter) {
1390+
os << number.blocks[iter];
1391+
for (; iter-- > 0;) {
13931392
os.fill([] { if constexpr (std::is_same_v<Char, char>) { return '0'; } else { return L'0'; } } ());
13941393
os.width(8); os << std::right << number.blocks[iter];
13951394
}

test/arithmetic/addition/addition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void testAdditionBasic() {
5151
template <typename T, std::size_t N>
5252
void runIncrementLoop(const mpz_class& l) {
5353
T value = l;
54-
const std::size_t increments = rand() % 100;
54+
const std::size_t increments = static_cast<std::size_t>(rand()) % 100;
5555
for (std::size_t j = 0; j < increments * 2; j += 2) {
5656
EXPECT_EQ(value++, l + j);
5757
EXPECT_EQ(++value, l + j + 2);

test/arithmetic/subtraction/subtraction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
template <typename T, std::size_t N>
77
void runDecrementLoop(const mpz_class& l) {
88
T value = l;
9-
const std::size_t decrements = rand() % 100;
9+
const std::size_t decrements = static_cast<std::size_t>(rand()) % 100;
1010
for (std::size_t j = 0; j < decrements * 2; j += 2) {
1111
EXPECT_EQ(value--, l - j);
1212
EXPECT_EQ(--value, l - j - 2);

test/bitwise/not.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ TEST(Unsigned_Bitwise, NOT) {
2222
for (auto byte = static_cast<uint8_t>(~getByteGmp(byteCount - 1)); byte; byte >>= 1)
2323
binary += (byte & 1 ? '1' : '0');
2424
ss << "0b" << std::string(binary.rbegin(), binary.rend());
25-
for(long long j = (long long)byteCount - 2; j >= 0; --j)
26-
ss << std::bitset<8>(~getByteGmp(j));
25+
for(std::size_t j = byteCount - 1; j-- > 0;)
26+
ss << std::bitset<8>(static_cast<uint8_t>(~getByteGmp(j)));
2727

2828
Aeu<N> aeu = value, notted = ss.str();
2929
EXPECT_EQ(~aeu, notted);

test/operations/binary_io.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ TEST(Unsigned_BinaryIO, BinaryRead) {
2525
ss2 << std::setw(8) << std::setfill('0') << block;
2626
}
2727

28-
for(long long j = blocks.size() - 1; j >= 0; --j)
28+
for(std::size_t j = blocks.size(); j-- > 0;)
2929
ss.write(reinterpret_cast<const char*>(&blocks[j]), sizeof(uint32_t));
3030

3131
a.readBinary(ss, false);
@@ -53,7 +53,7 @@ TEST(Unsigned_BinaryIO, BinaryWrite) {
5353
}
5454
} else {
5555
l.writeBinary(ss2, true);
56-
for (long long j = blocksNumber - 1; j >= 0; --j) {
56+
for (std::size_t j = static_cast<std::size_t>(blocksNumber); j-- > 0;) {
5757
ss2.read(reinterpret_cast<char*>(&temp), sizeof(uint32_t));
5858
r.setBlock(j, temp);
5959
}

0 commit comments

Comments
 (0)