math.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // ======================================================================== //
  2. // Copyright 2009-2019 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "common/platform.h"
  18. namespace oidn {
  19. constexpr float minVectorLength = 1e-10f;
  20. constexpr float minVectorLengthSqr = minVectorLength * minVectorLength;
  21. using std::log;
  22. using std::log2;
  23. using std::exp;
  24. using std::exp2;
  25. using std::pow;
  26. using std::isfinite;
  27. using std::isnan;
  28. __forceinline float sqr(float x)
  29. {
  30. return x * x;
  31. }
  32. __forceinline float rcp(float x)
  33. {
  34. __m128 r = _mm_rcp_ss(_mm_set_ss(x));
  35. return _mm_cvtss_f32(_mm_sub_ss(_mm_add_ss(r, r), _mm_mul_ss(_mm_mul_ss(r, r), _mm_set_ss(x))));
  36. }
  37. __forceinline float rsqrt(float x)
  38. {
  39. __m128 r = _mm_rsqrt_ss(_mm_set_ss(x));
  40. return _mm_cvtss_f32(_mm_add_ss(_mm_mul_ss(_mm_set_ss(1.5f), r),
  41. _mm_mul_ss(_mm_mul_ss(_mm_mul_ss(_mm_set_ss(x), _mm_set_ss(-0.5f)), r), _mm_mul_ss(r, r))));
  42. }
  43. __forceinline float maxSafe(float value, float minValue)
  44. {
  45. return isfinite(value) ? max(value, minValue) : minValue;
  46. }
  47. __forceinline float clampSafe(float value, float minValue, float maxValue)
  48. {
  49. return isfinite(value) ? clamp(value, minValue, maxValue) : minValue;
  50. }
  51. // Returns ceil(a / b) for non-negative integers
  52. template<class Int>
  53. __forceinline constexpr Int ceilDiv(Int a, Int b)
  54. {
  55. //assert(a >= 0);
  56. //assert(b > 0);
  57. return (a + b - 1) / b;
  58. }
  59. // Returns a rounded up to multiple of b
  60. template<class Int>
  61. __forceinline constexpr Int roundUp(Int a, Int b)
  62. {
  63. return ceilDiv(a, b) * b;
  64. }
  65. } // namespace oidn