fnv1a.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**--------------------------------------------------------------------------**\
  2. ========================================
  3. y_stringhash - Fast string comparisons
  4. ========================================
  5. Description:
  6. Allows you to hash strings at compile time to use them in a switch.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the YSI stringhash include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  23. Thanks:
  24. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN, whose limits continue to amaze me!
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 2.0
  43. Changelog:
  44. 06/03/11:
  45. Changed the order of some letters to better support numbers in _I.
  46. 01/03/11:
  47. Rewrote compile-time hashes to not require commas.
  48. 25/10/10:
  49. Updated comments.
  50. Added to YSI 1.0.
  51. Added FNV1 and FNV1a hashes to avoid collisions.
  52. 19/08/10:
  53. First version.
  54. Functions:
  55. stock:
  56. YHash - Generate a string hash at run time.
  57. Definitions:
  58. _H - Generate a string hash at compile time.
  59. _I - Generate a case insensitive string hash at compile time.
  60. \**--------------------------------------------------------------------------**/
  61. /*
  62. ,ad8888ba, 88 88 ,ad8888ba, 88
  63. d8"' `"8b 88 88 d8"' `"8b 88
  64. d8' `8b 88 88 d8' 88
  65. 88 88 88 ,adPPYb,88 88 ,adPPYYba, ,adPPYba, ,adPPYba, ,adPPYb,88
  66. 88 88 88 a8" `Y88 88 "" `Y8 I8[ "" a8P_____88 a8" `Y88
  67. Y8, ,8P 88 8b 88 Y8, ,adPPPPP88 `"Y8ba, 8PP""""""" 8b 88
  68. Y8a. .a8P 88 "8a, ,d88 Y8a. .a8P 88, ,88 aa ]8I "8b, ,aa "8a, ,d88
  69. `"Y8888Y"' 88 `"8bbdP"Y8 `"Y8888Y"' `"8bbdP"Y8 `"YbbdP"' `"Ybbd8"' `"8bbdP"Y8
  70. */
  71. // =============================
  72. // Old syntax (case sensitive)
  73. // =============================
  74. // Signify the end with two "@" symbols.
  75. #define HASH@fnv1a(%0) HASH_:_REHASH@a(%0,@,@)
  76. // Internal call.
  77. #define _REHASH@a(%0,%1) _HASH@a_%0(%1,)(2166136261)
  78. // The bit that actually does the work.
  79. #define _DOHASH@a(%0,%1,%2)(%8) _HASH@a_%1(%2)(((%8^%0)*16777619))
  80. // Space.
  81. #define _HASH@a_(%0)(%8) _DOHASH@a(32,%0)(%8)
  82. // Numbers.
  83. #define _HASH@a_0(%0)(%8) _DOHASH@a(48,%0)(%8)
  84. #define _HASH@a_1(%0)(%8) _DOHASH@a(49,%0)(%8)
  85. #define _HASH@a_2(%0)(%8) _DOHASH@a(50,%0)(%8)
  86. #define _HASH@a_3(%0)(%8) _DOHASH@a(51,%0)(%8)
  87. #define _HASH@a_4(%0)(%8) _DOHASH@a(52,%0)(%8)
  88. #define _HASH@a_5(%0)(%8) _DOHASH@a(53,%0)(%8)
  89. #define _HASH@a_6(%0)(%8) _DOHASH@a(54,%0)(%8)
  90. #define _HASH@a_7(%0)(%8) _DOHASH@a(55,%0)(%8)
  91. #define _HASH@a_8(%0)(%8) _DOHASH@a(56,%0)(%8)
  92. #define _HASH@a_9(%0)(%8) _DOHASH@a(57,%0)(%8)
  93. // Upper case letters.
  94. #define _HASH@a_A(%0)(%8) _DOHASH@a(65,%0)(%8)
  95. #define _HASH@a_B(%0)(%8) _DOHASH@a(66,%0)(%8)
  96. #define _HASH@a_C(%0)(%8) _DOHASH@a(67,%0)(%8)
  97. #define _HASH@a_D(%0)(%8) _DOHASH@a(68,%0)(%8)
  98. #define _HASH@a_E(%0)(%8) _DOHASH@a(69,%0)(%8)
  99. #define _HASH@a_F(%0)(%8) _DOHASH@a(70,%0)(%8)
  100. #define _HASH@a_G(%0)(%8) _DOHASH@a(71,%0)(%8)
  101. #define _HASH@a_H(%0)(%8) _DOHASH@a(72,%0)(%8)
  102. #define _HASH@a_I(%0)(%8) _DOHASH@a(73,%0)(%8)
  103. #define _HASH@a_J(%0)(%8) _DOHASH@a(74,%0)(%8)
  104. #define _HASH@a_K(%0)(%8) _DOHASH@a(75,%0)(%8)
  105. #define _HASH@a_L(%0)(%8) _DOHASH@a(76,%0)(%8)
  106. #define _HASH@a_M(%0)(%8) _DOHASH@a(77,%0)(%8)
  107. #define _HASH@a_N(%0)(%8) _DOHASH@a(78,%0)(%8)
  108. #define _HASH@a_O(%0)(%8) _DOHASH@a(79,%0)(%8)
  109. #define _HASH@a_P(%0)(%8) _DOHASH@a(80,%0)(%8)
  110. #define _HASH@a_Q(%0)(%8) _DOHASH@a(81,%0)(%8)
  111. #define _HASH@a_R(%0)(%8) _DOHASH@a(82,%0)(%8)
  112. #define _HASH@a_S(%0)(%8) _DOHASH@a(83,%0)(%8)
  113. #define _HASH@a_T(%0)(%8) _DOHASH@a(84,%0)(%8)
  114. #define _HASH@a_U(%0)(%8) _DOHASH@a(85,%0)(%8)
  115. #define _HASH@a_V(%0)(%8) _DOHASH@a(86,%0)(%8)
  116. #define _HASH@a_W(%0)(%8) _DOHASH@a(87,%0)(%8)
  117. #define _HASH@a_X(%0)(%8) _DOHASH@a(88,%0)(%8)
  118. #define _HASH@a_Y(%0)(%8) _DOHASH@a(89,%0)(%8)
  119. #define _HASH@a_Z(%0)(%8) _DOHASH@a(90,%0)(%8)
  120. // Underscore.
  121. #define _HASH@a__(%0)(%8) _DOHASH@a(95,%0)(%8)
  122. // Lower case letters.
  123. #define _HASH@a_a(%0)(%8) _DOHASH@a(97,%0)(%8)
  124. #define _HASH@a_b(%0)(%8) _DOHASH@a(98,%0)(%8)
  125. #define _HASH@a_c(%0)(%8) _DOHASH@a(99,%0)(%8)
  126. #define _HASH@a_d(%0)(%8) _DOHASH@a(100,%0)(%8)
  127. #define _HASH@a_e(%0)(%8) _DOHASH@a(101,%0)(%8)
  128. #define _HASH@a_f(%0)(%8) _DOHASH@a(102,%0)(%8)
  129. #define _HASH@a_g(%0)(%8) _DOHASH@a(103,%0)(%8)
  130. #define _HASH@a_h(%0)(%8) _DOHASH@a(104,%0)(%8)
  131. #define _HASH@a_i(%0)(%8) _DOHASH@a(105,%0)(%8)
  132. #define _HASH@a_j(%0)(%8) _DOHASH@a(106,%0)(%8)
  133. #define _HASH@a_k(%0)(%8) _DOHASH@a(107,%0)(%8)
  134. #define _HASH@a_l(%0)(%8) _DOHASH@a(108,%0)(%8)
  135. #define _HASH@a_m(%0)(%8) _DOHASH@a(109,%0)(%8)
  136. #define _HASH@a_n(%0)(%8) _DOHASH@a(110,%0)(%8)
  137. #define _HASH@a_o(%0)(%8) _DOHASH@a(111,%0)(%8)
  138. #define _HASH@a_p(%0)(%8) _DOHASH@a(112,%0)(%8)
  139. #define _HASH@a_q(%0)(%8) _DOHASH@a(113,%0)(%8)
  140. #define _HASH@a_r(%0)(%8) _DOHASH@a(114,%0)(%8)
  141. #define _HASH@a_s(%0)(%8) _DOHASH@a(115,%0)(%8)
  142. #define _HASH@a_t(%0)(%8) _DOHASH@a(116,%0)(%8)
  143. #define _HASH@a_u(%0)(%8) _DOHASH@a(117,%0)(%8)
  144. #define _HASH@a_v(%0)(%8) _DOHASH@a(118,%0)(%8)
  145. #define _HASH@a_w(%0)(%8) _DOHASH@a(119,%0)(%8)
  146. #define _HASH@a_x(%0)(%8) _DOHASH@a(120,%0)(%8)
  147. #define _HASH@a_y(%0)(%8) _DOHASH@a(121,%0)(%8)
  148. #define _HASH@a_z(%0)(%8) _DOHASH@a(122,%0)(%8)
  149. // String end.
  150. #define _HASH@a_@(%0)(%8) %8
  151. /*
  152. ,ad8888ba, 88 88 88
  153. d8"' `"8b 88 88 88
  154. d8' `8b 88 88 88
  155. 88 88 88 ,adPPYb,88 88 ,adPPYb,d8 8b,dPPYba, ,adPPYba, 8b,dPPYba, ,adPPYba,
  156. 88 88 88 a8" `Y88 88 a8" `Y88 88P' `"8a a8" "8a 88P' "Y8 a8P_____88
  157. Y8, ,8P 88 8b 88 88 8b 88 88 88 8b d8 88 8PP"""""""
  158. Y8a. .a8P 88 "8a, ,d88 88 "8a, ,d88 88 88 "8a, ,a8" 88 "8b, ,aa
  159. `"Y8888Y"' 88 `"8bbdP"Y8 88 `"YbbdP"Y8 88 88 `"YbbdP"' 88 `"Ybbd8"'
  160. aa, ,88
  161. "Y8bbdP"
  162. */
  163. // ===============================
  164. // Old syntax (case insensitive)
  165. // ===============================
  166. // Signify the end with two "@" symbols.
  167. #define HASHi@fnv1a(%0) _REHASH@ia(%0,@,@)
  168. // Internal call.
  169. #define _REHASH@ia(%0,%1) _HASH@ia_%0(%1,)(2166136261)
  170. // The bit that actually does the work.
  171. #define _DOHASH@ia(%0,%1,%2)(%8) _HASH@ia_%1(%2)(((%8^%0)*16777619))
  172. // Space.
  173. #define _HASH@ia_(%0)(%8) _DOHASH@ia(32,%0)(%8)
  174. // Numbers.
  175. #define _HASH@ia_0(%0)(%8) _DOHASH@ia(48,%0)(%8)
  176. #define _HASH@ia_1(%0)(%8) _DOHASH@ia(49,%0)(%8)
  177. #define _HASH@ia_2(%0)(%8) _DOHASH@ia(50,%0)(%8)
  178. #define _HASH@ia_3(%0)(%8) _DOHASH@ia(51,%0)(%8)
  179. #define _HASH@ia_4(%0)(%8) _DOHASH@ia(52,%0)(%8)
  180. #define _HASH@ia_5(%0)(%8) _DOHASH@ia(53,%0)(%8)
  181. #define _HASH@ia_6(%0)(%8) _DOHASH@ia(54,%0)(%8)
  182. #define _HASH@ia_7(%0)(%8) _DOHASH@ia(55,%0)(%8)
  183. #define _HASH@ia_8(%0)(%8) _DOHASH@ia(56,%0)(%8)
  184. #define _HASH@ia_9(%0)(%8) _DOHASH@ia(57,%0)(%8)
  185. // Upper case letters.
  186. #define _HASH@ia_A(%0)(%8) _DOHASH@ia(65,%0)(%8)
  187. #define _HASH@ia_B(%0)(%8) _DOHASH@ia(66,%0)(%8)
  188. #define _HASH@ia_C(%0)(%8) _DOHASH@ia(67,%0)(%8)
  189. #define _HASH@ia_D(%0)(%8) _DOHASH@ia(68,%0)(%8)
  190. #define _HASH@ia_E(%0)(%8) _DOHASH@ia(69,%0)(%8)
  191. #define _HASH@ia_F(%0)(%8) _DOHASH@ia(70,%0)(%8)
  192. #define _HASH@ia_G(%0)(%8) _DOHASH@ia(71,%0)(%8)
  193. #define _HASH@ia_H(%0)(%8) _DOHASH@ia(72,%0)(%8)
  194. #define _HASH@ia_I(%0)(%8) _DOHASH@ia(73,%0)(%8)
  195. #define _HASH@ia_J(%0)(%8) _DOHASH@ia(74,%0)(%8)
  196. #define _HASH@ia_K(%0)(%8) _DOHASH@ia(75,%0)(%8)
  197. #define _HASH@ia_L(%0)(%8) _DOHASH@ia(76,%0)(%8)
  198. #define _HASH@ia_M(%0)(%8) _DOHASH@ia(77,%0)(%8)
  199. #define _HASH@ia_N(%0)(%8) _DOHASH@ia(78,%0)(%8)
  200. #define _HASH@ia_O(%0)(%8) _DOHASH@ia(79,%0)(%8)
  201. #define _HASH@ia_P(%0)(%8) _DOHASH@ia(80,%0)(%8)
  202. #define _HASH@ia_Q(%0)(%8) _DOHASH@ia(81,%0)(%8)
  203. #define _HASH@ia_R(%0)(%8) _DOHASH@ia(82,%0)(%8)
  204. #define _HASH@ia_S(%0)(%8) _DOHASH@ia(83,%0)(%8)
  205. #define _HASH@ia_T(%0)(%8) _DOHASH@ia(84,%0)(%8)
  206. #define _HASH@ia_U(%0)(%8) _DOHASH@ia(85,%0)(%8)
  207. #define _HASH@ia_V(%0)(%8) _DOHASH@ia(86,%0)(%8)
  208. #define _HASH@ia_W(%0)(%8) _DOHASH@ia(87,%0)(%8)
  209. #define _HASH@ia_X(%0)(%8) _DOHASH@ia(88,%0)(%8)
  210. #define _HASH@ia_Y(%0)(%8) _DOHASH@ia(89,%0)(%8)
  211. #define _HASH@ia_Z(%0)(%8) _DOHASH@ia(90,%0)(%8)
  212. // Underscore.
  213. #define _HASH@ia__(%0)(%8) _DOHASH@ia(95,%0)(%8)
  214. // Lower case letters.
  215. #define _HASH@ia_a(%0)(%8) _DOHASH@ia(65,%0)(%8)
  216. #define _HASH@ia_b(%0)(%8) _DOHASH@ia(66,%0)(%8)
  217. #define _HASH@ia_c(%0)(%8) _DOHASH@ia(67,%0)(%8)
  218. #define _HASH@ia_d(%0)(%8) _DOHASH@ia(68,%0)(%8)
  219. #define _HASH@ia_e(%0)(%8) _DOHASH@ia(69,%0)(%8)
  220. #define _HASH@ia_f(%0)(%8) _DOHASH@ia(70,%0)(%8)
  221. #define _HASH@ia_g(%0)(%8) _DOHASH@ia(71,%0)(%8)
  222. #define _HASH@ia_h(%0)(%8) _DOHASH@ia(72,%0)(%8)
  223. #define _HASH@ia_i(%0)(%8) _DOHASH@ia(73,%0)(%8)
  224. #define _HASH@ia_j(%0)(%8) _DOHASH@ia(74,%0)(%8)
  225. #define _HASH@ia_k(%0)(%8) _DOHASH@ia(75,%0)(%8)
  226. #define _HASH@ia_l(%0)(%8) _DOHASH@ia(76,%0)(%8)
  227. #define _HASH@ia_m(%0)(%8) _DOHASH@ia(77,%0)(%8)
  228. #define _HASH@ia_n(%0)(%8) _DOHASH@ia(78,%0)(%8)
  229. #define _HASH@ia_o(%0)(%8) _DOHASH@ia(79,%0)(%8)
  230. #define _HASH@ia_p(%0)(%8) _DOHASH@ia(80,%0)(%8)
  231. #define _HASH@ia_q(%0)(%8) _DOHASH@ia(81,%0)(%8)
  232. #define _HASH@ia_r(%0)(%8) _DOHASH@ia(82,%0)(%8)
  233. #define _HASH@ia_s(%0)(%8) _DOHASH@ia(83,%0)(%8)
  234. #define _HASH@ia_t(%0)(%8) _DOHASH@ia(84,%0)(%8)
  235. #define _HASH@ia_u(%0)(%8) _DOHASH@ia(85,%0)(%8)
  236. #define _HASH@ia_v(%0)(%8) _DOHASH@ia(86,%0)(%8)
  237. #define _HASH@ia_w(%0)(%8) _DOHASH@ia(87,%0)(%8)
  238. #define _HASH@ia_x(%0)(%8) _DOHASH@ia(88,%0)(%8)
  239. #define _HASH@ia_y(%0)(%8) _DOHASH@ia(89,%0)(%8)
  240. #define _HASH@ia_z(%0)(%8) _DOHASH@ia(90,%0)(%8)
  241. // String end.
  242. #define _HASH@ia_@(%0)(%8) %8