12#ifndef OPENVDB_MATH_HALF_HAS_BEEN_INCLUDED
13#define OPENVDB_MATH_HALF_HAS_BEEN_INCLUDED
177#include <openvdb/version.h>
184#elif defined(__x86_64__)
185#include <x86intrin.h>
186#elif defined(__F16C__)
187#include <immintrin.h>
193#ifdef IMATH_HALF_ENABLE_FP_EXCEPTIONS
209#undef IMATH_HALF_USE_LOOKUP_TABLE
210#define IMATH_HALF_NO_LOOKUP_TABLE
220#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
223#define VDBB_HALF_DENORM_MIN 5.96046448e-08f
225#define VDB_HALF_NRM_MIN 6.10351562e-05f
227#define VDB_HALF_MIN 6.10351562e-05f
229#define VDB_HALF_MAX 65504.0f
231#define VDB_HALF_EPSILON 0.00097656f
234#define VDBB_HALF_DENORM_MIN 5.96046448e-08
236#define VDB_HALF_NRM_MIN 6.10351562e-05
238#define VDB_HALF_MIN 6.10351562e-05f
240#define VDB_HALF_MAX 65504.0
242#define VDB_HALF_EPSILON 0.00097656
246#define VDB_HALF_MANT_DIG 11
250#define VDB_HALF_DIG 3
255#define VDB_HALF_DECIMAL_DIG 5
257#define VDB_HALF_RADIX 2
260#define VDBB_HALF_DENORM_MIN_EXP -13
263#define VDB_HALF_MAX_EXP 16
266#define VDBB_HALF_DENORM_MIN_10_EXP -4
269#define VDB_HALF_MAX_10_EXP 4
281#if !defined(__cplusplus) && !defined(__CUDACC__)
286#if !defined(IMATH_HALF_NO_LOOKUP_TABLE)
287#if defined(__cplusplus)
310 return _mm_cvtss_f32 (_mm_cvtph_ps (_mm_set1_epi16 (h)));
312 return _cvtsh_ss (h);
314#elif defined(IMATH_HALF_USE_LOOKUP_TABLE) && !defined(IMATH_HALF_NO_LOOKUP_TABLE)
315 return imath_half_to_float_table[h].f;
322 uint32_t hexpmant = ( (uint32_t)(h) << 17 ) >> 4;
323 v.
i = ((uint32_t)(h >> 15)) << 31;
336 else if (hexpmant != 0)
345# if defined(_MSC_VER)
351 _BitScanReverse (&bsr, hexpmant);
353# elif defined(__GNUC__) || defined(__clang__)
354 lc = (uint32_t) __builtin_clz (hexpmant);
357 while (0 == ((hexpmant << lc) & 0x80000000))
373 v.
i |= (hexpmant << lc);
393 return _mm_extract_epi16 (
394 _mm_cvtps_ph (_mm_set_ss (f), (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC)),
398 return _cvtss_sh (f, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
403 uint32_t e, m, ui, r, shift;
407 ui = (v.
i & ~0x80000000);
408 ret = ((v.
i >> 16) & 0x8000);
411 if (ui >= 0x38800000)
417 if (ui == 0x7f800000)
419 m = (ui & 0x7fffff) >> 13;
421 return ret | (uint16_t)m | (uint16_t)(m == 0);
427# ifdef IMATH_HALF_ENABLE_FP_EXCEPTIONS
428 feraiseexcept (FE_OVERFLOW);
434 ui = ((ui + 0x00000fff + ((ui >> 13) & 1)) >> 13);
435 return ret | (uint16_t)ui;
441# ifdef IMATH_HALF_ENABLE_FP_EXCEPTIONS
444 feraiseexcept (FE_UNDERFLOW);
452 m = 0x800000 | (ui & 0x7fffff);
453 r = m << (32 - shift);
455 if (r > 0x80000000 || (r == 0x80000000 && (ret & 0x1) != 0))
525 half (
float f) noexcept;
528 constexpr
half (FromBitsTag, uint16_t
bits) noexcept;
542 operator
float() const noexcept;
548 constexpr
half operator-() const noexcept;
551 half& operator= (const
half& h) noexcept = default;
557 half& operator= (
float f) noexcept;
563 half& operator+= (
float f) noexcept;
569 half& operator-= (
float f) noexcept;
575 half& operator*= (
float f) noexcept;
581 half& operator/= (
float f) noexcept;
588 constexpr
half round (
unsigned int n) const noexcept;
595 constexpr
bool isFinite() const noexcept;
604 constexpr
bool isZero() const noexcept;
607 constexpr
bool isNan() const noexcept;
627 static constexpr
half qNan() noexcept;
630 static constexpr
half sNan() noexcept;
638 constexpr uint16_t
bits() const noexcept;
646 static_assert (sizeof (
float) == sizeof (uint32_t),
647 "Assumption about the size of floats correct");
652 constexpr uint16_t mantissa() const noexcept;
653 constexpr uint16_t exponent() const noexcept;
678inline half::operator float() const noexcept
702 uint16_t s = _h & 0x8000;
703 uint16_t e = _h & 0x7fff;
735 half h (FromBits, s | e);
747 return half (FromBits,
bits() ^ 0x8000);
751half::operator= (
float f)
noexcept
758half::operator+= (
half h)
noexcept
760 *
this =
half (
float (*
this) +
float (h));
765half::operator+= (
float f)
noexcept
767 *
this =
half (
float (*
this) + f);
772half::operator-= (
half h)
noexcept
774 *
this =
half (
float (*
this) -
float (h));
779half::operator-= (
float f)
noexcept
781 *
this =
half (
float (*
this) - f);
786half::operator*= (
half h)
noexcept
788 *
this =
half (
float (*
this) *
float (h));
793half::operator*= (
float f)
noexcept
795 *
this =
half (
float (*
this) * f);
800half::operator/= (
half h)
noexcept
802 *
this =
half (
float (*
this) /
float (h));
807half::operator/= (
float f)
noexcept
809 *
this =
half (
float (*
this) / f);
813inline constexpr uint16_t
814half::mantissa() const noexcept
819inline constexpr uint16_t
820half::exponent() const noexcept
822 return (_h >> 10) & 0x001f;
828 return exponent() < 31;
834 return exponent() > 0 && exponent() < 31;
840 return exponent() == 0 && mantissa() != 0;
846 return (_h & 0x7fff) == 0;
852 return exponent() == 31 && mantissa() != 0;
858 return exponent() == 31 && mantissa() == 0;
864 return (_h & 0x8000) != 0;
870 return half (FromBits, 0x7c00);
876 return half (FromBits, 0xfc00);
882 return half (FromBits, 0x7fff);
888 return half (FromBits, 0x7dff);
891inline constexpr uint16_t
928template <>
class numeric_limits<
openvdb::math::internal::half>
933 static constexpr openvdb::math::internal::half
min () noexcept
935 return openvdb::math::internal::half (openvdb::math::internal::half::FromBits, 0x0400);
937 static constexpr openvdb::math::internal::half
max () noexcept
939 return openvdb::math::internal::half (openvdb::math::internal::half::FromBits, 0x7bff);
941 static constexpr openvdb::math::internal::half
lowest ()
943 return openvdb::math::internal::half (openvdb::math::internal::half::FromBits, 0xfbff);
953 static constexpr openvdb::math::internal::half
epsilon () noexcept
955 return openvdb::math::internal::half (openvdb::math::internal::half::FromBits, 0x1400);
957 static constexpr openvdb::math::internal::half
round_error () noexcept
959 return openvdb::math::internal::half (openvdb::math::internal::half::FromBits, 0x3800);
970 static constexpr float_denorm_style
has_denorm = denorm_present;
972 static constexpr openvdb::math::internal::half
infinity () noexcept
974 return openvdb::math::internal::half (openvdb::math::internal::half::FromBits, 0x7c00);
976 static constexpr openvdb::math::internal::half
quiet_NaN () noexcept
978 return openvdb::math::internal::half (openvdb::math::internal::half::FromBits, 0x7fff);
982 return openvdb::math::internal::half (openvdb::math::internal::half::FromBits, 0x7dff);
984 static constexpr openvdb::math::internal::half
denorm_min () noexcept
986 return openvdb::math::internal::half (openvdb::math::internal::half::FromBits, 0x0001);
995 static constexpr float_round_style
round_style = round_to_nearest;
#define VDB_HALF_DECIMAL_DIG
uint16_t imath_half_bits_t
a type for both C-only programs and C++ to use the same utilities
Definition Half.h:279
OPENVDB_API void printBits(std::ostream &os, half h)
#define VDB_HALF_MAX_10_EXP
Definition Half.h:269
union imath_half_uif imath_half_uif_t
a type for both C-only programs and C++ to use the same utilities
#define VDBB_HALF_DENORM_MIN_EXP
#define VDBB_HALF_DENORM_MIN_10_EXP
Definition Half.h:266
static imath_half_bits_t imath_float_to_half(float f)
Definition Half.h:388
imath_half_bits_t half
if we're in a C-only context, alias the half bits type to half
Definition Half.h:283
#define VDB_HALF_MAX_EXP
Definition Half.h:263
OPENVDB_API std::istream & operator>>(std::istream &is, half &h)
Input h from is.
#define VDB_HALF_MANT_DIG
static float imath_half_to_float(imath_half_bits_t h)
Definition Half.h:300
OPENVDB_API std::ostream & operator<<(std::ostream &os, half h)
Output h to os, formatted as a float.
static constexpr half posInf() noexcept
Return +infinity.
Definition Half.h:868
constexpr bool isNormalized() const noexcept
Return true if a normalized number.
Definition Half.h:832
static constexpr half qNan() noexcept
Returns a NAN with the bit pattern 0111111111111111.
Definition Half.h:880
constexpr bool isFinite() const noexcept
Definition Half.h:826
constexpr bool isDenormalized() const noexcept
Return true if a denormalized number.
Definition Half.h:838
constexpr half operator-() const noexcept
Unary minus.
Definition Half.h:745
constexpr bool isNegative() const noexcept
Return true if the sign bit is set (negative)
Definition Half.h:862
constexpr bool isZero() const noexcept
Return true if zero.
Definition Half.h:844
imath_half_uif uif
Definition Half.h:648
constexpr void setBits(uint16_t bits) noexcept
Set the bit pattern.
Definition Half.h:898
constexpr uint16_t bits() const noexcept
Return the bit pattern.
Definition Half.h:892
constexpr bool isInfinity() const noexcept
Return true if a positive or a negative infinity.
Definition Half.h:856
static constexpr half sNan() noexcept
Return a NAN with the bit pattern 0111110111111111.
Definition Half.h:886
static constexpr half negInf() noexcept
Return -infinity.
Definition Half.h:874
constexpr bool isNan() const noexcept
Return true if NAN.
Definition Half.h:850
constexpr half round(unsigned int n) const noexcept
Definition Half.h:688
static constexpr int max_digits10
Definition Half.h:948
static constexpr bool traps
Definition Half.h:993
static constexpr openvdb::math::internal::half quiet_NaN() noexcept
Definition Half.h:976
static constexpr openvdb::math::internal::half round_error() noexcept
Definition Half.h:957
static constexpr int radix
Definition Half.h:952
static constexpr bool is_iec559
Definition Half.h:989
static constexpr float_denorm_style has_denorm
Definition Half.h:970
static constexpr openvdb::math::internal::half denorm_min() noexcept
Definition Half.h:984
static constexpr openvdb::math::internal::half infinity() noexcept
Definition Half.h:972
static constexpr int max_exponent
Definition Half.h:964
static constexpr int digits
Definition Half.h:946
static constexpr bool is_modulo
Definition Half.h:991
static constexpr openvdb::math::internal::half signaling_NaN() noexcept
Definition Half.h:980
static constexpr bool has_denorm_loss
Definition Half.h:971
static constexpr bool has_infinity
Definition Half.h:967
static constexpr int digits10
Definition Half.h:947
static constexpr openvdb::math::internal::half max() noexcept
Definition Half.h:937
static constexpr openvdb::math::internal::half min() noexcept
Definition Half.h:933
static constexpr int max_exponent10
Definition Half.h:965
static constexpr int min_exponent
Definition Half.h:962
static constexpr openvdb::math::internal::half lowest()
Definition Half.h:941
static constexpr bool is_integer
Definition Half.h:950
static constexpr bool is_exact
Definition Half.h:951
static constexpr bool is_signed
Definition Half.h:949
static const bool is_specialized
Definition Half.h:931
static constexpr bool is_bounded
Definition Half.h:990
static constexpr int min_exponent10
Definition Half.h:963
static constexpr openvdb::math::internal::half epsilon() noexcept
Definition Half.h:953
static constexpr bool tinyness_before
Definition Half.h:994
static constexpr bool has_signaling_NaN
Definition Half.h:969
static constexpr bool has_quiet_NaN
Definition Half.h:968
static constexpr float_round_style round_style
Definition Half.h:995
Definition Exceptions.h:13
a type for both C-only programs and C++ to use the same utilities
Definition Half.h:273
uint32_t i
Definition Half.h:274
float f
Definition Half.h:275
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218