func_integer.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
  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. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @ref core
  28. /// @file glm/detail/func_integer.hpp
  29. /// @date 2010-03-17 / 2011-06-18
  30. /// @author Christophe Riccio
  31. ///
  32. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  33. ///
  34. /// @defgroup core_func_integer Integer functions
  35. /// @ingroup core
  36. ///
  37. /// These all operate component-wise. The description is per component.
  38. /// The notation [a, b] means the set of bits from bit-number a through bit-number
  39. /// b, inclusive. The lowest-order bit is bit 0.
  40. ///////////////////////////////////////////////////////////////////////////////////
  41. #pragma once
  42. #include "setup.hpp"
  43. #include "precision.hpp"
  44. #include "func_common.hpp"
  45. #include "func_vector_relational.hpp"
  46. namespace glm
  47. {
  48. /// @addtogroup core_func_integer
  49. /// @{
  50. /// Adds 32-bit unsigned integer x and y, returning the sum
  51. /// modulo pow(2, 32). The value carry is set to 0 if the sum was
  52. /// less than pow(2, 32), or to 1 otherwise.
  53. ///
  54. /// @tparam genUType Unsigned integer scalar or vector types.
  55. ///
  56. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/uaddCarry.xml">GLSL uaddCarry man page</a>
  57. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  58. template <precision P, template <typename, precision> class vecType>
  59. GLM_FUNC_DECL vecType<uint, P> uaddCarry(
  60. vecType<uint, P> const & x,
  61. vecType<uint, P> const & y,
  62. vecType<uint, P> & carry);
  63. /// Subtracts the 32-bit unsigned integer y from x, returning
  64. /// the difference if non-negative, or pow(2, 32) plus the difference
  65. /// otherwise. The value borrow is set to 0 if x >= y, or to 1 otherwise.
  66. ///
  67. /// @tparam genUType Unsigned integer scalar or vector types.
  68. ///
  69. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/usubBorrow.xml">GLSL usubBorrow man page</a>
  70. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  71. template <precision P, template <typename, precision> class vecType>
  72. GLM_FUNC_DECL vecType<uint, P> usubBorrow(
  73. vecType<uint, P> const & x,
  74. vecType<uint, P> const & y,
  75. vecType<uint, P> & borrow);
  76. /// Multiplies 32-bit integers x and y, producing a 64-bit
  77. /// result. The 32 least-significant bits are returned in lsb.
  78. /// The 32 most-significant bits are returned in msb.
  79. ///
  80. /// @tparam genUType Unsigned integer scalar or vector types.
  81. ///
  82. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/umulExtended.xml">GLSL umulExtended man page</a>
  83. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  84. template <precision P, template <typename, precision> class vecType>
  85. GLM_FUNC_DECL void umulExtended(
  86. vecType<uint, P> const & x,
  87. vecType<uint, P> const & y,
  88. vecType<uint, P> & msb,
  89. vecType<uint, P> & lsb);
  90. /// Multiplies 32-bit integers x and y, producing a 64-bit
  91. /// result. The 32 least-significant bits are returned in lsb.
  92. /// The 32 most-significant bits are returned in msb.
  93. ///
  94. /// @tparam genIType Signed integer scalar or vector types.
  95. ///
  96. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/imulExtended.xml">GLSL imulExtended man page</a>
  97. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  98. template <precision P, template <typename, precision> class vecType>
  99. GLM_FUNC_DECL void imulExtended(
  100. vecType<int, P> const & x,
  101. vecType<int, P> const & y,
  102. vecType<int, P> & msb,
  103. vecType<int, P> & lsb);
  104. /// Extracts bits [offset, offset + bits - 1] from value,
  105. /// returning them in the least significant bits of the result.
  106. /// For unsigned data types, the most significant bits of the
  107. /// result will be set to zero. For signed data types, the
  108. /// most significant bits will be set to the value of bit offset + base - 1.
  109. ///
  110. /// If bits is zero, the result will be zero. The result will be
  111. /// undefined if offset or bits is negative, or if the sum of
  112. /// offset and bits is greater than the number of bits used
  113. /// to store the operand.
  114. ///
  115. /// @tparam T Signed or unsigned integer scalar or vector types.
  116. ///
  117. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/bitfieldExtract.xml">GLSL bitfieldExtract man page</a>
  118. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  119. template <typename T, precision P, template <typename, precision> class vecType>
  120. GLM_FUNC_DECL vecType<T, P> bitfieldExtract(
  121. vecType<T, P> const & Value,
  122. int Offset,
  123. int Bits);
  124. /// Returns the insertion the bits least-significant bits of insert into base.
  125. ///
  126. /// The result will have bits [offset, offset + bits - 1] taken
  127. /// from bits [0, bits - 1] of insert, and all other bits taken
  128. /// directly from the corresponding bits of base. If bits is
  129. /// zero, the result will simply be base. The result will be
  130. /// undefined if offset or bits is negative, or if the sum of
  131. /// offset and bits is greater than the number of bits used to
  132. /// store the operand.
  133. ///
  134. /// @tparam T Signed or unsigned integer scalar or vector types.
  135. ///
  136. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/bitfieldInsert.xml">GLSL bitfieldInsert man page</a>
  137. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  138. template <typename T, precision P, template <typename, precision> class vecType>
  139. GLM_FUNC_DECL vecType<T, P> bitfieldInsert(
  140. vecType<T, P> const & Base,
  141. vecType<T, P> const & Insert,
  142. int Offset,
  143. int Bits);
  144. /// Returns the reversal of the bits of value.
  145. /// The bit numbered n of the result will be taken from bit (bits - 1) - n of value,
  146. /// where bits is the total number of bits used to represent value.
  147. ///
  148. /// @tparam T Signed or unsigned integer scalar or vector types.
  149. ///
  150. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/bitfieldReverse.xml">GLSL bitfieldReverse man page</a>
  151. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  152. template <typename T, precision P, template <typename, precision> class vecType>
  153. GLM_FUNC_DECL vecType<T, P> bitfieldReverse(vecType<T, P> const & v);
  154. /// Returns the number of bits set to 1 in the binary representation of value.
  155. ///
  156. /// @tparam T Signed or unsigned integer scalar or vector types.
  157. ///
  158. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/bitCount.xml">GLSL bitCount man page</a>
  159. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  160. template <typename genType>
  161. GLM_FUNC_DECL int bitCount(genType v);
  162. /// Returns the number of bits set to 1 in the binary representation of value.
  163. ///
  164. /// @tparam T Signed or unsigned integer scalar or vector types.
  165. ///
  166. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/bitCount.xml">GLSL bitCount man page</a>
  167. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  168. template <typename T, precision P, template <typename, precision> class vecType>
  169. GLM_FUNC_DECL vecType<int, P> bitCount(vecType<T, P> const & v);
  170. /// Returns the bit number of the least significant bit set to
  171. /// 1 in the binary representation of value.
  172. /// If value is zero, -1 will be returned.
  173. ///
  174. /// @tparam T Signed or unsigned integer scalar types.
  175. ///
  176. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/findLSB.xml">GLSL findLSB man page</a>
  177. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  178. template <typename genIUType>
  179. GLM_FUNC_DECL int findLSB(genIUType x);
  180. /// Returns the bit number of the least significant bit set to
  181. /// 1 in the binary representation of value.
  182. /// If value is zero, -1 will be returned.
  183. ///
  184. /// @tparam T Signed or unsigned integer scalar types.
  185. ///
  186. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/findLSB.xml">GLSL findLSB man page</a>
  187. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  188. template <typename T, precision P, template <typename, precision> class vecType>
  189. GLM_FUNC_DECL vecType<int, P> findLSB(vecType<T, P> const & v);
  190. /// Returns the bit number of the most significant bit in the binary representation of value.
  191. /// For positive integers, the result will be the bit number of the most significant bit set to 1.
  192. /// For negative integers, the result will be the bit number of the most significant
  193. /// bit set to 0. For a value of zero or negative one, -1 will be returned.
  194. ///
  195. /// @tparam T Signed or unsigned integer scalar types.
  196. ///
  197. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/findMSB.xml">GLSL findMSB man page</a>
  198. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  199. template <typename genIUType>
  200. GLM_FUNC_DECL int findMSB(genIUType x);
  201. /// Returns the bit number of the most significant bit in the binary representation of value.
  202. /// For positive integers, the result will be the bit number of the most significant bit set to 1.
  203. /// For negative integers, the result will be the bit number of the most significant
  204. /// bit set to 0. For a value of zero or negative one, -1 will be returned.
  205. ///
  206. /// @tparam T Signed or unsigned integer scalar types.
  207. ///
  208. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/findMSB.xml">GLSL findMSB man page</a>
  209. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.8 Integer Functions</a>
  210. template <typename T, precision P, template <typename, precision> class vecType>
  211. GLM_FUNC_DECL vecType<int, P> findMSB(vecType<T, P> const & v);
  212. /// @}
  213. }//namespace glm
  214. #include "func_integer.inl"