y_bit.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. 22/02/12:
  50. Added the "BITS" iterator.
  51. 01/12/08:
  52. Rewrote most of the code to use shifts and ands not divs and mods.
  53. 24/06/07:
  54. Added Bit_GetBit
  55. 18/06/07:
  56. Added Bit_GetCount
  57. 30/04/07:
  58. Added Bit_SetAll
  59. 15/04/07:
  60. First version.
  61. Functions:
  62. Public:
  63. -
  64. Core:
  65. -
  66. Stock:
  67. Bit_Set - Sets a slot to the given value.
  68. Bit_Get - Gets a slot state.
  69. Bit_SetAll - Sets all the slots in an array to the same thing.
  70. Bit_GetAll - Gets the number of 1s in a bit array.
  71. Static:
  72. -
  73. Inline:
  74. Bit_Bits - Gets the number of cells required for a bit array.
  75. Bit_Let - Sets a slot to 1.
  76. Bit_Vet - Sets a slot to 0.
  77. Bit_GetBits - Gets the bit at a slot unsafely.
  78. API:
  79. -
  80. Callbacks:
  81. -
  82. Definitions:
  83. CELLSHIFT - Number of bits that can hold "cellbits"
  84. Enums:
  85. -
  86. Macros:
  87. -
  88. Tags:
  89. Bit - Bit array type.
  90. Variables:
  91. Global:
  92. -
  93. Static:
  94. -
  95. Commands:
  96. -
  97. Compile options:
  98. -
  99. \*----------------------------------------------------------------------------*/
  100. #include "internal\y_version"
  101. #include "y_debug"
  102. #include "y_utils"
  103. // This is redefined below, don't worry. It's like this so the function
  104. // prototypes can use a familiar syntax.
  105. #define BitArray:%1<%2> Bit:%1[%2]
  106. #if cellbits == 32
  107. #define CELLSHIFT (5)
  108. #else
  109. #if cellbits == 64
  110. #define CELLSHIFT (6)
  111. #else
  112. #if cellbits == 16
  113. #define CELLSHIFT (4)
  114. #else
  115. #error Unkown cell size
  116. #endif
  117. #endif
  118. #endif
  119. /*----------------------------------------------------------------------------*\
  120. Function:
  121. Bit_Bits
  122. Params:
  123. size - Number of bits required.
  124. Return:
  125. Number of cells required for the bit array.
  126. Notes:
  127. -
  128. \*----------------------------------------------------------------------------*/
  129. // If this ever changes, update the size reference in y_users.
  130. #define Bit_Bits(%1) (((%1)+cellbits-1)/cellbits)
  131. /*----------------------------------------------------------------------------*\
  132. Function:
  133. Bit_Slot
  134. Params:
  135. value - Value to get the slot for.
  136. Return:
  137. The true array slot for this value.
  138. Notes:
  139. -
  140. \*----------------------------------------------------------------------------*/
  141. #define Bit_Slot(%1) ((_:%1)>>>CELLSHIFT)
  142. /*----------------------------------------------------------------------------*\
  143. Function:
  144. Bit_Mask
  145. Params:
  146. value - Value to get the mask for
  147. Return:
  148. The bit in the array slot to use.
  149. Notes:
  150. -
  151. \*----------------------------------------------------------------------------*/
  152. #define Bit_Mask(%1) (Bit:(1<<((_:%1)&cellbits-1)))
  153. /*----------------------------------------------------------------------------*\
  154. Function:
  155. Bit_GetBit
  156. Params:
  157. Bit:array[] - Array of bits.
  158. slot - Bit slot.
  159. Return:
  160. State of the provided slot, 0 on fail.
  161. Notes:
  162. Unsafe but faster for when you're sure you're within range.
  163. \*----------------------------------------------------------------------------*/
  164. #define Bit_GetBit(%1,%2) (%1[(%2)>>>CELLSHIFT]&Bit:(1<<((%2)&cellbits-1)))
  165. /*----------------------------------------------------------------------------*\
  166. Function:
  167. Bit_Get
  168. Params:
  169. Bit:array[] - Array of bits.
  170. slot - Bit slot.
  171. size - Size of array.
  172. Return:
  173. State of the provided slot, 0 on fail.
  174. Notes:
  175. -
  176. native Bit_Get(BitArray:array<>, slot);
  177. \*----------------------------------------------------------------------------*/
  178. #define Bit_Get(%1,%2) bool:Bit_GetBit(Bit:%1,_:%2)
  179. /*----------------------------------------------------------------------------*\
  180. Function:
  181. Bit_Let
  182. Params:
  183. Bit:array[] - Array of bits.
  184. slot - Bit slot.
  185. Return:
  186. -
  187. Notes:
  188. Sets the slot to 1.
  189. \*----------------------------------------------------------------------------*/
  190. #define Bit_Let(%1,%2) %1[(%2)>>>CELLSHIFT]|=Bit:(1<<((%2)&cellbits-1))
  191. /*----------------------------------------------------------------------------*\
  192. Function:
  193. Bit_Vet
  194. Params:
  195. Bit:array[] - Array of bits.
  196. slot - Bit slot.
  197. Return:
  198. -
  199. Notes:
  200. Sets the slot to 0.
  201. \*----------------------------------------------------------------------------*/
  202. #define Bit_Vet(%1,%2) %1[(%2)>>>CELLSHIFT]&=Bit:~(1<<((%2)&cellbits-1))
  203. /*----------------------------------------------------------------------------*\
  204. Function:
  205. Bit_Set
  206. Params:
  207. Bit:array[] - Array of bits.
  208. slot - Bit slot.
  209. bool:set - State to set the slot to.
  210. size - Size of array.
  211. Return:
  212. -
  213. Notes:
  214. -
  215. native Bit_Set(BitArray:array<>, slot, bool:set, size = sizeof (array));
  216. \*----------------------------------------------------------------------------*/
  217. stock Bit_Set(BitArray:array<>, slot, bool:set)//, size = sizeof (array))
  218. {
  219. //if (slot >>> CELLSHIFT >= size) return;
  220. if (set) Bit_Let(array, slot);
  221. else Bit_Vet(array, slot);
  222. }
  223. //#define Bit_Set(%0,%1,%2) ((%2)?Bit_Let(%0,(%1)):Bit_Vet(%0,(%1)))
  224. /*----------------------------------------------------------------------------*\
  225. Function:
  226. Bit_SetAll
  227. Params:
  228. Bit:array[] - Array to set all values of.
  229. bool:set - Wether to set them all 0 or 1.
  230. size - Size of array.
  231. Return:
  232. -
  233. Notes:
  234. -
  235. native Bit_SetAll(BitArray:array<>, bool:set, size = sizeof (array));
  236. \*----------------------------------------------------------------------------*/
  237. stock Bit_SetAll(BitArray:array<>, bool:set, size = sizeof (array))
  238. {
  239. new
  240. Bit:val = (set) ? (Bit:0xFFFFFFFF) : (Bit:0);
  241. for (new i = 0; i != size; ++i) array[i] = val;
  242. }
  243. /*----------------------------------------------------------------------------*\
  244. Function:
  245. Bit_GetCount
  246. Params:
  247. Bit:array[] - Array to count.
  248. size - Size of array.
  249. Return:
  250. Number of 1s in the array.
  251. Notes:
  252. Code from:
  253. http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  254. native Bit_Count(BitArray:array<>, size = sizeof (array));
  255. \*----------------------------------------------------------------------------*/
  256. #define Bit_Count Bit_GetCount
  257. stock Bit_GetCount(BitArray:array<>, size = sizeof (array))
  258. {
  259. new
  260. count,
  261. v;
  262. for (new i = 0; i != size; ++i)
  263. {
  264. v = _:array[i];
  265. v = v - ((v >>> 1) & 0x55555555);
  266. v = (v & 0x33333333) + ((v >>> 2) & 0x33333333);
  267. count += ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24;
  268. }
  269. return count;
  270. }
  271. stock Bit_Display(BitArray:array<>, size = sizeof (array))
  272. {
  273. new
  274. ret[YSI_MAX_STRING];
  275. while (size--)
  276. {
  277. format(ret, sizeof (ret), "%s%016b%016b", ret, _:array[size] >>> 16, _:array[size] & 0xFFFF);
  278. }
  279. P:3("Bit_Display called: %s, %i", ret, size);
  280. return ret;
  281. }
  282. #define bitsof(%0) (sizeof(%0)*cellbits)
  283. stock Bits@YSII_Ag(BitArray:data<>, start, size = sizeof (data))
  284. {
  285. P:3("YSI_gABITSFunc called: %s, %i", Bit_Display(data), start);
  286. static const
  287. scDeBruijn[] =
  288. {
  289. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  290. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  291. };
  292. ++start;
  293. new
  294. cur,
  295. i = Bit_Slot(start);
  296. // Blank out the lowest bits to get the lowest bit not yet used.
  297. if ((cur = (_:data[i] & (~((1 << start) - 1)))))
  298. {
  299. // Bits left in the current cell.
  300. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  301. }
  302. ++i;
  303. while (i != size)
  304. {
  305. if ((cur = _:data[i]))
  306. {
  307. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  308. }
  309. ++i;
  310. }
  311. return -1;
  312. }
  313. stock Blanks@YSII_Ag(BitArray:data<>, start, size = sizeof (data))
  314. {
  315. P:3("YSI_gABlanksFunc called: %s, %i", Bit_Display(data), start);
  316. static const
  317. scDeBruijn[] =
  318. {
  319. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  320. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  321. };
  322. ++start;
  323. new
  324. cur,
  325. i = Bit_Slot(start);
  326. if ((cur = ~(_:data[i] & (~((1 << start) - 1)))))
  327. {
  328. // Bits left in the current cell.
  329. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  330. }
  331. ++i;
  332. while (i != size)
  333. {
  334. if ((cur = ~_:data[i]))
  335. {
  336. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  337. }
  338. ++i;
  339. }
  340. return -1;
  341. }
  342. #define bits<%1> Bit_Bits(%1)
  343. #undef BitArray
  344. #define BitArray:%1<%2> Bit:%1[bits<%2>]