type_float.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include "setup.hpp"
  3. #if GLM_COMPILER == GLM_COMPILER_VC12
  4. # pragma warning(push)
  5. # pragma warning(disable: 4512) // assignment operator could not be generated
  6. #endif
  7. namespace glm{
  8. namespace detail
  9. {
  10. template <typename T>
  11. union float_t
  12. {};
  13. // https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
  14. template <>
  15. union float_t<float>
  16. {
  17. typedef int int_type;
  18. typedef float float_type;
  19. GLM_CONSTEXPR float_t(float_type Num = 0.0f) : f(Num) {}
  20. GLM_CONSTEXPR float_t& operator=(float_t const& x)
  21. {
  22. f = x.f;
  23. return *this;
  24. }
  25. // Portable extraction of components.
  26. GLM_CONSTEXPR bool negative() const { return i < 0; }
  27. GLM_CONSTEXPR int_type mantissa() const { return i & ((1 << 23) - 1); }
  28. GLM_CONSTEXPR int_type exponent() const { return (i >> 23) & ((1 << 8) - 1); }
  29. int_type i;
  30. float_type f;
  31. };
  32. template <>
  33. union float_t<double>
  34. {
  35. typedef detail::int64 int_type;
  36. typedef double float_type;
  37. GLM_CONSTEXPR float_t(float_type Num = static_cast<float_type>(0)) : f(Num) {}
  38. GLM_CONSTEXPR float_t& operator=(float_t const& x)
  39. {
  40. f = x.f;
  41. return *this;
  42. }
  43. // Portable extraction of components.
  44. GLM_CONSTEXPR bool negative() const { return i < 0; }
  45. GLM_CONSTEXPR int_type mantissa() const { return i & ((int_type(1) << 52) - 1); }
  46. GLM_CONSTEXPR int_type exponent() const { return (i >> 52) & ((int_type(1) << 11) - 1); }
  47. int_type i;
  48. float_type f;
  49. };
  50. }//namespace detail
  51. }//namespace glm
  52. #if GLM_COMPILER == GLM_COMPILER_VC12
  53. # pragma warning(pop)
  54. #endif