floatInVec.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. Copyright (C) 2009 Sony Computer Entertainment Inc.
  3. All rights reserved.
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef _FLOATINVEC_H
  14. #define _FLOATINVEC_H
  15. #include <math.h>
  16. namespace Vectormath {
  17. class boolInVec;
  18. //--------------------------------------------------------------------------------------------------
  19. // floatInVec class
  20. //
  21. // A class representing a scalar float value contained in a vector register
  22. // This class does not support fastmath
  23. class floatInVec
  24. {
  25. private:
  26. float mData;
  27. public:
  28. // Default constructor; does no initialization
  29. //
  30. inline floatInVec( ) { };
  31. // Construct from a value converted from bool
  32. //
  33. inline floatInVec(boolInVec vec);
  34. // Explicit cast from float
  35. //
  36. explicit inline floatInVec(float scalar);
  37. // Explicit cast to float
  38. //
  39. inline float getAsFloat() const;
  40. #ifndef _VECTORMATH_NO_SCALAR_CAST
  41. // Implicit cast to float
  42. //
  43. inline operator float() const;
  44. #endif
  45. // Post increment (add 1.0f)
  46. //
  47. inline const floatInVec operator ++ (int);
  48. // Post decrement (subtract 1.0f)
  49. //
  50. inline const floatInVec operator -- (int);
  51. // Pre increment (add 1.0f)
  52. //
  53. inline floatInVec& operator ++ ();
  54. // Pre decrement (subtract 1.0f)
  55. //
  56. inline floatInVec& operator -- ();
  57. // Negation operator
  58. //
  59. inline const floatInVec operator - () const;
  60. // Assignment operator
  61. //
  62. inline floatInVec& operator = (floatInVec vec);
  63. // Multiplication assignment operator
  64. //
  65. inline floatInVec& operator *= (floatInVec vec);
  66. // Division assignment operator
  67. //
  68. inline floatInVec& operator /= (floatInVec vec);
  69. // Addition assignment operator
  70. //
  71. inline floatInVec& operator += (floatInVec vec);
  72. // Subtraction assignment operator
  73. //
  74. inline floatInVec& operator -= (floatInVec vec);
  75. };
  76. // Multiplication operator
  77. //
  78. inline const floatInVec operator * (floatInVec vec0, floatInVec vec1);
  79. // Division operator
  80. //
  81. inline const floatInVec operator / (floatInVec vec0, floatInVec vec1);
  82. // Addition operator
  83. //
  84. inline const floatInVec operator + (floatInVec vec0, floatInVec vec1);
  85. // Subtraction operator
  86. //
  87. inline const floatInVec operator - (floatInVec vec0, floatInVec vec1);
  88. // Less than operator
  89. //
  90. inline const boolInVec operator < (floatInVec vec0, floatInVec vec1);
  91. // Less than or equal operator
  92. //
  93. inline const boolInVec operator <= (floatInVec vec0, floatInVec vec1);
  94. // Greater than operator
  95. //
  96. inline const boolInVec operator > (floatInVec vec0, floatInVec vec1);
  97. // Greater than or equal operator
  98. //
  99. inline const boolInVec operator >= (floatInVec vec0, floatInVec vec1);
  100. // Equal operator
  101. //
  102. inline const boolInVec operator == (floatInVec vec0, floatInVec vec1);
  103. // Not equal operator
  104. //
  105. inline const boolInVec operator != (floatInVec vec0, floatInVec vec1);
  106. // Conditionally select between two values
  107. //
  108. inline const floatInVec select(floatInVec vec0, floatInVec vec1, boolInVec select_vec1);
  109. } // namespace Vectormath
  110. //--------------------------------------------------------------------------------------------------
  111. // floatInVec implementation
  112. //
  113. #include "boolInVec.h"
  114. namespace Vectormath {
  115. inline
  116. floatInVec::floatInVec(boolInVec vec)
  117. {
  118. mData = float(vec.getAsBool());
  119. }
  120. inline
  121. floatInVec::floatInVec(float scalar)
  122. {
  123. mData = scalar;
  124. }
  125. inline
  126. float
  127. floatInVec::getAsFloat() const
  128. {
  129. return mData;
  130. }
  131. #ifndef _VECTORMATH_NO_SCALAR_CAST
  132. inline
  133. floatInVec::operator float() const
  134. {
  135. return getAsFloat();
  136. }
  137. #endif
  138. inline
  139. const floatInVec
  140. floatInVec::operator ++ (int)
  141. {
  142. float olddata = mData;
  143. operator ++();
  144. return floatInVec(olddata);
  145. }
  146. inline
  147. const floatInVec
  148. floatInVec::operator -- (int)
  149. {
  150. float olddata = mData;
  151. operator --();
  152. return floatInVec(olddata);
  153. }
  154. inline
  155. floatInVec&
  156. floatInVec::operator ++ ()
  157. {
  158. *this += floatInVec(1.0f);
  159. return *this;
  160. }
  161. inline
  162. floatInVec&
  163. floatInVec::operator -- ()
  164. {
  165. *this -= floatInVec(1.0f);
  166. return *this;
  167. }
  168. inline
  169. const floatInVec
  170. floatInVec::operator - () const
  171. {
  172. return floatInVec(-mData);
  173. }
  174. inline
  175. floatInVec&
  176. floatInVec::operator = (floatInVec vec)
  177. {
  178. mData = vec.mData;
  179. return *this;
  180. }
  181. inline
  182. floatInVec&
  183. floatInVec::operator *= (floatInVec vec)
  184. {
  185. *this = *this * vec;
  186. return *this;
  187. }
  188. inline
  189. floatInVec&
  190. floatInVec::operator /= (floatInVec vec)
  191. {
  192. *this = *this / vec;
  193. return *this;
  194. }
  195. inline
  196. floatInVec&
  197. floatInVec::operator += (floatInVec vec)
  198. {
  199. *this = *this + vec;
  200. return *this;
  201. }
  202. inline
  203. floatInVec&
  204. floatInVec::operator -= (floatInVec vec)
  205. {
  206. *this = *this - vec;
  207. return *this;
  208. }
  209. inline
  210. const floatInVec
  211. operator * (floatInVec vec0, floatInVec vec1)
  212. {
  213. return floatInVec(vec0.getAsFloat() * vec1.getAsFloat());
  214. }
  215. inline
  216. const floatInVec
  217. operator / (floatInVec num, floatInVec den)
  218. {
  219. return floatInVec(num.getAsFloat() / den.getAsFloat());
  220. }
  221. inline
  222. const floatInVec
  223. operator + (floatInVec vec0, floatInVec vec1)
  224. {
  225. return floatInVec(vec0.getAsFloat() + vec1.getAsFloat());
  226. }
  227. inline
  228. const floatInVec
  229. operator - (floatInVec vec0, floatInVec vec1)
  230. {
  231. return floatInVec(vec0.getAsFloat() - vec1.getAsFloat());
  232. }
  233. inline
  234. const boolInVec
  235. operator < (floatInVec vec0, floatInVec vec1)
  236. {
  237. return boolInVec(vec0.getAsFloat() < vec1.getAsFloat());
  238. }
  239. inline
  240. const boolInVec
  241. operator <= (floatInVec vec0, floatInVec vec1)
  242. {
  243. return !(vec0 > vec1);
  244. }
  245. inline
  246. const boolInVec
  247. operator > (floatInVec vec0, floatInVec vec1)
  248. {
  249. return boolInVec(vec0.getAsFloat() > vec1.getAsFloat());
  250. }
  251. inline
  252. const boolInVec
  253. operator >= (floatInVec vec0, floatInVec vec1)
  254. {
  255. return !(vec0 < vec1);
  256. }
  257. inline
  258. const boolInVec
  259. operator == (floatInVec vec0, floatInVec vec1)
  260. {
  261. return boolInVec(vec0.getAsFloat() == vec1.getAsFloat());
  262. }
  263. inline
  264. const boolInVec
  265. operator != (floatInVec vec0, floatInVec vec1)
  266. {
  267. return !(vec0 == vec1);
  268. }
  269. inline
  270. const floatInVec
  271. select(floatInVec vec0, floatInVec vec1, boolInVec select_vec1)
  272. {
  273. return (select_vec1.getAsBool() == 0) ? vec0 : vec1;
  274. }
  275. } // namespace Vectormath
  276. #endif // floatInVec_h