compute_common.hpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. #include "setup.hpp"
  3. #include <limits>
  4. namespace glm{
  5. namespace detail
  6. {
  7. template<typename genFIType, bool /*signed*/>
  8. struct compute_abs
  9. {};
  10. template<typename genFIType>
  11. struct compute_abs<genFIType, true>
  12. {
  13. GLM_FUNC_QUALIFIER GLM_CONSTEXPR static genFIType call(genFIType x)
  14. {
  15. GLM_STATIC_ASSERT(
  16. std::numeric_limits<genFIType>::is_iec559 || std::numeric_limits<genFIType>::is_signed,
  17. "'abs' only accept floating-point and integer scalar or vector inputs");
  18. return x >= genFIType(0) ? x : -x;
  19. // TODO, perf comp with: *(((int *) &x) + 1) &= 0x7fffffff;
  20. }
  21. };
  22. #if GLM_COMPILER & GLM_COMPILER_CUDA
  23. template<>
  24. struct compute_abs<float, true>
  25. {
  26. GLM_FUNC_QUALIFIER GLM_CONSTEXPR static float call(float x)
  27. {
  28. return fabsf(x);
  29. }
  30. };
  31. #endif
  32. template<typename genFIType>
  33. struct compute_abs<genFIType, false>
  34. {
  35. GLM_FUNC_QUALIFIER GLM_CONSTEXPR static genFIType call(genFIType x)
  36. {
  37. GLM_STATIC_ASSERT(
  38. (!std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer),
  39. "'abs' only accept floating-point and integer scalar or vector inputs");
  40. return x;
  41. }
  42. };
  43. }//namespace detail
  44. }//namespace glm