Skip to content

Commit bb5145e

Browse files
committed
Port Gull Linux back to Windows.
Requires cross-compilation using MinGW. Not yet tested.
1 parent ae1784f commit bb5145e

File tree

5 files changed

+260
-8
lines changed

5 files changed

+260
-8
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
Gull 3 Chess (LINUX/MACOSX/SYZYGY)
2-
==================================
1+
Gull 3 Chess (SYZYGY)
2+
=====================
33

4-
This is a Linux/MacOSX port of the Gull chess engine version 3!
4+
This is a portable (Linux/MacOSX/Windows) version the Gull chess engine
5+
version 3.
56

67
To build simply run `make` (from a terminal) in the `src` directory.
78

src/Gull.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ typedef int sint32;
5454
typedef unsigned long long uint64;
5555
typedef long long sint64;
5656

57+
#ifdef LINUX
5758
#include "Linux.h"
59+
#endif
60+
61+
#ifdef WINDOWS
62+
#include "Windows.h"
63+
#endif
5864

5965
#define Convert(x,type) ((type)(x))
6066

@@ -1102,7 +1108,13 @@ void uci();
11021108
#include "tbprobe.h"
11031109
#endif
11041110

1111+
#ifdef LINUX
11051112
#include "Linux.cpp"
1113+
#endif
1114+
1115+
#ifdef WINDOWS
1116+
#include "Windows.cpp"
1117+
#endif
11061118

11071119
#ifdef TUNER
11081120
#ifndef RECORD_GAMES
@@ -2077,15 +2089,13 @@ void pgn_stat() {
20772089

20782090
#ifndef W32_BUILD
20792091
__forceinline int lsb(uint64 x) {
2080-
register unsigned long y;
2081-
// _BitScanForward64(&y, x);
2092+
register unsigned long long y;
20822093
__asm__("bsfq %1, %0": "=r"(y): "rm"(x));
20832094
return y;
20842095
}
20852096

20862097
__forceinline int msb(uint64 x) {
2087-
register unsigned long y;
2088-
// _BitScanReverse64(&y, x);
2098+
register unsigned long long y;
20892099
__asm__("bsrq %1, %0": "=r"(y): "rm"(x));
20902100
return y;
20912101
}
@@ -6933,6 +6943,7 @@ int main(int argc, char *argv[]) {
69336943

69346944
if (parent) {
69356945
#ifdef WINDOWS
6946+
DWORD p;
69366947
int CPUInfo[4] = { -1 };
69376948
__cpuid(CPUInfo, 1);
69386949
if (((CPUInfo[3] >> 28) & 1) && GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation") != NULL) {
@@ -6994,6 +7005,7 @@ int main(int argc, char *argv[]) {
69947005
#endif
69957006

69967007
#ifdef WINDOWS
7008+
DWORD p;
69977009
StreamHandle = GetStdHandle(STD_INPUT_HANDLE);
69987010
Console = GetConsoleMode(StreamHandle, &p);
69997011
if (Console) {

src/Makefile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,30 @@ gull.macosx:
3434
$(STRIP) Gull
3535
cp Gull Gull.macosx
3636

37-
gull.syzygy.macosx:
37+
gull.syzygy.macosx: tbprobe.o
3838
$(CC) $(CFLAGS) -D MACOSX -D TB Gull.cpp tbprobe.o -o Gull
3939
$(STRIP) Gull
4040
cp Gull Gull.syzygy.macosx
4141

42+
gull.windows: CC=x86_64-w64-mingw32-g++
43+
gull.windows: STRIP=x86_64-w64-mingw32-strip
44+
gull.windows:
45+
$(CC) $(CFLAGS) -D WINDOWS Gull.cpp -o Gull.exe
46+
$(STRIP) Gull.exe
47+
48+
gull.syzygy.windows: CC=x86_64-w64-mingw32-g++
49+
gull.syzygy.windows: STRIP=x86_64-w64-mingw32-strip
50+
gull.syzygy.windows: tbprobe_windows.o
51+
$(CC) $(CFLAGS) -D WINDOWS -D TB Gull.cpp tbprobe_windows.o -o Gull.exe
52+
$(STRIP) Gull.exe
53+
4254
tbprobe.o: tbprobe.c tbcore.c
4355
$(CC) $(CFLAGS) -c tbprobe.c
4456

57+
tbprobe_windows.o: CC=x86_64-w64-mingw32-g++
58+
tbprobe_windows.o: tbprobe.c tbcore.c
59+
$(CC) $(CFLAGS) -c tbprobe.c -o tbprobe_windows.o
60+
4561
tbprobe.c: Fathom-master.zip
4662
unzip -o -j Fathom-master.zip "Fathom-master/src/tbprobe.h" -d "./"
4763
unzip -o -j Fathom-master.zip "Fathom-master/src/tbprobe.c" -d "./"

src/Windows.cpp

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Windows.cpp
3+
* Original Gull code is in the "public domain".
4+
* New code: Copyright (c) 2015 the copyright holders
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a
7+
* copy of this software and associated documentation files (the "Software"),
8+
* to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
* and/or sell copies of the Software, and to permit persons to whom the
11+
* Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
* DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
#include <setjmp.h>
26+
#include <stdio.h>
27+
#include <stdlib.h>
28+
#include <math.h>
29+
#include <windows.h>
30+
31+
#include <xmmintrin.h>
32+
#include <popcntintrin.h>
33+
34+
#define builtin_cpuid(f, ax, bx, cx, dx) \
35+
__asm__ __volatile__ ("cpuid" : "=a" (ax), "=b" (bx), "=c" (cx), \
36+
"=d" (dx) : "a" (f))
37+
38+
#define builtin_popcnt_u64(x) _mm_popcnt_u64((x))
39+
40+
#define builtin_sync_fetch_and_add(x, y) \
41+
__sync_fetch_and_add((x), (y))
42+
#define builtin_sync_fetch_and_or(x, y) \
43+
__sync_fetch_and_or((x), (y))
44+
#define builtin_sync_fetch_and_and(x, y) \
45+
__sync_fetch_and_and((x), (y))
46+
#define builtin_sync_bit_test_and_reset(x, y) \
47+
((__sync_fetch_and_and((x), ~(1 << (y))) & (1 << (y))) != 0)
48+
#define builtin_sync_lock_test_and_set(x, y) \
49+
__sync_lock_test_and_set((x), (y))
50+
#define builtin_sync_val_compare_and_swap(x, y, z) \
51+
__sync_val_compare_and_swap((x), (z), (y))
52+
53+
void init_hash() {
54+
#ifdef TUNER
55+
return;
56+
#endif
57+
char name[256];
58+
sint64 size = (hash_size * sizeof(GEntry));
59+
int min_page_size;
60+
HINSTANCE hDll;
61+
sprintf(name, "GULL_HASH_%d", WinParId);
62+
int initialized = 0;
63+
if (parent && HASH != NULL) {
64+
initialized = 1;
65+
UnmapViewOfFile(Hash);
66+
CloseHandle(HASH);
67+
}
68+
if (parent) {
69+
if (!LargePages) goto no_lp;
70+
#ifndef LARGE_PAGES
71+
goto no_lp;
72+
#endif
73+
typedef int(*GETLARGEPAGEMINIMUM)(void);
74+
GETLARGEPAGEMINIMUM pGetLargePageMinimum;
75+
hDll = LoadLibrary(TEXT("kernel32.dll"));
76+
if (hDll == NULL) goto no_lp;
77+
pGetLargePageMinimum = (GETLARGEPAGEMINIMUM)GetProcAddress(hDll, "GetLargePageMinimum");
78+
if (pGetLargePageMinimum == NULL) goto no_lp;
79+
min_page_size = (*pGetLargePageMinimum)();
80+
if (size < min_page_size) size = min_page_size;
81+
if (!initialized) {
82+
TOKEN_PRIVILEGES tp;
83+
HANDLE hToken;
84+
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
85+
LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &tp.Privileges[0].Luid);
86+
tp.PrivilegeCount = 1;
87+
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
88+
AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
89+
}
90+
HASH = NULL;
91+
HASH = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES, size >> 32, size & 0xFFFFFFFF, name);
92+
if (HASH != NULL) {
93+
fprintf(stdout, "Large page hash\n");
94+
goto hash_allocated;
95+
}
96+
no_lp:
97+
HASH = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, size >> 32, size & 0xFFFFFFFF, name);
98+
} else HASH = OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, name);
99+
hash_allocated:
100+
Hash = (GEntry*)MapViewOfFile(HASH, FILE_MAP_ALL_ACCESS, 0, 0, size);
101+
if (parent) memset(Hash, 0, size);
102+
hash_mask = hash_size - 4;
103+
}
104+
105+
void init_shared() {
106+
#ifdef TUNER
107+
return;
108+
#endif
109+
char name[256];
110+
sint64 size = SharedPVHashOffset + pv_hash_size * sizeof(GPVEntry);
111+
sprintf(name, "GULL_SHARED_%d", WinParId);
112+
if (parent && SHARED != NULL) {
113+
UnmapViewOfFile(Smpi);
114+
CloseHandle(SHARED);
115+
}
116+
if (parent) SHARED = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, name);
117+
else SHARED = OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, name);
118+
Smpi = (GSMPI*)MapViewOfFile(SHARED, FILE_MAP_ALL_ACCESS, 0, 0, size);
119+
if (parent) memset(Smpi, 0, size);
120+
Material = (GMaterial*)(((char*)Smpi) + SharedMaterialOffset);
121+
MagicAttacks = (uint64*)(((char*)Smpi) + SharedMagicOffset);
122+
PVHash = (GPVEntry*)(((char*)Smpi) + SharedPVHashOffset);
123+
if (parent) memset(PVHash, 0, pv_hash_size * sizeof(GPVEntry));
124+
}
125+
126+
int input() {
127+
if (child) return 0;
128+
DWORD p;
129+
if (F(Input)) return 0;
130+
if (F(Console)) {
131+
if (PeekNamedPipe(StreamHandle,NULL,0,NULL,&p,NULL)) return (p > 0);
132+
else return 1;
133+
} else return 0;
134+
}
135+
136+
HANDLE CreateChildProcess(int child_id) {
137+
char name[1024];
138+
TCHAR szCmdline[1024];
139+
PROCESS_INFORMATION piProcInfo;
140+
STARTUPINFO siStartInfo;
141+
BOOL bSuccess = FALSE;
142+
143+
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
144+
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
145+
ZeroMemory(szCmdline, 1024 * sizeof(TCHAR));
146+
ZeroMemory(name, 1024);
147+
148+
siStartInfo.cb = sizeof(STARTUPINFO);
149+
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
150+
151+
GetModuleFileName(NULL, name, 1024);
152+
sprintf(szCmdline, " child %d %d", WinParId, child_id);
153+
154+
bSuccess = CreateProcess(TEXT(name), TEXT(szCmdline), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &siStartInfo, &piProcInfo);
155+
156+
if (bSuccess) {
157+
CloseHandle(piProcInfo.hThread);
158+
return piProcInfo.hProcess;
159+
} else {
160+
fprintf(stdout, "Error %d\n", GetLastError());
161+
return NULL;
162+
}
163+
}
164+
165+
static void msleep(unsigned ms)
166+
{
167+
Sleep(ms);
168+
}
169+
170+
sint64 get_time() {
171+
#ifdef CPU_TIMING
172+
#ifndef TIMING
173+
if (CpuTiming) {
174+
#endif
175+
uint64 ctime;
176+
QueryProcessCycleTime(GetCurrentProcess(), &ctime);
177+
#ifdef TIMING
178+
return ctime / (CyclesPerMSec / 1000);
179+
#endif
180+
return (ctime / CyclesPerMSec);
181+
#ifndef TIMING
182+
}
183+
#endif
184+
#endif
185+
return GetTickCount();
186+
}
187+

src/Windows.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Windows.h
3+
* Copyright (c) 2015 the copyright holders
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a
6+
* copy of this software and associated documentation files (the "Software"),
7+
* to deal in the Software without restriction, including without limitation
8+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
* and/or sell copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
#ifndef LINUX_H
25+
#define LINUX_H
26+
27+
#include <stdlib.h>
28+
#include <setjmp.h>
29+
#include <windows.h>
30+
31+
typedef HANDLE GHandle;
32+
typedef HANDLE GProcess;
33+
34+
#define __align(x) __attribute__((aligned(x)))
35+
36+
#endif

0 commit comments

Comments
 (0)