Commit 5bed9cc9 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

enable clang warning float-conversion

Summary: Enable it as an error and fix all resulting build failures in folly.

Reviewed By: luciang

Differential Revision: D33838564

fbshipit-source-id: 13130e4b9c090db6a4ec7c8fcd150d6aa9c000b9
parent a88662de
......@@ -222,7 +222,7 @@ static std::pair<double, UserCounters> runBenchmarkGetNSPerIterationEstimate(
// We do measurements in several epochs to account for jitter.
constexpr unsigned int epochs = 1000;
size_t actualEpochs = 0;
const unsigned int estimateCount = max(1.0, 5e+7 / estPerIter);
const unsigned int estimateCount = to_integral(max(1.0, 5e+7 / estPerIter));
std::vector<EpochResultType> epochResults(epochs);
const auto maxRunTime = seconds(5);
auto globalStart = high_resolution_clock::now();
......
......@@ -390,7 +390,7 @@ std::string prettyPrint(double val, PrettyType type, bool addSpace) {
buf,
sizeof buf,
"%.4g%s%s",
(suffixes[i].val ? (val / suffixes[i].val) : val),
(suffixes[i].val != 0. ? (val / suffixes[i].val) : val),
(addSpace ? " " : ""),
suffixes[i].suffix);
return std::string(buf);
......@@ -434,8 +434,8 @@ double prettyToDouble(
"Unable to parse suffix \"", *prettyString, "\""));
}
prettyString->advance(size_t(longestPrefixLen));
return suffixes[bestPrefixId].val ? value * suffixes[bestPrefixId].val
: value;
return suffixes[bestPrefixId].val != 0. ? value * suffixes[bestPrefixId].val
: value;
}
double prettyToDouble(folly::StringPiece prettyString, const PrettyType type) {
......
......@@ -266,7 +266,7 @@ class alignas(64) BucketTable {
(folly::popcount(max_size_ - 1) + ShardBits <= 32)));
auto buckets = Buckets::create(initial_buckets, cohort);
buckets_.store(buckets, std::memory_order_release);
load_factor_nodes_ = initial_buckets * load_factor_;
load_factor_nodes_ = to_integral(initial_buckets * load_factor_);
bucket_count_.store(initial_buckets, std::memory_order_relaxed);
}
......@@ -332,7 +332,7 @@ class alignas(64) BucketTable {
auto buckets = buckets_.load(std::memory_order_relaxed);
DCHECK(buckets); // Use-after-destruction by user.
auto newbuckets = Buckets::create(bucket_count, cohort);
load_factor_nodes_ = bucket_count * load_factor_;
load_factor_nodes_ = to_integral(bucket_count * load_factor_);
for (size_t i = 0; i < oldcount; i++) {
auto bucket = &buckets->buckets_[i]();
auto node = bucket->load(std::memory_order_relaxed);
......@@ -1556,7 +1556,8 @@ class alignas(64) SIMDTable {
}
auto new_chunks = Chunks::create(new_chunk_count, cohort);
auto old_chunks = chunks_.load(std::memory_order_relaxed);
grow_threshold_ = new_chunk_count * Chunk::kCapacity * load_factor_;
grow_threshold_ =
to_integral(new_chunk_count * Chunk::kCapacity * load_factor_);
for (size_t i = 0; i < old_chunk_count; i++) {
DCHECK(old_chunks); // Use-after-destruction by user.
......
......@@ -25,6 +25,7 @@
#include <folly/ConstexprMath.h>
#include <folly/Likely.h>
#include <folly/Random.h>
#include <folly/Utility.h>
namespace folly {
......@@ -164,7 +165,7 @@ class Coinflip {
// is a user-initialized state and that this shouldn't automatically
// generate an event.
double U = Random::randDouble01(std::forward<RNG>(rng));
return -log(1 - U) * kScale + 1;
return to_integral(-log(1 - U) * kScale + 1);
}
template <class RNG = ThreadLocalPRNG>
......@@ -182,7 +183,7 @@ class Coinflip {
// will only happen when *counter is manually initialized to 0.
return coinflip_naive(wait, std::forward<RNG>(rng));
}
const uint64_t step_precise = -log(1 - 1.0 / wait) * kScale;
const uint64_t step_precise = to_integral(-log(1 - 1.0 / wait) * kScale);
// The false positive rate is close to 50% when wait is a power of 2 and
// is close to 0% when wait-1 is a power of 2.
return old_counter <= step_precise;
......
......@@ -28,7 +28,7 @@ QuotientMultiSetBuilder::QuotientMultiSetBuilder(
size_t keyBits, size_t expectedElements, double loadFactor)
: keyBits_(keyBits), maxKey_(qms_detail::maxValue(keyBits_)) {
expectedElements = std::max<size_t>(expectedElements, 1);
uint64_t numSlots = ceil(expectedElements / loadFactor);
uint64_t numSlots = to_integral(ceil(expectedElements / loadFactor));
// Make sure 1:1 mapping between key space and <divisor, remainder> pairs.
divisor_ = divCeil(maxKey_, numSlots);
......
......@@ -30,7 +30,7 @@ std::vector<uint8_t> buffer(kBufferSize + 16);
template <class T>
void benchmarkSet(size_t n, T) {
size_t size = sizeof(T) * 6.9; // use 6.9 bits/byte
size_t size = folly::to_integral(sizeof(T) * 6.9); // use 6.9 bits/byte
const size_t k = 16;
T values[k];
BENCHMARK_SUSPEND {
......@@ -78,7 +78,7 @@ std::atomic<int64_t> sum(0);
template <class T>
void benchmarkGet(size_t n, T x) {
size_t size = sizeof(T) * 6.9; // use 6.9 bits/byte
size_t size = folly::to_integral(sizeof(T) * 6.9); // use 6.9 bits/byte
for (size_t i = 0; i < n; ++i) {
size_t bit = (i * 2973) % (kBufferSize * 8);
size_t drop = i % size;
......
......@@ -166,7 +166,7 @@ uint64_t step_approx(uint32_t wait) {
}
uint64_t step_exact(uint32_t wait) {
return -log(1 - 1.0 / wait) * kScale;
return folly::to_integral(-log(1 - 1.0 / wait) * kScale);
}
TEST(coinflip_test, approx_ge_exact) {
......
......@@ -503,7 +503,7 @@ ReturnType BucketedTimeSeries<VT, CT>::rangeAdjust(
TimePoint intervalEnd = std::min(end, nextBucketStart);
float scale =
(intervalEnd - intervalStart) * 1.f / (nextBucketStart - bucketStart);
return input * scale;
return static_cast<ReturnType>(input * scale);
}
template <typename VT, typename CT>
......
......@@ -178,8 +178,8 @@ TEST_F(BufferedSlidingWindowTest, PartiallyPassedExpiry) {
EXPECT_EQ(1, digests.size());
EXPECT_EQ(3, digests[0].getValues().size());
for (double i = 0; i < 3; ++i) {
EXPECT_EQ(i, digests[0].getValues()[i]);
for (size_t i = 0; i < 3; ++i) {
EXPECT_EQ(double(i), digests[0].getValues()[i]);
}
}
......@@ -197,8 +197,8 @@ TEST_F(BufferedSlidingWindowTest, ForceUpdate) {
digests = bsw->get();
EXPECT_EQ(1, digests.size());
EXPECT_EQ(3, digests[0].getValues().size());
for (double i = 0; i < 3; ++i) {
EXPECT_EQ(i, digests[0].getValues()[i]);
for (size_t i = 0; i < 3; ++i) {
EXPECT_EQ(double(i), digests[0].getValues()[i]);
}
// append 3 and flush again; 3 will be merged with
......@@ -208,8 +208,8 @@ TEST_F(BufferedSlidingWindowTest, ForceUpdate) {
digests = bsw->get();
EXPECT_EQ(1, digests.size());
EXPECT_EQ(4, digests[0].getValues().size());
for (double i = 0; i < 4; ++i) {
EXPECT_EQ(i, digests[0].getValues()[i]);
for (size_t i = 0; i < 4; ++i) {
EXPECT_EQ(double(i), digests[0].getValues()[i]);
}
// append 4 and do a regular get. previous values
......@@ -218,8 +218,8 @@ TEST_F(BufferedSlidingWindowTest, ForceUpdate) {
digests = bsw->get();
EXPECT_EQ(1, digests.size());
EXPECT_EQ(4, digests[0].getValues().size());
for (double i = 0; i < 4; ++i) {
EXPECT_EQ(i, digests[0].getValues()[i]);
for (size_t i = 0; i < 4; ++i) {
EXPECT_EQ(double(i), digests[0].getValues()[i]);
}
// pass expiry
......@@ -231,8 +231,8 @@ TEST_F(BufferedSlidingWindowTest, ForceUpdate) {
EXPECT_EQ(4, digests[0].getValues().front());
EXPECT_EQ(4, digests[1].getValues().size());
for (double i = 0; i < 4; ++i) {
EXPECT_EQ(i, digests[1].getValues()[i]);
for (size_t i = 0; i < 4; ++i) {
EXPECT_EQ(double(i), digests[1].getValues()[i]);
}
}
......
......@@ -61,7 +61,7 @@ template <typename SampleDataType>
class StreamingStatsTest : public testing::Test {
public:
void SetUp() override {
for (SampleDataType value = 1.0; value < 11.0; value += 1.0) {
for (SampleDataType value = 1; value < 11; value += 1) {
stats.add(value);
}
}
......
......@@ -352,7 +352,8 @@ static void runFairness(std::size_t numThreads) {
}
// Calulate some stats
unsigned long sum = std::accumulate(results.begin(), results.end(), 0.0);
unsigned long sum =
folly::to_integral(std::accumulate(results.begin(), results.end(), 0.0));
double m = sum / results.size();
double accum = 0.0;
......
......@@ -887,8 +887,11 @@ TEST(Expected, TestUnique) {
2, **mk().then([](auto r) { return std::make_unique<int>(*r + 1); }));
// Test converting errors works
struct Convertible {
/* implicit */ operator int() const noexcept { return 17; }
};
EXPECT_EQ(
2, **mk().then([](auto r) -> Expected<std::unique_ptr<int>, double> {
2, **mk().then([](auto r) -> Expected<std::unique_ptr<int>, Convertible> {
return std::make_unique<int>(*r + 1);
}));
}
......
......@@ -830,7 +830,12 @@ TEST(Function, ReturnConvertible) {
Function<double()> f1 = []() -> int { return 5; };
EXPECT_EQ(5.0, f1());
Function<int()> f2 = []() -> double { return 5.2; };
struct Convertible {
double value;
/* implicit */ Convertible(double v) noexcept : value{v} {}
/* implicit */ operator int() const noexcept { return int(value); }
};
Function<int()> f2 = []() -> Convertible { return 5.2; };
EXPECT_EQ(5, f2());
CDerived derived;
......@@ -868,14 +873,20 @@ TEST(Function, ConvertReturnType) {
};
struct CDerived : CBase {};
struct Convertible {
double value;
/* implicit */ Convertible(double v) noexcept : value{v} {}
/* implicit */ operator int() const noexcept { return int(value); }
};
Function<int()> f1 = []() -> int { return 5; };
Function<double()> cf1 = std::move(f1);
EXPECT_EQ(5.0, cf1());
Function<int()> ccf1 = std::move(cf1);
Function<Convertible()> ccf1 = std::move(cf1);
EXPECT_EQ(5, ccf1());
Function<double()> f2 = []() -> double { return 5.2; };
Function<int()> cf2 = std::move(f2);
Function<Convertible()> cf2 = std::move(f2);
EXPECT_EQ(5, cf2());
Function<double()> ccf2 = std::move(cf2);
EXPECT_EQ(5.0, ccf2());
......
......@@ -37,9 +37,10 @@ void bench(
static std::vector<size_t> src_offsets;
BENCHMARK_SUSPEND {
size_t src_buffer_size = sysconf(_SC_PAGE_SIZE) *
std::ceil(static_cast<double>(max + 2 * align) /
sysconf(_SC_PAGE_SIZE));
size_t src_buffer_size = folly::to_integral(
sysconf(_SC_PAGE_SIZE) *
std::ceil(
static_cast<double>(max + 2 * align) / sysconf(_SC_PAGE_SIZE)));
size_t dst_buffer_size;
if (hot) {
dst_buffer_size = src_buffer_size;
......
......@@ -656,7 +656,7 @@ static void runMixed(
std::minstd_rand engine;
engine.seed(t);
long writeThreshold = writeFraction * 0x7fffffff;
long writeThreshold = to_integral(writeFraction * 0x7fffffff);
Lock privateLock;
Lock* lock = useSeparateLocks ? &privateLock : &(padded.globalLock);
Locker locker;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment