Ticket #145: hash.c.patch

File hash.c.patch, 3.5 kB (added by draqo, 2 years ago)
  • gnugo/engine/hash.c

    RCS file: /sources/gnugo/gnugo/engine/hash.c,v
    retrieving revision 1.38
    diff -u -r1.38 hash.c
     
    2828#include <stdio.h> 
    2929#include <stdlib.h> 
    3030#include <limits.h> 
     31#include <string.h> 
    3132 
    3233 
    3334 
     
    5859{ 
    5960  int i; 
    6061  Hashvalue h = 0; 
     62  const int limit = CHAR_BIT * sizeof(Hashvalue); 
    6163 
    62   for (i = 0; 32*i < (int) (CHAR_BIT*sizeof(Hashvalue)); i++) 
    63     h |= (Hashvalue) gg_urand() << 32*i; 
     64  for (i = 0; i < limit; i += 32) 
     65    h |= ((Hashvalue) gg_urand()) << i; 
    6466 
    6567  return h; 
    6668} 
     
    7072hash_init_zobrist_array(Hash_data *array, int size) 
    7173{ 
    7274  int i, j; 
    73   for (i = 0; i < size; i++) 
    74     for (j = 0; j < NUM_HASHVALUES; j++) 
     75  for (i = 0; i < size; ++i) 
     76    for (j = 0; j < NUM_HASHVALUES; ++j) 
    7577      array[i].hashval[j] = hash_rand(); 
    7678} 
    7779 
     
    111113 
    112114  hashdata_clear(target); 
    113115   
    114   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    115     if (p[pos] == WHITE) 
     116  scan_board(pos, 
     117 
     118    Intersection intersection = p[pos]; 
     119 
     120    if (intersection == WHITE) 
    116121      hashdata_xor(*target, white_hash[pos]); 
    117     else if (p[pos] == BLACK) 
     122    else if (intersection == BLACK) 
    118123      hashdata_xor(*target, black_hash[pos]); 
    119   } 
     124  ) 
    120125 
    121   if (ko_pos != 0) 
     126  if (ko_pos != NO_MOVE) 
    122127    hashdata_xor(*target, ko_hash[ko_pos]); 
    123128} 
    124129 
    125130/* Clear hashdata. */ 
    126 void 
     131inline void 
    127132hashdata_clear(Hash_data *hd) 
    128133{ 
    129   int i; 
    130   for (i = 0; i < NUM_HASHVALUES; i++) 
    131     hd->hashval[i] = 0; 
     134  memset(hd->hashval, 0, NUM_HASHVALUES * sizeof(Hashvalue)); 
    132135} 
    133136 
    134137/* Set or remove ko in the hash value and hash position.  */ 
    135 void 
     138inline void 
    136139hashdata_invert_ko(Hash_data *hd, int pos) 
    137140{ 
    138141  hashdata_xor(*hd, ko_hash[pos]); 
     
    140143 
    141144 
    142145/* Set or remove a stone of COLOR at pos in a Hash_data.  */ 
    143 void 
     146inline void 
    144147hashdata_invert_stone(Hash_data *hd, int pos, int color) 
    145148{ 
    146149  if (color == BLACK) 
     
    151154 
    152155 
    153156/* Set or remove the komaster value in the hash data. */ 
    154 void 
     157inline void 
    155158hashdata_invert_komaster(Hash_data *hd, int komaster) 
    156159{ 
    157160  hashdata_xor(*hd, komaster_hash[komaster]); 
    158161} 
    159162 
    160163/* Set or remove the komaster position in the hash data. */ 
    161 void 
     164inline void 
    162165hashdata_invert_kom_pos(Hash_data *hd, int kom_pos) 
    163166{ 
    164167  hashdata_xor(*hd, kom_pos_hash[kom_pos]); 
     
    168171Hash_data 
    169172goal_to_hashvalue(const signed char *goal) 
    170173{ 
    171   int pos; 
    172174  Hash_data return_value; 
     175  int pos; 
    173176   
    174177  hashdata_clear(&return_value); 
    175    
    176   for (pos = BOARDMIN; pos < BOARDMAX; pos++) 
    177     if (ON_BOARD(pos) && goal[pos]) 
     178 
     179  scan_board(pos, 
     180    if (goal[pos]) 
    178181      hashdata_xor(return_value, goal_hash[pos]); 
     182  ); 
     183   
     184  /* code in scan_board is equal but faster 
     185 
     186  for (pos = BOARDMIN; pos < BOARDMAX; ++pos) 
     187    if (ON_BOARD(pos) && *(goal + pos)) 
     188      hashdata_xor(return_value, *(goal_hash + pos));*/ 
    179189   
    180190  return return_value; 
    181191} 
     
    191201  int k; 
    192202 
    193203  /* Loop backwards for consistency between 32 and 64 bit platforms. */ 
    194   for (k = NUM_HASHVALUES - 1; k >= 0; k--) { 
     204  for (k = NUM_HASHVALUES - 1; k >= 0; --k) { 
    195205    n += sprintf(buffer + n, HASHVALUE_PRINT_FORMAT, 
    196206                 HASHVALUE_NUM_DIGITS, hashdata->hashval[k]); 
    197207    gg_assert(n < BUFFER_SIZE); 
     
    205215hashdata_is_equal_func(Hash_data *hd1, Hash_data *hd2) 
    206216{ 
    207217  int i; 
    208   for (i = 0; i < NUM_HASHVALUES; i++) 
     218  for (i = 0; i < NUM_HASHVALUES; ++i) 
    209219    if (hd1->hashval[i] != hd2->hashval[i]) 
    210220      return 0; 
    211221