gnugo-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnugo-devel] Bugfixes in hash code


From: Gunnar Farneback
Subject: [gnugo-devel] Bugfixes in hash code
Date: Thu, 15 Jan 2004 01:02:17 +0100
User-agent: EMH/1.14.1 SEMI/1.14.3 (Ushinoya) FLIM/1.14.2 (Yagi-Nishiguchi) APEL/10.3 Emacs/20.7 (sparc-sun-solaris2.7) (with unibyte mode)

This patch fixes no less than three shortcomings/bugs in the
definition of NUM_HASHVALUES in hash.h. It also reimplements
hashdata_to_string() to be more general and contains some minor
maintenance fixes.

- computation of NUM_HASHVALUES corrected in hash.h
- hashdata_to_string reimplemented

/Gunnar

Index: engine/hash.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/hash.c,v
retrieving revision 1.22
diff -u -r1.22 hash.c
--- engine/hash.c       13 Nov 2003 22:48:40 -0000      1.22
+++ engine/hash.c       14 Jan 2004 23:57:09 -0000
@@ -275,15 +275,19 @@
 }
 
 
+#define BUFFER_SIZE (1 + NUM_HASHVALUES * (1 + (CHAR_BIT * SIZEOF_HASHVALUE \
+                                               - 1) / 4))
 char *
 hashdata_to_string(Hash_data *hashdata)
 {
-  static char buffer[17];
-
-  sprintf(buffer, "%lx", hashdata->hashval[0]);
-#if NUM_HASHVALUES == 2
-  sprintf(buffer + 8, "%lx", hashdata->hashval[1]);
-#endif
+  static char buffer[BUFFER_SIZE];
+  int n = 0;
+  int k;
+  
+  for (k = 0; k < NUM_HASHVALUES; k++) {
+    n += sprintf(buffer + n, HASHVALUE_PRINT_FORMAT, hashdata->hashval[k]);
+    gg_assert(n < BUFFER_SIZE);
+  }
 
   return buffer;
 }
Index: engine/hash.h
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/hash.h,v
retrieving revision 1.20
diff -u -r1.20 hash.h
--- engine/hash.h       13 Nov 2003 22:48:41 -0000      1.20
+++ engine/hash.h       14 Jan 2004 23:57:09 -0000
@@ -20,9 +20,12 @@
  * Boston, MA 02111, USA.                                            *
 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
+#ifndef _HASH_H_
+#define _HASH_H_
 
 #include "config.h"
 #include "board.h"
+#include <limits.h>
 
 /*
  * This file, together with engine/hash.c implements hashing of go positions
@@ -33,9 +36,6 @@
 /* Dump (almost) all read results. */
 #define TRACE_READ_RESULTS 0
 
-#ifndef _HASH_H_
-#define _HASH_H_
-
 /* Hash values and the compact board representation should use the
  * longest integer type that the platform can handle efficiently.
  * Typically this would be a 32 bit integer on a 32 bit platform and a
@@ -55,8 +55,8 @@
  * cost.
  */
 typedef unsigned long Hashvalue;
-typedef unsigned long Compacttype;
-
+#define SIZEOF_HASHVALUE SIZEOF_LONG
+#define HASHVALUE_PRINT_FORMAT "%l0x"
 
 /* for testing: Enables a lot of checks. */
 #define CHECK_HASHING 0
@@ -67,9 +67,9 @@
  * With 64 bits, there should be less than one such mistake in 10^9 games.
  * Set this to 96 if this is not safe enough for you.
  */
-#define MIN_HASHBITS           64              
+#define MIN_HASHBITS   64              
 
-#define NUM_HASHVALUES                 (MIN_HASHBITS / ( 8 * SIZEOF_LONG))
+#define NUM_HASHVALUES (1 + (MIN_HASHBITS - 1) / (CHAR_BIT * SIZEOF_HASHVALUE))
 
 /*
  * This struct is maintained by the machinery that updates the board
Index: engine/cache.h
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/cache.h,v
retrieving revision 1.37
diff -u -r1.37 cache.h
--- engine/cache.h      13 Nov 2003 22:48:40 -0000      1.37
+++ engine/cache.h      14 Jan 2004 23:57:09 -0000
@@ -20,6 +20,9 @@
  * Boston, MA 02111, USA.                                            *
 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
+#ifndef _CACHE_H_
+#define _CACHE_H_
+
 #include <stdio.h>
 #include "hash.h"
 
@@ -28,9 +31,6 @@
  * using a method known as Zobrist hashing.  See the Texinfo documentation
  * (Reading/Hashing) for more information.  
  */
-
-#ifndef _CACHE_H_
-#define _CACHE_H_
 
 /* Define to 1 if you want the new transposition table. */
 #define USE_HASHTABLE_NG 1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]