Skip to content

Commit 47d20f9

Browse files
authored
Merge pull request #8 from Klamath233/master
Ported to M1 Mac (ARM)
2 parents 20ac273 + 467e991 commit 47d20f9

10 files changed

+212
-15
lines changed

I_GGX64/3/3对2/1车1马1兵对1车1马.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void my_m_MT_R_1che1ma1pawn_B_1che1ma(typePOS &POSITION, EvalInfo &ei){
197197
}
198198
}
199199
// fen 2b1k4/4a4/4ba3/9/9/1p3r3/1nR6/5N3/4A4/2B1KA3 w - - 230 230
200-
if(PB90(mma MY_ADD 9) == my_pawn){
200+
if(((unsigned int) (mma MY_ADD 9) < 90) && PB90(mma MY_ADD 9) == my_pawn){
201201
if(PB90(MY_SQ1C) == my_ma && PB90(MY_SQ1D) == your_che){
202202
RETRUN_MUL(4);
203203
}

I_GGX64/7/chess.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
#ifdef USE_CYCLONE_UCI
6161
#define NOT_OUT_UPBOND_OR_LOW_BAND
6262
#else
63-
#define CAN_OUT_EVAL_INFO
63+
// #define CAN_OUT_EVAL_INFO
6464
#endif
6565

6666

I_GGX64/bitboard.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,32 @@
7070

7171
Bitboard BetweenBB[90][90]; // 二个在同一线上的棋子之间的位棋盘.不包括自己的信息.
7272

73+
// void print_bb(Bitboard bb){
74+
// printf("%s\n", "=========");
75+
// for (int row = 0; row < 10; row++) {
76+
// for (int col = 0; col < 9; col++) {
77+
// int bit = row * 9 + col;
78+
// uint64_t occupied;
79+
// if (bit > 63) {
80+
// occupied = (bb[1] >> (bit - 64)) & 1;
81+
// } else {
82+
// occupied = (bb[0] >> bit) & 1;
83+
// }
84+
85+
// if (occupied) {
86+
// printf("%s", "*");
87+
// } else {
88+
// printf("%s", "o");
89+
// }
90+
// }
91+
// printf("%s\n", "");
92+
// if (row == 4) {
93+
// printf("%s\n", "---------");
94+
// }
95+
// }
96+
// printf("%s\n", "=========");
97+
// }
98+
7399
// const char BB_BDISPLAY_CHESS[3] = {"●"};
74100

75101
//将位棋盘打印出来

I_GGX64/bitboard.h

+149-10
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,27 @@ extern uint64 XIANG_Mult[90];
228228

229229
#endif
230230

231+
#if __arm64__
232+
#define m128_from_2u64(data0,data1) (vcombine_u64(vcreate_u64(data1), vcreate_u64(data0)))
231233

234+
#define m_not(bb) vandq_u64(~bb, ALL_B90)
235+
236+
#define m_xor(xmm1,xmm2) veorq_u64(xmm1, xmm2)
237+
238+
#define m_and(xmm1,xmm2) vandq_u64(xmm1, xmm2)
239+
240+
#define m_or(xmm1,xmm2) vorrq_u64(xmm1, xmm2)
241+
242+
243+
#define BB_u64(bb,i) vgetq_lane_u64(bb, i)
244+
#define BB_i64(bb,i) vgetq_lane_s64(bb, i)
245+
246+
#define set_mask_bb(s) SetMaskBB[s]
247+
#define clear_mask_bb(s) ClearMaskBB[s]
248+
249+
250+
#define M128_get_Or64(bb) (BB_u64(bb, 1) | BB_u64(bb, 0))
251+
#else
232252
#define m128_from_2u64(bb,data0,data1) (bb = _mm_set_epi64x(data0,data1))
233253

234254
#define m_not(bb) _mm_andnot_si128(bb,ALL_B90)
@@ -248,6 +268,7 @@ extern uint64 XIANG_Mult[90];
248268

249269

250270
#define M128_get_Or64(bb) (_mm_extract_epi64(bb,1) | _mm_extract_epi64(bb,0))
271+
#endif
251272
//////////////////////////////////////////////////////////////////////////
252273
#define transform_bbm(bb,magic64,bits) ((M128_get_Or64(bb) * (magic64)) >> (bits))
253274
#define transform_mul(bb,magic64,bits) ((M128_get_Or64(bb) * (magic64)) >> (64 - (bits)))
@@ -264,7 +285,16 @@ extern uint64 XIANG_Mult[90];
264285
#define transform_bba(bb,magic64,bits) ((M128_get_Or64(bb) * (magic64)) >> (bits));
265286
#define get_transfrom_u64(bb,magic64) (M128_get_Or64(bb) * (magic64))
266287

288+
#ifdef __arm64__
289+
__inline int count_1s(Bitboard bb){
290+
return (int) vaddvq_u8(vcntq_u8(bb));
291+
}
267292

293+
__inline int count_1s(Bitboard b1, Bitboard b2){
294+
Bitboard bb = m_and(b1, b2);
295+
return (int) vaddvq_u8(vcntq_u8(bb));
296+
}
297+
#else
268298
__inline int count_1s(Bitboard bb){
269299
return (_mm_popcnt_u64(_mm_extract_epi64(bb, 0)) + _mm_popcnt_u64(_mm_extract_epi64(bb, 1)));
270300
}
@@ -273,9 +303,117 @@ __inline int count_1s(Bitboard b1, Bitboard b2){
273303
Bitboard bb = m_and(b1, b2);
274304
return (_mm_popcnt_u64(_mm_extract_epi64(bb, 0)) + _mm_popcnt_u64(_mm_extract_epi64(bb, 1)));
275305
}
306+
#endif
276307

277308

278309
//__
310+
#ifdef __arm64__
311+
312+
__inline uint64 m_have_bit(Bitboard bb) {
313+
return (BB_u64(bb, 0) | BB_u64(bb, 1));
314+
}
315+
316+
FORCE_INLINE bool m128_is_same(Bitboard &b1, Bitboard &b2) {
317+
uint64x2_t cmp = vceqq_u64(b1, b2);
318+
return (BB_u64(cmp, 0) == 0xFFFFFFFFFFFFFFFF) && (BB_u64(cmp, 1) == 0xFFFFFFFFFFFFFFFF);
319+
}
320+
321+
__inline uint64 have_bit(Bitboard b1, Bitboard b2){
322+
Bitboard bb = vandq_u64(b1, b2);
323+
return (BB_u64(bb, 0) | BB_u64(bb, 1));
324+
}
325+
326+
__inline uint64 bit_is_set(Bitboard bb, Square sq){
327+
return ((BB_u64(bb, 0) & BB_u64(SetMaskBB[sq], 0)) | (BB_u64(bb, 1) & BB_u64(SetMaskBB[sq], 1)));
328+
}
329+
330+
#define set_bit(bb,sq) (bb = vorrq_u64(bb,SetMaskBB[sq]))
331+
#define clear_bit(bb,sq) (bb = vandq_u64(bb,ClearMaskBB[sq]))
332+
333+
FORCE_INLINE bool pop_1st_bit_sq(Bitboard &b, Square &sq) {
334+
335+
uint64x2_t board_empty_v = vceqzq_u64(b);
336+
uint64_t board_empty = board_empty_v[0] & board_empty_v[1];
337+
338+
if (!!board_empty) {
339+
return false;
340+
}
341+
342+
uint64_t lo;
343+
uint64_t hi;
344+
unsigned long index;
345+
346+
lo = BB_u64(b, 0);
347+
348+
// Count leading zeros of the reverse of lo
349+
index = __builtin_clzll(__builtin_arm_rbit64(lo));
350+
351+
if (index < 64) {
352+
// First bit is in the bottom 64 bits
353+
// Clear the bit to be popped
354+
lo = lo & (lo - 1);
355+
b[0] = lo;
356+
} else {
357+
hi = BB_u64(b, 1);
358+
index = 64 + __builtin_clzll(__builtin_arm_rbit64(hi));
359+
360+
hi = hi & (hi - 1);
361+
b[1] = hi;
362+
}
363+
364+
sq = (Square) index;
365+
return true;
366+
}
367+
368+
FORCE_INLINE Square pop_1st_bit_sq(Bitboard &b) {
369+
Square sq;
370+
371+
pop_1st_bit_sq(b, sq);
372+
373+
return sq;
374+
}
375+
376+
#define m_Lsf(bb,count) {\
377+
Bitboard sltmp; \
378+
sltmp = vcombine_u64(vcreate_u64(0), vcreate_u64(vgetq_lane_u64(bb, 0)));\
379+
sltmp = vshrq_n_u64(sltmp, 64-(count));\
380+
bb = vshlq_n_u64(bb,count);\
381+
bb = vorrq_u64(bb,sltmp);\
382+
}
383+
384+
#define m_Rsf(bb,count) {\
385+
Bitboard sltmp; \
386+
sltmp = vcombine_u64(vcreate_u64(vgetq_lane_u64(bb, 1)), vcreate_u64(0));\
387+
sltmp = vshlq_n_u64(sltmp,64-(count));\
388+
bb = vshrq_n_u64(bb,count);\
389+
bb = vorrq_u64(bb,sltmp);\
390+
}
391+
392+
inline uint32 msb(uint64 b) {
393+
return 63 - __builtin_clzll(b);
394+
}
395+
396+
__inline Square first_1(Bitboard b){
397+
uint64_t lo;
398+
uint64_t hi;
399+
unsigned long index;
400+
401+
lo = BB_u64(b, 0);
402+
403+
// Count leading zeros of the reverse of lo
404+
index = __builtin_clzll(__builtin_arm_rbit64(lo));
405+
406+
if (index < 64) {
407+
// First bit is in the bottom 64 bits
408+
} else {
409+
hi = BB_u64(b, 1);
410+
index = 64 + __builtin_clzll(__builtin_arm_rbit64(hi));
411+
}
412+
413+
return (Square) index;
414+
}
415+
416+
#else
279417

280418
#define USE_SSE_BIT_OPERATION
281419

@@ -496,16 +634,6 @@ inline uint32 msb(uint64 b) {
496634
return index;
497635
}
498636

499-
500-
#define one_rpawn_rk_attacks(sq) OneRpawnOrRking_AttackBB[sq]
501-
#define one_bpawn_bk_attacks(sq) OneBpawnOrBking_AttackBB[sq]
502-
503-
#define attacks_by_rpawn_rk(sq) Attack_By_Rpawn_Rking[sq]
504-
#define attacks_by_bpawn_bk(sq) Attack_By_Bpawn_Bking[sq]
505-
506-
#define shi_attacks(sq) ShiAttackBB[sq]
507-
508-
509637
__inline Square first_1(Bitboard b){
510638
unsigned long index;
511639
if(_mm_extract_epi64(b,0)){
@@ -518,6 +646,17 @@ __inline Square first_1(Bitboard b){
518646
return (Square)index;
519647
}
520648

649+
#endif
650+
651+
652+
#define one_rpawn_rk_attacks(sq) OneRpawnOrRking_AttackBB[sq]
653+
#define one_bpawn_bk_attacks(sq) OneBpawnOrBking_AttackBB[sq]
654+
655+
#define attacks_by_rpawn_rk(sq) Attack_By_Rpawn_Rking[sq]
656+
#define attacks_by_bpawn_bk(sq) Attack_By_Bpawn_Bking[sq]
657+
658+
#define shi_attacks(sq) ShiAttackBB[sq]
659+
521660
/// squares_between returns a bitboard representing all squares between
522661
/// two squares. For instance, squares_between(SQ_C4, SQ_F7) returns a
523662
/// bitboard with the bits for square d5 and e6 set. If s1 and s2 are not

I_GGX64/eval_total.h

+7
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@
88
#include "thread.h"
99
#include "uci.h"
1010
#include "eval_const.h"
11+
12+
#pragma once
13+
14+
template<Color Us> Score eval_king_up_one_string(Position& pos, EvalInfo& ei);
15+
template<Color Us> int PawnOverRiver(Position& pos, EvalInfo& ei);
16+
template<Color Us> Score eval_zhou_and_moveless(Position& pos, EvalInfo& ei,
17+
Square mid, PieceType XPiece, Square xPos);

I_GGX64/platform.h

+17
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,25 @@ typedef unsigned int UINT;
8181
typedef uint64 Key;
8282

8383
#define XMM_ALIGN __attribute__((aligned(16)))
84+
85+
#if __arm64__ && __ARM_NEON__
86+
#include <arm_neon.h>
87+
typedef uint64x2_t __m128i;
88+
89+
#define Bitboard uint64x2_t
90+
91+
// Translation of some xmm functions used
92+
#define _mm_andnot_si128(a, b) vandq_u64(~a, b)
93+
#define _mm_and_si128(a, b) vandq_u64(a, b)
94+
#define _mm_or_si128(a, b) vorrq_u64(a, b)
95+
#define _mm_setzero_si128() vcombine_u64(vcreate_u64(0), vcreate_u64(0))
96+
#define _mm_load_si128(vp) (*vp)
97+
#define _mm_setr_epi32(e3, e2, e1, e0) vsetq_lane_u32(e3, vsetq_lane_u32(e2, vsetq_lane_u32(e1, vsetq_lane_u32(e0, vcombine_u64(vcreate_u64(0), vcreate_u64(0)), 3), 2), 1), 0)
98+
#define _mm_set1_epi32(a) vdupq_n_u32(a)
99+
#else
84100
#include <immintrin.h>
85101
#define Bitboard __m128i
102+
#endif
86103
#define TRUE 1
87104
#define FALSE 0
88105

I_GGX64/pos_test.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ bool Position::pos_is_ok(int* failedStep) {
293293
for(Piece p = RKING; p <= BPAWN; p++){
294294
if(p == EMPTY || p == _X_X) continue;
295295
if(pieceCount[p] != (int)count_1s(pieces(p))){
296+
std::cout << pieceCount[p] << " and " << count_1s(pieces(p)) << "mismatch" << std::endl;
296297
return false;
297298
}
298299
//for(int i = 0; i < pos.piece_count(RPAO + delt); i++){

I_GGX64/position.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct StateInfo {
6666
};
6767

6868
// In a std::deque references to elements are unaffected upon resizing
69-
typedef std::unique_ptr<std::deque<StateInfo>> StateListPtr;
69+
typedef std::unique_ptr<std::deque<StateInfo> > StateListPtr;
7070

7171
/// 常捉判断结构
7272
/// 共有多少个新增吃子步. 最多16个吃子步?

I_GGX64/stock_fish_data.h

+4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ static const uint64 XMM_ALIGN BpawnOverBB[2] = { 0xffffe00000000000,0x000000
2424

2525
static const uint64 XMM_ALIGN WHOLE_BITBOARD90[2] = {0xffffffffffffffff,0x03ffffff}; //所有90格棋子
2626

27+
#ifdef __arm64__
28+
#define ALL_B90 m128_from_2u64(WHOLE_BITBOARD90[1], WHOLE_BITBOARD90[0])
29+
#else
2730
#define ALL_B90 _mm_load_si128((__m128i*)(WHOLE_BITBOARD90))
31+
#endif
2832

2933
static const uint64 XMM_ALIGN TiMenBit_Pawn[2] = { 0x0000000000a0d844,0x0000000000886c14};
3034

Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ CC := clang
33
LD := ld
44

55
SRC_DIR := I_GGX64
6+
ARCH ?= x86_64
67

78
INCLUDE_PATHS += -I$(PWD)/I_GGX64 -I$(PWD)/endgame
8-
COMMON_DEFS += $(INCLUDE_PATHS)
9-
CCFLAGS += -stdlib=libc++ -Wno-shift-negative-value -msse -msse2 -msse3 -msse4.1 -msse4.2 $(COMMON_DEFS)
9+
COMMON_DEFS += $(INCLUDE_PATHS) -std=c++11 -O3
10+
CCFLAGS-x86_64 = -msse -msse2 -msse3 -msse4.1 -msse4.2
11+
CCFLAGS-arm64 = -arch arm64
12+
CCFLAGS += -stdlib=libc++ -Wno-shift-negative-value -Wno-c++11-extensions -Wno-switch $(CCFLAGS-$(ARCH)) $(COMMON_DEFS)
1013
LDFLAGS += -lc++ -lpthread
1114

1215
objs :=

0 commit comments

Comments
 (0)