Megatech


abs

#include <concepts>
#include <bit>
#include <limits>

template <std::signed_integral Type>
constexpr Type abs(Type x) {
  constexpr auto bits = std::numeric_limits<Type>::digits;
  return (x ^ (x >> bits)) - (x >> bits);
}

template <std::floating_point Type>
constexpr Type abs(Type x) {
  static_assert(std::numeric_limits<Type>::is_iec559);
  if constexpr (std::endian::native == std::endian::little)
  {
    reinterpret_cast<char*>(&x)[sizeof(Type) - 1] &= 0x7f;
  }
  else if constexpr (std::endian::native == std::endian::big)
  {
    reinterpret_cast<char*>(&x)[0] &= 0x7f;
  }
  return x;
}