impl.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /**--------------------------------------------------------------------------**\
  2. ===========================
  3. Y Sever Includes - Bit Core
  4. ===========================
  5. Description:
  6. Provides functions for bit manipulation and bit arrays greater than 32bits.
  7. The arrays are usually bigger than required due to cell boundaries but this
  8. shouldn't cause a major problem (bit tests on the 101st bit of a 100 bit
  9. array won't return 0 for out of bounds, but the 129th will).
  10. Note that y_commands has a few optimisations which bypass the code in here
  11. so any modifications to bit array layouts will need to be reflected there.
  12. Legal:
  13. Version: MPL 1.1
  14. The contents of this file are subject to the Mozilla Public License Version
  15. 1.1 (the "License"); you may not use this file except in compliance with
  16. the License. You may obtain a copy of the License at
  17. http://www.mozilla.org/MPL/
  18. Software distributed under the License is distributed on an "AS IS" basis,
  19. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  20. for the specific language governing rights and limitations under the
  21. License.
  22. The Original Code is the YSI bit include.
  23. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  24. Portions created by the Initial Developer are Copyright (C) 2011
  25. the Initial Developer. All Rights Reserved.
  26. Contributors:
  27. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  28. Thanks:
  29. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  30. ZeeX - Very productive conversations.
  31. koolk - IsPlayerinAreaEx code.
  32. TheAlpha - Danish translation.
  33. breadfish - German translation.
  34. Fireburn - Dutch translation.
  35. yom - French translation.
  36. 50p - Polish translation.
  37. Zamaroht - Spanish translation.
  38. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  39. for me to strive to better.
  40. Pixels^ - Running XScripters where the idea was born.
  41. Matite - Pestering me to release it and using it.
  42. Very special thanks to:
  43. Thiadmer - PAWN, whose limits continue to amaze me!
  44. Kye/Kalcor - SA:MP.
  45. SA:MP Team past, present and future - SA:MP.
  46. Version:
  47. 0.2
  48. Changelog:
  49. 21/10/12:
  50. Changed "Bit_Display" to print in the correct order.
  51. 22/02/12:
  52. Added the "BITS" iterator.
  53. 01/12/08:
  54. Rewrote most of the code to use shifts and ands not divs and mods.
  55. 24/06/07:
  56. Added Bit_GetBit
  57. 18/06/07:
  58. Added Bit_GetCount
  59. 30/04/07:
  60. Added Bit_SetAll
  61. 15/04/07:
  62. First version.
  63. Functions:
  64. Public:
  65. -
  66. Core:
  67. -
  68. Stock:
  69. Bit_Set - Sets a slot to the given value.
  70. Bit_Get - Gets a slot state.
  71. Bit_SetAll - Sets all the slots in an array to the same thing.
  72. Bit_GetCount - Gets the number of 1s in a bit array.
  73. Static:
  74. -
  75. Inline:
  76. Bit_Bits - Gets the number of cells required for a bit array.
  77. Bit_Let - Sets a slot to 1.
  78. Bit_Vet - Sets a slot to 0.
  79. Bit_GetBits - Gets the bit at a slot unsafely.
  80. API:
  81. -
  82. Callbacks:
  83. -
  84. Definitions:
  85. CELLSHIFT - Number of bits that can hold "cellbits"
  86. Enums:
  87. -
  88. Macros:
  89. -
  90. Tags:
  91. Bit - Bit array type.
  92. Variables:
  93. Global:
  94. -
  95. Static:
  96. -
  97. Commands:
  98. -
  99. Compile options:
  100. -
  101. \**--------------------------------------------------------------------------**/
  102. // This is redefined below, don't worry. It's like this so the function
  103. // prototypes can use a familiar syntax.
  104. #define BitArray:%1<%2> Bit:%1[%2]
  105. #if cellbits == 32
  106. #define CELLSHIFT (5)
  107. #else
  108. #if cellbits == 64
  109. #define CELLSHIFT (6)
  110. #else
  111. #if cellbits == 16
  112. #define CELLSHIFT (4)
  113. #else
  114. #error Unkown cell size
  115. #endif
  116. #endif
  117. #endif
  118. /**--------------------------------------------------------------------------**\
  119. <summary>Bit_Bits</summary>
  120. <param name="size">Number of bits required.</param>
  121. <returns>
  122. Number of cells required for the bit array.
  123. </returns>
  124. <remarks>
  125. -
  126. </remarks>
  127. \**--------------------------------------------------------------------------**/
  128. // If this ever changes, update the size reference in y_users.
  129. #define Bit_Bits(%1) (((%1)+cellbits-1)/cellbits)
  130. /**--------------------------------------------------------------------------**\
  131. <summary>Bit_Slot</summary>
  132. <param name="value">Value to get the slot for.</param>
  133. <returns>
  134. The true array slot for this value.
  135. </returns>
  136. <remarks>
  137. -
  138. </remarks>
  139. \**--------------------------------------------------------------------------**/
  140. #define Bit_Slot(%1) ((_:%1)>>>CELLSHIFT)
  141. /**--------------------------------------------------------------------------**\
  142. <summary>Bit_Mask</summary>
  143. <param name="value">Value to get the mask for</param>
  144. <returns>
  145. The bit in the array slot to use.
  146. </returns>
  147. <remarks>
  148. -
  149. </remarks>
  150. \**--------------------------------------------------------------------------**/
  151. #define Bit_Mask(%1) (Bit:(1<<((_:%1)&cellbits-1)))
  152. /**--------------------------------------------------------------------------**\
  153. <summary>Bit_GetBit</summary>
  154. <param name="Bit:array[]">Array of bits.</param>
  155. <param name="slot">Bit slot.</param>
  156. <returns>
  157. State of the provided slot, 0 on fail.
  158. </returns>
  159. <remarks>
  160. Unsafe but faster for when you're sure you're within range.
  161. </remarks>
  162. \**--------------------------------------------------------------------------**/
  163. #define Bit_GetBit(%1,%2) (%1[(%2)>>>CELLSHIFT]&Bit:(1<<((%2)&cellbits-1)))
  164. /**--------------------------------------------------------------------------**\
  165. <summary>Bit_Get</summary>
  166. <param name="Bit:array[]">Array of bits.</param>
  167. <param name="slot">Bit slot.</param>
  168. <param name="size">Size of array.</param>
  169. <returns>
  170. State of the provided slot, 0 on fail.
  171. </returns>
  172. <remarks>
  173. -
  174. native Bit_Get(BitArray:array<>, slot);
  175. </remarks>
  176. \**--------------------------------------------------------------------------**/
  177. #define Bit_Get(%1,%2) bool:Bit_GetBit(Bit:%1,_:%2)
  178. /**--------------------------------------------------------------------------**\
  179. <summary>Bit_Let</summary>
  180. <param name="Bit:array[]">Array of bits.</param>
  181. <param name="slot">Bit slot.</param>
  182. <returns>
  183. -
  184. </returns>
  185. <remarks>
  186. Sets the slot to 1.
  187. </remarks>
  188. \**--------------------------------------------------------------------------**/
  189. #define Bit_Let(%1,%2) %1[(%2)>>>CELLSHIFT]|=Bit:(1<<((%2)&cellbits-1))
  190. /**--------------------------------------------------------------------------**\
  191. <summary>Bit_Vet</summary>
  192. <param name="Bit:array[]">Array of bits.</param>
  193. <param name="slot">Bit slot.</param>
  194. <returns>
  195. -
  196. </returns>
  197. <remarks>
  198. Sets the slot to 0.
  199. </remarks>
  200. \**--------------------------------------------------------------------------**/
  201. #define Bit_Vet(%1,%2) %1[(%2)>>>CELLSHIFT]&=Bit:~(1<<((%2)&cellbits-1))
  202. /**--------------------------------------------------------------------------**\
  203. <summary>Bit_Set</summary>
  204. <param name="Bit:array[]">Array of bits.</param>
  205. <param name="slot">Bit slot.</param>
  206. <param name="bool:set">State to set the slot to.</param>
  207. <param name="size">Size of array.</param>
  208. <returns>
  209. -
  210. </returns>
  211. <remarks>
  212. -
  213. native Bit_Set(BitArray:array<>, slot, bool:set, size = sizeof (array));
  214. </remarks>
  215. \**--------------------------------------------------------------------------**/
  216. stock Bit_Set(BitArray:array<>, slot, bool:set)//, size = sizeof (array))
  217. {
  218. //if (slot >>> CELLSHIFT >= size) return;
  219. if (set) Bit_Let(array, slot);
  220. else Bit_Vet(array, slot);
  221. }
  222. /**--------------------------------------------------------------------------**\
  223. <summary>Bit_FastSet</summary>
  224. <param name="Bit:array[]">Array of bits.</param>
  225. <param name="slot">Bit slot.</param>
  226. <param name="bool:set">State to set the slot to.</param>
  227. <param name="size">Size of array.</param>
  228. <returns>
  229. -
  230. </returns>
  231. <remarks>
  232. Exactly the same as "Bit_Set", but as a macro not a function.
  233. native Bit_FastSet(BitArray:array<>, slot, bool:set, size = sizeof (array));
  234. </remarks>
  235. \**--------------------------------------------------------------------------**/
  236. #define Bit_FastSet(%0,%1,%2) ((%2)?(Bit_Let(%0,(%1))):(Bit_Vet(%0,(%1))))
  237. /**--------------------------------------------------------------------------**\
  238. <summary>Bit_SetAll</summary>
  239. <param name="Bit:array[]">Array to set all values of.</param>
  240. <param name="bool:set">Wether to set them all 0 or 1.</param>
  241. <param name="size">Size of array.</param>
  242. <returns>
  243. -
  244. </returns>
  245. <remarks>
  246. -
  247. native Bit_SetAll(BitArray:array<>, bool:set, size = sizeof (array));
  248. </remarks>
  249. \**--------------------------------------------------------------------------**/
  250. stock Bit_SetAll(BitArray:array<>, bool:set, size = sizeof (array))
  251. {
  252. memset(_:array, set ? 0xFFFFFFFF : 0, size);
  253. }
  254. /**--------------------------------------------------------------------------**\
  255. <summary>Bit_GetCount</summary>
  256. <param name="Bit:array[]">Array to count.</param>
  257. <param name="size">Size of array.</param>
  258. <returns>
  259. Number of 1s in the array.
  260. </returns>
  261. <remarks>
  262. Code from:
  263. http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  264. native Bit_Count(BitArray:array<>, size = sizeof (array));
  265. </remarks>
  266. \**--------------------------------------------------------------------------**/
  267. #define Bit_Count Bit_GetCount
  268. stock Bit_GetCount(BitArray:array<>, size = sizeof (array))
  269. {
  270. new
  271. count;
  272. for (new i = 0; i != size; ++i)
  273. {
  274. count += Cell_CountBits(array[i]);
  275. }
  276. return count;
  277. }
  278. stock Bit_Display(BitArray:array<>, size = sizeof (array))
  279. {
  280. new
  281. ret[YSI_MAX_STRING],
  282. val;
  283. while (size--)
  284. {
  285. val = Cell_ReverseBits(array[size]);
  286. format(ret, sizeof (ret), "%016b%016b%s", val >>> 16, val & 0xFFFF, ret);
  287. }
  288. //P:7("Bit_Display called: %s, %i", ret, size);
  289. return ret;
  290. }
  291. #define bitsof(%0) (sizeof(%0)*cellbits)
  292. stock Iter_Func@Bits(start, BitArray:data<>, size = sizeof (data))
  293. {
  294. P:3("Iter_Func@Bits called: %s, %i", Bit_Display(data, size), start);
  295. new
  296. cur,
  297. i = Bit_Slot(++start);
  298. if (i == size)
  299. {
  300. return -1;
  301. }
  302. // Blank out the lowest bits to get the lowest bit not yet used.
  303. if ((cur = _:(data[i] & ~(Bit_Mask(start) - Bit:1))))
  304. {
  305. P:7("Iter_Func@Bits: %d %d %d %d", cur, _:data[i], start, _:~(Bit_Mask(start) - Bit:1));
  306. // Bits left in the current cell.
  307. return Cell_GetLowestBit(cur) + (i << CELLSHIFT);
  308. }
  309. while (++i != size)
  310. {
  311. if ((cur = _:data[i]))
  312. {
  313. return Cell_GetLowestBit(cur) + (i << CELLSHIFT);
  314. }
  315. }
  316. return -1;
  317. }
  318. #define iterstart@Bits -1
  319. stock Iter_Func@Blanks(start, BitArray:data<>, size = sizeof (data))
  320. {
  321. P:3("Iter_Func@Blanks called: %s, %i", Bit_Display(data), start);
  322. new
  323. cur,
  324. i = Bit_Slot(++start);
  325. if (i == size)
  326. {
  327. return -1;
  328. }
  329. if ((cur = _:(~data[i] & ~(Bit_Mask(start) - Bit:1))))
  330. {
  331. // Bits left in the current cell.
  332. return Cell_GetLowestBit(cur) + (i << CELLSHIFT);
  333. }
  334. while (++i != size)
  335. {
  336. if ((cur = ~_:data[i]))
  337. {
  338. return Cell_GetLowestBit(cur) + (i << CELLSHIFT);
  339. }
  340. }
  341. return -1;
  342. }
  343. #define iterstart@Blanks -1
  344. #define bits<%1> Bit_Bits(%1)
  345. #undef BitArray
  346. #define BitArray:%1<%2> Bit:%1[bits<%2>]