Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lroundl error on Windows. [5505] #549

Merged
merged 3 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions include/fastrtps/rtps/common/Time_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
namespace eprosima{
namespace fastrtps{

namespace rtps{
// 1 fraction = 1/(2^32) seconds
constexpr long double FRACTION_TO_NANO = 0.23283064365386962890625; // 1000000000 / 4294967296
constexpr long double NANO_TO_FRACTION = 4.294967296; // 4294967296 / 1000000000
} // namespace rtps

/**
* Structure Time_t, used to describe times.
* @ingroup COMMON_MODULE
Expand Down
60 changes: 40 additions & 20 deletions src/cpp/rtps/common/Time_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@

using namespace eprosima::fastrtps;

namespace { // unnamed namespace for inline functions in compilation unit. Better practice than static inline.

constexpr uint64_t C_FRACTIONS_PER_SEC = 4294967296ULL;
constexpr uint64_t C_NANOSECONDS_PER_SEC = 1000000000ULL;

inline uint32_t frac_to_nano(
uint32_t fractions)
{
return static_cast<uint32_t>((fractions * C_NANOSECONDS_PER_SEC) / C_FRACTIONS_PER_SEC);
}

inline uint32_t nano_to_frac(
uint32_t nanosecs)
{
return static_cast<uint32_t>((nanosecs * C_FRACTIONS_PER_SEC) / C_NANOSECONDS_PER_SEC);
}
} // unnamed namespace

Time_t::Time_t()
{
seconds = 0;
Expand All @@ -37,37 +55,38 @@ Time_t::Time_t(
long double sec)
{
seconds = static_cast<int32_t>(sec);
nanosec = static_cast<uint32_t>((sec - seconds) * 1000000000ULL);
nanosec = static_cast<uint32_t>((sec - seconds) * C_NANOSECONDS_PER_SEC);
}

void Time_t::fraction(
uint32_t frac)
{
nanosec = (frac == 0xffffffff)
? 0xffffffff
: static_cast<uint32_t>(std::lroundl(frac * rtps::FRACTION_TO_NANO));
: frac_to_nano(frac);
}

uint32_t Time_t::fraction() const
{
if (nanosec == 0xffffffff)
{
return nanosec;
}
uint32_t fraction = (nanosec == 0xffffffff)
? 0xffffffff
: nano_to_frac(nanosec);

uint32_t fraction = static_cast<uint32_t>(std::lroundl(nanosec * rtps::NANO_TO_FRACTION));
uint32_t nano_check = static_cast<uint32_t>(std::lroundl(fraction * rtps::FRACTION_TO_NANO));
while (nano_check != nanosec)
if (fraction != 0xffffffff)
{
nano_check = static_cast<uint32_t>(std::lroundl(++fraction * rtps::FRACTION_TO_NANO));
uint32_t nano_check = frac_to_nano(fraction);
while (nano_check != nanosec)
{
nano_check = frac_to_nano(++fraction);
}
}

return fraction;
}

int64_t Time_t::to_ns() const
{
int64_t nano = seconds * 1000000000ULL;
int64_t nano = seconds * static_cast<int64_t>(C_NANOSECONDS_PER_SEC);
nano += nanosec;
return nano;
}
Expand All @@ -91,7 +110,7 @@ rtps::Time_t::Time_t(
long double sec)
{
seconds_ = static_cast<int32_t>(sec);
set_fraction(static_cast<uint32_t>((sec - seconds_) * 4294967296ULL));
set_fraction(static_cast<uint32_t>((sec - seconds_) * C_FRACTIONS_PER_SEC));
}

rtps::Time_t::Time_t(
Expand All @@ -103,7 +122,7 @@ rtps::Time_t::Time_t(

int64_t rtps::Time_t::to_ns() const
{
int64_t nano = seconds_ * 1000000000ULL;
int64_t nano = seconds_ * static_cast<int64_t>(C_NANOSECONDS_PER_SEC);
nano += nanosec_;
return nano;
}
Expand Down Expand Up @@ -132,7 +151,7 @@ uint32_t rtps::Time_t::nanosec() const
void rtps::Time_t::nanosec(
uint32_t nanos)
{
const uint32_t s_to_nano = 1000000000UL;
const uint32_t s_to_nano = static_cast<uint32_t>(C_NANOSECONDS_PER_SEC);
if (nanos >= s_to_nano)
{
nanos %= s_to_nano; // Remove the seconds
Expand Down Expand Up @@ -168,23 +187,24 @@ void rtps::Time_t::set_fraction(
fraction_ = frac;
nanosec_ = (fraction_ == 0xffffffff)
? 0xffffffff
: static_cast<uint32_t>(std::lroundl(fraction_ * FRACTION_TO_NANO));
: frac_to_nano(fraction_);
}

void rtps::Time_t::set_nanosec(
uint32_t nanos)
{
nanosec_ = nanos;
fraction_ = (nanosec_ == 0xffffffff)

fraction_ = (nanos == 0xffffffff)
? 0xffffffff
: static_cast<uint32_t>(std::lroundl(nanosec_ * NANO_TO_FRACTION));
: nano_to_frac(nanos);

if (fraction_ != 0xffffffff)
{
uint32_t nano_check = static_cast<uint32_t>(std::lroundl(fraction_ * FRACTION_TO_NANO));
uint32_t nano_check = frac_to_nano(fraction_);
while (nano_check != nanosec_)
{
nano_check = static_cast<uint32_t>(std::lroundl(++fraction_ * FRACTION_TO_NANO));
nano_check = frac_to_nano(++fraction_);
}
}
}
}