blob: aa1b6f87c52d32afa04e7b5e05b9262fd97ee9bc [file] [log] [blame]
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Use greatest common denominator routine to stress test branch/modulo op
// combinations.
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include "crt/kelvin.h"
template <typename T>
T get_gcd(const T a, const T b) {
T mod = a % b;
if (mod == 0) {
return b;
}
return get_gcd<T>(b, mod);
}
uint32_t rand_uint32_input() {
uint32_t a = static_cast<uint32_t>(
((rand() & 0xffff) << 16) | // NOLINT(runtime/threadsafe_fn)
(rand() & 0xffff)); // NOLINT(runtime/threadsafe_fn)
return std::max<uint32_t>(a, 1);
}
int32_t rand_int32_input() {
int32_t a = ((rand() & 0x7fff) << 16) | // NOLINT(runtime/threadsafe_fn)
(rand() & 0xffff); // NOLINT(runtime/threadsafe_fn)
return std::max<int32_t>(a, 1);
}
int main() {
constexpr int kSeed = 1000;
srand(kSeed);
uint32_t uint32_a = rand_uint32_input();
uint32_t uint32_b = rand_uint32_input();
int32_t int32_a = rand_int32_input();
int32_t int32_b = rand_int32_input();
printf("unsigned numbers: a:%lu, b:%lu\n", uint32_a, uint32_b);
auto uint32_gcd = (uint32_a > uint32_b)
? get_gcd<uint32_t>(uint32_a, uint32_b)
: get_gcd<uint32_t>(uint32_b, uint32_a);
printf("gcd: %lu\n", uint32_gcd);
if (!(uint32_a % uint32_gcd == 0 && uint32_b % uint32_gcd == 0)) {
printf("Invalid common denominator %lu for %lu and %lu\n", uint32_gcd,
uint32_a, uint32_b);
exit(-1);
}
printf("signed numbers: a:%ld, b:%ld\n", int32_a, int32_b);
auto int32_gcd = (int32_a > int32_b) ? get_gcd<int32_t>(int32_a, int32_b)
: get_gcd<int32_t>(int32_b, int32_a);
printf("gcd: %ld\n", int32_gcd);
if (!(int32_a % int32_gcd == 0 && int32_b % int32_gcd == 0)) {
printf("Invalid common denominator %ld for %ld and %ld\n", int32_gcd,
int32_a, int32_b);
exit(-1);
}
return 0;
}