Skip to content

Commit

Permalink
Push current work. Need to revise log2 to resolve bugs later.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinzii committed Mar 4, 2024
1 parent 4de20f6 commit 22fba25
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,10 @@ namespace ccm::internal
}
} // namespace impl
} // namespace

template <typename T>
[[nodiscard]] inline constexpr T log2_double(T num) noexcept
{
return static_cast<T>(impl::log2_double_impl(static_cast<double>(num)));
}
} // namespace ccm::internal
7 changes: 5 additions & 2 deletions include/ccmath/detail/exponential/details/log2_float_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace ccm::internal
constexpr auto k_logTableN_flt = (1 << ccm::internal::k_logTableBitsFlt);
constexpr auto k_logTableOff_flt = 0x3f330000;
*/
inline constexpr double log2_double_impl(double x)
inline constexpr double log2_float_impl(double x)
{
ccm::double_t z{};
ccm::double_t r{};
Expand Down Expand Up @@ -81,9 +81,12 @@ namespace ccm::internal

rhi = ccm::helpers::uint64_to_double(ccm::helpers::double_to_uint64(r) & -1ULL << 32);
rlo = r - rhi;
hi = rhi * ccm::internal::ln2_hi;
//hi = rhi * ccm::internal::ln2_hi;


}

return 0;
}
}
}
Expand Down
64 changes: 56 additions & 8 deletions include/ccmath/detail/exponential/log2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,66 @@
#include "ccmath/detail/compare/isnan.hpp"

#include <type_traits>

#include <array>
#include <cstdint>
#include <limits>
#include <type_traits>

#include "ccmath/internal/helpers/bits.hpp"
#include "ccmath/internal/helpers/floating_point_type.hpp"
#include "ccmath/internal/predef/unlikely.hpp"
#include "ccmath/detail/compare/signbit.hpp"

#include "ccmath/detail/exponential/details/log2_double_impl.hpp"
#include "ccmath/detail/exponential/details/log2_float_impl.hpp"

namespace ccm
{
//template
template <typename T>
inline constexpr T log2(T num) noexcept
{
// If the argument is ±0, -∞ is returned
if (num == static_cast<T>(0))
{
return -std::numeric_limits<T>::infinity();
}

// If the argument is 1, +0 is returned.
if (num == static_cast<T>(1))
{
return 0;
}

// If the argument is negative, NaN is returned
if (ccm::signbit(num))
{
return std::numeric_limits<T>::quiet_NaN();
}

// If the argument is +∞, +∞ is returned.
if (num == std::numeric_limits<T>::infinity())
{
return std::numeric_limits<T>::infinity();
}

// If the argument is NaN, NaN is returned.
if (ccm::isnan(num))
{
return std::numeric_limits<T>::quiet_NaN();
}

// TODO: implement the float specific version of this.
return ccm::internal::log2_double(num);
}

template <typename Integer, std::enable_if_t<std::is_integral_v<Integer>, int> = 0>
inline constexpr double log2(Integer num) noexcept
{
return ccm::log2(static_cast<double>(num));
}

inline constexpr float log2f( float num )
{
return ccm::log2( num );
}

inline constexpr long double log2l( long double num )
{
return ccm::log2( num );
}

} // namespace ccm
13 changes: 13 additions & 0 deletions test/exponential/log2_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,18 @@

TEST(CcmathExponentialTests, Log2)
{
EXPECT_EQ(ccm::log2(1.0), std::log2(1.0));
EXPECT_EQ(ccm::log2(2.0), std::log2(2.0));
EXPECT_EQ(ccm::log2(4.0), std::log2(4.0));
EXPECT_EQ(ccm::log2(8.0), std::log2(8.0));
EXPECT_EQ(ccm::log2(16.0), std::log2(16.0));
EXPECT_EQ(ccm::log2(32.0), std::log2(32.0));
EXPECT_EQ(ccm::log2(64.0), std::log2(64.0));
EXPECT_EQ(ccm::log2(128.0), std::log2(128.0));
EXPECT_EQ(ccm::log2(256.0), std::log2(256.0));
EXPECT_EQ(ccm::log2(512.0), std::log2(512.0));
EXPECT_EQ(ccm::log2(1024.0), std::log2(1024.0));
EXPECT_EQ(ccm::log2(2048.0), std::log2(2048.0));
EXPECT_EQ(ccm::log2(4096.0), std::log2(4096.0));

}

0 comments on commit 22fba25

Please sign in to comment.