Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial port to wasm #118

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
src/[Mm]akefile
test/[Mm]akefile
examples/[Mm]akefile
examples/*.js
examples/*.wasm
test/dtest
test/itest

Expand Down
4 changes: 3 additions & 1 deletion examples/AnalysePlayBin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main()
char line[80];
bool match;

#if defined(__linux) || defined(__APPLE__)
#if defined(__linux) || defined(__APPLE__) || defined(__WASM__)
SetMaxThreads(0);
#endif

Expand Down Expand Up @@ -62,7 +62,9 @@ int main()

if (res != RETURN_NO_FAULT)
{
#ifndef __WASM__
ErrorMessage(res, line);
#endif
printf("DDS error: %s\n", line);
}

Expand Down
119 changes: 119 additions & 0 deletions examples/Makefiles/Makefile_wasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# This is a Makefile for the examples,
# for Mac and the clang compiler.

# It does assume a Unix-like setup for some commands,
# but if you only want to call "make" with the default target,
# you should be OK.

# If your compiler name is not given here, change it.
CC = em++

CC_FLAGS = -O3 -flto -mtune=generic

# These flags are not turned on by default, but DDS should pass them.
# Turn them on below.
WARN_FLAGS = \
-Wshadow \
-Wsign-conversion \
-pedantic -Wall -Wextra \
-Wcast-align -Wcast-qual \
-Wctor-dtor-privacy \
-Wdisabled-optimization \
-Winit-self \
-Wmissing-declarations \
-Wmissing-include-dirs \
-Wcomment \
-Wold-style-cast \
-Woverloaded-virtual \
-Wredundant-decls \
-Wsign-promo \
-Wstrict-overflow=1 \
-Wswitch-default -Wundef \
-Werror \
-Wno-unused \
-Wno-unknown-pragmas \
-Wno-long-long \
-Wno-format

# Here you can turn on warnings.
# CC_FULL_FLAGS = $(CC_FLAGS)
CC_FULL_FLAGS = $(CC_FLAGS) $(WARN_FLAGS) -D__WASM__

DLLBASE = dds
STATIC_LIB = lib$(DLLBASE).a

COMMON_SOURCE_FILES = \
hands.cpp

ALL_EXAMPLE_FILES = \
AnalysePlayBin.js \
AnalysePlayPBN \
AnalyseAllPlaysBin \
AnalyseAllPlaysPBN \
CalcDDtable.cpp \
CalcDDtablePBN.cpp \
CalcAllTables.cpp \
CalcAllTablesPBN.cpp \
DealerPar.cpp \
Par.cpp \
SolveBoard.cpp \
SolveBoardPBN.cpp \
SolveAllBoards.cpp

LIB_FLAGS = -L. $(STATIC_LIB)

OBJ_FILES = $(subst .cpp,.o,$(COMMON_SOURCE_FILES))
EX_OBJ_FILES = $(subst .cpp,.o,$(ALL_EXAMPLE_FILES))
EX_EXE_FILES = $(subst .cpp,,$(ALL_EXAMPLE_FILES))

AnalysePlayBin.html: $(OBJ_FILES) AnalysePlayBin.cpp $(STATIC_LIB)
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) AnalysePlayBin.cpp -o AnalysePlayBin.html

AnalysePlayPBN: $(OBJ_FILES) AnalysePlayPBN.o
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) AnalysePlayPBN.o -o AnalysePlayPBN

AnalyseAllPlaysBin: $(OBJ_FILES) AnalyseAllPlaysBin.o
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) AnalyseAllPlaysBin.o -o AnalyseAllPlaysBin

AnalyseAllPlaysPBN: $(OBJ_FILES) AnalyseAllPlaysPBN.o
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) AnalyseAllPlaysPBN.o -o AnalyseAllPlaysPBN

CalcDDtable: $(OBJ_FILES) CalcDDtable.o
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) CalcDDtable.o -o CalcDDtable

CalcDDtablePBN: $(OBJ_FILES) CalcDDtablePBN.o
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) CalcDDtablePBN.o -o CalcDDtablePBN

CalcAllTables: $(OBJ_FILES) CalcAllTables.o
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) CalcAllTables.o -o CalcAllTables

CalcAllTablesPBN: $(OBJ_FILES) CalcAllTablesPBN.o
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) CalcAllTablesPBN.o -o CalcAllTablesPBN

DealerPar: $(OBJ_FILES) DealerPar.o
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) DealerPar.o -o DealerPar

Par: $(OBJ_FILES) Par.o
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) Par.o -o Par

SolveBoard: $(OBJ_FILES) SolveBoard.cpp $(STATIC_LIB)
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) SolveBoard.cpp -o SolveBoard.html

dds: $(OBJ_FILES) dds.cpp $(STATIC_LIB)
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) dds.cpp -o dds.html

SolveBoardPBN: $(OBJ_FILES) SolveBoardPBN.o
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) SolveBoardPBN.o -o SolveBoardPBN.html

SolveAllBoards: $(OBJ_FILES) SolveAllBoards.cpp $(STATIC_LIB)
$(CC) $(CC_FULL_FLAGS) $(LD_FLAGS) $(LIB_FLAGS) $(OBJ_FILES) SolveAllBoards.cpp -o SolveAllBoards.html

%.o: %.cpp
$(CC) $(CC_FULL_FLAGS) -c $< -o $*.o

depend:
makedepend -Y -- $(cOMMON_SOURCE_FILES) $(ALL_EXAMPLE_FILES)

clean:
rm -f *.o $(EX_EXE_FILES) $(STATIC_LIB)

2 changes: 1 addition & 1 deletion examples/SolveBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main()
bool match2;
bool match3;

#if defined(__linux) || defined(__APPLE__)
#if defined(__linux) || defined(__APPLE__) || defined(__WASM__)
SetMaxThreads(0);
#endif

Expand Down
2 changes: 1 addition & 1 deletion examples/SolveBoardPBN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main()
bool match2,
match3;

#if defined(__linux) || defined(__APPLE__)
#if defined(__linux) || defined(__APPLE__) || defined(__WASM__)
SetMaxThreads(0);
#endif

Expand Down
148 changes: 148 additions & 0 deletions examples/dds.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
DDS, a bridge double dummy solver.

Copyright (C) 2006-2014 by Bo Haglund /
2014-2016 by Bo Haglund & Soren Hein.

See LICENSE and README.
*/

/*

S "N:QJ6.K652.J85.T98 873.J97.AT764.Q4 K5.T83.KQ9.A7652 AT942.AQ4.32.KJ3" xxxxxx
U "E:QJT5432.T.6.QJ82 .J97543.K7532.94 87.A62.QJT4.AT75 AK96.KQ8.A98.K63" xxxxxx
S "N:73.QJT.AQ54.T752 QT6.876.KJ9.AQ84 5.A95432.7632.K6 AKJ9842.K.T8.J93" xxxxxx

*/
// Test program for the SolveBoard function.
// Uses the hands pre-set in hands.cpp.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../include/dll.h"
#include "../src/PBN.h"
#include "hands.h"

/*
unsigned char dcardRank[16] =
{
'x', 'x', '2', '3', '4', '5', '6', '7',
'8', '9', 'T', 'J', 'Q', 'K', 'A', '-'
};

unsigned char dcardSuit[5] = { 'S', 'H', 'D', 'C', 'N' };
unsigned char dcardHand[4] = { 'N', 'E', 'S', 'W' };
*/
int convert_trump_or_seat(char tmpChar)
{
int rank = tmpChar - '0';
if (rank < 15 && rank > 1)
return rank;
if (tmpChar == 'N') // North
return 0;
else if (tmpChar == 'E')
return 1;
else if (tmpChar == 'S')
return 2;
else if (tmpChar == 'W')
return 3;
else if (tmpChar == 'C')
return 3;
else if (tmpChar == 'D')
return 2;
else if (tmpChar == 'H')
return 1;
else if (tmpChar == 'S')
return 0;
else if (tmpChar == 'U') // Trump
return 4;
else if (tmpChar == 'x') //
return 0;
else if (tmpChar == 'A') //
return 14;
else if (tmpChar == 'K') //
return 13;
else if (tmpChar == 'Q') //
return 12;
else if (tmpChar == 'J') //
return 11;
else if (tmpChar == 'T') //
return 10;
else
return -1;
}

/*
DDS, a bridge double dummy solver solveBoardPBN cli interface.
trump: 0-4 Clubs, Diamonds, Hearts, Spades, No Trump
currentTrickSuit: CDHS, AKQJT98765432
*/
int main(int argc, char *argv[])
{
if (argc < 4) {
dealPBN dlPBN;
printf("Usage: %s <board> argc=%d trump first[NESW]:dot_space_pbn currentTricks\n", argv[0], argc);
return 1;
}

deal dl;
futureTricks fut2, // solutions == 2
fut3; // solutions == 3

int target;
int solutions;
int mode;
int threadIndex = 0;
int res;
char line[80];
bool match2;
bool match3;

#if defined(__linux) || defined(__APPLE__) || defined(__WASM__)
SetMaxThreads(0);
#endif

for (int handno = 0; handno < 1; handno++)
{
dl.trump = convert_trump_or_seat(argv[1][0]);
dl.first = convert_trump_or_seat(argv[2][0]);
// xxxxxx means 0,0,0,0,0,0
dl.currentTrickSuit[0] = convert_trump_or_seat(argv[3][0]);
dl.currentTrickSuit[1] = convert_trump_or_seat(argv[3][2]);
dl.currentTrickSuit[2] = convert_trump_or_seat(argv[3][4]);

dl.currentTrickRank[0] = convert_trump_or_seat(argv[3][1]);
dl.currentTrickRank[1] = convert_trump_or_seat(argv[3][3]);
dl.currentTrickRank[2] = convert_trump_or_seat(argv[3][5]);

if (ConvertFromPBN(argv[2], dl.remainCards) != RETURN_NO_FAULT) {
return RETURN_PBN_FAULT;
}


target = -1;
mode = 0;
solutions = 2;
res = SolveBoard(dl, target, solutions, mode, &fut2, threadIndex);
if (res != RETURN_NO_FAULT)
{
ErrorMessage(res, line);
printf("DDS error: %s\n", line);
}
/*
sprintf(line,
"SolveBoard, hand %d: solutions 3 %s, solutions 2 %s\n",
handno + 1,
(match3 ? "OK" : "ERROR"),
(match2 ? "OK" : "ERROR"));

PrintHand(line, dl.remainCards);

sprintf(line, "solutions == 3\n");
PrintFut(line, &fut3);
sprintf(line, "solutions == 2\n");
*/
PrintFut(line, &fut2);
}
}
1 change: 1 addition & 0 deletions src/Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ void STDCALL SetResources(
else
thrMax = min(maxThreadsIn, ncores);

// printf("%d threads, %d MB memory\n", thrMax, memMaxMB);
// For simplicity we won't vary the amount of memory per thread
// in the small and large versions.

Expand Down
Loading