-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 評価関数ファイルを読み込んで、C++の文字列literalに変換するスクリプト追加。
- ここで変換した.cppをやねうら王のプロジェクトに追加してビルドする。 - ビルドする時に、EVAL_EMBEDDINGをdefineしてビルドする。 - 一つ前のcommitでビルドできなくなっていたの修正。
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# 評価関数ファイルを読み込んで、C++の文字列literalに変換する。[2023/12/13] | ||
# ここで変換した.cppをやねうら王のプロジェクトに追加してビルドする。 | ||
# ビルドする時に、EVAL_EMBEDDINGをdefineしてビルドする。 | ||
|
||
def binary_to_cpp_literal(input_file:str, output_file:str): | ||
|
||
# バイナリファイルを読み込む | ||
with open(input_file, 'rb') as file: | ||
binary_data = file.read() | ||
|
||
# ファイルサイズを取得 | ||
file_size:int = len(binary_data) | ||
|
||
# 16進数の文字列に変換 | ||
hex_string = '\\x' + '\\x'.join(f'{byte:02x}' for byte in binary_data) | ||
|
||
with open(output_file, 'w') as file: | ||
file.write('#include <cstddef>\n') | ||
file.write('extern const char* gEmbeddedNNUEData;\n') | ||
file.write('extern const std::size_t gEmbeddedNNUESize;\n\n') | ||
file.write(f'const char* gEmbeddedNNUEData = "{hex_string}";\n') | ||
file.write(f'const std::size_t gEmbeddedNNUESize = {file_size};\n') | ||
|
||
def binary_to_cpp_literal2(input_file:str, output_file:str): | ||
|
||
# ファイルを開き、ヘッダと宣言を書き込む | ||
with open(output_file, 'w') as outfile: | ||
|
||
outfile.write('#include <cstddef>\n') | ||
outfile.write('extern const char* gEmbeddedNNUEData;\n') | ||
outfile.write('extern const std::size_t gEmbeddedNNUESize;\n\n') | ||
|
||
outfile.write(f'const char* gEmbeddedNNUEData = "') | ||
|
||
# バイナリファイルを開いて、ストリームで読み込みながら処理 | ||
file_size = 0 | ||
with open(input_file, 'rb') as infile: | ||
while True: | ||
chunk = infile.read(1024) # 1024バイトずつ読み込む | ||
if not chunk: | ||
break | ||
# 16進数の文字列に変換して出力 | ||
hex_string = '\\x' + '\\x'.join(f'{byte:02x}' for byte in chunk) | ||
outfile.write(hex_string) | ||
file_size += len(chunk) | ||
|
||
outfile.write('";\n') | ||
outfile.write(f'const std::size_t gEmbeddedNNUESize = {file_size};\n') | ||
|
||
# 例:'nn.bin'を読み込み、'nnue_bin.cpp'に出力 | ||
binary_to_cpp_literal2('nn.bin', 'nnue_bin.cpp') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters