diff options
Diffstat (limited to 'src/lib/netlist/plib/ptime.h')
-rw-r--r-- | src/lib/netlist/plib/ptime.h | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/lib/netlist/plib/ptime.h b/src/lib/netlist/plib/ptime.h index 4044f518e24..c0a6cd5a34b 100644 --- a/src/lib/netlist/plib/ptime.h +++ b/src/lib/netlist/plib/ptime.h @@ -99,11 +99,18 @@ namespace plib } constexpr internal_type as_raw() const noexcept { return m_time; } - constexpr double as_double() const noexcept + + template <typename FT> + constexpr typename std::enable_if<std::is_floating_point<FT>::value, FT>::type + as_fp() const noexcept { - return static_cast<double>(m_time) * inv_res; + return static_cast<FT>(m_time) * inv_res<FT>(); } + constexpr double as_double() const noexcept { return as_fp<double>(); } + constexpr double as_float() const noexcept { return as_fp<float>(); } + constexpr double as_long_double() const noexcept { return as_fp<long double>(); } + // for save states .... C14CONSTEXPR internal_type *get_internaltype_ptr() noexcept { return &m_time; } @@ -113,7 +120,19 @@ namespace plib static constexpr const ptime from_sec(const internal_type s) noexcept { return ptime(s, UINT64_C( 1)); } static constexpr const ptime from_hz(const internal_type hz) noexcept { return ptime(1 , hz); } static constexpr const ptime from_raw(const internal_type raw) noexcept { return ptime(raw); } - static constexpr const ptime from_double(const double t) noexcept { return ptime(static_cast<internal_type>(std::floor(t * static_cast<double>(RES) + 0.5)), RES); } + + template <typename FT> + static constexpr const typename std::enable_if<std::is_floating_point<FT>::value, ptime>::type + from_fp(const FT t) noexcept { return ptime(static_cast<internal_type>(std::floor(t * static_cast<FT>(RES) + 0.5)), RES); } + + static constexpr const ptime from_double(const double t) noexcept + { return from_fp<double>(t); } + + static constexpr const ptime from_float(const float t) noexcept + { return from_fp<float>(t); } + + static constexpr const ptime from_long_double(const long double t) noexcept + { return from_fp<long double>(t); } static constexpr const ptime zero() noexcept { return ptime(0, RES); } static constexpr const ptime quantum() noexcept { return ptime(1, RES); } @@ -126,7 +145,8 @@ namespace plib constexpr internal_type in_sec() const noexcept { return m_time / (RES / UINT64_C( 1)); } private: - static constexpr const double inv_res = 1.0 / static_cast<double>(RES); + template <typename FT> + static constexpr FT inv_res() noexcept { return static_cast<FT>(1.0) / static_cast<FT>(RES); } internal_type m_time; }; |