qnan.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /** @file qnan.h
  2. * @brief Some utilities for our dealings with qnans.
  3. *
  4. * @note Some loaders use qnans heavily to mark invalid values (and they're
  5. * even returned by Gen(Smooth)Normals if normals are undefined for a
  6. * primitive). Their whole usage is wrapped here, so you can easily
  7. * fix issues with platforms with a different qnan implementation.
  8. */
  9. #if (!defined AI_QNAN_H_INCLUDED)
  10. #define AI_QNAN_H_INCLUDED
  11. // ---------------------------------------------------------------------------
  12. /** @brief Data structure for the bit pattern of a 32 Bit
  13. * IEEE 754 floating-point number.
  14. */
  15. union _IEEESingle
  16. {
  17. float Float;
  18. struct
  19. {
  20. uint32_t Frac : 23;
  21. uint32_t Exp : 8;
  22. uint32_t Sign : 1;
  23. } IEEE;
  24. } ;
  25. // ---------------------------------------------------------------------------
  26. /** @brief check whether a float is qNaN.
  27. * @param in Input value
  28. */
  29. AI_FORCE_INLINE bool is_qnan(float in)
  30. {
  31. return (in != in);
  32. }
  33. // ---------------------------------------------------------------------------
  34. /** @brief check whether a float is NOT qNaN.
  35. * @param in Input value
  36. */
  37. AI_FORCE_INLINE bool is_not_qnan(float in)
  38. {
  39. return (in == in);
  40. }
  41. // ---------------------------------------------------------------------------
  42. /** @brief check whether a float is either NaN or (+/-) INF.
  43. *
  44. * Denorms return false, they're treated like normal values.
  45. * @param in Input value
  46. */
  47. AI_FORCE_INLINE bool is_special_float(float in)
  48. {
  49. return (((_IEEESingle*)&in)->IEEE.Exp == (1u << 8)-1);
  50. }
  51. // ---------------------------------------------------------------------------
  52. /** @brief Get a qnan
  53. */
  54. AI_FORCE_INLINE float get_qnan()
  55. {
  56. return std::numeric_limits<float>::quiet_NaN();
  57. }
  58. #endif // !! AI_QNAN_H_INCLUDED