aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-11-20 12:32:11 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-11-20 12:32:11 +0800
commit55fb850402e59033911dd282162a5e10ce4fa9b1 (patch)
treeeeaef306120afeb25af83173545e7ac612e61960
parenta7f69a0857b1628576020a6532670a4f053098cf (diff)
downloadtangerine-mcl-55fb850402e59033911dd282162a5e10ce4fa9b1.tar.gz
tangerine-mcl-55fb850402e59033911dd282162a5e10ce4fa9b1.tar.zst
tangerine-mcl-55fb850402e59033911dd282162a5e10ce4fa9b1.zip
add wasm option(TBD)
-rw-r--r--Makefile7
-rw-r--r--docs/demo/she_c.wasmbin537177 -> 537163 bytes
-rw-r--r--src/gen.cpp62
-rw-r--r--src/low_func_llvm.hpp2
4 files changed, 66 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index 9a33922..3f5b151 100644
--- a/Makefile
+++ b/Makefile
@@ -140,6 +140,9 @@ $(OBJ_DIR)/$(CPU).bmi2.o: $(ASM_SRC_PATH_NAME).bmi2.s
src/base$(BIT).bmi2.ll: $(GEN_EXE)
$(GEN_EXE) $(GEN_EXE_OPT) -f $(FUNC_LIST) -s bmi2 > $@
+src/base64m.ll: $(GEN_EXE)
+ $(GEN_EXE) $(GEN_EXE_OPT) -wasm > $@
+
$(FUNC_LIST): $(LOW_ASM_SRC)
ifeq ($(USE_LOW_ASM),1)
$(shell awk '/global/ { print $$2}' $(LOW_ASM_SRC) > $(FUNC_LIST))
@@ -220,6 +223,10 @@ EMCC_OPT=-I./include -I./src -I../cybozulib/include
EMCC_OPT+=-O3 -DNDEBUG -DMCLBN_FP_UNIT_SIZE=4 -DMCL_MAX_BIT_SIZE=256 -DMCLSHE_WIN_SIZE=8
EMCC_OPT+=-s WASM=1 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s "EXPORTED_FUNCTIONS=[$(shell cat $(EXPORTED_TXT))]" --pre-js ffi/js/pre.js
JS_DEP=src/fp.cpp src/she_c256.cpp src/she_c_impl.hpp include/mcl/she.hpp $(EXPORTED_JSON) Makefile ffi/js/pre.js $(EXPORTED_TXT)
+ifeq ($(MCL_USE_LLVM),2)
+ EMCC_OPT+=src/base64m.ll -DMCL_USE_LLVM
+ JS_DEP+=src/base64m.ll
+endif
docs/demo/she_c.js: $(JS_DEP)
emcc -o $@ src/fp.cpp src/she_c256.cpp $(EMCC_OPT) -s "MODULARIZE=1"
diff --git a/docs/demo/she_c.wasm b/docs/demo/she_c.wasm
index ebfb5fb..cc4776f 100644
--- a/docs/demo/she_c.wasm
+++ b/docs/demo/she_c.wasm
Binary files differ
diff --git a/src/gen.cpp b/src/gen.cpp
index 189860f..763f64b 100644
--- a/src/gen.cpp
+++ b/src/gen.cpp
@@ -16,9 +16,11 @@ struct Code : public mcl::Generator {
uint32_t bit;
uint32_t N;
const StrSet *privateFuncList;
+ bool wasm;
std::string suf;
std::string unitStr;
Function mulUU;
+ Function mul32x32; // for WASM
Function extractHigh;
Function mulPos;
Function makeNIST_P192;
@@ -35,7 +37,7 @@ struct Code : public mcl::Generator {
FunctionMap mcl_fpDbl_sqrPreM;
FunctionMap mcl_fp_montM;
FunctionMap mcl_fp_montRedM;
- Code() : unit(0), unit2(0), bit(0), N(0), privateFuncList(0) { }
+ Code() : unit(0), unit2(0), bit(0), N(0), privateFuncList(0), wasm(false) { }
void verifyAndSetPrivate(Function& f)
{
if (privateFuncList && privateFuncList->find(f.name) != privateFuncList->end()) {
@@ -80,8 +82,49 @@ struct Code : public mcl::Generator {
}
return v;
}
+ void gen_mul32x32()
+ {
+ const int u = 32;
+ resetGlobalIdx();
+ Operand z(Int, u * 2);
+ Operand x(Int, u);
+ Operand y(Int, u);
+ mul32x32 = Function("mul32x32L", z, x, y);
+ mul32x32.setPrivate();
+ verifyAndSetPrivate(mul32x32);
+ beginFunc(mul32x32);
+
+ x = zext(x, u * 2);
+ y = zext(y, u * 2);
+ z = mul(x, y);
+ ret(z);
+ endFunc();
+ }
+ void gen_mul64x64(Operand& z, Operand& x, Operand& y)
+ {
+ Operand a = trunc(lshr(x, 32), 32);
+ Operand b = trunc(x, 32);
+ Operand c = trunc(lshr(y, 32), 32);
+ Operand d = trunc(y, 32);
+ Operand ad = call(mul32x32, a, d);
+ Operand bd = call(mul32x32, b, d);
+ bd = zext(bd, 96);
+ ad = shl(zext(ad, 96), 32);
+ ad = add(ad, bd);
+ Operand ac = call(mul32x32, a, c);
+ Operand bc = call(mul32x32, b, c);
+ bc = zext(bc, 96);
+ ac = shl(zext(ac, 96), 32);
+ ac = add(ac, bc);
+ ad = zext(ad, 128);
+ ac = shl(zext(ac, 128), 32);
+ z = add(ac, ad);
+ }
void gen_mulUU()
{
+ if (wasm) {
+ gen_mul32x32();
+ }
resetGlobalIdx();
Operand z(Int, unit2);
Operand x(Int, unit);
@@ -93,9 +136,13 @@ struct Code : public mcl::Generator {
verifyAndSetPrivate(mulUU);
beginFunc(mulUU);
- x = zext(x, unit2);
- y = zext(y, unit2);
- z = mul(x, y);
+ if (wasm) {
+ gen_mul64x64(z, x, y);
+ } else {
+ x = zext(x, unit2);
+ y = zext(y, unit2);
+ z = mul(x, y);
+ }
ret(z);
endFunc();
}
@@ -890,6 +937,9 @@ struct Code : public mcl::Generator {
{
this->suf = suf;
this->privateFuncList = &privateFuncList;
+#ifdef FOR_WASM
+ gen_mulUU();
+#else
gen_once();
uint32_t end = ((maxBitSize + unit - 1) / unit);
for (uint32_t n = 1; n <= end; n++) {
@@ -904,6 +954,7 @@ struct Code : public mcl::Generator {
gen_all();
}
}
+#endif
}
};
@@ -912,11 +963,13 @@ int main(int argc, char *argv[])
{
uint32_t unit;
bool oldLLVM;
+ bool wasm;
std::string suf;
std::string privateFile;
cybozu::Option opt;
opt.appendOpt(&unit, uint32_t(sizeof(void*)) * 8, "u", ": unit");
opt.appendBoolOpt(&oldLLVM, "old", ": old LLVM(before 3.8)");
+ opt.appendBoolOpt(&wasm, "wasm", ": for wasm");
opt.appendOpt(&suf, "", "s", ": suffix of function name");
opt.appendOpt(&privateFile, "", "f", ": private function list file");
opt.appendHelp("h");
@@ -936,6 +989,7 @@ int main(int argc, char *argv[])
if (oldLLVM) {
c.setOldLLVM();
}
+ c.wasm = wasm;
c.setUnit(unit);
uint32_t maxBitSize = MCL_MAX_BIT_SIZE;
c.gen(privateFuncList, maxBitSize, suf);
diff --git a/src/low_func_llvm.hpp b/src/low_func_llvm.hpp
index 104b7fa..8a44c22 100644
--- a/src/low_func_llvm.hpp
+++ b/src/low_func_llvm.hpp
@@ -41,7 +41,7 @@ template<>const void3u MontRed<n, tag>::f = &mcl_fp_montRed ## n ## suf; \
template<>const void4u DblAdd<n, tag>::f = &mcl_fpDbl_add ## n ## suf; \
template<>const void4u DblSub<n, tag>::f = &mcl_fpDbl_sub ## n ## suf; \
-#if CYBOZU_HOST == CYBOZU_HOST_INTEL
+#if (CYBOZU_HOST == CYBOZU_HOST_INTEL) && !defined(MCL_USE_VINT)
#define MCL_DEF_LLVM_FUNC(n) \
MCL_DEF_LLVM_FUNC2(n, Ltag, L) \
MCL_DEF_LLVM_FUNC2(n, LBMI2tag, Lbmi2)