You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.6KB

  1. // Copyright 2014 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Fast reciprocal of square-root routines.
  28. #ifndef STMLIB_DSP_RSQRT_H_
  29. #define STMLIB_DSP_RSQRT_H_
  30. #include "stmlib/stmlib.h"
  31. namespace stmlib {
  32. template<typename To, typename From>
  33. struct unsafe_bit_cast_t {
  34. union {
  35. From from;
  36. To to;
  37. };
  38. };
  39. template<typename To, typename From>
  40. To unsafe_bit_cast(From from) {
  41. unsafe_bit_cast_t<To, From> u;
  42. u.from = from;
  43. return u.to;
  44. }
  45. static inline float fast_rsqrt_carmack(float x) {
  46. uint32_t i;
  47. float x2, y;
  48. const float threehalfs = 1.5f;
  49. y = x;
  50. i = unsafe_bit_cast<uint32_t, float>(y);
  51. i = 0x5f3759df - (i >> 1);
  52. y = unsafe_bit_cast<float, uint32_t>(i);
  53. x2 = x * 0.5f;
  54. y = y * (threehalfs - (x2 * y * y));
  55. return y;
  56. }
  57. static inline float fast_rsqrt_accurate(float fp0) {
  58. float _min = 1.0e-38;
  59. float _1p5 = 1.5;
  60. float fp1, fp2, fp3;
  61. uint32_t q = unsafe_bit_cast<uint32_t, float>(fp0);
  62. fp2 = unsafe_bit_cast<float, uint32_t>(0x5F3997BB - ((q >> 1) & 0x3FFFFFFF));
  63. fp1 = _1p5 * fp0 - fp0;
  64. fp3 = fp2 * fp2;
  65. if (fp0 < _min) {
  66. return fp0 > 0 ? fp2 : 1000.0;
  67. }
  68. fp3 = _1p5 - fp1 * fp3;
  69. fp2 = fp2 * fp3;
  70. fp3 = fp2 * fp2;
  71. fp3 = _1p5 - fp1 * fp3;
  72. fp2 = fp2 * fp3;
  73. fp3 = fp2 * fp2;
  74. fp3 = _1p5 - fp1 * fp3;
  75. return fp2 * fp3;
  76. }
  77. } // namespace stmlib
  78. #endif // STMLIB_DSP_RSQRT_H_