Skip to content

Commit

Permalink
Tweak the types of various loop index and bounds variables (NFC)
Browse files Browse the repository at this point in the history
This is done to ensure comparisons are done between types of the same type,
or at least signedness.
  • Loading branch information
fwojcik committed Jul 29, 2023
1 parent f7da3cd commit 7478241
Show file tree
Hide file tree
Showing 27 changed files with 87 additions and 87 deletions.
2 changes: 1 addition & 1 deletion hashes/ascon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ static const uint8_t KAT[KAT_NUM][2][256 / 8] = {
static bool ascon_xof_selftest( void ) {
uint8_t input[KAT_NUM - 1];

for (int i = 0; i < sizeof(input); i++) { input[i] = (uint8_t)i; }
for (size_t i = 0; i < sizeof(input); i++) { input[i] = (uint8_t)i; }

bool passed = true;
for (int i = 0; i < KAT_NUM; i++) {
Expand Down
2 changes: 1 addition & 1 deletion hashes/chaskey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static void chaskey_impl( uint8_t * tag, const uint8_t * m, const size_t mlen, c
v[2] ^= lastkey[2];
v[3] ^= lastkey[3];

for (int i = 0; i < tagwords; i++) {
for (uint32_t i = 0; i < tagwords; i++) {
PUT_U32<bswap>(v[i], tag, 4 * i);
}
}
Expand Down
2 changes: 1 addition & 1 deletion hashes/cityhash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static uint32_t Hash32Len0to4( const uint8_t * s, size_t len, uint32_t seed ) {
uint32_t b = seed;
uint32_t c = 9;

for (int i = 0; i < len; i++) {
for (size_t i = 0; i < len; i++) {
b = b * c1 + s[i];
c ^= b;
}
Expand Down
6 changes: 3 additions & 3 deletions hashes/farsh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static uint64_t farsh_full_block( const uint8_t * data, const uint32_t * key ) {
const __m256i * xdata = (const __m256i *)data;
const __m256i * xkey = (const __m256i *)key;

for (int i = 0; i < STRIPE / sizeof(__m256i); i++) {
for (size_t i = 0; i < STRIPE / sizeof(__m256i); i++) {
__m256i d = _mm256_loadu_si256(xdata + i);
if (bswap) { d = mm256_bswap32(d); }
__m256i k = _mm256_loadu_si256(xkey + i );
Expand Down Expand Up @@ -152,7 +152,7 @@ static uint64_t farsh_full_block( const uint8_t * data, const uint32_t * key ) {
#else
#define FARSH_IMPL_STR "portable"
sum64 = 0;
for (int i = 0; i < STRIPE_ELEMENTS; i += 2) {
for (size_t i = 0; i < STRIPE_ELEMENTS; i += 2) {
sum64 += (GET_U32<bswap>(data, i * 4) + key[i]) *
(uint64_t)(GET_U32<bswap>(data, (i + 1) * 4) + key[i + 1]);
}
Expand All @@ -171,7 +171,7 @@ static uint64_t farsh_partial_block( const uint8_t * data, size_t bytes, const u

memcpy(extra_data, data + 4 * elements, extra_bytes);

int i;
size_t i;
for (i = 0; i < elements; i += 2) {
sum += (GET_U32<bswap>(data, i * 4) + key[i]) *
(uint64_t)(GET_U32<bswap>(data, (i + 1) * 4) + key[i + 1]);
Expand Down
4 changes: 2 additions & 2 deletions hashes/poly_mersenne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static uintptr_t poly_mersenne_seed_init( const seed_t seed ) {
// a has be at most 2^60, or the lazy modular reduction won't work.
poly_mersenne_data.poly_mersenne_a = rand_u128(&BSD_nextrand) % (MERSENNE_61 / 2);
poly_mersenne_data.poly_mersenne_b = rand_u128(&BSD_nextrand) % MERSENNE_61;
for (int i = 0; i < POLY_MERSENNE_MAX_K + 1; i++) {
for (uint32_t i = 0; i < POLY_MERSENNE_MAX_K + 1; i++) {
// The random values should be at most 2^61-2, or the lazy
// modular reduction won't work.
poly_mersenne_data.poly_mersenne_random[i] = rand_u128(&BSD_nextrand) % MERSENNE_61;
Expand Down Expand Up @@ -152,7 +152,7 @@ static void Poly_Mersenne( const void * in, const size_t len, const seed_t seed,
if (K != 0) {
uint64_t h0 = h;
h = data->poly_mersenne_random[0];
for (int i = 1; i <= std::min(K, POLY_MERSENNE_MAX_K); i++) {
for (uint32_t i = 1; i <= std::min(K, POLY_MERSENNE_MAX_K); i++) {
h = mult_combine61(h, h0, data->poly_mersenne_random[i]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion hashes/rmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ static void rmd_init( rmd_ctx * ctx ) {

template <uint32_t hashwidth, bool bswap>
static void rmd_done( rmd_ctx * ctx, uint8_t * out ) {
int i;
unsigned int i;

/* increase the length of the message */
ctx->length += ctx->curlen * 8;
Expand Down
2 changes: 1 addition & 1 deletion hashes/sha3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static void sha3_Finalize( sha3_context * ctx, size_t digest_words, uint8_t * di

uint32_t maxdigest_words = ctx->capacityWords / 2;
if (digest_words > maxdigest_words) { digest_words = maxdigest_words; }
for (int i = 0; i < digest_words; i++) {
for (size_t i = 0; i < digest_words; i++) {
PUT_U64<bswap>(ctx->s[i], digest, 8 * i);
}

Expand Down
2 changes: 1 addition & 1 deletion hashes/wyhash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ static bool wyhash64_selftest( void ) {
},
};

for (int i = 0; i < sizeof(selftests) / sizeof(selftests[0]); i++) {
for (size_t i = 0; i < sizeof(selftests) / sizeof(selftests[0]); i++) {
uint64_t h;
if (isLE()) {
Wyhash64<false, false>(selftests[i].key, strlen(selftests[i].key), i, &h);
Expand Down
2 changes: 1 addition & 1 deletion hashes/xxhash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ static NEVER_INLINE uint64_t XXH3_len_129to240_64b( const uint8_t * RESTRICT inp
#pragma clang loop vectorize(disable)
#endif

for (int i = 8; i < nbRounds; i++) {
for (unsigned i = 8; i < nbRounds; i++) {
/*
* Prevents clang for unrolling the acc loop and interleaving with this one.
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/AvalancheTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ static bool AvalancheImpl( HashFn hash, const seed_t seed, const int keybits,
} else {
#if defined(HAVE_THREADS)
std::thread t[g_NCPU];
for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
t[i] = std::thread {
calcBiasRange<hashtype>, hash, seed, std::ref(bins[i]),
keybytes, &keys[0], std::ref(irep), reps, drawdots
};
}
for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
t[i].join();
}
for (int i = 1; i < g_NCPU; i++) {
for (unsigned i = 1; i < g_NCPU; i++) {
for (int b = 0; b < arraysize; b++) {
bins[0][b] += bins[i][b];
}
Expand Down
24 changes: 12 additions & 12 deletions tests/BadSeedsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@

const std::set<int> testlens = { 1, 2, 3, 6, 15, 18, 32, 52, 80 };
const std::vector<uint8_t> testbytes = { 0, 2, 8, 32, 127, 128, 223, 247, 253, 255 };
const unsigned numtestbytes = testbytes.size();
const unsigned numtestlens = testlens.size();
const size_t numtestbytes = testbytes.size();
const size_t numtestlens = testlens.size();

#if defined(HAVE_THREADS)
// For keeping track of progress printouts across threads
Expand Down Expand Up @@ -108,7 +108,7 @@ static void TestSeedRangeThread( const HashInfo * hinfo, const uint64_t hi, cons

/* Premake all the test keys */
uint8_t keys[numtestbytes][128];
for (int i = 0; i < numtestbytes; i++) {
for (size_t i = 0; i < numtestbytes; i++) {
memset(&keys[i][0], testbytes[i], 128);
}

Expand All @@ -134,7 +134,7 @@ static void TestSeedRangeThread( const HashInfo * hinfo, const uint64_t hi, cons

memset(&hashes[0], 0, numtestbytes * numtestlens * sizeof(hashtype));
unsigned cnt = 0;
for (int i = 0; i < numtestbytes; i++) {
for (size_t i = 0; i < numtestbytes; i++) {
for (int len: testlens) {
hash(&keys[i][0], len, hseed, &hashes[cnt++]);
}
Expand Down Expand Up @@ -163,7 +163,7 @@ static void TestSeedRangeThread( const HashInfo * hinfo, const uint64_t hi, cons
// Can't just print hashes vector because it's now sorted
hashtype v;
printf("Colliding hashes:\n");
for (int i = 0; i < numtestbytes; i++) {
for (size_t i = 0; i < numtestbytes; i++) {
for (int len: testlens) {
hash(&keys[i][0], len, hseed, &v);
if (std::find(collisions.begin(), collisions.end(), v) != collisions.end()) {
Expand Down Expand Up @@ -194,7 +194,7 @@ static void TestSeedRangeThread( const HashInfo * hinfo, const uint64_t hi, cons
if (!known_seed && (fails < 32)) { // don't print too many lines
hashtype v;
printf("Zero hashes:\n");
for (int i = 0; i < numtestbytes; i++) {
for (size_t i = 0; i < numtestbytes; i++) {
for (int len: testlens) {
hash(&keys[i][0], len, hseed, &v);
if (v == zero) {
Expand Down Expand Up @@ -236,7 +236,7 @@ static bool TestManySeeds( const HashInfo * hinfo, const uint64_t hi, bool & new
bool * newresults = new bool[g_NCPU]();

printf("%d threads starting...\n", g_NCPU);
for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
const uint32_t start = i * len;
const uint32_t end = (i < (g_NCPU - 1)) ? start + (len - 1) : 0xffffffff;
t[i] = std::thread {
Expand All @@ -247,13 +247,13 @@ static bool TestManySeeds( const HashInfo * hinfo, const uint64_t hi, bool & new

std::this_thread::sleep_for(std::chrono::seconds(1));

for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
t[i].join();
}

printf("All %d threads ended\n", g_NCPU);

for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
result &= results[i];
newresult |= newresults[i];
}
Expand Down Expand Up @@ -315,7 +315,7 @@ static bool TestSingleSeed( const HashInfo * hinfo, const seed_t seed ) {

/* Premake all the test keys */
uint8_t keys[numtestbytes][128];
for (int i = 0; i < numtestbytes; i++) {
for (size_t i = 0; i < numtestbytes; i++) {
memset(&keys[i][0], testbytes[i], 128);
}

Expand All @@ -324,7 +324,7 @@ static bool TestSingleSeed( const HashInfo * hinfo, const seed_t seed ) {

memset(&hashes[0], 0, numtestbytes * numtestlens * sizeof(hashtype));
unsigned cnt = 0;
for (int i = 0; i < numtestbytes; i++) {
for (size_t i = 0; i < numtestbytes; i++) {
for (int len: testlens) {
hash(&keys[i][0], len, hseed, &hashes[cnt++]);
}
Expand All @@ -336,7 +336,7 @@ static bool TestSingleSeed( const HashInfo * hinfo, const seed_t seed ) {
#if 0
hashtype v;
cnt = 0;
for (int i = 0; i < numtestbytes; i++) {
for (size_t i = 0; i < numtestbytes; i++) {
for (int len: testlens) {
hash(&keys[i][0], len, hseed, &v);
if (std::find(collisions.begin(), collisions.end(), v) != collisions.end()) {
Expand Down
12 changes: 6 additions & 6 deletions tests/BitIndependenceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static void BicTestBatch( HashFn hash, const seed_t seed, std::vector<uint32_t>

uint8_t buf[keybytes];
hashtype h1, h2;
int irep;
size_t irep;

while ((irep = irepp++) < reps) {
progressdots(irep, 0, reps - 1, 12);
Expand Down Expand Up @@ -202,20 +202,20 @@ static bool BicTestImpl( HashFn hash, const seed_t seed, const size_t keybytes,
} else {
#if defined(HAVE_THREADS)
std::thread t[g_NCPU];
for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
t[i] = std::thread {
BicTestBatch<hashtype>, hash, seed, std::ref(popcounts[i]), std::ref(andcounts[i]),
keybytes, &keys[0], std::ref(irep), reps
};
}
for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
t[i].join();
}
for (int i = 1; i < g_NCPU; i++) {
for (int b = 0; b < keybits * hashbits; b++) {
for (unsigned i = 1; i < g_NCPU; i++) {
for (size_t b = 0; b < keybits * hashbits; b++) {
popcounts[0][b] += popcounts[i][b];
}
for (int b = 1; b < keybits * hashbitpairs + 1; b++) {
for (size_t b = 1; b < keybits * hashbitpairs + 1; b++) {
andcounts[0][b] += andcounts[i][b];
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/DifferentialTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,16 @@ static bool DiffTestImpl( HashFn hash, const seed_t seed, int diffbits, int reps
} else {
#if defined(HAVE_THREADS)
std::thread t[g_NCPU];
for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
t[i] = std::thread {
DiffTestImplThread<keytype, hashtype>, hash, seed, std::ref(diffcounts[i]),
&keys[0], diffbits, std::ref(irep), reps
};
}
for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
t[i].join();
}
for (int i = 1; i < g_NCPU; i++) {
for (unsigned i = 1; i < g_NCPU; i++) {
for (std::pair<keytype, uint32_t> dc: diffcounts[i]) {
diffcounts[0][dc.first] += dc.second;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/PerlinNoiseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ static bool PerlinNoise( int Xbits, int Ybits, int inputLen, int step,
assert(inputLen <= INPUT_LEN_MAX);

std::vector<hashtype> hashes;
uint8_t key[INPUT_LEN_MAX] = { 0 };
int const xMax = (1 << Xbits);
int const yMax = (1 << Ybits);
const HashFn hash = hinfo->hashFn(g_hashEndian);
uint8_t key[INPUT_LEN_MAX] = { 0 };
const uint64_t xMax = (UINT64_C(1) << Xbits);
const uint64_t yMax = (UINT64_C(1) << Ybits);
const HashFn hash = hinfo->hashFn(g_hashEndian);

printf("Generating coordinates from %3i-byte keys - %d keys\n", inputLen, xMax * yMax);
printf("Generating coordinates from %3i-byte keys - %ld keys\n", inputLen, xMax * yMax);

addVCodeInput(yMax);
// Since seeding can be expensive, loop over the seed-dependent
Expand Down
2 changes: 1 addition & 1 deletion tests/PermutationKeysetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static void CombinationKeygenRecurse( uint8_t * key, int len, int maxlen, const
uint32_t blocksz, HashFn hash, const seed_t seed, std::vector<hashtype> & hashes ) {
if (len == maxlen) { return; } // end recursion

for (int i = 0; i < blockcount; i++) {
for (size_t i = 0; i < blockcount; i++) {
memcpy(&key[len * blocksz], &blocks[i * blocksz], blocksz);

hashtype h;
Expand Down
10 changes: 5 additions & 5 deletions tests/PopcountTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static bool PopcountResults( long double srefh, long double srefl, long double b

static bool PopcountTestImpl( const HashInfo * hinfo, int inputSize, int step ) {
const long double n = UINT64_C(0x100000000) / step;
const int hbits = std::min(hinfo->bits, 64U); // limited due to popcount8
const uint64_t hbits = std::min(hinfo->bits, UINT32_C(64)); // limited due to popcount8

assert(hbits <= HASH_SIZE_MAX * 8);
assert(inputSize >= 4);
Expand Down Expand Up @@ -243,7 +243,7 @@ static bool PopcountTestImpl( const HashInfo * hinfo, int inputSize, int step )
printf("%d threads starting... ", g_NCPU);

const uint64_t len = UINT64_C(0x100000000) / (step * g_NCPU);
for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
const uint32_t start = i * len * step;
const uint32_t end = (i < (g_NCPU - 1)) ? start + (len * step - 1) : 0xffffffff;
// printf("thread[%d]: %d, 0x%x - 0x%x %d\n", i, inputSize, start, end, step);
Expand All @@ -254,13 +254,13 @@ static bool PopcountTestImpl( const HashInfo * hinfo, int inputSize, int step )

std::this_thread::sleep_for(std::chrono::seconds(1));

for (int i = 0; i < g_NCPU; i++) {
for (unsigned i = 0; i < g_NCPU; i++) {
t[i].join();
}

printf(" done\n");
for (int i = 1; i < g_NCPU; i++) {
for (int j = 0; j <= hbits; j++) {
for (unsigned i = 1; i < g_NCPU; i++) {
for (uint64_t j = 0; j <= hbits; j++) {
rawhash[0][j] += rawhash[i][j];
xorhash[0][j] += xorhash[i][j];
}
Expand Down
Loading

0 comments on commit 7478241

Please sign in to comment.