1
0

y_bit.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 SA:MP script information include.
  23. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  24. Portions created by the Initial Developer are Copyright (C) 2008
  25. the Initial Developer. All Rights Reserved.
  26. Contributors:
  27. ZeeX, koolk
  28. Thanks:
  29. Peter, Cam - Support.
  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.
  44. Kye/Kalcor - SA:MP.
  45. SA:MP Team past, present and future - SA:MP.
  46. Version:
  47. 0.2
  48. Changelog:
  49. 01/12/08:
  50. Rewrote most of the code to use shifts and ands not divs and mods.
  51. 24/06/07:
  52. Added Bit_GetBit
  53. 18/06/07:
  54. Added Bit_GetCount
  55. 30/04/07:
  56. Added Bit_SetAll
  57. 15/04/07:
  58. First version.
  59. Functions:
  60. Public:
  61. -
  62. Core:
  63. -
  64. Stock:
  65. Bit_Set - Sets a slot to the given value.
  66. Bit_Get - Gets a slot state.
  67. Bit_SetAll - Sets all the slots in an array to the same thing.
  68. Bit_GetAll - Gets the number of 1s in a bit array.
  69. Static:
  70. -
  71. Inline:
  72. Bit_Bits - Gets the number of cells required for a bit array.
  73. Bit_Let - Sets a slot to 1.
  74. Bit_Vet - Sets a slot to 0.
  75. Bit_GetBits - Gets the bit at a slot unsafely.
  76. API:
  77. -
  78. Callbacks:
  79. -
  80. Definitions:
  81. CELLSHIFT - Number of bits that can hold "cellbits"
  82. Enums:
  83. -
  84. Macros:
  85. -
  86. Tags:
  87. Bit - Bit array type.
  88. Variables:
  89. Global:
  90. -
  91. Static:
  92. -
  93. Commands:
  94. -
  95. Compile options:
  96. -
  97. -*----------------------------------------------------------------------------*/
  98. #include <YSI\internal\y_version>
  99. // This is redefined below, don't worry. It's like this so the function
  100. // prototypes can use a familiar syntax.
  101. #define BitArray:%1<%2> Bit:%1[%2]
  102. #if cellbits == 32
  103. #define CELLSHIFT (5)
  104. #else
  105. #if cellbits == 64
  106. #define CELLSHIFT (6)
  107. #else
  108. #if cellbits == 16
  109. #define CELLSHIFT (4)
  110. #else
  111. #error Unkown cell size
  112. #endif
  113. #endif
  114. #endif
  115. /*----------------------------------------------------------------------------*-
  116. Function:
  117. Bit_Bits
  118. Params:
  119. size - Number of bits required.
  120. Return:
  121. Number of cells required for the bit array.
  122. Notes:
  123. -
  124. -*----------------------------------------------------------------------------*/
  125. #define Bit_Bits(%1) (((%1)+cellbits-1)/cellbits)
  126. /*----------------------------------------------------------------------------*-
  127. Function:
  128. Bit_Slot
  129. Params:
  130. value - Value to get the slot for.
  131. Return:
  132. The true array slot for this value.
  133. Notes:
  134. -
  135. -*----------------------------------------------------------------------------*/
  136. #define Bit_Slot(%1) ((_:%1)>>>CELLSHIFT)
  137. /*----------------------------------------------------------------------------*-
  138. Function:
  139. Bit_Mask
  140. Params:
  141. value - Value to get the mask for
  142. Return:
  143. The bit in the array slot to use.
  144. Notes:
  145. -
  146. -*----------------------------------------------------------------------------*/
  147. #define Bit_Mask(%1) (Bit:(1<<((_:%1)&cellbits-1)))
  148. /*----------------------------------------------------------------------------*-
  149. Function:
  150. Bit_GetBit
  151. Params:
  152. Bit:array[] - Array of bits.
  153. slot - Bit slot.
  154. Return:
  155. State of the provided slot, 0 on fail.
  156. Notes:
  157. Unsafe but faster for when you're sure you're within range.
  158. -*----------------------------------------------------------------------------*/
  159. #define Bit_GetBit(%1,%2) (%1[(%2)>>>CELLSHIFT]&Bit:(1<<((%2)&cellbits-1)))
  160. /*----------------------------------------------------------------------------*-
  161. Function:
  162. Bit_Get
  163. Params:
  164. Bit:array[] - Array of bits.
  165. slot - Bit slot.
  166. size - Size of array.
  167. Return:
  168. State of the provided slot, 0 on fail.
  169. Notes:
  170. -
  171. native Bit_Get(BitArray:array<>, slot);
  172. -*----------------------------------------------------------------------------*/
  173. #define Bit_Get(%1,%2) bool:Bit_GetBit(Bit:%1,_:%2)
  174. /*----------------------------------------------------------------------------*-
  175. Function:
  176. Bit_Let
  177. Params:
  178. Bit:array[] - Array of bits.
  179. slot - Bit slot.
  180. Return:
  181. -
  182. Notes:
  183. Sets the slot to 1.
  184. -*----------------------------------------------------------------------------*/
  185. #define Bit_Let(%1,%2) %1[(%2)>>>CELLSHIFT]|=Bit:(1<<((%2)&cellbits-1))
  186. /*----------------------------------------------------------------------------*-
  187. Function:
  188. Bit_Vet
  189. Params:
  190. Bit:array[] - Array of bits.
  191. slot - Bit slot.
  192. Return:
  193. -
  194. Notes:
  195. Sets the slot to 0.
  196. -*----------------------------------------------------------------------------*/
  197. #define Bit_Vet(%1,%2) %1[(%2)>>>CELLSHIFT]&=Bit:~(1<<((%2)&cellbits-1))
  198. /*----------------------------------------------------------------------------*-
  199. Function:
  200. Bit_Set
  201. Params:
  202. Bit:array[] - Array of bits.
  203. slot - Bit slot.
  204. bool:set - State to set the slot to.
  205. size - Size of array.
  206. Return:
  207. -
  208. Notes:
  209. -
  210. native Bit_Set(BitArray:array<>, slot, bool:set, size = sizeof (array));
  211. -*----------------------------------------------------------------------------*/
  212. stock Bit_Set(BitArray:array<>, slot, bool:set, size = sizeof (array))
  213. {
  214. if (slot >>> CELLSHIFT >= size) return;
  215. if (set) Bit_Let(array, slot);
  216. else Bit_Vet(array, slot);
  217. }
  218. /*----------------------------------------------------------------------------*-
  219. Function:
  220. Bit_SetAll
  221. Params:
  222. Bit:array[] - Array to set all values of.
  223. bool:set - Wether to set them all 0 or 1.
  224. size - Size of array.
  225. Return:
  226. -
  227. Notes:
  228. -
  229. native Bit_SetAll(BitArray:array<>, bool:set, size = sizeof (array));
  230. -*----------------------------------------------------------------------------*/
  231. stock Bit_SetAll(BitArray:array<>, bool:set, size = sizeof (array))
  232. {
  233. new
  234. Bit:val = (set) ? (Bit:0xFFFFFFFF) : (Bit:0);
  235. for (new i = 0; i != size; ++i) array[i] = val;
  236. }
  237. /*----------------------------------------------------------------------------*-
  238. Function:
  239. Bit_GetCount
  240. Params:
  241. Bit:array[] - Array to count.
  242. size - Size of array.
  243. Return:
  244. Number of 1s in the array.
  245. Notes:
  246. Code from:
  247. http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  248. native Bit_Count(BitArray:array<>, size = sizeof (array));
  249. -*----------------------------------------------------------------------------*/
  250. stock Bit_GetCount(BitArray:array<>, size = sizeof (array))
  251. {
  252. new
  253. count,
  254. v;
  255. for (new i = 0; i != size; ++i)
  256. {
  257. v = _:array[i];
  258. v = v - ((v >>> 1) & 0x55555555);
  259. v = (v & 0x33333333) + ((v >>> 2) & 0x33333333);
  260. count += ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24;
  261. }
  262. return count;
  263. }
  264. #define bits<%1> \
  265. Bit_Bits(%1)
  266. #define bitsof(%0) (sizeof(%0)*cellbits)
  267. #undef BitArray
  268. #define BitArray:%1<%2> Bit:%1[bits<%2>]