y_bit.inc 10 KB

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