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.

176 lines
4.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file 3d_fastmath.h
  26. * @brief Defines math related functions
  27. */
  28. #ifndef _3D_FASTMATH_H
  29. #define _3D_FASTMATH_H
  30. #include <cmath>
  31. #include <cstdint>
  32. #include <cstring>
  33. // Define this flag to use fast math optimizations
  34. #define FASTMATH_USE
  35. #define L1_CACHE_LINE_SIZE 64
  36. #ifdef FASTMATH_USE
  37. #define INTFLOORF(s) (lrintf( (s) - (0.5f - FLT_EPSILON) ))
  38. #else
  39. #define INTFLOORF(s) ((int)( floor(s) ))
  40. #endif
  41. // Fast Float Random Numbers
  42. // a small and fast implementation for random float numbers in the range [-1,1]
  43. // This is not thread safe
  44. float Fast_RandFloat();
  45. int Fast_rand( void );
  46. void Fast_srand( unsigned int seed );
  47. /**
  48. * This part contains some functions from the PBRT 3 source code.
  49. * https://github.com/mmp/pbrt-v3/blob/master/src/core/pbrt.h
  50. */
  51. /*
  52. pbrt source code is Copyright(c) 1998-2015
  53. Matt Pharr, Greg Humphreys, and Wenzel Jakob.
  54. This file is part of pbrt.
  55. Redistribution and use in source and binary forms, with or without
  56. modification, are permitted provided that the following conditions are
  57. met:
  58. - Redistributions of source code must retain the above copyright
  59. notice, this list of conditions and the following disclaimer.
  60. - Redistributions in binary form must reproduce the above copyright
  61. notice, this list of conditions and the following disclaimer in the
  62. documentation and/or other materials provided with the distribution.
  63. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  64. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  65. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  66. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  67. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  68. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  69. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  70. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  71. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  72. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  73. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  74. */
  75. // Global Inline Functions
  76. inline uint32_t FloatToBits( float f )
  77. {
  78. uint32_t ui;
  79. memcpy( &ui, &f, sizeof( float ) );
  80. return ui;
  81. }
  82. inline float BitsToFloat( uint32_t ui )
  83. {
  84. float f;
  85. memcpy( &f, &ui, sizeof (uint32_t ) );
  86. return f;
  87. }
  88. inline uint64_t FloatToBits( double f )
  89. {
  90. uint64_t ui;
  91. memcpy( &ui, &f, sizeof( double ) );
  92. return ui;
  93. }
  94. inline double BitsToFloat( uint64_t ui )
  95. {
  96. double f;
  97. memcpy( &f, &ui, sizeof( uint64_t ) );
  98. return f;
  99. }
  100. inline float NextFloatUp( float v )
  101. {
  102. // Handle infinity and negative zero for _NextFloatUp()_
  103. if( std::isinf( v ) && (v > 0.) )
  104. return v;
  105. if( v == -0.f )
  106. v = 0.f;
  107. // Advance _v_ to next higher float
  108. uint32_t ui = FloatToBits( v );
  109. if( v >= 0. )
  110. ++ui;
  111. else
  112. --ui;
  113. return BitsToFloat( ui );
  114. }
  115. inline float NextFloatDown( float v )
  116. {
  117. // Handle infinity and positive zero for _NextFloatDown()_
  118. if( std::isinf( v ) && (v < 0.) )
  119. return v;
  120. if( v == 0.f )
  121. v = -0.f;
  122. uint32_t ui = FloatToBits( v );
  123. if( v > 0. )
  124. --ui;
  125. else
  126. ++ui;
  127. return BitsToFloat( ui );
  128. }
  129. #endif // 3D_FASTMATH_H