No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

127 líneas
4.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. //
  11. // The Half class
  12. // --------------
  13. //
  14. #if !defined __LOL_HALF_H__
  15. #define __LOL_HALF_H__
  16. #include <cstdio>
  17. #include <stdint.h>
  18. namespace lol
  19. {
  20. class half
  21. {
  22. public:
  23. /* Constructors. Always inline so that the code can work in registers
  24. * instead of calling routines with the hidden "this" parameter. */
  25. inline half() { }
  26. inline half(int f) { *this = makefast((float)f); }
  27. inline half(float f) { *this = makefast(f); }
  28. inline half(double f) { *this = makefast((float)f); }
  29. inline int is_nan() const
  30. {
  31. return ((bits & 0x7c00u) == 0x7c00u) && (bits & 0x03ffu);
  32. }
  33. inline int is_finite() const
  34. {
  35. return (bits & 0x7c00u) != 0x7c00u;
  36. }
  37. inline int is_inf() const
  38. {
  39. return (uint16_t)(bits << 1) == (0x7c00u << 1);
  40. }
  41. inline int is_normal() const
  42. {
  43. return (is_finite() && (bits & 0x7c00u)) || ((bits & 0x7fffu) == 0);
  44. }
  45. /* Cast to other types -- always inline, see constructors */
  46. inline half &operator =(int f) { return *this = makefast((float)f); }
  47. inline half &operator =(float f) { return *this = makefast(f); }
  48. inline half &operator =(double f) { return *this = makefast((float)f); }
  49. inline operator int() const { return (int)tofloat(*this); }
  50. inline operator float() const { return tofloat(*this); }
  51. static float tofloat(half h);
  52. /* Array conversions */
  53. static size_t convert(half *dst, float const *src, size_t nelem);
  54. static size_t convert(float *dst, half const *src, size_t nelem);
  55. /* Operations */
  56. bool operator ==(half x) const { return (float)*this == (float)x; }
  57. bool operator !=(half x) const { return (float)*this != (float)x; }
  58. bool operator <(half x) const { return (float)*this < (float)x; }
  59. bool operator >(half x) const { return (float)*this > (float)x; }
  60. bool operator <=(half x) const { return (float)*this <= (float)x; }
  61. bool operator >=(half x) const { return (float)*this >= (float)x; }
  62. bool operator !() const { return !(bool)*this; }
  63. operator bool() const { return bits & 0x7fffu; }
  64. inline half operator -() const { return makebits(bits ^ 0x8000u); }
  65. inline half operator +() const { return *this; }
  66. inline half &operator +=(float f) { return (*this = (half)(*this + f)); }
  67. inline half &operator -=(float f) { return (*this = (half)(*this - f)); }
  68. inline half &operator *=(float f) { return (*this = (half)(*this * f)); }
  69. inline half &operator /=(float f) { return (*this = (half)(*this / f)); }
  70. inline half &operator +=(half h) { return (*this = (half)(*this + h)); }
  71. inline half &operator -=(half h) { return (*this = (half)(*this - h)); }
  72. inline half &operator *=(half h) { return (*this = (half)(*this * h)); }
  73. inline half &operator /=(half h) { return (*this = (half)(*this / h)); }
  74. inline float operator +(float f) const { return (float)*this + f; }
  75. inline float operator -(float f) const { return (float)*this - f; }
  76. inline float operator *(float f) const { return (float)*this * f; }
  77. inline float operator /(float f) const { return (float)*this / f; }
  78. inline float operator +(half h) const { return (float)*this + (float)h; }
  79. inline float operator -(half h) const { return (float)*this - (float)h; }
  80. inline float operator *(half h) const { return (float)*this * (float)h; }
  81. inline float operator /(half h) const { return (float)*this / (float)h; }
  82. /* Factories */
  83. static half makefast(float f);
  84. static half makeaccurate(float f);
  85. static inline half makebits(uint16_t x)
  86. {
  87. half ret;
  88. ret.bits = x;
  89. return ret;
  90. }
  91. /* Internal representation */
  92. uint16_t bits;
  93. };
  94. inline float &operator +=(float &f, half h) { return f += (float)h; }
  95. inline float &operator -=(float &f, half h) { return f -= (float)h; }
  96. inline float &operator *=(float &f, half h) { return f *= (float)h; }
  97. inline float &operator /=(float &f, half h) { return f /= (float)h; }
  98. inline float operator +(float f, half h) { return f + (float)h; }
  99. inline float operator -(float f, half h) { return f - (float)h; }
  100. inline float operator *(float f, half h) { return f * (float)h; }
  101. inline float operator /(float f, half h) { return f / (float)h; }
  102. } /* namespace lol */
  103. #endif // __LOL_HALF_H__