int24.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. class UInt24
  2. {
  3. public:
  4. UInt24()
  5. : m_value(0u)
  6. {
  7. }
  8. UInt24(const UInt24 &other)
  9. : m_value(other.m_value)
  10. {
  11. }
  12. UInt24(const auto &other)
  13. : m_value(mask(other))
  14. {
  15. }
  16. UInt24 & operator=(const UInt24 &other)
  17. {
  18. m_value = other.m_value;
  19. return *this;
  20. }
  21. UInt24 & operator=(const auto &other)
  22. {
  23. m_value = mask(other);
  24. return *this;
  25. }
  26. operator unsigned int() const
  27. {
  28. return m_value;
  29. }
  30. UInt24 operator++() // prefix-inc
  31. {
  32. m_value = mask(m_value + 1u);
  33. return *this;
  34. }
  35. UInt24 operator++(int) // postfix-inc
  36. {
  37. UInt24 old(*this);
  38. ++(*this);
  39. return old;
  40. }
  41. UInt24 operator--() // prefix-dec
  42. {
  43. m_value = mask(m_value - 1u);
  44. return *this;
  45. }
  46. UInt24 operator--(int) // postfix-dec
  47. {
  48. UInt24 old(*this);
  49. --(*this);
  50. return old;
  51. }
  52. UInt24 & operator+=(const UInt24 &other)
  53. {
  54. m_value = mask(m_value + other.m_value);
  55. return *this;
  56. }
  57. UInt24 & operator+=(const auto &other)
  58. {
  59. m_value = mask(m_value + mask(other));
  60. return *this;
  61. }
  62. UInt24 & operator-=(const UInt24 &other)
  63. {
  64. m_value = mask(m_value - other.m_value);
  65. return *this;
  66. }
  67. UInt24 & operator-=(const auto &other)
  68. {
  69. m_value = mask(m_value - mask(other));
  70. return *this;
  71. }
  72. UInt24 operator&(const UInt24 &other) const
  73. {
  74. return UInt24(m_value & other.m_value);
  75. }
  76. UInt24 operator&(const auto &other) const
  77. {
  78. return UInt24(m_value & mask(other));
  79. }
  80. UInt24 operator|(const UInt24 &other) const
  81. {
  82. return UInt24(m_value | other.m_value);
  83. }
  84. UInt24 operator|(const auto &other) const
  85. {
  86. return UInt24(m_value | mask(other));
  87. }
  88. UInt24 operator^(const UInt24 &other) const
  89. {
  90. return UInt24(m_value ^ other.m_value);
  91. }
  92. UInt24 operator^(const auto &other) const
  93. {
  94. return UInt24(m_value ^ mask(other));
  95. }
  96. const UInt24 operator+(const UInt24 &other) const
  97. {
  98. return UInt24(m_value + other.m_value);
  99. }
  100. const UInt24 operator+(const auto &other) const
  101. {
  102. return UInt24(m_value + mask(other));
  103. }
  104. const UInt24 operator-(const UInt24 &other) const
  105. {
  106. return UInt24(m_value - other.m_value);
  107. }
  108. const UInt24 operator-(const auto &other) const
  109. {
  110. return UInt24(m_value - mask(other));
  111. }
  112. const UInt24 operator*(const UInt24 &other) const
  113. {
  114. return UInt24(m_value * other.m_value);
  115. }
  116. const UInt24 operator*(const auto &other) const
  117. {
  118. return UInt24(m_value * mask(other));
  119. }
  120. const UInt24 operator/(const UInt24 &other) const
  121. {
  122. return UInt24(m_value / other.m_value);
  123. }
  124. const UInt24 operator/(const auto &other) const
  125. {
  126. return UInt24(m_value / mask(other));
  127. }
  128. bool operator<(const UInt24 &other) const
  129. {
  130. return m_value < other.m_value;
  131. }
  132. bool operator<(const auto &other) const
  133. {
  134. return m_value < mask(other);
  135. }
  136. bool operator==(const UInt24 &other) const
  137. {
  138. return m_value == other.m_value;
  139. }
  140. bool operator==(const auto &other) const
  141. {
  142. return m_value == mask(other);
  143. }
  144. bool operator!=(const UInt24 &other) const
  145. {
  146. return m_value != other.m_value;
  147. }
  148. bool operator!=(const auto &other) const
  149. {
  150. return m_value != mask(other);
  151. }
  152. bool operator>(const UInt24 &other) const
  153. {
  154. return m_value > other.m_value;
  155. }
  156. bool operator>(const auto &other) const
  157. {
  158. return m_value > mask(other);
  159. }
  160. protected:
  161. unsigned int mask(const auto &value) const
  162. {
  163. return static_cast<unsigned int>(value) & 0x00FFFFFFu;
  164. }
  165. protected:
  166. unsigned int m_value;
  167. };
  168. class Int24
  169. {
  170. public:
  171. Int24()
  172. : m_value(0)
  173. {
  174. }
  175. Int24(const Int24 &other)
  176. : m_value(other.m_value)
  177. {
  178. }
  179. Int24(const auto &other)
  180. : m_value(extend(other))
  181. {
  182. }
  183. Int24 & operator=(const Int24 &other)
  184. {
  185. m_value = other.m_value;
  186. return *this;
  187. }
  188. Int24 & operator=(const auto &other)
  189. {
  190. m_value = extend(other);
  191. return *this;
  192. }
  193. operator int() const
  194. {
  195. return m_value;
  196. }
  197. Int24 operator++() // prefix-inc
  198. {
  199. m_value = extend(m_value + 1);
  200. return *this;
  201. }
  202. Int24 operator++(int) // postfix-inc
  203. {
  204. Int24 old(*this);
  205. ++(*this);
  206. return old;
  207. }
  208. Int24 operator--() // prefix-dec
  209. {
  210. m_value = extend(m_value - 1);
  211. return *this;
  212. }
  213. Int24 operator--(int) // postfix-dec
  214. {
  215. Int24 old(*this);
  216. --(*this);
  217. return old;
  218. }
  219. Int24 & operator+=(const Int24 &other)
  220. {
  221. m_value = extend(m_value + other.m_value);
  222. return *this;
  223. }
  224. Int24 & operator+=(const auto &other)
  225. {
  226. m_value = extend(m_value + extend(other));
  227. return *this;
  228. }
  229. Int24 & operator-=(const Int24 &other)
  230. {
  231. m_value = extend(m_value - other.m_value);
  232. return *this;
  233. }
  234. Int24 & operator-=(const auto &other)
  235. {
  236. m_value = extend(m_value - extend(other));
  237. return *this;
  238. }
  239. Int24 operator&(const Int24 &other) const
  240. {
  241. return Int24(m_value & other.m_value);
  242. }
  243. Int24 operator&(const auto &other) const
  244. {
  245. return Int24(m_value & extend(other));
  246. }
  247. Int24 operator|(const Int24 &other) const
  248. {
  249. return Int24(m_value | other.m_value);
  250. }
  251. Int24 operator|(const auto &other) const
  252. {
  253. return Int24(m_value | extend(other));
  254. }
  255. Int24 operator^(const Int24 &other) const
  256. {
  257. return Int24(m_value ^ other.m_value);
  258. }
  259. Int24 operator^(const auto &other) const
  260. {
  261. return Int24(m_value ^ extend(other));
  262. }
  263. const Int24 operator+(const Int24 &other) const
  264. {
  265. return Int24(m_value + other.m_value);
  266. }
  267. const Int24 operator+(const auto &other) const
  268. {
  269. return Int24(m_value + extend(other));
  270. }
  271. const Int24 operator-(const Int24 &other) const
  272. {
  273. return Int24(m_value - other.m_value);
  274. }
  275. const Int24 operator-(const auto &other) const
  276. {
  277. return Int24(m_value - extend(other));
  278. }
  279. const Int24 operator*(const Int24 &other) const
  280. {
  281. return Int24(m_value * other.m_value);
  282. }
  283. const Int24 operator*(const auto &other) const
  284. {
  285. return Int24(m_value * extend(other));
  286. }
  287. const Int24 operator/(const Int24 &other) const
  288. {
  289. return Int24(m_value / other.m_value);
  290. }
  291. const Int24 operator/(const auto &other) const
  292. {
  293. return Int24(m_value / extend(other));
  294. }
  295. bool operator<(const Int24 &other) const
  296. {
  297. return m_value < other.m_value;
  298. }
  299. bool operator<(const auto &other) const
  300. {
  301. return m_value < extend(other);
  302. }
  303. bool operator==(const Int24 &other) const
  304. {
  305. return m_value == other.m_value;
  306. }
  307. bool operator==(const auto &other) const
  308. {
  309. return m_value == extend(other);
  310. }
  311. bool operator!=(const Int24 &other) const
  312. {
  313. return m_value != other.m_value;
  314. }
  315. bool operator!=(const auto &other) const
  316. {
  317. return m_value != extend(other);
  318. }
  319. bool operator>(const Int24 &other) const
  320. {
  321. return m_value > other.m_value;
  322. }
  323. bool operator>(const auto &other) const
  324. {
  325. return m_value > extend(other);
  326. }
  327. protected:
  328. int extend(const auto &value) const
  329. {
  330. unsigned int v = static_cast<unsigned int>(value) & 0x00FFFFFFu;
  331. if (v & (1u << 23))
  332. v |= 0xFF000000u;
  333. return static_cast<int>(v);
  334. }
  335. protected:
  336. int m_value;
  337. };