myopic_core/
hash.rs

1use crate::castlezone::CastleZone;
2use crate::pieces::Piece;
3use crate::square::Square;
4use crate::Side;
5use rand::prelude::*;
6use rand_pcg::Mcg128Xsl64;
7use enumset::EnumSet;
8
9// Total number of hashing features
10const N_FEATURES: usize = 64 * 12 + 8 + 4 + 1;
11
12/// Get the hash of the given piece sat on the given square
13pub fn piece(piece: Piece, square: Square) -> u64 {
14    FEATURES[(piece as usize) * 64 + (square as usize)]
15}
16
17/// Get the hash of the given side to move
18pub fn side(side: Side) -> u64 {
19    match side {
20        Side::Black => FEATURES[N_FEATURES - 1],
21        Side::White => 0,
22    }
23}
24
25/// Get the hash of enpassant on the file of the given square
26pub fn enpassant(square: Square) -> u64 {
27    FEATURES[N_FEATURES - 6 - square.file_index()]
28}
29
30/// Get the hash of the given castling zone
31pub fn zone(zone: CastleZone) -> u64 {
32    FEATURES[N_FEATURES - 2 - zone as usize]
33}
34
35/// Get the combined hash of the given set of castling zones
36pub fn zones(zones: EnumSet<CastleZone>) -> u64 {
37    zones.iter().fold(0u64, |l, r| l ^ zone(r))
38}
39
40pub fn gen_features(seed: u64) -> Vec<u64> {
41    return gen_unique(seed, N_FEATURES);
42}
43
44/// O(n^2) complexity but hey ho.
45fn gen_unique(seed: u64, count: usize) -> Vec<u64> {
46    let mut prng = Mcg128Xsl64::seed_from_u64(seed);
47    let mut dest: Vec<u64> = Vec::with_capacity(count);
48    while dest.len() < count {
49        let attempt = prng.gen();
50        if !dest.contains(&attempt) {
51            dest.push(attempt);
52        }
53    }
54    dest
55}
56
57#[cfg(test)]
58mod test {
59    use super::*;
60    use crate::square::Square;
61
62    #[test]
63    fn test_uniqueness() {
64        let mut dest: Vec<u64> = Vec::new();
65        // add piece-square features
66        for p in Piece::all() {
67            for square in Square::iter() {
68                unique_add(&mut dest, piece(p, square));
69            }
70        }
71        for z in CastleZone::iter() {
72            unique_add(&mut dest, zone(z));
73        }
74        for square in Square::iter().take(8) {
75            unique_add(&mut dest, enpassant(square));
76        }
77        unique_add(&mut dest, side(Side::Black));
78    }
79
80    fn unique_add(dest: &mut Vec<u64>, next: u64) {
81        assert!(!dest.contains(&next));
82        dest.push(next);
83    }
84}
85
86const FEATURES: [u64; N_FEATURES] = [
87    16981440902837726800,
88    13389170232993973050,
89    5915283093704561019,
90    7787432441127208769,
91    1966729422648614027,
92    1004244818048038580,
93    1853522389119074431,
94    5287739706683080441,
95    3538965732359089149,
96    11236549938769817864,
97    11273752147298579980,
98    15166966677929695261,
99    17897269199954134925,
100    7383615875585469227,
101    11675488791015230966,
102    17705071937219203604,
103    13910087315951721474,
104    17672973962292435677,
105    2087198845650289340,
106    2489416606013789919,
107    7686955509004980489,
108    4111679370899898898,
109    756142749130300811,
110    7850773384198433747,
111    7763590541416864709,
112    12859767900442091124,
113    9803371559138797389,
114    1697588071027889763,
115    11778885532974868226,
116    5018691072048477544,
117    17837264342147531266,
118    17308470473709875841,
119    3284999138407155866,
120    2234540566939425335,
121    11672597389975731742,
122    113925859510141131,
123    17888960742101554848,
124    198522506003701973,
125    14744030592063693996,
126    9019334672976306733,
127    15324628443818436974,
128    4276739153523772669,
129    14540712398942049040,
130    181063307457451611,
131    5267203788622667407,
132    1396089518310541523,
133    16443974734258105599,
134    9012239377239449084,
135    3767844762083120871,
136    9043331136045802917,
137    844898957469977630,
138    10009333424106395143,
139    14393513436980990656,
140    6284950616819923260,
141    17073520956005657405,
142    8445934885230272836,
143    1111260418580946510,
144    7712373379082987899,
145    257156913768387148,
146    11493912022878268011,
147    12457444285176387138,
148    6032339089540612206,
149    2097894935176106272,
150    17729415393095682895,
151    17534032059655281165,
152    4939560762123033905,
153    1000141795268252831,
154    5820632148997907684,
155    14801036260355049086,
156    6347524598714327970,
157    2257931013154887960,
158    17574136123557516314,
159    12782382684396665890,
160    1417715927437219295,
161    6139902329343993877,
162    14642288864783344345,
163    4238361023316997914,
164    16584311436457832387,
165    6494556909165396433,
166    2188756563560302411,
167    12298305304634443896,
168    1245943159118438547,
169    15683337702701008192,
170    4637599955618130653,
171    2023171518915183931,
172    11664495601488365200,
173    8431729122260036659,
174    3583359942811457743,
175    15644818981640393762,
176    15797980515029460716,
177    15832882640073614794,
178    5459220189126676999,
179    17819930588704181483,
180    868080937993296704,
181    15161010368006826719,
182    17787188702677304430,
183    8480075803561676901,
184    1746601858436370175,
185    16802781714871002273,
186    44653435730856164,
187    5676499869122172900,
188    7212165138833167423,
189    4308696437022378609,
190    812032021344457253,
191    1173849770843648003,
192    9848460590623375532,
193    2322319528786935699,
194    1594862157284633887,
195    3988132221382468736,
196    1007525035915700746,
197    11414912469915481540,
198    10186086404849712877,
199    11795848339474413511,
200    16223744791485962524,
201    2415104587434659178,
202    15215287237009658751,
203    12439538854653880460,
204    8604687927379866499,
205    12790413574688326185,
206    10053548339088493587,
207    16541227698935406507,
208    12846642872508585709,
209    14621297931877575620,
210    8458091976445179009,
211    765304077438221492,
212    11384059072213645067,
213    12807272510369817057,
214    11748000481569239800,
215    10756966683836440797,
216    15959696851678323046,
217    17852617901254585173,
218    14051254448123051657,
219    4249650985615670810,
220    16771838534810592806,
221    8793649601145643866,
222    15557594545164035088,
223    1367861461658931867,
224    10173930793515036393,
225    17706382436367035615,
226    11386096032622286468,
227    11822935571961106337,
228    2822729993215684382,
229    13622937390892810336,
230    5974800057675400385,
231    18005888695189415542,
232    8982834618576565692,
233    11992148366063726017,
234    6212011146778419324,
235    3383659900489864285,
236    6638835942620756084,
237    11082119825974763804,
238    17475316062582275603,
239    13865744833939933354,
240    15877529096486741721,
241    17140683834409562400,
242    8533399242042307646,
243    3533006719834017026,
244    2728005726930704502,
245    2239221915099941252,
246    17766334833411449638,
247    17071027450822358914,
248    8275890668909283483,
249    14207956512643963982,
250    16592256798277286327,
251    13828395856910416999,
252    13967082832784369445,
253    14931749123288390139,
254    16555163081623900508,
255    633176993048996814,
256    14503502129152036179,
257    11767639506575875563,
258    1994212030061231329,
259    17021907713973271310,
260    4893568497044070130,
261    14638442714878473590,
262    14636665116956964595,
263    12390700674343150133,
264    1413389201753238034,
265    4191496913777683463,
266    10333086363940913677,
267    4326026427228585078,
268    2766616212145182587,
269    12570475070181553637,
270    7914420175756489549,
271    14760771439434162185,
272    3747514533968092237,
273    10052882731959713560,
274    17066018503833474582,
275    10357311922340294723,
276    15020520049297896670,
277    14253796184179996730,
278    10075417326645185200,
279    12041896712706243156,
280    804518457089768480,
281    15437486209641432923,
282    7425987104494752126,
283    8994396476895312524,
284    18413697504468812571,
285    15925351811426033236,
286    5684227400682757094,
287    1689107101566877098,
288    3771363221659470313,
289    14400751621040591778,
290    3341609887760577728,
291    18237875477891244098,
292    10639433611117548832,
293    10787798666344498933,
294    13831215910667312367,
295    17608015098865130012,
296    132452958223944949,
297    4111767292333111578,
298    16984194959545162343,
299    3351599281569476502,
300    2846756746018523073,
301    8524174304920853646,
302    10603763668260269943,
303    15755100370626276626,
304    4487446259928583508,
305    5459264997129738112,
306    1744723028406162594,
307    7875016526527891974,
308    5979590250000127130,
309    15854105676621605923,
310    17148162633319372221,
311    15821897974454048866,
312    7598445937254487125,
313    4016504085236401372,
314    11032095813687248072,
315    9075175924292050135,
316    3946391828261057609,
317    9643001230593938973,
318    2313592739345408218,
319    6429686437017532404,
320    4428083145575052647,
321    352929674481287274,
322    17598505218568505448,
323    12401178125762131673,
324    18225897226333848087,
325    17914117453949797823,
326    145554855894033257,
327    3964662490342047409,
328    1028393360377817957,
329    14619461174996860866,
330    5816148956936332410,
331    10766704777215021180,
332    7615441876473997751,
333    9845001476110530488,
334    7841919117518124809,
335    5157253787288385022,
336    11392908418860405534,
337    17103499311285971506,
338    1438916672526641798,
339    16380451730072627543,
340    3505636861781892763,
341    15600834920739268830,
342    13165042031134684012,
343    15608370233919131127,
344    3239456627947045306,
345    12941161434483936349,
346    10500069176481567753,
347    12893424581692053839,
348    7188782994947478447,
349    15496250116470653815,
350    7847491536997101472,
351    7936129584880266747,
352    3710199633319267639,
353    2626049985507079870,
354    10424956325123329890,
355    17200125073067836748,
356    16954668415805241211,
357    17455667210555599338,
358    16092005756880451680,
359    5541155984531079993,
360    1336926179890123504,
361    13379839300735774,
362    9608371535920101118,
363    3659866219551783716,
364    17170060472922074912,
365    17704903928195410462,
366    16983670829028408848,
367    10469599759830762497,
368    13308123700236060681,
369    7744389734950958920,
370    7199752856684522236,
371    3792841997567633541,
372    206064465786166147,
373    5496195613434532627,
374    8256085973168829683,
375    7300968381888301207,
376    2716280711465438845,
377    8738088861610832956,
378    5123460718434707792,
379    4536554237769334173,
380    7393911606551506096,
381    3179025506913827578,
382    4133277924410948673,
383    2952112554688537879,
384    5317453954694695626,
385    5576063569527385142,
386    10550768835483796768,
387    402295702004574222,
388    8142189163914186526,
389    9775487735260993959,
390    7226952715866944488,
391    8663048547551794707,
392    6122531019625024823,
393    16473732477974489834,
394    15374256359314784592,
395    5939383355574455605,
396    5764159518396661873,
397    6964133938222096648,
398    1251859055792163323,
399    2480394503304691045,
400    11111770319389450259,
401    6988416286368443614,
402    13090382075707415528,
403    15326974929603839161,
404    5825930622975554849,
405    16053865982897240815,
406    15625401903215454814,
407    8868982248458153554,
408    733182225232026238,
409    12447733072768680678,
410    14999337050394390544,
411    7435880958478013268,
412    3165014568029972803,
413    1008549413798521684,
414    11289180268186953751,
415    18205912641350485375,
416    4166607612991957421,
417    9577694872297656799,
418    3643867553648398102,
419    11758086969277933213,
420    10074550763735226627,
421    5532255560108887464,
422    10143184825563659757,
423    11037812124482263105,
424    4677210809614065621,
425    7153464112453398022,
426    12849688607795275308,
427    18376736164052500860,
428    10106641667055913233,
429    4162210995984583537,
430    7580027449532574891,
431    8829529741413948026,
432    18340447326015986115,
433    11052127354184335684,
434    12887247908437046838,
435    17462561313857163641,
436    14769723646935312044,
437    10650848470902165013,
438    4721498067522814014,
439    3143346908491014487,
440    9588370006728357240,
441    15690566490456726334,
442    16635900043156689319,
443    8176429884922703958,
444    8166070692866995446,
445    17042979797157562074,
446    8510065112081165238,
447    7311025307581223246,
448    3633907091117472612,
449    17890362641335463703,
450    10916278191791267908,
451    9776460115793702718,
452    7255522633014737394,
453    10301770621111975548,
454    14609416817730991232,
455    5381155528052775982,
456    15659803118380268929,
457    10490995275500090707,
458    8752193074546730450,
459    13821959181540632351,
460    3636963056329802195,
461    8134784660434725944,
462    18371911915287810509,
463    5956986179371079789,
464    8349774583723449314,
465    8844149935911429816,
466    10254407496782914897,
467    781322695494250418,
468    7267317324339976618,
469    14507946596436328226,
470    7718143025107680873,
471    17014439746559604282,
472    1411575457665869053,
473    4465800980151424468,
474    11500948080223210955,
475    1469229226738898453,
476    11779250622065611825,
477    2712629813857438444,
478    5773459174556854610,
479    6446031086891076108,
480    2508599485732312871,
481    13373157478225896825,
482    17757536621630891145,
483    6996250219173292487,
484    9156251719315275068,
485    2075416526060790501,
486    14436586815449702145,
487    10873040221387067648,
488    2524932959498357530,
489    6863574893968730098,
490    11374992319802592313,
491    5895801599298340189,
492    7421933909847235676,
493    962511150887857556,
494    9533234979899257054,
495    5329544982663113766,
496    9620336618469799741,
497    6338146860962751625,
498    1000593042181945210,
499    6197789421261403045,
500    2707611089238098254,
501    2975823852531059811,
502    7995018343840914623,
503    11970866236657165163,
504    18387312502732838610,
505    2308360573047509169,
506    11665504120051658400,
507    7130978558192522447,
508    6464444222806853424,
509    4965731383174217691,
510    13797827297061691568,
511    11116112146854726013,
512    13288348174511126767,
513    17093003921995046340,
514    6749813857991395702,
515    17413004005182998580,
516    13865618957165211596,
517    8208466226224067928,
518    3747271158820060877,
519    7771852898908055027,
520    6778966741854835118,
521    14202461753638463482,
522    10298186073163174324,
523    10707842758271974847,
524    7702810787605761188,
525    10984387047855367675,
526    7738276509738687748,
527    9132415030329522204,
528    13215904454678184973,
529    17325106198738524878,
530    14020473208318059251,
531    13134332735787631373,
532    25182375855953351,
533    14702967354293303051,
534    5814696463067909139,
535    16994977048215955574,
536    8377321384431296805,
537    8288347687876361567,
538    2546214541320911855,
539    15423075881906584957,
540    13364713454058539575,
541    7001033024468371133,
542    1311763047076445814,
543    14104619009551497627,
544    1661838026083404425,
545    16017076139764770137,
546    16982029739553298654,
547    13749256774482774276,
548    4135413926024345955,
549    13660047004567450040,
550    707149457515953623,
551    13127756638687238514,
552    8495448986762010217,
553    6761225154573654751,
554    10702034216256355369,
555    15203893890477956506,
556    1793442075017174216,
557    10711593423996834704,
558    9392214502028785226,
559    11578502447332157370,
560    6134288153781194747,
561    14701381427442711314,
562    3492750704896025621,
563    5436470616468563672,
564    18035327306298966057,
565    11991941050772401773,
566    15600996815899208992,
567    7539279813326845556,
568    15158352183505766349,
569    11560319025986333054,
570    1463076978199081969,
571    5449620489226180089,
572    2192877756289108834,
573    240124960483381520,
574    9791728250138369880,
575    502507578166718858,
576    6871925502662934902,
577    16123766733030627545,
578    17178142140031651002,
579    8046301962344978312,
580    13164531567077682765,
581    4927705194122595074,
582    1486517102808013844,
583    3447971452897557972,
584    7063010880855891412,
585    3646171838134343398,
586    15646119743012451433,
587    15357475108019973877,
588    15762553019167447197,
589    4030191000102724403,
590    14893045223380180099,
591    2672484731029480206,
592    2588453762148315381,
593    15392604684421721176,
594    12784856215211866098,
595    1739738534772637274,
596    10122978903649259448,
597    10808397433983521478,
598    841145325713238936,
599    2363330484626913556,
600    1158723944246010482,
601    12372401037964017343,
602    6562240992354539409,
603    5763682497681598182,
604    3132503880921237762,
605    12370247871750289532,
606    4576132994610759469,
607    15055922860338762171,
608    3227993278069578391,
609    922776869874499472,
610    14009471597853386235,
611    15444883717274977867,
612    17632831752328733257,
613    2881177181469471628,
614    18383210560349262569,
615    12309470273209872052,
616    16211561403310225290,
617    14626287815607860901,
618    451054182091668913,
619    2829912774081243214,
620    489587204535402571,
621    11303109673277475490,
622    6909741520975834973,
623    9460965074755458941,
624    5587561167253754566,
625    2802071487948023739,
626    16744047975830131922,
627    16774931961449600258,
628    17892345189125601744,
629    16180261968508228381,
630    2018869920186893441,
631    2798503971911261182,
632    9313949859643420667,
633    160642059289901622,
634    17492928090416439326,
635    679582835213049563,
636    12395217952366320637,
637    9759931835646874119,
638    15668468649046882066,
639    12692621652486377521,
640    10572640182039841571,
641    2711875542311801281,
642    3277199026858969713,
643    7190694155822238562,
644    15506548902919521105,
645    11265585183640385044,
646    14622014610653536633,
647    10108940553519459942,
648    6565802169047937594,
649    1099186779805423199,
650    10089378015720013894,
651    12305241585212041896,
652    13060196433876257048,
653    3568009479028144685,
654    12607844350062784463,
655    12817285060733022316,
656    15025805333104060350,
657    6046181038975112643,
658    14054068478134312330,
659    4428916327995047952,
660    2103371458587184597,
661    15504633510571143549,
662    16367172203648961139,
663    9206314586700689782,
664    18395057570397893463,
665    12858484620459943457,
666    7082804389892935929,
667    14107959608625795600,
668    15533075401077877362,
669    805792539244624979,
670    5911419190259846436,
671    13607635742716850934,
672    1156442824011560015,
673    4461093234809374487,
674    13412278306801653428,
675    7638951972874474530,
676    10461117339692397431,
677    12999380474515218775,
678    7126865034213407924,
679    6874972581485957266,
680    979034496620222987,
681    9699822164670942569,
682    12594135423526004314,
683    17685639924926973677,
684    17531738324265636704,
685    1246528380808303450,
686    10589626595403243353,
687    4547057486255789694,
688    716543655670283833,
689    11961545459251072505,
690    6130403706152456156,
691    3212878284431368512,
692    4668331299476369204,
693    5771795439605986422,
694    18299344349161333407,
695    13029186050478255314,
696    11143731813390608079,
697    11433889198204399022,
698    377515900033001821,
699    5025861874165143653,
700    11320487031268560458,
701    11146003087953080393,
702    8327224431235996258,
703    5680947911510426098,
704    8611732634055988347,
705    17189504668331702972,
706    2137704489870069337,
707    1378448216505069965,
708    8030709208362703452,
709    8666613820958198890,
710    13142842692868310384,
711    6208160380629429843,
712    18102431228369362346,
713    13169648338726172716,
714    11407300568792459755,
715    18039663391910611745,
716    18259747255051256104,
717    4705998338316260846,
718    1985091279822237734,
719    16564609859225225918,
720    10019551786908891335,
721    216369030731738952,
722    6151371219650668450,
723    17535055610358758271,
724    3352710064004427492,
725    13227510456929008239,
726    5922073173583441920,
727    12298739905415478199,
728    3852927506303466270,
729    2147647967495813175,
730    15263279347321183320,
731    9157182273100737235,
732    13885575554768108605,
733    13573448658843315345,
734    16824375270184292952,
735    1946970976637008525,
736    2288189900966236343,
737    815548732335515572,
738    1251259657162877680,
739    2676432467796118281,
740    984186782704629377,
741    6243705798998213754,
742    6673256566216044366,
743    7525477542539551133,
744    2153898177431574363,
745    10789008581688134490,
746    17128321264444936858,
747    4041611272152290800,
748    14423923597191096495,
749    5191563333712839157,
750    16449441985684390000,
751    5742945053354308366,
752    17578305991590899133,
753    16063953382461797237,
754    8893485931949406299,
755    16928818683562099445,
756    7937572989564352906,
757    17773238151168026751,
758    2271809131422587931,
759    4290737518890822912,
760    11402438845226454099,
761    12956077576887103799,
762    13064695591657350995,
763    2185044125977727379,
764    15390028727184137458,
765    7801826940976619161,
766    7851518661083293234,
767    8091934838373487108,
768    9727291491341875092,
769    685853814980118501,
770    2238489935872807491,
771    8775731037240587065,
772    15280174713831115332,
773    12226689027174362608,
774    8985738288679733702,
775    2006652145239649626,
776    420643544891119691,
777    1535027598839898231,
778    11997988936390993495,
779    15960561717919773693,
780    8597131750489177648,
781    9624905229902601141,
782    5398809140237935360,
783    196857438571320955,
784    9394527300968427926,
785    9033177375321503119,
786    7361795198702195134,
787    5915250907338674296,
788    2965396197842727184,
789    7913536996188062647,
790    5968459071968716419,
791    8962580249121889293,
792    3099943052943679477,
793    12717497320350185143,
794    8009978572527643229,
795    3728055697131702034,
796    7461325579688461571,
797    6985286124887288900,
798    13324503245133103659,
799    177065616115990004,
800    5646036415599138033,
801    16497152413789738698,
802    2388677939155355876,
803    6636290434354156210,
804    14803977265134405591,
805    4007065349627048055,
806    16735476974568699126,
807    9875146173162828192,
808    14992560426402473085,
809    1268893318567863370,
810    12249474062520691542,
811    12460475188027312479,
812    12579284938590547355,
813    1205218504956649615,
814    369734714373705010,
815    13850332846202501051,
816    14751777331135717738,
817    11294452961942994790,
818    10975434618846997101,
819    2562844337082134647,
820    12188079577320643577,
821    3728153480303386379,
822    12036820635161006969,
823    14342648372940053851,
824    17333249371434531263,
825    12591994446587538518,
826    15895638948292048463,
827    10245061382320656067,
828    6613861251595812103,
829    7786024098247637678,
830    8016291687622422464,
831    11987553914083441411,
832    14868395867193960624,
833    1718162313549290514,
834    15786837862207838031,
835    6806487083107602322,
836    18353218416970874198,
837    1507521568313343916,
838    3897906849464647275,
839    8953454096290763151,
840    3285831659581931643,
841    5545324167674857003,
842    10132103878284564531,
843    17452872784917814398,
844    3005326549973574238,
845    1359058237790886262,
846    11342424127833138443,
847    88862214912896100,
848    8149710741706528412,
849    5338325291441386679,
850    1908916719817830424,
851    7731887820847009561,
852    265127237898025079,
853    13770838568219628902,
854    6807950187556197467,
855    3323165315149945038,
856    6365648104236759924,
857    1621102043423766732,
858    10249336505490214576,
859    9616021352835475313,
860    9563975895673580657,
861    15612122441449425634,
862    13367508551891674842,
863    10705688998729503783,
864    10350016333854978882,
865    5089885123724170365,
866    9843661064086812320,
867    5963312476232014387,
868];