Skip to content

Commit 348e9aa

Browse files
vvfedorenkofacebook-github-bot
authored andcommitted
avoid floating point calculations
Summary: The constant of nanoseconds in second used in the code has type of double while is used in mostly integer operations. Replace it with proper constant. based on https://fb.workplace.com/groups/cpp.is.tricky/permalink/2541032339575142/ Reviewed By: crmdias Differential Revision: D76508594 fbshipit-source-id: 4ac1bc3a96d994391625c7361ad1bb73c5abf5f1
1 parent 74efedd commit 348e9aa

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

fbclock/cpp_test/test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,17 @@ TEST(fbclockTest, test_fbclock_calculate_time_v2) {
334334
FBCLOCK_TAI);
335335
ASSERT_EQ(err, 0);
336336

337-
EXPECT_EQ(truetime.earliest_ns, 1647269091803103381);
338-
EXPECT_EQ(truetime.latest_ns, 1647269091803104619);
337+
EXPECT_EQ(truetime.earliest_ns, 1647269091803103338);
338+
EXPECT_EQ(truetime.latest_ns, 1647269091803104576);
339339

340340
// WOU is very big
341341
error_bound = 1000.0;
342342
sysclock_time_ns += 6 * 3600 * 1000000000ULL; // + 6 hours
343343
err = fbclock_calculate_time_v2(
344344
error_bound, h_value, &state, sysclock_time_ns, &truetime, FBCLOCK_TAI);
345345
ASSERT_EQ(err, 0);
346-
EXPECT_EQ(truetime.earliest_ns, 1647290691803360857);
347-
EXPECT_EQ(truetime.latest_ns, 1647290691803363751);
346+
EXPECT_EQ(truetime.earliest_ns, 1647290691803360710);
347+
EXPECT_EQ(truetime.latest_ns, 1647290691803363604);
348348
}
349349

350350
TEST(fbclockTest, test_fbclock_apply_smear_after_2017_leap_second) {

fbclock/fbclock.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ limitations under the License.
3737
#define FBCLOCK_CLOCKDATA_SIZE sizeof(fbclock_clockdata)
3838
#define FBCLOCK_CLOCKDATA_V2_SIZE sizeof(fbclock_clockdata_v2)
3939
#define FBCLOCK_MAX_READ_TRIES 1000
40-
#define NANOSECONDS_IN_SECONDS 1e9
40+
#define NANOSECONDS_IN_SECONDS 1000000000ULL
4141
#define SMEAR_DURATION 62500
4242

4343
#ifdef __x86_64__
@@ -470,10 +470,11 @@ uint64_t fbclock_apply_utc_offset(
470470
int multiplier = state->utc_offset_post_s - state->utc_offset_pre_s;
471471

472472
// Switch to nanoseconds
473-
uint64_t smear_end_ns = state->clock_smearing_end_s * 1e9;
474-
uint64_t smear_start_ns = state->clock_smearing_start_s * 1e9;
475-
uint64_t offset_post_ns = state->utc_offset_post_s * 1e9;
476-
uint64_t offset_pre_ns = state->utc_offset_pre_s * 1e9;
473+
uint64_t smear_end_ns = state->clock_smearing_end_s * NANOSECONDS_IN_SECONDS;
474+
uint64_t smear_start_ns =
475+
state->clock_smearing_start_s * NANOSECONDS_IN_SECONDS;
476+
uint64_t offset_post_ns = state->utc_offset_post_s * NANOSECONDS_IN_SECONDS;
477+
uint64_t offset_pre_ns = state->utc_offset_pre_s * NANOSECONDS_IN_SECONDS;
477478

478479
return fbclock_apply_smear(
479480
phctime_ns,
@@ -508,10 +509,11 @@ uint64_t fbclock_apply_utc_offset_v2(
508509

509510
// Switch to nanoseconds
510511
uint64_t smear_end_ns =
511-
(state->clock_smearing_start_s + SMEAR_DURATION) * 1e9;
512-
uint64_t smear_start_ns = state->clock_smearing_start_s * 1e9;
513-
uint64_t offset_post_ns = state->utc_offset_post_s * 1e9;
514-
uint64_t offset_pre_ns = state->utc_offset_pre_s * 1e9;
512+
(state->clock_smearing_start_s + SMEAR_DURATION) * NANOSECONDS_IN_SECONDS;
513+
uint64_t smear_start_ns =
514+
state->clock_smearing_start_s * NANOSECONDS_IN_SECONDS;
515+
uint64_t offset_post_ns = state->utc_offset_post_s * NANOSECONDS_IN_SECONDS;
516+
uint64_t offset_pre_ns = state->utc_offset_pre_s * NANOSECONDS_IN_SECONDS;
515517

516518
return fbclock_apply_smear(
517519
phctime_ns,
@@ -523,7 +525,7 @@ uint64_t fbclock_apply_utc_offset_v2(
523525
}
524526

525527
const char* fbclock_strerror(int err_code) {
526-
const char* err_info = "unknown error";
528+
const char* err_info;
527529
switch (err_code) {
528530
case FBCLOCK_E_SHMEM_MAP_FAILED:
529531
err_info = "shmem map error";

0 commit comments

Comments
 (0)