y_cell.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**--------------------------------------------------------------------------**\
  2. ============================
  3. Y Sever Includes - Cell Core
  4. ============================
  5. Description:
  6. Provides a few functions for manipulating the bits in single cells. Note
  7. that this is distinct from the y_bit library.
  8. Legal:
  9. Version: MPL 1.1
  10. The contents of this file are subject to the Mozilla Public License Version
  11. 1.1 (the "License"); you may not use this file except in compliance with
  12. the License. You may obtain a copy of the License at
  13. http://www.mozilla.org/MPL/
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the
  17. License.
  18. The Original Code is the YSI bit include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2011
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  24. Thanks:
  25. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  26. ZeeX - Very productive conversations.
  27. koolk - IsPlayerinAreaEx code.
  28. TheAlpha - Danish translation.
  29. breadfish - German translation.
  30. Fireburn - Dutch translation.
  31. yom - French translation.
  32. 50p - Polish translation.
  33. Zamaroht - Spanish translation.
  34. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  35. for me to strive to better.
  36. Pixels^ - Running XScripters where the idea was born.
  37. Matite - Pestering me to release it and using it.
  38. Very special thanks to:
  39. Thiadmer - PAWN, whose limits continue to amaze me!
  40. Kye/Kalcor - SA:MP.
  41. SA:MP Team past, present and future - SA:MP.
  42. Version:
  43. 0.2
  44. Changelog:
  45. 18/06/11:
  46. First version.
  47. Functions:
  48. Public:
  49. -
  50. Core:
  51. -
  52. Stock:
  53. -
  54. Static:
  55. -
  56. Inline:
  57. -
  58. API:
  59. -
  60. Callbacks:
  61. -
  62. Definitions:
  63. -
  64. Enums:
  65. -
  66. Macros:
  67. -
  68. Tags:
  69. -
  70. Variables:
  71. Global:
  72. -
  73. Static:
  74. -
  75. Commands:
  76. -
  77. Compile options:
  78. -
  79. </remarks>
  80. \**--------------------------------------------------------------------------**/
  81. #include "internal\y_version"
  82. #include "y_utils"
  83. stock Cell_ReverseBits(GLOBAL_TAG_TYPES:data)
  84. {
  85. // Swap adjacent bits.
  86. data = ((data & 0b10101010101010101010101010101010) >>> 1) | ((data & 0b01010101010101010101010101010101) << 1);
  87. // Swap adjacent pairs.
  88. data = ((data & 0b11001100110011001100110011001100) >>> 2) | ((data & 0b00110011001100110011001100110011) << 2);
  89. // Swap adjacent nibbles.
  90. data = ((data & 0b11110000111100001111000011110000) >>> 4) | ((data & 0b00001111000011110000111100001111) << 4);
  91. // Swap all bytes.
  92. return (data >>> 24) | ((data & 0x00FF0000) >> 8) | ((data & 0x0000FF00) << 8) | (data << 24);
  93. }
  94. stock Cell_ReverseNibbles(GLOBAL_TAG_TYPES:data)
  95. {
  96. // Swap adjacent nibbles.
  97. data = ((data & 0b11110000111100001111000011110000) >>> 4) | ((data & 0b00001111000011110000111100001111) << 4);
  98. // Swap all bytes.
  99. return (data >>> 24) | ((data & 0x00FF0000) >> 8) | ((data & 0x0000FF00) << 8) | (data << 24);
  100. }
  101. stock Cell_ReverseBytes(GLOBAL_TAG_TYPES:data)
  102. {
  103. // Swap all bytes.
  104. return (data >>> 24) | ((data & 0x00FF0000) >> 8) | ((data & 0x0000FF00) << 8) | (data << 24);
  105. }
  106. stock Cell_CountBits(GLOBAL_TAG_TYPES:data)
  107. {
  108. data = data - ((data >>> 1) & 0x55555555);
  109. data = (data & 0x33333333) + ((data >>> 2) & 0x33333333);
  110. return ((data + (data >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24;
  111. }
  112. stock Cell_GetLowestBit(GLOBAL_TAG_TYPES:data)
  113. {
  114. static const
  115. scDeBruijn[] =
  116. {
  117. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  118. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  119. };
  120. // http://supertech.csail.mit.edu/papers/debruijn.pdf
  121. return scDeBruijn[((data & -data) * 0x077CB531) >>> 27];
  122. }
  123. stock Cell_GetLowestComponent(GLOBAL_TAG_TYPES:data)
  124. {
  125. return data & -data;
  126. }