y_bit.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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_GetAll - 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. </remarks>
  102. \**--------------------------------------------------------------------------**/
  103. #include "internal\y_version"
  104. #include "y_debug"
  105. #include "y_utils"
  106. #include "y_cell"
  107. // This is redefined below, don't worry. It's like this so the function
  108. // prototypes can use a familiar syntax.
  109. #define BitArray:%1<%2> Bit:%1[%2]
  110. #if cellbits == 32
  111. #define CELLSHIFT (5)
  112. #else
  113. #if cellbits == 64
  114. #define CELLSHIFT (6)
  115. #else
  116. #if cellbits == 16
  117. #define CELLSHIFT (4)
  118. #else
  119. #error Unkown cell size
  120. #endif
  121. #endif
  122. #endif
  123. /**--------------------------------------------------------------------------**\
  124. <summary>Bit_Bits</summary>
  125. <param name="size">Number of bits required.</param>
  126. <returns>
  127. Number of cells required for the bit array.
  128. </returns>
  129. <remarks>
  130. -
  131. </remarks>
  132. \**--------------------------------------------------------------------------**/
  133. // If this ever changes, update the size reference in y_users.
  134. #define Bit_Bits(%1) (((%1)+cellbits-1)/cellbits)
  135. /**--------------------------------------------------------------------------**\
  136. <summary>Bit_Slot</summary>
  137. <param name="value">Value to get the slot for.</param>
  138. <returns>
  139. The true array slot for this value.
  140. </returns>
  141. <remarks>
  142. -
  143. </remarks>
  144. \**--------------------------------------------------------------------------**/
  145. #define Bit_Slot(%1) ((_:%1)>>>CELLSHIFT)
  146. /**--------------------------------------------------------------------------**\
  147. <summary>Bit_Mask</summary>
  148. <param name="value">Value to get the mask for</param>
  149. <returns>
  150. The bit in the array slot to use.
  151. </returns>
  152. <remarks>
  153. -
  154. </remarks>
  155. \**--------------------------------------------------------------------------**/
  156. #define Bit_Mask(%1) (Bit:(1<<((_:%1)&cellbits-1)))
  157. /**--------------------------------------------------------------------------**\
  158. <summary>Bit_GetBit</summary>
  159. <param name="Bit:array[]">Array of bits.</param>
  160. <param name="slot">Bit slot.</param>
  161. <returns>
  162. State of the provided slot, 0 on fail.
  163. </returns>
  164. <remarks>
  165. Unsafe but faster for when you're sure you're within range.
  166. </remarks>
  167. \**--------------------------------------------------------------------------**/
  168. #define Bit_GetBit(%1,%2) (%1[(%2)>>>CELLSHIFT]&Bit:(1<<((%2)&cellbits-1)))
  169. /**--------------------------------------------------------------------------**\
  170. <summary>Bit_Get</summary>
  171. <param name="Bit:array[]">Array of bits.</param>
  172. <param name="slot">Bit slot.</param>
  173. <param name="size">Size of array.</param>
  174. <returns>
  175. State of the provided slot, 0 on fail.
  176. </returns>
  177. <remarks>
  178. -
  179. native Bit_Get(BitArray:array<>, slot);
  180. </remarks>
  181. \**--------------------------------------------------------------------------**/
  182. #define Bit_Get(%1,%2) bool:Bit_GetBit(Bit:%1,_:%2)
  183. /**--------------------------------------------------------------------------**\
  184. <summary>Bit_Let</summary>
  185. <param name="Bit:array[]">Array of bits.</param>
  186. <param name="slot">Bit slot.</param>
  187. <returns>
  188. -
  189. </returns>
  190. <remarks>
  191. Sets the slot to 1.
  192. </remarks>
  193. \**--------------------------------------------------------------------------**/
  194. #define Bit_Let(%1,%2) %1[(%2)>>>CELLSHIFT]|=Bit:(1<<((%2)&cellbits-1))
  195. /**--------------------------------------------------------------------------**\
  196. <summary>Bit_Vet</summary>
  197. <param name="Bit:array[]">Array of bits.</param>
  198. <param name="slot">Bit slot.</param>
  199. <returns>
  200. -
  201. </returns>
  202. <remarks>
  203. Sets the slot to 0.
  204. </remarks>
  205. \**--------------------------------------------------------------------------**/
  206. #define Bit_Vet(%1,%2) %1[(%2)>>>CELLSHIFT]&=Bit:~(1<<((%2)&cellbits-1))
  207. /**--------------------------------------------------------------------------**\
  208. <summary>Bit_Set</summary>
  209. <param name="Bit:array[]">Array of bits.</param>
  210. <param name="slot">Bit slot.</param>
  211. <param name="bool:set">State to set the slot to.</param>
  212. <param name="size">Size of array.</param>
  213. <returns>
  214. -
  215. </returns>
  216. <remarks>
  217. -
  218. native Bit_Set(BitArray:array<>, slot, bool:set, size = sizeof (array));
  219. </remarks>
  220. \**--------------------------------------------------------------------------**/
  221. stock Bit_Set(BitArray:array<>, slot, bool:set)//, size = sizeof (array))
  222. {
  223. //if (slot >>> CELLSHIFT >= size) return;
  224. if (set) Bit_Let(array, slot);
  225. else Bit_Vet(array, slot);
  226. }
  227. //#define Bit_Set(%0,%1,%2) ((%2)?Bit_Let(%0,(%1)):Bit_Vet(%0,(%1)))
  228. /**--------------------------------------------------------------------------**\
  229. <summary>Bit_SetAll</summary>
  230. <param name="Bit:array[]">Array to set all values of.</param>
  231. <param name="bool:set">Wether to set them all 0 or 1.</param>
  232. <param name="size">Size of array.</param>
  233. <returns>
  234. -
  235. </returns>
  236. <remarks>
  237. -
  238. native Bit_SetAll(BitArray:array<>, bool:set, size = sizeof (array));
  239. </remarks>
  240. \**--------------------------------------------------------------------------**/
  241. stock Bit_SetAll(BitArray:array<>, bool:set, size = sizeof (array))
  242. {
  243. new
  244. Bit:val = (set) ? (Bit:0xFFFFFFFF) : (Bit:0);
  245. for (new i = 0; i != size; ++i) array[i] = val;
  246. }
  247. /**--------------------------------------------------------------------------**\
  248. <summary>Bit_GetCount</summary>
  249. <param name="Bit:array[]">Array to count.</param>
  250. <param name="size">Size of array.</param>
  251. <returns>
  252. Number of 1s in the array.
  253. </returns>
  254. <remarks>
  255. Code from:
  256. http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  257. native Bit_Count(BitArray:array<>, size = sizeof (array));
  258. </remarks>
  259. \**--------------------------------------------------------------------------**/
  260. #define Bit_Count Bit_GetCount
  261. stock Bit_GetCount(BitArray:array<>, size = sizeof (array))
  262. {
  263. new
  264. count,
  265. v;
  266. for (new i = 0; i != size; ++i)
  267. {
  268. v = _:array[i];
  269. v = v - ((v >>> 1) & 0x55555555);
  270. v = (v & 0x33333333) + ((v >>> 2) & 0x33333333);
  271. count += ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24;
  272. }
  273. return count;
  274. }
  275. stock Bit_Display(BitArray:array<>, size = sizeof (array))
  276. {
  277. new
  278. ret[YSI_MAX_STRING],
  279. val;
  280. while (size--)
  281. {
  282. val = Cell_ReverseBits(array[size]);
  283. format(ret, sizeof (ret), "%016b%016b%s", val >>> 16, val & 0xFFFF, ret);
  284. }
  285. //P:7("Bit_Display called: %s, %i", ret, size);
  286. return ret;
  287. }
  288. #define bitsof(%0) (sizeof(%0)*cellbits)
  289. stock Bits@YSII_Ag(BitArray:data<>, start, size = sizeof (data))
  290. {
  291. P:3("YSI_gABITSFunc called: %s, %i", Bit_Display(data, size), start);
  292. static const
  293. scDeBruijn[] =
  294. {
  295. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  296. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  297. };
  298. ++start;
  299. new
  300. cur,
  301. i = Bit_Slot(start);
  302. if (i == size)
  303. {
  304. return -1;
  305. }
  306. // Blank out the lowest bits to get the lowest bit not yet used.
  307. if ((cur = (_:data[i] & ~((1 << (start & (cellbits - 1))) - 1))))
  308. {
  309. //printf("%d %d %d %d", cur, data[i], start, ~((1 << (start & (cellbits - 1))) - 1));
  310. // Bits left in the current cell.
  311. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  312. }
  313. ++i;
  314. while (i != size)
  315. {
  316. if ((cur = _:data[i]))
  317. {
  318. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  319. }
  320. ++i;
  321. }
  322. return -1;
  323. }
  324. stock Blanks@YSII_Ag(BitArray:data<>, start, size = sizeof (data))
  325. {
  326. P:3("YSI_gABlanksFunc called: %s, %i", Bit_Display(data), start);
  327. static const
  328. scDeBruijn[] =
  329. {
  330. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  331. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  332. };
  333. //++start;
  334. new
  335. cur,
  336. i = Bit_Slot(start);
  337. if (i == size)
  338. {
  339. return -1;
  340. }
  341. if ((cur = ~(_:(data[i] & ~((1 << (start & (cellbits - 1))) - 1)))))
  342. {
  343. // Bits left in the current cell.
  344. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  345. }
  346. ++i;
  347. while (i != size)
  348. {
  349. if ((cur = ~_:data[i]))
  350. {
  351. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  352. }
  353. ++i;
  354. }
  355. return -1;
  356. }
  357. #define bits<%1> Bit_Bits(%1)
  358. #undef BitArray
  359. #define BitArray:%1<%2> Bit:%1[bits<%2>]