├── make ├── b.h ├── a.h ├── b.cpp ├── a.cpp ├── main.cpp ├── Makefile └── depend.md ├── dll ├── bin │ └── .emptydir ├── sub.cpp ├── main.cpp ├── mk.bat └── gen.py ├── go ├── share │ ├── lib │ │ └── .emptydir │ ├── test.cpp │ ├── add.cpp │ └── Makefile ├── wasm │ ├── cgo │ │ ├── lib.h │ │ ├── lib.c │ │ ├── main.go │ │ └── Makefile │ └── go │ │ ├── main.go │ │ └── Makefile ├── pkg-test.pc ├── cstr │ ├── main.cpp │ ├── cstr.go │ ├── cstr.cpp │ └── Makefile ├── thread │ ├── t.go │ ├── main.cpp │ ├── Makefile │ └── wait.cpp ├── callback │ ├── lib.h │ ├── Makefile │ ├── lib.c │ ├── sub.go │ ├── callback.go │ └── main.go ├── go-in-c │ ├── Makefile │ ├── libtest.c │ └── lib.c ├── channel.go ├── rand.go ├── seccon2013 │ ├── run.py │ ├── exec.cpp │ └── crypt300.cpp ├── rand │ └── rand_test.go ├── pkg-test.go ├── finalizer.go └── unsafe.go ├── depend ├── c.h ├── a.cpp ├── a.h ├── b.h ├── b.cpp ├── m.cpp └── Makefile ├── longint ├── .gitignore ├── bitint_x64.cpp ├── mcl.cpp └── sum.cpp ├── motoko ├── test │ ├── package-set.dhall │ ├── Makefile │ └── vessel.dhall ├── vessel.dhall ├── Makefile └── package-set.dhall ├── const_div ├── readme.md ├── mk.bat └── constdiv.py ├── map └── leaf │ ├── readme.md │ └── map.html ├── mulrev.cpp ├── rust ├── mcl │ ├── mcl-sys │ │ ├── wrapper.h │ │ ├── Cargo.toml │ │ └── build.rs │ └── Cargo.toml └── thread │ └── t.rs ├── split ├── inst.cpp ├── t.cpp ├── t.h ├── impl.h └── Makefile ├── webpack ├── test1 │ ├── Makefile │ ├── src │ │ ├── index.js │ │ └── func.js │ ├── config.js │ └── package.json └── test2 │ ├── Makefile │ ├── src │ ├── test.js │ ├── index.js │ ├── add.cpp │ ├── add.js │ └── Makefile │ ├── test-webpack.js │ ├── demo.js │ ├── index.html │ ├── webpack.config.js │ └── package.json ├── mfd ├── chap4.pdf └── chap5.pdf ├── sanitize ├── ignorelist.txt ├── Makefile └── strlen_err.cpp ├── debug └── aarch64 │ ├── func.s │ ├── Makefile │ ├── test.cpp │ └── script.txt ├── precompile ├── t.cpp ├── inc │ └── t.h └── Makefile ├── security ├── hpke.md ├── L1FT │ ├── figure1.png │ └── figure4-5.png ├── TLBleed │ ├── ecc.png │ ├── svm.png │ ├── figure7.png │ └── table1.png ├── lazyfp │ └── lazyfp.pptx ├── cryptorec │ ├── img │ │ ├── guideline.png │ │ ├── 1-rtt.txt │ │ ├── tls1.2.txt │ │ ├── 0-rtt.txt │ │ └── tls1.3.txt │ └── Makefile └── montgomery.md ├── node ├── wasm2 │ ├── add.cpp │ ├── index.js │ ├── index2.js │ ├── Makefile │ └── add2.js └── wasm3 │ ├── sub.c │ ├── add.cpp │ ├── index.js │ └── Makefile ├── wasm ├── sample │ ├── src │ │ ├── index.ts │ │ ├── sample.cpp │ │ └── sample.ts │ ├── test │ │ └── test.js │ ├── tsconfig.json │ ├── Makefile │ └── package.json ├── exception │ ├── index.js │ ├── exception.js │ ├── test.js │ ├── exception.cpp │ └── Makefile └── Makefile ├── slow-pti ├── RunCmd.class ├── runcmd.py └── loop.cpp ├── text2sound ├── sample.txt ├── readme.md └── text2sound.js ├── bbs ├── wasm │ ├── dep.cpp │ ├── index.ts │ └── getRandomValues.ts ├── browser │ └── package.json ├── tsconfig.json └── webpack.demo.config.js ├── broadcast ├── mk.bat ├── t.cpp └── test.asm ├── cpp ├── img │ └── dpcpp │ │ ├── fig13-2.png │ │ └── fig13-3.png ├── throw_in_c │ ├── s.c │ ├── v.c │ ├── w.cpp │ ├── t.cpp │ ├── u.cpp │ └── Makefile ├── sample │ ├── inline_var.hpp │ ├── over-aligned-new.cpp │ ├── inline_var2.cpp │ ├── parallel.cpp │ ├── initializer_list.cpp │ ├── inline_var1.cpp │ ├── template_auto.cpp │ ├── template_argument_deduction.cpp │ ├── constexpr_if.cpp │ ├── structued_binding.cpp │ └── fold.cpp ├── extint.cpp ├── item22-1-main.cpp └── std_function_gcc_bug.cpp ├── double-link ├── lib.c ├── test.c └── Makefile ├── react ├── test2 │ ├── .babelrc │ ├── readme.md │ ├── public │ │ └── index.html │ ├── webpack.config.js │ ├── src │ │ └── index.js │ └── package.json └── test1 │ ├── public │ └── index.html │ └── src │ └── index.js ├── extint ├── exti_test.cpp ├── wasm │ ├── api.h │ ├── sub.cpp │ ├── mul128.ll │ ├── carry.cpp │ └── multi3.ll ├── mk-test.bat ├── add256.cpp ├── bitint_if.hpp ├── uint128.cpp ├── bitint_if.cpp ├── ladd256.ll ├── lmul256.ll └── mul256.cpp ├── hpc ├── run.sh └── fmath.hpp ├── dlopen ├── sub2.c ├── sub3.c ├── sub.c ├── main.c ├── main-dl.c ├── main2.c └── readme.md ├── verify_stack_frame.hpp ├── fma ├── readme.md ├── Makefile ├── fma_test.cpp └── gen-fma.py ├── mapto ├── misc.txt ├── Makefile └── HashAndMap_test.go ├── large-file-lfs.txt ├── shared ├── t.c ├── b.c ├── Makefile ├── mymalloc.h └── a.c ├── whole-archive ├── main.cpp ├── init_f.cpp ├── sub.cpp └── Makefile ├── xbyak_fixed ├── t2.cpp ├── t4.cpp ├── mk.bat ├── Makefile └── t1.cpp ├── swig ├── test.i ├── set-java-path.bat ├── run-test.bat ├── byte-array │ ├── sample │ │ ├── SampleJNI.java │ │ └── Sample.java │ ├── SampleTest.java │ ├── sample.i │ └── Makefile ├── test.hpp ├── make_wrap.bat ├── TestTest.java └── common.mk ├── avx-512 ├── mk.bat ├── Makefile ├── util.hpp ├── mask-load.cpp └── ifma-test.cpp ├── crypto └── fft │ └── Makefile ├── gf256 ├── mk.bat ├── Makefile └── gen.cpp ├── slow_sin_test.bat ├── python ├── ecc │ ├── Makefile │ ├── ecdh.py │ └── org │ │ └── ecdsa-test.py ├── test.html └── test.js ├── llvm ├── Makefile ├── lp-test.c └── gen_lp.py ├── s_xbyak ├── run.bat └── Makefile ├── check_stack ├── mk.bat ├── t.cpp └── f.asm ├── static-var ├── s.cpp ├── t.cpp ├── Makefile └── var.hpp ├── xbyak_split ├── t1.cpp ├── run.sh ├── t2.cpp ├── t3.cpp └── t4.cpp ├── static-cstr ├── common.h ├── vc-test.bat ├── Makefile ├── sub1.cpp ├── sub2.cpp └── main.cpp ├── rop ├── Makefile ├── test-x64.cpp └── test-aarch64.cpp ├── sha-sample.cpp ├── unicode-file.cpp ├── extract-text.sh ├── uint128.cpp ├── stack └── getstack.cpp ├── sve ├── t.cpp ├── floatformat.hpp ├── faddv.cpp ├── low_func.s ├── fexpa.hpp ├── call.cpp ├── Makefile ├── direct-jmp.cpp ├── perm.cpp ├── ff.cpp ├── softmax.cpp ├── gather-test.cpp ├── low_func.h └── simple-inv.cpp ├── calling.cpp ├── emb ├── t.cpp └── test.sh ├── softmax └── t.py ├── fft └── Makefile ├── dev └── dead-lock.cpp ├── nop.asm ├── naf.py ├── netspeed └── Makefile ├── atomic ├── atomic.cpp └── readme.md ├── hmac.cpp ├── neon ├── add.cpp └── Makefile ├── zig └── run.sh ├── strlen_test.cpp ├── kakko └── fac.c ├── malloc-free-openmp.cpp ├── inv └── Makefile ├── fib.cpp ├── omp └── custom.cpp ├── rc.cpp ├── vinsertf128.cpp ├── addnc.cpp ├── codec-calc.cpp ├── safegcd └── Makefile ├── call_user.c ├── dec-sub.cpp ├── local-label.txt ├── tfns └── Makefile ├── combination2.cpp ├── mul128.cpp ├── slow_sin.cpp ├── vc_iterator.cpp ├── signal_test.c ├── montgomery.cpp ├── unsigned_mul.cpp ├── thread-local.cpp ├── slow_sin2.cpp ├── inline └── inline_call.cpp ├── ecdsa-test.py ├── memory-speed.cpp ├── peterson.cpp ├── codec-calc-ans.cpp ├── p1.cpp ├── sma_test.cpp ├── crash-gdb.cpp ├── vzeroupper.cpp ├── hash └── MurmurHash3.h ├── extract-jpeg.py └── pptx2pdf.vbs /make/b.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dll/bin/.emptydir: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /go/share/lib/.emptydir: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /make/a.h: -------------------------------------------------------------------------------- 1 | #include "b.h" 2 | -------------------------------------------------------------------------------- /depend/c.h: -------------------------------------------------------------------------------- 1 | #include 2 | -------------------------------------------------------------------------------- /make/b.cpp: -------------------------------------------------------------------------------- 1 | #include "b.h" 2 | -------------------------------------------------------------------------------- /depend/a.cpp: -------------------------------------------------------------------------------- 1 | #include "a.h" 2 | 3 | -------------------------------------------------------------------------------- /depend/a.h: -------------------------------------------------------------------------------- 1 | #include "b.h" 2 | 3 | -------------------------------------------------------------------------------- /depend/b.h: -------------------------------------------------------------------------------- 1 | #include "c.h" 2 | 3 | -------------------------------------------------------------------------------- /make/a.cpp: -------------------------------------------------------------------------------- 1 | #include "a.h" 2 | #include "b.h" 3 | -------------------------------------------------------------------------------- /depend/b.cpp: -------------------------------------------------------------------------------- 1 | #include "c.h" 2 | #include "b.h" 3 | -------------------------------------------------------------------------------- /longint/.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | *.d 3 | *.o 4 | *.a 5 | -------------------------------------------------------------------------------- /motoko/test/package-set.dhall: -------------------------------------------------------------------------------- 1 | ../package-set.dhall 2 | -------------------------------------------------------------------------------- /depend/m.cpp: -------------------------------------------------------------------------------- 1 | #include "b.h" 2 | 3 | int main() 4 | { 5 | } 6 | -------------------------------------------------------------------------------- /go/wasm/cgo/lib.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | int add(int a, int b); 3 | -------------------------------------------------------------------------------- /const_div/readme.md: -------------------------------------------------------------------------------- 1 | moved to https://github.com/herumi/constdiv 2 | -------------------------------------------------------------------------------- /map/leaf/readme.md: -------------------------------------------------------------------------------- 1 | https://ktgis.net/service/leafletlearn/index.html -------------------------------------------------------------------------------- /mulrev.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/mulrev.cpp -------------------------------------------------------------------------------- /rust/mcl/mcl-sys/wrapper.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | -------------------------------------------------------------------------------- /split/inst.cpp: -------------------------------------------------------------------------------- 1 | #define PRECOMPILE_HEADER 2 | #include "t.h" 3 | 4 | -------------------------------------------------------------------------------- /webpack/test1/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | npx webpack --config config.js 3 | -------------------------------------------------------------------------------- /webpack/test2/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | npx webpack #--config config.js 3 | -------------------------------------------------------------------------------- /mfd/chap4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/mfd/chap4.pdf -------------------------------------------------------------------------------- /mfd/chap5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/mfd/chap5.pdf -------------------------------------------------------------------------------- /sanitize/ignorelist.txt: -------------------------------------------------------------------------------- 1 | fun:*_M_clear* 2 | fun:__interceptor_strlen 3 | -------------------------------------------------------------------------------- /debug/aarch64/func.s: -------------------------------------------------------------------------------- 1 | .global func 2 | func: 3 | adds w0, w0, w1 4 | ret 5 | 6 | -------------------------------------------------------------------------------- /precompile/t.cpp: -------------------------------------------------------------------------------- 1 | #include "t.h" 2 | 3 | int main() 4 | { 5 | f(5); 6 | } 7 | -------------------------------------------------------------------------------- /security/hpke.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/security/hpke.md -------------------------------------------------------------------------------- /webpack/test1/src/index.js: -------------------------------------------------------------------------------- 1 | import { put } from './func' 2 | 3 | put(123) 4 | 5 | -------------------------------------------------------------------------------- /make/main.cpp: -------------------------------------------------------------------------------- 1 | #include "a.h" 2 | 3 | int main() 4 | { 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /node/wasm2/add.cpp: -------------------------------------------------------------------------------- 1 | extern "C" int add(int x, int y) 2 | { 3 | return x + y; 4 | } 5 | -------------------------------------------------------------------------------- /wasm/sample/src/index.ts: -------------------------------------------------------------------------------- 1 | export { initializeSample as init } from './sample' 2 | 3 | -------------------------------------------------------------------------------- /go/pkg-test.pc: -------------------------------------------------------------------------------- 1 | Name: pkg-test 2 | Description: test 3 | Version: 1.0 4 | Cflags: -DAAA=5 5 | -------------------------------------------------------------------------------- /slow-pti/RunCmd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/slow-pti/RunCmd.class -------------------------------------------------------------------------------- /split/t.cpp: -------------------------------------------------------------------------------- 1 | #include "t.h" 2 | 3 | int main() 4 | { 5 | X x; 6 | x.put(); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /text2sound/sample.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/text2sound/sample.txt -------------------------------------------------------------------------------- /bbs/wasm/dep.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | EM_JS_DEPS(deps, "$stackSave,$stackAlloc"); 4 | -------------------------------------------------------------------------------- /broadcast/mk.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | nasm -fwin64 test.asm 3 | cl t.cpp test.obj 4 | sde -- t.exe 5 | -------------------------------------------------------------------------------- /cpp/img/dpcpp/fig13-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/cpp/img/dpcpp/fig13-2.png -------------------------------------------------------------------------------- /cpp/img/dpcpp/fig13-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/cpp/img/dpcpp/fig13-3.png -------------------------------------------------------------------------------- /security/L1FT/figure1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/security/L1FT/figure1.png -------------------------------------------------------------------------------- /security/TLBleed/ecc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/security/TLBleed/ecc.png -------------------------------------------------------------------------------- /security/TLBleed/svm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/security/TLBleed/svm.png -------------------------------------------------------------------------------- /double-link/lib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void put() 4 | { 5 | printf("LIB=%d\n", LIB); 6 | } 7 | -------------------------------------------------------------------------------- /go/wasm/cgo/lib.c: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | 3 | int add(int a, int b) 4 | { 5 | return a + b; 6 | } 7 | -------------------------------------------------------------------------------- /security/L1FT/figure4-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/security/L1FT/figure4-5.png -------------------------------------------------------------------------------- /security/TLBleed/figure7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/security/TLBleed/figure7.png -------------------------------------------------------------------------------- /security/TLBleed/table1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/security/TLBleed/table1.png -------------------------------------------------------------------------------- /security/lazyfp/lazyfp.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/security/lazyfp/lazyfp.pptx -------------------------------------------------------------------------------- /bbs/wasm/index.ts: -------------------------------------------------------------------------------- 1 | export { default as getRandomValues } from './getRandomValues' 2 | export * from './bbs' 3 | -------------------------------------------------------------------------------- /motoko/vessel.dhall: -------------------------------------------------------------------------------- 1 | { 2 | dependencies = [ "base", "iterext" ], 3 | compiler = Some "0.6.18" 4 | } 5 | -------------------------------------------------------------------------------- /react/test2/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "@babel/preset-env", 4 | "@babel/preset-react" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /wasm/sample/src/sample.cpp: -------------------------------------------------------------------------------- 1 | extern "C" __attribute__((used)) int func(int x) 2 | { 3 | return x + 10000; 4 | } 5 | -------------------------------------------------------------------------------- /extint/exti_test.cpp: -------------------------------------------------------------------------------- 1 | #include "exti.hpp" 2 | 3 | int main() 4 | { 5 | exti256_t x; 6 | x = 123; 7 | dump(x); 8 | } 9 | -------------------------------------------------------------------------------- /precompile/inc/t.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | void f(T x) 5 | { 6 | printf("F %d\n", x); 7 | } 8 | -------------------------------------------------------------------------------- /security/cryptorec/img/guideline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herumi/misc/HEAD/security/cryptorec/img/guideline.png -------------------------------------------------------------------------------- /const_div/mk.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | cl /EHsc /Ox /DNDEBUG div7_test.cpp div7.cpp /openmp -I ./ -I ../../mcl/include -I ../../mcl/src/ -------------------------------------------------------------------------------- /go/share/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "add.h" 3 | 4 | int main() 5 | { 6 | printf("%d\n", add(3, 5)); 7 | } 8 | -------------------------------------------------------------------------------- /go/wasm/go/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello, WebAssembly!") 7 | } 8 | -------------------------------------------------------------------------------- /hpc/run.sh: -------------------------------------------------------------------------------- 1 | rm -rf sample.sh.o* 2 | FCCpx -Kfast -Krestp=all expf_test.cpp -I./ 3 | #FCCpx trismul.cpp 4 | pjsub ./sample.sh 5 | -------------------------------------------------------------------------------- /webpack/test1/src/func.js: -------------------------------------------------------------------------------- 1 | const X = 1 2 | function put(x) { 3 | console.log(`x=${x+X}`) 4 | } 5 | 6 | export { put } 7 | 8 | -------------------------------------------------------------------------------- /dlopen/sub2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | clock_t clock(void) { puts("clock is called in sub2.c"); return 123; } 4 | -------------------------------------------------------------------------------- /verify_stack_frame.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | extern "C" { 3 | void verifyStackFrame() 4 | __attribute__((no_instrument_function)); 5 | } 6 | -------------------------------------------------------------------------------- /double-link/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void put(); 4 | 5 | int main() 6 | { 7 | puts("call put"); 8 | put(); 9 | } 10 | -------------------------------------------------------------------------------- /fma/readme.md: -------------------------------------------------------------------------------- 1 | # how to test 2 | 3 | Copy [s_xbyak.py](https://github.com/herumi/s_xbyak) in this folder. 4 | 5 | ``` 6 | make test 7 | ``` 8 | 9 | -------------------------------------------------------------------------------- /mapto/misc.txt: -------------------------------------------------------------------------------- 1 | t1 0 1 2 | t2 0 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa 3 | out 0 0 1 0 0 0 4 | -------------------------------------------------------------------------------- /split/t.h: -------------------------------------------------------------------------------- 1 | struct X { 2 | void put() const; 3 | }; 4 | 5 | #ifndef USE_PRECOMPILE_HEADER 6 | #include "impl.h" 7 | #endif 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /extint/wasm/api.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef __wasm__ 4 | #define API __attribute__((visibility("default"))) 5 | #else 6 | #define API 7 | #endif 8 | 9 | -------------------------------------------------------------------------------- /large-file-lfs.txt: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:10913362c5e9ba66593f30c8086645167d4ec4c54bc103bf05df9eaeb10a13a7 3 | size 32 4 | -------------------------------------------------------------------------------- /node/wasm2/index.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | const add = require("./add.js") 4 | add.onRuntimeInitialized = () => { 5 | console.log(add._add(10, 20)) 6 | } 7 | -------------------------------------------------------------------------------- /shared/t.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char *p = malloc(5); 6 | // buffer overrun 7 | p[5] = 'a'; 8 | free(p); 9 | } 10 | -------------------------------------------------------------------------------- /whole-archive/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" void sub(); 4 | 5 | int main() 6 | { 7 | puts("call sub"); 8 | sub(); 9 | } 10 | -------------------------------------------------------------------------------- /node/wasm3/sub.c: -------------------------------------------------------------------------------- 1 | void func_none() {} 2 | 3 | __attribute__((used)) void func_used() {} 4 | 5 | __attribute__((visibility("default"))) void func_default() {} 6 | -------------------------------------------------------------------------------- /xbyak_fixed/t2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" int fff(int, int); 4 | 5 | int main() 6 | { 7 | printf("%d + %d = %d\n", 3, 5, fff(3, 5)); 8 | } 9 | -------------------------------------------------------------------------------- /xbyak_fixed/t4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" int fff(int, int); 4 | 5 | int main() 6 | { 7 | printf("%d + %d = %d\n", 3, 5, fff(3, 5)); 8 | } 9 | -------------------------------------------------------------------------------- /cpp/throw_in_c/s.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern void g(); 4 | extern void f(); 5 | 6 | void f() 7 | { 8 | puts("call g"); 9 | g(); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /go/cstr/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" void cstr(); 4 | 5 | int main() 6 | { 7 | puts("main"); 8 | cstr(); 9 | puts("end"); 10 | } 11 | -------------------------------------------------------------------------------- /motoko/test/Makefile: -------------------------------------------------------------------------------- 1 | default: 2 | $(shell vessel bin)/moc $(shell vessel sources) -wasi-system-api -o Test.wasm Test.mo && wasmtime Test.wasm 3 | rm -f Test.wasm 4 | -------------------------------------------------------------------------------- /webpack/test1/config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | entry: './src/index.js', 3 | output: { 4 | path: __dirname + '/', 5 | filename: 'index.js' 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /bbs/browser/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "dist/index.js", 3 | "author": "herumi (https://github.com/herumi/)", 4 | "license": "BSD-3-Clause" 5 | } 6 | -------------------------------------------------------------------------------- /bbs/wasm/getRandomValues.ts: -------------------------------------------------------------------------------- 1 | const getRandomValues = (buf: Uint8Array): Uint8Array => { 2 | return crypto.getRandomValues(buf) 3 | } 4 | 5 | export default getRandomValues 6 | -------------------------------------------------------------------------------- /swig/test.i: -------------------------------------------------------------------------------- 1 | %module Test 2 | 3 | %include "std_string.i" 4 | %include "std_except.i" 5 | 6 | %{ 7 | #include "test.hpp" 8 | %} 9 | 10 | %include "test.hpp" 11 | 12 | -------------------------------------------------------------------------------- /dll/sub.cpp: -------------------------------------------------------------------------------- 1 | extern "C" int fff(int, int); 2 | 3 | extern "C" { 4 | 5 | __declspec(dllexport) int ggg(int x, int y) 6 | { 7 | return fff(x, y) + 100; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /dlopen/sub3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void sub3(void) 5 | { 6 | printf("sub3 clock=%p\n", clock); 7 | printf("sub3 clock()=%d\n", (int)clock()); 8 | } 9 | -------------------------------------------------------------------------------- /wasm/sample/test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const sample = require('../dist/index.js') 3 | 4 | sample.init(123) 5 | .then(() => { 6 | console.log(`sample.init`) 7 | }) 8 | 9 | -------------------------------------------------------------------------------- /webpack/test2/src/test.js: -------------------------------------------------------------------------------- 1 | const lib = require('./index.js') 2 | 3 | lib.init().then(() => { 4 | console.log('test.js') 5 | console.log(`add(3,5)=${lib.mod._add(3,5)}`) 6 | }) 7 | -------------------------------------------------------------------------------- /avx-512/mk.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | set SRC=%1 3 | set EXE=%SRC:~0,-4%.exe 4 | cl /EHsc /arch:AVX2 -I ../../xbyak -I ../../cybozulib/include /W4 %SRC% 5 | ..\..\intel\sde -future -- %EXE% 6 | -------------------------------------------------------------------------------- /go/thread/t.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /* 4 | void waitTask(int n); 5 | #cgo LDFLAGS:-lwait -L./ -lstdc++ 6 | */ 7 | import "C" 8 | 9 | func main() { 10 | C.waitTask(8) 11 | } 12 | -------------------------------------------------------------------------------- /go/callback/lib.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | typedef int (*FuncType)(int); 4 | void setCallbackC(FuncType f); 5 | void callCallbackC(int x); 6 | int wrapCallbackCgo(int x); 7 | 8 | -------------------------------------------------------------------------------- /react/test2/readme.md: -------------------------------------------------------------------------------- 1 | # sample of mcl-wasm in React 2 | 3 | ``` 4 | cd test2 5 | ln -s ~/Program/mcl-wasm src 6 | npm install 7 | npx webpack 8 | python3 -m http.server 9 | ``` 10 | 11 | -------------------------------------------------------------------------------- /webpack/test2/test-webpack.js: -------------------------------------------------------------------------------- 1 | const lib = require('./index.js') 2 | 3 | lib.init().then(() => { 4 | console.log('test-webpack.js') 5 | console.log(`add(3,5)=${lib.mod._add(3,5)}`) 6 | }) 7 | -------------------------------------------------------------------------------- /wasm/exception/index.js: -------------------------------------------------------------------------------- 1 | const createModule = require('./exception_c.js') 2 | const setupFactory = require('./exception') 3 | 4 | const ex = setupFactory(createModule) 5 | 6 | module.exports = ex 7 | -------------------------------------------------------------------------------- /webpack/test2/src/index.js: -------------------------------------------------------------------------------- 1 | const createModule = require(`./add_c.js`) 2 | const setupFactory = require(`./add.js`) 3 | 4 | console.log('index.js') 5 | module.exports = setupFactory(createModule) 6 | -------------------------------------------------------------------------------- /cpp/throw_in_c/v.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static void g(void f()) 4 | { 5 | puts("g"); 6 | f(); 7 | } 8 | 9 | void (*get())(void f()) 10 | { 11 | puts("get"); 12 | return g; 13 | } 14 | -------------------------------------------------------------------------------- /shared/b.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void sub_free(void *p) 5 | { 6 | puts("sub_free"); 7 | printf("malloc=%p, free=%p in sub\n", malloc, free); 8 | free(p); 9 | } 10 | -------------------------------------------------------------------------------- /swig/set-java-path.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | if "%JAVA_HOME%"=="" ( 3 | set JAVA_DIR=c:/p/Java/jdk 4 | ) else ( 5 | set JAVA_DIR=%JAVA_HOME% 6 | ) 7 | echo JAVA_DIR=%JAVA_DIR% 8 | rem set PATH=%PATH%;%JAVA_DIR%\bin 9 | -------------------------------------------------------------------------------- /motoko/test/vessel.dhall: -------------------------------------------------------------------------------- 1 | let mainVessel = ../vessel.dhall 2 | 3 | let additionalDependencies = [] : List Text 4 | 5 | in mainVessel 6 | with dependencies = mainVessel.dependencies # additionalDependencies 7 | -------------------------------------------------------------------------------- /xbyak_fixed/mk.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | set INC=-I../../xbyak -I../../cybozulib/include 3 | set OUT=t1-out 4 | cl /EHsc t1.cpp %INC% 5 | t1.exe %OUT%.asm 6 | nasm -fwin64 %OUT%.asm 7 | cl /EHsc t2.cpp %OUT%.obj 8 | t2.exe 9 | -------------------------------------------------------------------------------- /crypto/fft/Makefile: -------------------------------------------------------------------------------- 1 | MCL_DIR?=../../../mcl 2 | %.exe: %.cpp 3 | g++ -Wall -Wextra -o $@ $< -lmcl -L$(MCL_DIR)/lib -I$(MCL_DIR)/include 4 | 5 | test: ntt_test.exe 6 | ./$< 7 | 8 | clean: 9 | rm -rf *.exe 10 | -------------------------------------------------------------------------------- /extint/mk-test.bat: -------------------------------------------------------------------------------- 1 | clang-cl /c /O2 /DNDEBUG bitint_if64.ll 2 | cl /O2 /DNDEBUG /DNOMINMAX /EHsc bitint_llvm_test.cpp bitint_if64.obj -I ../../mcl/include -I ../../cybozulib_ext/include /link /LIBPATH:../../cybozulib_ext/lib -------------------------------------------------------------------------------- /gf256/mk.bat: -------------------------------------------------------------------------------- 1 | python3 gen_asm.py -m masm > gf256_gfni.asm 2 | ml64 /c /nologo gf256_gfni.asm 3 | cl /nologo /EHsc /Ox /DNDEBUG gf256_test.cpp gf256_gfni.obj -I ../../cybozulib/include -I ../../xbyak 4 | gf256_test.exe 5 | -------------------------------------------------------------------------------- /node/wasm2/index2.js: -------------------------------------------------------------------------------- 1 | const m = require('./add2.js') 2 | 3 | console.log('111') 4 | m.onModuleInit(function() { 5 | console.log('222') 6 | console.log(m.mod._add(3, 5)) 7 | }) 8 | console.log('333') 9 | 10 | -------------------------------------------------------------------------------- /slow_sin_test.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | set OPT=/EHsc /Ox /Ob2 /fp:fast /Zi 3 | echo "no avx" 4 | cl %OPT% slow_sin.cpp && slow_sin 5 | echo "with avx" 6 | cl %OPT% /arch:AVX slow_sin.cpp /Feslow_sin_avx.exe && slow_sin_avx 7 | -------------------------------------------------------------------------------- /python/ecc/Makefile: -------------------------------------------------------------------------------- 1 | all: fr.py 2 | 3 | fr.py: fp.py 4 | sed 's/Fp/Fr/g' < fp.py > fr.py 5 | 6 | test: fr.py 7 | python3 fp.py 8 | python3 fr.py 9 | python3 ec.py 10 | python3 ecdh.py 11 | python3 ecdsa.py 12 | -------------------------------------------------------------------------------- /split/impl.h: -------------------------------------------------------------------------------- 1 | #include 2 | #ifdef PRECOMPILE_HEADER 3 | #define INLINE_INST 4 | #else 5 | #define INLINE_INST inline 6 | #endif 7 | 8 | INLINE_INST void X::put() const 9 | { 10 | puts("AAA"); 11 | } 12 | -------------------------------------------------------------------------------- /go/wasm/cgo/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /* 4 | #cgo LDFLAGS:-L./ -llib 5 | #include "lib.h" 6 | */ 7 | import "C" 8 | import "fmt" 9 | 10 | func main() { 11 | a := int(C.add(3, 5)) 12 | fmt.Printf("%d\n", a) 13 | } 14 | -------------------------------------------------------------------------------- /precompile/Makefile: -------------------------------------------------------------------------------- 1 | all: test 2 | CFLAGS=-DNDEBUG -Ofast -I ./inc 3 | 4 | pre: inc/t.h 5 | $(CXX) $< $(CFLAGS) -H 6 | 7 | test: t.cpp 8 | time $(CXX) -o $@ $< $(CFLAGS) 9 | 10 | clean: 11 | -$(RM) t.h.gch test 12 | -------------------------------------------------------------------------------- /dlopen/sub.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void sub_free(void *p) 5 | { 6 | puts("sub_free"); 7 | printf("malloc=%p, free=%p in sub\n", malloc, free); 8 | printf("p=%p\n", p); 9 | free(p); 10 | } 11 | -------------------------------------------------------------------------------- /swig/run-test.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | echo [[compile %JAVA_NAME%Test.java]] 3 | %JAVA_DIR%\bin\javac %JAVA_NAME%Test.java 4 | 5 | echo [[run %JAVA_NAME%Test]] 6 | %JAVA_DIR%\bin\java -classpath .\ %JAVA_NAME%Test %1 %2 %3 %4 %5 %6 7 | popd 8 | -------------------------------------------------------------------------------- /cpp/sample/inline_var.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static inline int aaa = 3; 4 | inline int bbb = 5; 5 | 6 | template 7 | struct X { 8 | static int aaa; 9 | }; 10 | 11 | template 12 | int X::aaa = 8; 13 | 14 | -------------------------------------------------------------------------------- /cpp/sample/over-aligned-new.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct alignas(64) X { 6 | uint32_t a[16]; 7 | }; 8 | 9 | int main() 10 | { 11 | X *p = new X(); 12 | printf("p=%p\n", p); 13 | } 14 | -------------------------------------------------------------------------------- /extint/wasm/sub.cpp: -------------------------------------------------------------------------------- 1 | #include "api.h" 2 | 3 | extern "C" { 4 | 5 | API int sub(int x, int y) 6 | { 7 | return x - y; 8 | } 9 | 10 | API void setMem(char *p, int n) 11 | { 12 | for (int i = 0; i < n; i++) p[i] = i; 13 | } 14 | 15 | } 16 | 17 | -------------------------------------------------------------------------------- /go/cstr/cstr.go: -------------------------------------------------------------------------------- 1 | package main 2 | /* 3 | #cgo LDFLAGS:-lcstr -L./ -lstdc++ 4 | void cstr(); 5 | */ 6 | import "C" 7 | import ( 8 | "fmt" 9 | ) 10 | 11 | func main() { 12 | fmt.Printf("call C.cstr\n") 13 | C.cstr(); 14 | fmt.Printf("end\n") 15 | } 16 | -------------------------------------------------------------------------------- /whole-archive/init_f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" void (*g_f)(); 4 | 5 | extern "C" void f() 6 | { 7 | puts("f is called"); 8 | } 9 | 10 | static struct X { 11 | X() 12 | { 13 | puts("g_f is set"); 14 | g_f = &f; 15 | } 16 | } x; 17 | -------------------------------------------------------------------------------- /whole-archive/sub.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" { 4 | 5 | void (*g_f)(); 6 | 7 | } 8 | 9 | extern "C" void sub() 10 | { 11 | if (g_f == 0) { 12 | puts("not inited"); 13 | } else { 14 | puts("call f"); 15 | g_f(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /go/callback/Makefile: -------------------------------------------------------------------------------- 1 | %.o: %.c 2 | gcc -c -O2 -fPIC -Wall -Wextra -o $@ -g $< 3 | 4 | liblib.a: lib.o 5 | ar r $@ $< 6 | 7 | test: liblib.a main.go callback.go sub.go 8 | go run main.go callback.go sub.go 9 | 10 | clean: 11 | rm -rf *.a *.o callback 12 | -------------------------------------------------------------------------------- /go/wasm/go/Makefile: -------------------------------------------------------------------------------- 1 | R=$(shell go env GOROOT) 2 | main.wasm: main.go 3 | GOOS=js GOARCH=wasm go build -o main.wasm main.go 4 | 5 | test: 6 | env GOOS=js GOARCH=wasm go run -exec=$(R)/misc/wasm/go_js_wasm_exec main.go 7 | 8 | clean: 9 | $(RM) -rf *.wasm 10 | -------------------------------------------------------------------------------- /cpp/throw_in_c/w.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void f_throw() 5 | { 6 | puts("f_throw"); 7 | throw std::runtime_error("f_throw"); 8 | } 9 | 10 | void (*getCpp())() 11 | { 12 | puts("getCpp"); 13 | return f_throw; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /dlopen/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void sub_free(void *p); 5 | 6 | int main() 7 | { 8 | void *p; 9 | puts("main"); 10 | printf("malloc=%p, free=%p\n", malloc, free); 11 | p = malloc(123); 12 | sub_free(p); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /security/cryptorec/Makefile: -------------------------------------------------------------------------------- 1 | all: img/tls1.2.svg img/tls1.3.svg img/1-rtt.svg img/0-rtt.svg 2 | 3 | .txt.svg: 4 | uml2img $? 5 | 6 | img/tls1.2.svg: img/tls1.2.txt 7 | img/tls1.3.svg: img/tls1.3.txt 8 | img/1-rtt.svg: img/1-rtt.txt 9 | img/0-rtt.svg: img/0-rtt.txt 10 | -------------------------------------------------------------------------------- /webpack/test2/demo.js: -------------------------------------------------------------------------------- 1 | function setValue(name, val) { document.getElementsByName(name)[0].innerHTML = val } 2 | 3 | function test() { 4 | console.log(bls) 5 | bls.init().then(() => { 6 | const a = bls.mod._add(3, 4) 7 | setValue('ret', a) 8 | }) 9 | } 10 | -------------------------------------------------------------------------------- /node/wasm2/Makefile: -------------------------------------------------------------------------------- 1 | all=add.wasm 2 | 3 | add.wasm: add.cpp 4 | emcc -O3 -o add.js -s WASM=1 -s EXPORTED_FUNCTIONS="['_add']" add.cpp 5 | 6 | test: add.wasm 7 | node index.js 8 | 9 | test2: add.wasm 10 | node index2.js 11 | 12 | clean: 13 | rm -rf add.wasm add.js 14 | -------------------------------------------------------------------------------- /wasm/sample/src/sample.ts: -------------------------------------------------------------------------------- 1 | const createModule = require('./sample_c.js') 2 | 3 | export const initializeSample = async (x:number) => { 4 | console.log('initializeSample') 5 | const mod = await createModule() 6 | const r = mod._func(x) 7 | console.log(`func(${x})=${r}`) 8 | } 9 | -------------------------------------------------------------------------------- /extint/add256.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void add256(uint64_t *pz, const uint64_t *px, const uint64_t *py) 4 | { 5 | unsigned _ExtInt(256) x = *(unsigned _ExtInt(256)*)px; 6 | unsigned _ExtInt(256) y = *(unsigned _ExtInt(256)*)py; 7 | *(unsigned _ExtInt(256)*)pz = x + y; 8 | } 9 | -------------------------------------------------------------------------------- /extint/bitint_if.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /* 3 | interface of bitint 4 | */ 5 | #include 6 | 7 | 8 | extern "C" { 9 | 10 | size_t mclb_divFullBit256(mcl::fp::Unit *q, size_t qn, mcl::fp::Unit *x, size_t xn, const mcl::fp::Unit *y); 11 | 12 | } // extern "C" 13 | -------------------------------------------------------------------------------- /go/thread/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern "C" void waitTask(int n); 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | int n = argc == 1 ? 8 : atoi(argv[1]); 9 | printf("main n=%d\n", n); 10 | waitTask(n); 11 | puts("main end"); 12 | } 13 | -------------------------------------------------------------------------------- /llvm/Makefile: -------------------------------------------------------------------------------- 1 | all: gen-lp.exe 2 | CXX=clang-14 3 | 4 | a.ll: gen_lp.py 5 | python3 gen_lp.py > a.ll 6 | 7 | gen-lp.exe: a.ll lp-test.c 8 | $(CXX) -o gen-lp.exe a.ll lp-test.c 9 | 10 | test: gen-lp.exe 11 | ./gen-lp.exe 12 | 13 | clean: 14 | rm -rf a.ll gen-lp.exe 15 | 16 | -------------------------------------------------------------------------------- /s_xbyak/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | rem python3 gen.py -m masm > func_s.masm 4 | rem ml64 /c /nologo func_s.masm 5 | 6 | python3 gen.py -m nasm -win > func_s.nasm 7 | nasm -f win64 func_s.nasm 8 | 9 | cl /EHsc /nologo test.cpp func_s.obj /I ../../cybozulib/include 10 | test.exe 11 | -------------------------------------------------------------------------------- /webpack/test2/src/add.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #ifdef __EMSCRIPTEN__ 3 | #define API __attribute__((used)) 4 | #else 5 | #define API __attribute__((visibility("default"))) 6 | #endif 7 | extern "C" API uint32_t add(uint32_t x, uint32_t y) 8 | { 9 | return x + y; 10 | } 11 | -------------------------------------------------------------------------------- /check_stack/mk.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | @echo with __chkstk 3 | @nasm -fwin64 -DUSE_CHKSTK f.asm 4 | @cl /EHsc /Ox t.cpp f.obj /nologo /Zi 5 | @echo run 6 | t.exe 7 | 8 | @echo without __chkstk 9 | @nasm -fwin64 f.asm 10 | @cl /EHsc /Ox t.cpp f.obj /nologo /Zi 11 | @echo run 12 | t.exe 13 | -------------------------------------------------------------------------------- /check_stack/t.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" const void* f(); 4 | 5 | extern "C" void fff(const void*); 6 | 7 | char ggg() 8 | { 9 | char buf[64]; 10 | fff(buf); 11 | return buf[0]; 12 | } 13 | 14 | int main() 15 | { 16 | f(); 17 | ggg(); 18 | puts("ok"); 19 | } 20 | -------------------------------------------------------------------------------- /dll/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" { 4 | 5 | //__declspec(dllimport) 6 | int fff(int, int); 7 | //__declspec(dllimport) 8 | int ggg(int, int); 9 | 10 | } 11 | 12 | int main() 13 | { 14 | printf("fff=%d\n", fff(2, 3)); 15 | printf("ggg=%d\n", ggg(5, 9)); 16 | } 17 | -------------------------------------------------------------------------------- /extint/uint128.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef __attribute__((mode(TI))) unsigned int uint128; 3 | 4 | void add128(uint64_t *pz, const uint64_t *px, const uint64_t *py) 5 | { 6 | uint128 x = *(uint128*)px; 7 | uint128 y = *(uint128*)py; 8 | *(uint128*)pz = x + y; 9 | } 10 | -------------------------------------------------------------------------------- /node/wasm3/add.cpp: -------------------------------------------------------------------------------- 1 | #if defined(__wasm__) 2 | #define DLL_API __attribute__((visibility("default"))) 3 | #endif 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | DLL_API int add(int x, int y) 9 | { 10 | return x + y; 11 | } 12 | 13 | #ifdef __cplusplus 14 | } 15 | #endif 16 | -------------------------------------------------------------------------------- /static-var/s.cpp: -------------------------------------------------------------------------------- 1 | #define DLL_EXPORT 2 | #include "var.hpp" 3 | 4 | void put() 5 | { 6 | printf("sub msg=%p\n", X::get()); 7 | } 8 | 9 | void incX() 10 | { 11 | puts("call incX"); 12 | X().inc(); 13 | printf("sub x=%d\n", X::x); 14 | } 15 | 16 | int XX::x = 4; 17 | -------------------------------------------------------------------------------- /static-var/t.cpp: -------------------------------------------------------------------------------- 1 | #include "var.hpp" 2 | 3 | int main() 4 | { 5 | printf("msg=%p\n", X::get()); 6 | put(); 7 | incX(); 8 | incX(); 9 | X().inc(); 10 | int x = X::x; 11 | printf("main x=%d(%s)\n",x, x == 8 ? "ok" : "ng"); 12 | printf("XX::x=%d\n", XX::x); 13 | } 14 | -------------------------------------------------------------------------------- /cpp/sample/inline_var2.cpp: -------------------------------------------------------------------------------- 1 | #include "inline_var.hpp" 2 | 3 | void put2() 4 | { 5 | puts("put2"); 6 | printf("static inline aaa=%d (%p)\n", aaa, &aaa); 7 | printf(" inline bbb=%d (%p)\n", bbb, &bbb); 8 | printf("X::aaa =%d (%p)\n", X::aaa, &X::aaa); 9 | } 10 | -------------------------------------------------------------------------------- /go/cstr/cstr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct X { 4 | char *p; 5 | X() 6 | : p(new char[123]) 7 | { 8 | puts("X cstr"); 9 | } 10 | ~X() 11 | { 12 | puts("X dstr"); 13 | delete[] p; 14 | } 15 | }; 16 | 17 | extern "C" void cstr() 18 | { 19 | static X x; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /go/thread/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-Wall -Wextra -O3 -I./include -std=c++11 -pthread 2 | 3 | wait.o: wait.cpp 4 | $(CXX) $(CFLAGS) -fPIC -c -o $@ -g $< 5 | 6 | main: main.cpp libwait.a 7 | $(CXX) $(CFLAGS) -o $@ main.cpp libwait.a 8 | 9 | libwait.a: wait.o 10 | ar r libwait.a wait.o 11 | 12 | -------------------------------------------------------------------------------- /llvm/lp-test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | uint32_t sum(uint32_t n); 5 | 6 | int main() 7 | { 8 | uint32_t v1 = 0; 9 | for (int i = 0; i < 10; i++) { 10 | uint32_t v2 = sum(i); 11 | printf("%d %u %c\n", i, v2, v1 == v2 ? 'o' : 'x'); 12 | v1 += i; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /react/test2/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | test2 6 | 7 | 8 |

this is a test

9 |
10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /cpp/sample/parallel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::array a = { 1, 2,3,4, 5, 6, 7, 8 }; 9 | int sum = std::reduce(std::execution::par_unseq, a.begin(), a.end()); 10 | printf("sum=%d\n", sum); 11 | } 12 | -------------------------------------------------------------------------------- /mapto/Makefile: -------------------------------------------------------------------------------- 1 | a.out: test.cpp ../../mcl/include/mcl/mapto_wb19.hpp 2 | g++ -Wall -Wextra test.cpp -I ../../mcl/include/ -lmcl -L../../mcl/lib -lcrypto -lgmp -lgmpxx 3 | 4 | test: a.out 5 | # -python3 ../../bls12-381_hash/python-impl/opt_swu_g2.py 6 | ./a.out 7 | 8 | clean: 9 | rm -rf a.out 10 | -------------------------------------------------------------------------------- /cpp/throw_in_c/t.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern "C" void f(); 5 | 6 | extern "C" void g() 7 | { 8 | throw std::runtime_error("g"); 9 | } 10 | 11 | int main() 12 | try 13 | { 14 | f(); 15 | } catch (std::exception& e){ 16 | printf("e=%s\n", e.what()); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /xbyak_split/t1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Code : Xbyak::CodeGenerator { 5 | Code() 6 | { 7 | mov(eax, 123); 8 | ret(); 9 | } 10 | }; 11 | 12 | int main() 13 | { 14 | Code c; 15 | int (*f)() = c.getCode(); 16 | printf("f=%d\n", f()); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /go/share/add.cpp: -------------------------------------------------------------------------------- 1 | #if defined(_MSC_VER) 2 | #define DLL_EXPORT 3 | #ifdef DLL_EXPORT 4 | #define DLL_API __declspec(dllexport) 5 | #else 6 | #define DLL_API __declspec(dllimport) 7 | #endif 8 | #else 9 | #define DLL_API 10 | #endif 11 | 12 | extern "C" DLL_API int add(int x, int y) 13 | { 14 | return x + y; 15 | } -------------------------------------------------------------------------------- /static-cstr/common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifndef PRIORITY 4 | #define PRIORITY 65535 5 | #endif 6 | 7 | #ifdef __GNUC__ 8 | #define CSTR __attribute__((constructor(PRIORITY))) 9 | #define INIT_PRIORITY(x) __attribute__((init_priority(x))) 10 | #else 11 | #define CSTR 12 | #define INIT_PRIORITY(x) 13 | #endif 14 | -------------------------------------------------------------------------------- /text2sound/readme.md: -------------------------------------------------------------------------------- 1 | # テキストを音声再生する 2 | 3 | ## コンソールを開いて 4 | ``` 5 | cscript text2sound.js text-file 6 | ``` 7 | とするとtext-fileの中身を読みます。 8 | 9 | ## ポーズ 10 | 「@pause」だけの行があるとポーズします。 11 | リターンキーを押すと再開します。 12 | 13 | ## 音声 14 | コントロールパネル→音声認識→音声合成→音声の選択 15 | で選択されている音声で再生されます(デフォルトではMicrosoft Harukaという音声)。 16 | -------------------------------------------------------------------------------- /go/callback/lib.c: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | 3 | static FuncType s_callbackC; 4 | 5 | void setCallbackC(FuncType f) 6 | { 7 | s_callbackC = f; 8 | } 9 | 10 | void callCallbackC(int x) 11 | { 12 | printf("callCallbackC x=%d\n", x); 13 | int ret = s_callbackC(x + 1); 14 | printf("callCallbackC ret=%d\n", ret); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /rust/mcl/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "mcl" 3 | version = "0.1.0" 4 | authors = ["MITSUNARI Shigeo "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | 11 | [build-dependencies] 12 | cc = "1.0" 13 | 14 | -------------------------------------------------------------------------------- /cpp/sample/initializer_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void put(const std::initializer_list& v) 5 | { 6 | for (int x : v) { 7 | printf("%d ", x); 8 | } 9 | printf("\n"); 10 | } 11 | int main() 12 | { 13 | auto x1 = {1}; 14 | // put(x1); 15 | auto x2 {2, 3}; 16 | // put(x2); 17 | } 18 | -------------------------------------------------------------------------------- /extint/bitint_if.cpp: -------------------------------------------------------------------------------- 1 | #include "bitint.hpp" 2 | #include "bitint_if.hpp" 3 | 4 | extern "C" { 5 | 6 | size_t mclb_divFullBit256(uint64_t *q, size_t qn, uint64_t *x, size_t xn, const uint64_t *y) 7 | { 8 | #if MCL_SIZEOF_UNIT == 8 9 | return mcl::bint::divFullBitT<4>(q, qn, x, xn, y); 10 | #endif 11 | } 12 | 13 | } // extern "C" 14 | -------------------------------------------------------------------------------- /rop/Makefile: -------------------------------------------------------------------------------- 1 | 2 | PREFIX?=aarch64-linux-gnu- 3 | XBYAK_DIR?=../../fujitsu/xbyak_aarch64 4 | test: emu 5 | env QEMU_LD_PREFIX=/usr/aarch64-linux-gnu qemu-aarch64 -cpu max,sve512=on ./emu 6 | 7 | emu: test-aarch64.cpp 8 | $(PREFIX)$(CXX) $(CFLAGS) -o emu test-aarch64.cpp -I $(XBYAK_DIR) -L $(XBYAK_DIR)/lib -lxbyak_aarch64 9 | 10 | -------------------------------------------------------------------------------- /go/callback/sub.go: -------------------------------------------------------------------------------- 1 | package main 2 | /* 3 | #include "lib.h" 4 | int wrapCallbackGo(int); // exported from callback.go 5 | int wrapCallbackCgo(int x) 6 | { 7 | printf(" wrapCallbackCgo x=%d\n", x); 8 | int ret = wrapCallbackGo(x + 1); 9 | printf(" wrapCallbackCgo ret=%d\n", ret); 10 | return ret; 11 | } 12 | */ 13 | import "C" 14 | 15 | -------------------------------------------------------------------------------- /rust/mcl/mcl-sys/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "mcl-sys" 3 | version = "0.1.0" 4 | authors = ["MITSUNARI Shigeo "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | 11 | [build-dependencies] 12 | bindgen = "0.44" 13 | -------------------------------------------------------------------------------- /sha-sample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | unsigned char md[128]; 7 | const unsigned char *src = (const unsigned char*)"abc"; 8 | SHA256(src, 3, md); 9 | for (size_t i = 0; i < SHA256_DIGEST_LENGTH; i++) { 10 | printf("%02X", (unsigned char)md[i]); 11 | } 12 | printf("\n"); 13 | } 14 | -------------------------------------------------------------------------------- /unicode-file.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | const wchar_t tbl[][10] = { 8 | { 0x01d6 }, 9 | { 0x00fc, 0x0304 }, 10 | { 0x0075, 0x0308, 0x0304 }, 11 | { }, 12 | }; 13 | for (const wchar_t *file : tbl) { 14 | std::wofstream f(file, std::ios::binary); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /cpp/throw_in_c/u.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern "C" void (*get())(void f()); 5 | void (*getCpp())(); 6 | 7 | int main() 8 | try 9 | { 10 | void (*g)(void f()) = get(); 11 | void (*f)() = getCpp(); 12 | puts("callback"); 13 | g(f); 14 | } catch (std::exception& e) { 15 | printf("e=%s\n", e.what()); 16 | } 17 | -------------------------------------------------------------------------------- /extract-text.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | SUF=aarch64-linux-gnu- 4 | ${SUF}as $1 -o tmp.o 5 | VALS=($(${SUF}objdump -h tmp.o | awk '/\.text/ { printf("%s %s\n", $3, $6)}')) 6 | SIZE=$((0x${VALS[0]})) 7 | OFFSET=$((0x${VALS[1]})) 8 | echo size=${SIZE} 9 | echo offset=${OFFSET} 10 | dd if=tmp.o of=out.bin ibs=1 skip=${OFFSET} count=${SIZE} status=none 11 | -------------------------------------------------------------------------------- /uint128.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | typedef __attribute__((mode(TI))) unsigned int uint128; 8 | uint128 a = 1; 9 | for (int i = 0; i < 128; i++) { 10 | uint64_t b[2]; 11 | memcpy(b, &a, sizeof(b)); 12 | printf("%016lx %016lx\n", b[1], b[0]); 13 | a <<= 1; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /go/go-in-c/Makefile: -------------------------------------------------------------------------------- 1 | all: liblib.a libtest 2 | 3 | %.o: %.c 4 | gcc -c -O2 -fPIC -Wall -Wextra -o $@ -g $< 5 | 6 | liblib.a: lib.o 7 | ar r $@ $< 8 | 9 | libtest: libtest.o liblib.a 10 | gcc -o $@ $< -L./ -llib 11 | 12 | test: libtest 13 | ./libtest 14 | 15 | test_go: liblib.a 16 | go run main.go 17 | 18 | clean: 19 | rm -rf *.o *.a libtest 20 | 21 | -------------------------------------------------------------------------------- /longint/bitint_x64.cpp: -------------------------------------------------------------------------------- 1 | #include "bitint_x64.hpp" 2 | 3 | #if 1 4 | extern "C" uint64_t xxx(uint64_t *z, const uint64_t *x, uint64_t y) 5 | { 6 | return mcl::x64::mulUnitAddT<4>(z, x, y); 7 | } 8 | #else 9 | extern "C" uint64_t xxx(uint64_t *z, const uint64_t *x, const uint64_t *y) 10 | { 11 | return mcl::x64::subT<8>(z, x, y); 12 | } 13 | #endif 14 | 15 | -------------------------------------------------------------------------------- /check_stack/f.asm: -------------------------------------------------------------------------------- 1 | global f 2 | global fff 3 | extern __chkstk 4 | 5 | %define STACK_SIZE (4096*6) 6 | 7 | segment .text 8 | f: 9 | %ifdef USE_CHKSTK 10 | mov [rsp+8], ecx 11 | mov eax, STACK_SIZE 12 | call __chkstk 13 | sub rsp, rax 14 | %else 15 | sub rsp, STACK_SIZE 16 | %endif 17 | mov rax, [rsp] 18 | add rsp, STACK_SIZE 19 | ret 20 | 21 | fff: 22 | ret -------------------------------------------------------------------------------- /extint/ladd256.ll: -------------------------------------------------------------------------------- 1 | define void @add256(i64* nocapture %0, i64* nocapture readonly %1, i64* nocapture readonly %2) { 2 | %4 = bitcast i64* %1 to i256* 3 | %5 = load i256, i256* %4 4 | %6 = bitcast i64* %2 to i256* 5 | %7 = load i256, i256* %6 6 | %8 = add i256 %7, %5 7 | %9 = bitcast i64* %0 to i256* 8 | store i256 %8, i256* %9 9 | ret void 10 | } 11 | 12 | -------------------------------------------------------------------------------- /extint/wasm/mul128.ll: -------------------------------------------------------------------------------- 1 | define void @mul128(i128* nocapture %pz, i64* nocapture readonly %px, i64* nocapture readonly %py) { 2 | %x = load i64, i64* %px, align 8 3 | %y = load i64, i64* %py, align 8 4 | %xx = zext i64 %x to i128 5 | %yy = zext i64 %y to i128 6 | %zz = mul nuw i128 %xx, %yy 7 | store i128 %zz, i128* %pz, align 8 8 | ret void 9 | } 10 | 11 | -------------------------------------------------------------------------------- /go/go-in-c/libtest.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ggg(void f(void *, int), void *buf, int n); 4 | 5 | int fff(); 6 | 7 | void GoFunc1() 8 | { 9 | puts("test GoFunc1"); 10 | } 11 | 12 | void GoFunc2(const void *buf, int n) 13 | { 14 | printf("test GoFunc1 buf=%p, n=%d\n", buf, n); 15 | } 16 | 17 | int main() 18 | { 19 | printf("fff=%d\n", fff()); 20 | } 21 | -------------------------------------------------------------------------------- /hpc/fmath.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /** 3 | @brief fast math library 4 | @author herumi 5 | @url http://homepage1.nifty.com/herumi/ 6 | @note modified new BSD license 7 | http://opensource.org/licenses/BSD-3-Clause 8 | 9 | */ 10 | 11 | namespace fmath { 12 | 13 | void init(); 14 | 15 | double exp(double x); 16 | float exp(float x); 17 | 18 | } // fmath 19 | 20 | -------------------------------------------------------------------------------- /broadcast/t.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" double b[8]; 4 | extern "C" double c[8]; 5 | extern "C" void f(); 6 | extern "C" void g(); 7 | 8 | void put(const double *p) 9 | { 10 | for (int i = 0; i < 8; i++) { 11 | printf("%f ", p[i]); 12 | } 13 | printf("\n"); 14 | } 15 | 16 | int main() 17 | { 18 | f(); 19 | put(b); 20 | g(); 21 | put(c); 22 | } 23 | -------------------------------------------------------------------------------- /longint/mcl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mcl.hpp" 3 | 4 | #ifdef _MSC_VER 5 | #pragma warning(disable : 4127) 6 | #endif 7 | 8 | Code s_mcl; 9 | 10 | int mcl_init(const char *pStr) 11 | try 12 | { 13 | s_mcl.init(pStr); 14 | return 0; 15 | } catch (std::exception& e) { 16 | fprintf(stderr, "err=%s\n", e.what()); 17 | return 1; 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /static-var/Makefile: -------------------------------------------------------------------------------- 1 | test1: t.cpp s.cpp 2 | $(CXX) t.cpp s.cpp -o test1 3 | 4 | 5 | test: test1 test2 6 | echo test1 7 | ./test1 8 | echo test2 9 | env LD_PRELOAD=./sub.so ./test2 10 | 11 | sub.so: s.cpp 12 | $(CXX) -O2 -fPIC -shared -o $@ -g $< 13 | 14 | test2: t.cpp sub.so 15 | $(CXX) t.cpp -o $@ ./sub.so 16 | 17 | clean: 18 | rm -rf test1 test2 sub.so 19 | 20 | -------------------------------------------------------------------------------- /node/wasm2/add2.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | console.log('AAA') 4 | const mod = require("./add.js") 5 | exports.mod = mod 6 | let initCb = null 7 | 8 | exports.onModuleInit = function(cb) { 9 | console.log('onModuleInit') 10 | initCb = cb 11 | } 12 | mod.onRuntimeInitialized = () => { 13 | console.log('onRuntimeInitialized') 14 | initCb() 15 | } 16 | 17 | console.log('BBB') 18 | -------------------------------------------------------------------------------- /webpack/test2/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 3+4 12 | 13 | 14 | -------------------------------------------------------------------------------- /cpp/sample/inline_var1.cpp: -------------------------------------------------------------------------------- 1 | #include "inline_var.hpp" 2 | 3 | void put1() 4 | { 5 | puts("put1"); 6 | printf("static inline aaa=%d (%p)\n", aaa, &aaa); 7 | printf(" inline bbb=%d (%p)\n", bbb, &bbb); 8 | printf("X::aaa =%d (%p)\n", X::aaa, &X::aaa); 9 | } 10 | 11 | extern void put2(); 12 | 13 | int main() 14 | { 15 | put1(); 16 | put2(); 17 | } 18 | -------------------------------------------------------------------------------- /cpp/sample/template_auto.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | void f() { printf("size=%zd %d\n", sizeof(n), n); } 5 | 6 | #ifdef CPP17 7 | template 8 | void g() { printf("size=%zd %d\n", sizeof(n), n); } 9 | #endif 10 | 11 | int main() 12 | { 13 | f(); 14 | f(); 15 | #ifdef CPP17 16 | g<1>(); 17 | g<'A'>(); 18 | #endif 19 | } 20 | -------------------------------------------------------------------------------- /split/Makefile: -------------------------------------------------------------------------------- 1 | all = test1 test2 2 | 3 | test1: inst.cpp t.h t.cpp impl.h 4 | $(CXX) t.cpp -o test1 5 | 6 | test2: inst.o t.cpp t.h 7 | $(CXX) t.cpp inst.o -DUSE_PRECOMPILE_HEADER -o test2 8 | 9 | inst.o: inst.cpp impl.h t.h 10 | $(CXX) -c inst.cpp 11 | 12 | test: test1 test2 13 | echo test1 14 | ./test1 15 | echo test2 16 | ./test2 17 | 18 | clean: 19 | rm -rf *.o test1 test2 20 | -------------------------------------------------------------------------------- /s_xbyak/Makefile: -------------------------------------------------------------------------------- 1 | test: test.exe 2 | ./test.exe 3 | 4 | CFLAGS=-g -Wall -Wextra -I ../../cybozulib/include 5 | 6 | test.exe: test.o gen.o 7 | $(CXX) -o $@ $^ 8 | 9 | %.o: %.cpp 10 | $(CXX) -c -o $@ $< $(CFLAGS) 11 | 12 | %.o: %.S 13 | $(CXX) -c -o $@ $< 14 | 15 | gen.S: gen.py 16 | python3 gen.py -m gas > gen.S 17 | 18 | clean: 19 | rm -rf *.o *.exe *.S 20 | 21 | .PHONY: test clean 22 | -------------------------------------------------------------------------------- /mapto/HashAndMap_test.go: -------------------------------------------------------------------------------- 1 | package BN254 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "testing" 7 | ) 8 | 9 | func TestMain(t *testing.T) { 10 | if Init() == BLS_FAIL { 11 | fmt.Printf("err") 12 | return 13 | } 14 | H := NewHashAndMap() 15 | for i := 0; i < 10000; i++ { 16 | s := strconv.Itoa(i) 17 | P := H.SetHashOf([]byte(s)) 18 | fmt.Printf("%d %v\n", i, P.ToString()) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /go/channel.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | n := 5 7 | v := make([]int, n) 8 | cs := make(chan int, n) 9 | 10 | add := func(i int) { 11 | v[i] = v[i] + i 12 | cs <- 1 13 | } 14 | for i := 0; i < n-1; i++ { 15 | go add(i) 16 | } 17 | for i := 0; i < n-1; i++ { 18 | <-cs 19 | } 20 | for i := 0; i < n; i++ { 21 | fmt.Printf("v[%v]=%v\n", i, v[i]) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /webpack/test2/src/add.js: -------------------------------------------------------------------------------- 1 | function setupFactory(createModule) { 2 | const exports = {} 3 | exports.init = async () => { 4 | console.log('exports.init') 5 | exports.mod = await createModule() 6 | } 7 | return exports 8 | } 9 | 10 | // NodeJS export 11 | if (typeof exports === 'object' && typeof module === 'object') { 12 | module.exports = setupFactory 13 | console.log('module.exports') 14 | } 15 | -------------------------------------------------------------------------------- /webpack/test2/webpack.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // mode: 'development', 3 | entry: './src/index.js', 4 | output: { 5 | path: __dirname + '/', 6 | library: 'bls', 7 | libraryTarget: 'umd', 8 | filename: 'index.js' 9 | }, 10 | resolve: { 11 | fallback: { 12 | "path": false, 13 | "fs": false 14 | } 15 | }, 16 | // target: "node" 17 | target: "web" 18 | } 19 | -------------------------------------------------------------------------------- /depend/Makefile: -------------------------------------------------------------------------------- 1 | SRC=a.cpp b.cpp m.cpp 2 | OBJ=$(SRC:.cpp=.o) 3 | DEP=$(SRC:.cpp=.d) 4 | 5 | TARGET=m.exe 6 | all: $(TARGET) 7 | 8 | %.o: %.cpp 9 | $(CXX) $(CFLAGS) -c $< -o $@ -MMD -MP -MF $(@:.o=.d) 10 | 11 | $(TARGET): $(OBJ) 12 | $(CXX) $< -o $@ $(LDFLAGS) $(OBJ) 13 | 14 | -include $(DEP) 15 | 16 | 17 | clean: 18 | $(RM) $(TARGET) $(OBJ) $(DEP) 19 | 20 | .PHONY: clean 21 | 22 | .SECONDARY: $(OBJ) 23 | 24 | -------------------------------------------------------------------------------- /motoko/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: check docs test 2 | 3 | check: 4 | find src -type f -name '*.mo' -print0 | xargs -0 $(shell vessel bin)/moc $(shell vessel sources) --check 5 | 6 | all: check-strict docs test 7 | 8 | check-strict: 9 | find src -type f -name '*.mo' -print0 | xargs -0 $(shell vessel bin)/moc $(shell vessel sources) -Werror --check 10 | docs: 11 | $(shell vessel bin)/mo-doc 12 | test: 13 | make -C test 14 | -------------------------------------------------------------------------------- /python/ecc/ecdh.py: -------------------------------------------------------------------------------- 1 | from ec import Ec, Fp, Fr, initSecp256k1 2 | 3 | def main(): 4 | P = initSecp256k1() 5 | a = Fr() 6 | b = Fr() 7 | a.setRand() 8 | b.setRand() 9 | print(f"a={a}") 10 | print(f"b={b}") 11 | aP = P * a 12 | bP = P * b 13 | baP = aP * b 14 | abP = bP * a 15 | print(f"baP={baP}") 16 | print(f"abP={abP}") 17 | print(f"baP == abP? {baP == abP}") 18 | 19 | if __name__ == '__main__': 20 | main() 21 | -------------------------------------------------------------------------------- /cpp/throw_in_c/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-O2 -Wall -Wextra 2 | CXXFLAGS=$(CFLAGS) 3 | 4 | .cpp: .o 5 | $(CXX) -c $< $(CXXFLAGS) 6 | 7 | .c: .o 8 | $(CC) -c $< $(CFLAGS) 9 | 10 | t1: t.o s.o 11 | $(CXX) -o t1 t.o s.o $(LDFLAGS) 12 | 13 | test1: t1 14 | ./t1 15 | 16 | t2: u.o v.o w.o 17 | $(CXX) -o t2 u.o v.o w.o $(LDFLAGS) 18 | 19 | test1: t1 20 | ./t1 21 | 22 | test2: t2 23 | ./t2 24 | 25 | clean: 26 | $(RM) -rf *.o t1 t2 27 | -------------------------------------------------------------------------------- /stack/getstack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t getSp() 4 | { 5 | size_t sp; 6 | __asm__ volatile("mov %%rsp, %0" : "=r"(sp)); 7 | return sp; 8 | } 9 | 10 | int main() 11 | { 12 | size_t sp1 = getSp(); 13 | printf("sp1=%zx\n", sp1); 14 | int sp2 = sp1 - 0x1000; 15 | __asm__ volatile("mov %0, %%esp":"=r"(sp2)); 16 | size_t sp3 = getSp(); 17 | printf("sp2=%x\n", sp2); 18 | printf("sp3=%zx\n", sp3); 19 | } 20 | -------------------------------------------------------------------------------- /sve/t.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace Xbyak_aarch64; 4 | 5 | struct Code : public CodeGenerator { 6 | Code() 7 | { 8 | fadd(z0.s, p0/T_m, z0.s); 9 | ret(); 10 | } 11 | }; 12 | 13 | int main() 14 | try 15 | { 16 | Code c; 17 | c.ready(); 18 | const uint32_t* d = c.getCode(); 19 | printf("d=%08x\n", *d); 20 | } catch (std::exception& e) { 21 | printf("err %s\n", e.what()); 22 | } 23 | -------------------------------------------------------------------------------- /wasm/sample/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es6", 4 | "module": "commonjs", 5 | "rootDir": "./src", 6 | "allowJs": true, 7 | "declaration": true, 8 | "outDir": "./dist", 9 | "stripInternal": true, 10 | "esModuleInterop": true, 11 | "forceConsistentCasingInFileNames": true, 12 | "strict": true, 13 | "skipLibCheck": true 14 | }, 15 | "include": ["./src/**/*"] 16 | } 17 | -------------------------------------------------------------------------------- /webpack/test1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "test1", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "webpack --config config.js" 8 | }, 9 | "keywords": [], 10 | "author": "herumi (https://github.com/herumi/)", 11 | "license": "BSD-3-Clause", 12 | "devDependencies": { 13 | "webpack": "^5.4.0", 14 | "webpack-cli": "^4.2.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /webpack/test2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "test1", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "webpack --config config.js" 8 | }, 9 | "keywords": [], 10 | "author": "herumi (https://github.com/herumi/)", 11 | "license": "BSD-3-Clause", 12 | "devDependencies": { 13 | "webpack": "^5.4.0", 14 | "webpack-cli": "^4.2.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /calling.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | g++ -g calling.cpp verify_stack_frame.cpp -finstrument-functions && ./a.out 3 | */ 4 | #include 5 | #include "verify_stack_frame.hpp" 6 | 7 | void k() 8 | { 9 | puts("k"); 10 | } 11 | 12 | void h() 13 | { 14 | puts("h"); 15 | } 16 | void g() 17 | { 18 | puts("g"); 19 | h(); 20 | } 21 | void f() 22 | { 23 | puts("f"); 24 | g(); 25 | } 26 | 27 | int main() 28 | { 29 | puts("main"); 30 | f(); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /cpp/sample/template_argument_deduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::pair p1 = { 234, 4.2 }; 9 | std::array p2 = { 1, 3, 4, 2 }; 10 | std::mutex m; 11 | std::lock_guard lk1(m); 12 | #ifdef CPP17 13 | std::pair q1 = { 234, 4.2 }; 14 | std::array q2 = { 1, 3, 4, 2 }; 15 | std::lock_guard lk2(m); 16 | #endif 17 | } 18 | -------------------------------------------------------------------------------- /emb/t.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | if (argc == 1) return 1; 9 | std::ifstream ifs(argv[1], std::ios::binary); 10 | std::map m; 11 | std::string s; 12 | while (ifs >> s) { 13 | m[s]++; 14 | } 15 | for (const auto& e : m) { 16 | printf("%s %d\n", e.first.c_str(), e.second); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /make/Makefile: -------------------------------------------------------------------------------- 1 | SRC=a.cpp b.cpp main.cpp 2 | OBJ=$(SRC:.cpp=.o) 3 | DEP=$(SRC:.cpp=.d) 4 | 5 | TARGET=main 6 | CFLAGS+=-O3 -Wall -Wextra -DNDEUBG -g 7 | 8 | all: $(TARGET) 9 | 10 | %.o: %.cpp 11 | $(CXX) -c $< -o $@ $(CFLAGS) -MMD -MP -MF $(@:.o=.d) 12 | 13 | -include $(DEP) 14 | 15 | $(TARGET): $(OBJ) 16 | $(CXX) -o $@ $? $(LDFLAGS) 17 | 18 | clean: 19 | $(RM) -rf $(OBJ) $(DEP) $(TARGET) 20 | 21 | .PHONY: clean 22 | 23 | .SECONDARY: $(DEP) 24 | -------------------------------------------------------------------------------- /debug/aarch64/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS+=-Wall -Wextra -O2 -g 2 | 3 | OBJS=test.o func.o 4 | all: test.exe 5 | 6 | %.o: %.cpp 7 | $(PRE)$(CXX) -c -o $@ $< $(CFLAGS) 8 | %.o: %.s 9 | $(PRE)$(CXX) -c -o $@ $< 10 | 11 | test.exe: $(OBJS) 12 | $(PRE)$(CXX) -o $@ $(OBJS) 13 | 14 | test: test.exe 15 | ./test.exe 16 | 17 | result: test.exe 18 | @gdb -nx -q -batch -x script.txt ./test.exe | grep QQQ 19 | 20 | clean: 21 | $(RM) *.o *.exe 22 | 23 | .PHONY: clean 24 | -------------------------------------------------------------------------------- /go/wasm/cgo/Makefile: -------------------------------------------------------------------------------- 1 | R=$(shell go env GOROOT) 2 | all: liblib.a 3 | 4 | %.o: %.c 5 | gcc -c -O2 -fPIC -Wall -Wextra -o $@ -g $< 6 | 7 | liblib.a: lib.o 8 | ar r $@ $< 9 | 10 | test: liblib.a 11 | go run main.go 12 | 13 | main.wasm: main.go 14 | GOOS=js GOARCH=wasm CGO_ENABLED=1 go build -o main.wasm main.go 15 | 16 | test_go: 17 | env GOOS=js GOARCH=wasm go run -exec=$(R)/misc/wasm/go_js_wasm_exec main.go 18 | 19 | clean: 20 | rm -rf *.o *.a 21 | 22 | -------------------------------------------------------------------------------- /go/cstr/Makefile: -------------------------------------------------------------------------------- 1 | all: libcstr.a 2 | 3 | %.o: %.cpp 4 | $(CXX) -c -O2 -fPIC -Wall -Wextra -o $@ -g $< 5 | 6 | libcstr.a: cstr.o 7 | ar r $@ $< 8 | 9 | main: main.cpp libcstr.a 10 | $(CXX) -o $@ $< -lcstr -L./ 11 | 12 | test: main libcstr.a 13 | echo "run main" 14 | ./main 15 | echo "run cstr.go" 16 | go run cstr.go 17 | echo "valgrind" 18 | go build cstr.go 19 | valgrind --leak-check=full ./cstr 20 | 21 | clean: 22 | rm -rf *.o *.a main cstr 23 | 24 | -------------------------------------------------------------------------------- /wasm/exception/exception.js: -------------------------------------------------------------------------------- 1 | const setupFactory = (createModule) => { 2 | const exports = {} 3 | 4 | const setup = (exports) => { 5 | const mod = exports.mod 6 | mod.func_nocatch = mod._func_nocatch 7 | mod.func_catch = mod._func_catch 8 | } 9 | 10 | exports.init = async () => { 11 | exports.mod = await createModule({ 12 | }) 13 | setup(exports) 14 | } 15 | return exports 16 | } 17 | 18 | module.exports = setupFactory 19 | 20 | -------------------------------------------------------------------------------- /emb/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #objdump -CS -M intel --no-show-raw-insn ./a.out > a.asm 3 | PYTHON=python3 4 | g++ t.cpp -m32 5 | ./a.out t.cpp > a.txt 6 | $PYTHON embed-str.py a.out 7 | 8 | $PYTHON embed-str.py a.out -o emb -s 'BinaryHacksRebooted' 9 | $PYTHON embed-str.py emb 10 | 11 | chmod +x emb 12 | ./emb t.cpp > b.txt 13 | 14 | objdump -D --no-show-raw-insn ./a.out > a.asm 15 | objdump -D --no-show-raw-insn ./emb > b.asm 16 | diff a.asm b.asm 17 | diff a.txt b.txt 18 | -------------------------------------------------------------------------------- /extint/lmul256.ll: -------------------------------------------------------------------------------- 1 | define void @mul256(i64* nocapture %0, i64* nocapture readonly %1, i64* nocapture readonly %2) { 2 | %4 = bitcast i64* %1 to i256* 3 | %5 = load i256, i256* %4, align 8 4 | %6 = bitcast i64* %2 to i256* 5 | %7 = load i256, i256* %6, align 8 6 | %8 = zext i256 %5 to i512 7 | %9 = zext i256 %7 to i512 8 | %10 = mul nuw i512 %9, %8 9 | %11 = bitcast i64* %0 to i512* 10 | store i512 %10, i512* %11, align 8 11 | ret void 12 | } 13 | 14 | -------------------------------------------------------------------------------- /security/cryptorec/img/1-rtt.txt: -------------------------------------------------------------------------------- 1 | @startuml 2 | title 1-RTT 3 | 4 | participant "クライアント" as C 5 | participant "サーバ" as S 6 | 7 | C -> S: ClientHello(KeyShare, psk_key_exchange_modes, pre_shared_key) 8 | note right: 事前に渡したPSKの使用許可 9 | S -> C: ServerHello(KeyShare, pre_shared_key) 10 | S -> C: EncryptedExtensions 11 | S -> C: Finished 12 | note left: サーバが指示したPSK仕様 13 | C -> S: Finished 14 | S -> C: Application Data 15 | 16 | @enduml 17 | 18 | -------------------------------------------------------------------------------- /static-cstr/vc-test.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set CFLAGS=/nologo /EHsc /Zi /c /O2 4 | 5 | cl %CFLAGS% main.cpp 6 | cl %CFLAGS% sub1.cpp 7 | cl %CFLAGS% sub2.cpp 8 | 9 | link /out:main1.exe main.obj sub1.obj sub2.obj 10 | link /out:main2.exe main.obj sub2.obj sub1.obj 11 | link /out:main3.exe sub1.obj sub2.obj main.obj 12 | link /out:main4.exe sub2.obj sub1.obj main.obj 13 | 14 | for %%i in (1 2 3 4) do ( 15 | main%%i.exe 16 | ) 17 | 18 | 19 | exit /b 20 | 21 | :run 22 | -------------------------------------------------------------------------------- /fma/Makefile: -------------------------------------------------------------------------------- 1 | test: fma_test.exe 2 | @echo OP=$(OP) 3 | ./fma_test.exe 4 | 5 | fma_test.exe: fma_test.o fma.o 6 | $(CXX) -o $@ $^ 7 | 8 | fma_test.o: fma_test.cpp func.h 9 | $(CXX) -c -o $@ $< -DNDEBUG -O2 -g -I ../../cybozulib/include 10 | 11 | func.h: gen.py 12 | python3 gen.py -header > $@ 13 | 14 | fma.o: fma.S 15 | $(CXX) -c -o $@ $< 16 | 17 | OP?=fma 18 | fma.S: gen.py 19 | python3 gen.py -m gas -op $(OP) > $@ 20 | 21 | clean: 22 | rm -f *.S *.o *.exe func.h 23 | 24 | -------------------------------------------------------------------------------- /security/cryptorec/img/tls1.2.txt: -------------------------------------------------------------------------------- 1 | @startuml 2 | title TLS1.2 3 | 4 | participant "クライアント" as C 5 | participant "サーバ" as S 6 | 7 | C -> S: ClientHello 8 | S -> C: ServerHello 9 | S -> C: ServerCertificate 10 | S -> C: ServerKeyExchange 11 | S -> C: ServerHelloDone 12 | C -> S: ClientKeyExchange 13 | C -> S: ChangeCipherSpec 14 | C -> S: Finished 15 | S -> C: ChangeCipherSpec 16 | note right: ここから暗号化する 17 | S -> C: Finished 18 | 19 | @enduml 20 | 21 | -------------------------------------------------------------------------------- /cpp/extint.cpp: -------------------------------------------------------------------------------- 1 | // for clang+--11 2 | #include 3 | #include 4 | 5 | typedef unsigned _ExtInt(256) uint256_t; 6 | 7 | void dump(const uint256_t& x) 8 | { 9 | const uint64_t *p = (const uint64_t*)&x; 10 | for (int i = 0; i < 4; i++) { 11 | printf("%016lx", p[3 - i]); 12 | } 13 | printf("\n"); 14 | } 15 | 16 | int main() 17 | { 18 | uint256_t x = 1; 19 | uint256_t y = 2; 20 | for (int i = 0; i < 256; i++) { 21 | dump(x); 22 | x *= y; 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /map/leaf/map.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Leaflet 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /double-link/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-g -O2 2 | test1: test.c lib1.so lib2.so 3 | gcc -o $@ $< $(CFLAGS) ./lib1.so ./lib2.so 4 | 5 | test2: test.c lib1.so lib2.so 6 | gcc -o $@ $< $(CFLAGS) ./lib2.so ./lib1.so 7 | 8 | lib1.so: ./lib.c 9 | gcc -o $@ $< $(CFLAGS) -fPIC -shared -DLIB=1 10 | 11 | lib2.so: ./lib.c 12 | gcc -o $@ $< $(CFLAGS) -fPIC -shared -DLIB=2 13 | 14 | test: test1 test2 15 | echo test1 16 | ./test1 17 | echo test2 18 | ./test2 19 | 20 | clean: 21 | rm -rf *.so test1 test2 22 | 23 | -------------------------------------------------------------------------------- /softmax/t.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def softmax(v): 4 | sum = 0 5 | for x in v: 6 | sum += math.exp(x) 7 | ret = [] 8 | for x in v: 9 | ret.append(math.exp(x)/sum) 10 | return ret 11 | 12 | def logsoftmax(v): 13 | maxv = max(v) 14 | sum = 0 15 | for x in v: 16 | sum += math.exp(x - maxv) 17 | sum = math.log(sum) 18 | ret = [] 19 | for x in v: 20 | ret.append(x - maxv - sum) 21 | return ret 22 | 23 | print(softmax([90,89])) 24 | print(logsoftmax([90,89])) 25 | -------------------------------------------------------------------------------- /fft/Makefile: -------------------------------------------------------------------------------- 1 | MKL_DIR=/opt/intel/mkl/lib/intel64/ 2 | MKL_LIB=-L /opt/intel/mkl/lib/intel64/ -Wl,--start-group ${MKL_DIR}/libmkl_cdft_core.a ${MKL_DIR}/libmkl_intel_lp64.a ${MKL_DIR}/libmkl_sequential.a ${MKL_DIR}/libmkl_core.a ${MKL_DIR}/libmkl_blacs_intelmpi_lp64.a -Wl,--end-group -lpthread -ldl 3 | CFLAGS=-I /opt/intel/mkl/include -I ../../TFHEpp/thirdparties/spqlios 4 | LDFLAGS=../../TFHEpp/build/thirdparties/spqlios/libspqlios.a $(MKL_LIB) 5 | 6 | test: test.cpp 7 | $(CXX) test.cpp -o $@ $(CFLAGS) $(LDFLAGS) 8 | -------------------------------------------------------------------------------- /dev/dead-lock.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | g++ -std=c++11 -g -pthread dead-lock.cpp 3 | */ 4 | #include 5 | #include 6 | #include 7 | 8 | void f(std::mutex& m) 9 | { 10 | puts("f"); 11 | puts("try lock m"); 12 | std::lock_guard lk(m); 13 | puts("locked m"); 14 | } 15 | 16 | int main() 17 | try 18 | { 19 | std::mutex m; 20 | m.lock(); 21 | std::thread t(f, std::ref(m)); 22 | t.join(); 23 | } catch (std::exception& e) { 24 | printf("err %s\n", e.what()); 25 | return 1; 26 | } 27 | -------------------------------------------------------------------------------- /go/rand.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func readRand() []byte { 9 | file, err := os.Open("/dev/urandom") 10 | if err != nil { 11 | fmt.Printf("can't open\n") 12 | return nil 13 | } 14 | defer file.Close() 15 | buf := make([]byte, 16) 16 | n, err := file.Read(buf) 17 | if err != nil { 18 | fmt.Printf("read err\n") 19 | return nil 20 | } 21 | return buf[:n] 22 | } 23 | 24 | func main() { 25 | buf := readRand() 26 | fmt.Printf("buf=%v\n", buf) 27 | } 28 | -------------------------------------------------------------------------------- /longint/sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern "C" int sum(int); 4 | extern "C" int sum2(int); 5 | extern "C" int test1(int); 6 | extern "C" int test2(int); 7 | 8 | int main() 9 | { 10 | puts("sum"); 11 | for (int i = 1; i < 11; i++) { 12 | printf("%d %d %d\n", i, sum(i), sum2(i)); 13 | } 14 | puts("test1"); 15 | for (int i = 2; i < 6; i++) { 16 | printf("%d %d\n", i, test1(i)); 17 | } 18 | puts("test2"); 19 | for (int i = 0; i < 4; i++) { 20 | printf("%d %d\n", i, test2(i)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /nop.asm: -------------------------------------------------------------------------------- 1 | ; nasm -fwin32 nop.asm / nasm -fwin64 nop.asm 2 | ; link nop.obj msvcrt.lib ; nop.exe 3 | ; nasm -felf32 nop.asm -o nop.o && gcc -m32 nop.o 4 | ; nasm -felf64 nop.asm -o nop.o && gcc -m64 nop.o 5 | global main 6 | 7 | section .text 8 | main: 9 | db 0x0f, 0x1f, 0x44, 0x00, 0x00 10 | db 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00 11 | db 0x66, 0x90 12 | db 0x0f, 0x1f, 0x00 13 | nop 14 | db 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 15 | db 0x0f, 0x1f, 0x40, 0x00 16 | db 0xc2, 0x00, 0x00 17 | -------------------------------------------------------------------------------- /broadcast/test.asm: -------------------------------------------------------------------------------- 1 | global b 2 | global c 3 | global f 4 | global g 5 | 6 | segment .data 7 | a dq 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 8 | b dq 0, 0, 0, 0, 0, 0, 0, 0 9 | c dq 0, 0, 0, 0, 0, 0, 0, 0 10 | 11 | segment .text 12 | f: 13 | vxorpd zmm0, zmm0, zmm0 14 | mov rax, a 15 | vaddpd zmm0, zmm0, [rax] 16 | mov rax, b 17 | vmovdqu64 [rax], zmm0 18 | ret 19 | 20 | g: 21 | vxorpd zmm0, zmm0, zmm0 22 | mov rax, a 23 | vaddpd zmm0, zmm0, [rax]{1to8} 24 | mov rax, c 25 | vmovdqu64 [rax], zmm0 26 | ret 27 | -------------------------------------------------------------------------------- /debug/aarch64/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern "C" uint32_t func(uint32_t, uint32_t); 5 | 6 | int main() 7 | { 8 | const uint32_t tbl[] = { 0, 1, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff }; 9 | const size_t n = sizeof(tbl) / sizeof(tbl[0]); 10 | for (size_t i = 0; i < n; i++) { 11 | for (size_t j = 0; j < n; j++) { 12 | uint32_t x = tbl[i]; 13 | uint32_t y = tbl[j]; 14 | uint32_t z = func(x, y); 15 | printf("%08x+%08x=%08x\n", x, y, z); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /dll/mk.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | rm -f bin\*.dll bin\*.exe *.obj *.lib 3 | rem python3 gen.py -win -m masm > asm.asm 4 | rem ml64 /c asm.asm 5 | python3 gen.py -win -m nasm > asm.asm 6 | nasm -fwin64 asm.asm 7 | cl /c /O2 sub.cpp 8 | link /nologo /DLL /OUT:bin\suba.dll asm.obj sub.obj /implib:suba.lib 9 | dumpbin /exports suba.lib 10 | cl /O2 main.cpp suba.lib /Fe:bin\main.exe 11 | bin\main.exe 12 | 13 | lib /nologo /OUT:subb.lib /nodefaultlib sub.obj asm.obj 14 | cl /O2 main.cpp subb.lib /Fe:bin\mainb.exe 15 | bin\mainb.exe 16 | -------------------------------------------------------------------------------- /cpp/item22-1-main.cpp: -------------------------------------------------------------------------------- 1 | #include "item22-1.hpp" 2 | 3 | int main() 4 | { 5 | { 6 | puts("A"); 7 | A a0; 8 | a0.put(); 9 | A a1 = a0; 10 | a1.put(); 11 | A a2(std::move(a0)); 12 | a0.put(); 13 | a2.put(); 14 | A a3(a0); 15 | a3.put(); 16 | a0 = a1; 17 | a0.put(); 18 | } 19 | { 20 | puts("B"); 21 | B a0; 22 | a0.put(); 23 | B a1 = a0; 24 | a1.put(); 25 | B a2(std::move(a0)); 26 | a0.put(); 27 | a2.put(); 28 | B a3(a0); 29 | a3.put(); 30 | a0 = a1; 31 | a0.put(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /go/seccon2013/run.py: -------------------------------------------------------------------------------- 1 | import subprocess, sys 2 | 3 | def main(): 4 | if len(sys.argv) != 3: 5 | print "run exe file" 6 | exit(1) 7 | exe = sys.argv[1] 8 | name = sys.argv[2] 9 | f = open(name) 10 | for line in f: 11 | line = line.split() 12 | print line[0], line[1] 13 | try: 14 | p = subprocess.check_output([exe, line[1]]) 15 | if p.find("0609") >= 0: 16 | print "found", line[0] 17 | break 18 | except: 19 | print "err" 20 | 21 | if __name__ == '__main__': 22 | main() 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /naf.py: -------------------------------------------------------------------------------- 1 | def convNaf(x): 2 | n3 = bin(x * 3)[2:] 3 | n1 = bin(x)[2:] 4 | padding = len(n3) - len(n1) 5 | if padding: 6 | n1 = '0' * padding + n1 7 | print n3 8 | print n1 9 | ret = [] 10 | for i in xrange(len(n3)): 11 | ret.append(ord(n3[i]) - ord(n1[i])) 12 | return ret[:-1] 13 | 14 | def getVal(v): 15 | z = 0 16 | n = len(v) 17 | for i in xrange(n): 18 | z += (1 << i) * v[n - 1 - i] 19 | return z 20 | 21 | 22 | z = 4965661367192848881 23 | print z 24 | v = convNaf(z) 25 | print v 26 | print getVal(v) 27 | -------------------------------------------------------------------------------- /netspeed/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-Wall -Wextra -O3 -I../../cybozulib/include -pthread -std=c++14 2 | LDFLAGS=-lpthread 3 | 4 | ALL_SRC=netspeed.cpp roundtrip.cpp 5 | all: netspeed roundtrip 6 | 7 | %.o: %.cpp 8 | $(CXX) $(CFLAGS) -c $< -o $@ -MMD -MP -MF $(@:.o=.d) 9 | 10 | netspeed: netspeed.o 11 | $(CXX) $< -o $@ $(LDFLAGS) 12 | 13 | roundtrip: roundtrip.o 14 | $(CXX) $< -o $@ $(LDFLAGS) 15 | 16 | test: netspeed 17 | ./netspeed 18 | 19 | clean: 20 | rm -rf netspeed roundtrip *.o *.d 21 | 22 | .SECONDARY: $(ALL_SRC:.cpp=.o) 23 | -------------------------------------------------------------------------------- /security/cryptorec/img/0-rtt.txt: -------------------------------------------------------------------------------- 1 | @startuml 2 | title 0-RTT 3 | 4 | participant "クライアント" as C 5 | participant "サーバ" as S 6 | 7 | C -> S: ClientHello(early_data, KeyShare, psk_key_exchange_modes, pre_shared_key) 8 | note left: 事前に取得したPSKを使用 9 | C -> S: Application Data 10 | S -> C: ServerHello(pre_shared_key, KeyShare) 11 | S -> C: EncryptedExtensions(early_data) 12 | S -> C: Finished 13 | C -> S: EndOfEarlyData 14 | C -> S: Finished 15 | S -> C: Application Data 16 | C -> S: Application Data 17 | @enduml 18 | -------------------------------------------------------------------------------- /go/rand/rand_test.go: -------------------------------------------------------------------------------- 1 | package rand 2 | 3 | import ( 4 | "crypto/rand" 5 | "fmt" 6 | "io" 7 | "testing" 8 | ) 9 | 10 | var sRandReader io.Reader 11 | 12 | func TestRand(t *testing.T) { 13 | /* 14 | buf := ReadRand() 15 | if buf == nil { 16 | t.Fatalf("ReadRand err") 17 | } 18 | fmt.Printf("buf=%v\n", buf) 19 | */ 20 | buf := make([]byte, 16) 21 | sRandReader = rand.Reader 22 | 23 | n, err := sRandReader.Read(buf) 24 | if err != nil { 25 | t.Fatalf("rand.Read err") 26 | } 27 | fmt.Printf("buf2=%v\n", buf[:n]) 28 | } 29 | -------------------------------------------------------------------------------- /atomic/atomic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void store0(int& x) 4 | { 5 | x = 5; 6 | } 7 | 8 | int load0(const int& x) 9 | { 10 | return x; 11 | } 12 | 13 | void store1(std::atomic& x) 14 | { 15 | x = 3; 16 | } 17 | 18 | int load1(const std::atomic& x) 19 | { 20 | return x; 21 | } 22 | 23 | void store2(std::atomic& x) 24 | { 25 | x.store(7, std::memory_order_release); 26 | } 27 | 28 | int load2(const std::atomic& x) 29 | { 30 | return x.load(std::memory_order_acquire); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /hmac.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::string out(21, 0); 8 | std::string key("\xC9\xFA\xCA\x54\x36\x84\x99\x06\xB6\x00\xDE\x95\xE1\x55\xB4\x7A\x01\xAB\xED\xD0", 20); 9 | cybozu::Mmap m("hmac.cpp"); 10 | 11 | unsigned int outLen = 0; 12 | if (HMAC(EVP_sha1(), key.c_str(), key.size(), (const uint8_t*)m.get(), m.size(), (uint8_t*)&out[0], &outLen)) { 13 | out.resize(outLen); 14 | fwrite(out.c_str(), 1, outLen, stdout); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /swig/byte-array/sample/SampleJNI.java: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------------- 2 | * This file was automatically generated by SWIG (http://www.swig.org). 3 | * Version 3.0.12 4 | * 5 | * Do not make changes to this file unless you know what you are doing--modify 6 | * the SWIG interface file instead. 7 | * ----------------------------------------------------------------------------- */ 8 | 9 | package sample; 10 | 11 | public class SampleJNI { 12 | public final static native byte[] getStr(); 13 | } 14 | -------------------------------------------------------------------------------- /python/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | json sample 6 | 7 | 8 | 9 |
10 | key : 11 |

12 | 13 | value : 14 |
15 | 16 | 17 | 18 | 19 | result :
20 |
21 | 22 | 23 | -------------------------------------------------------------------------------- /react/test1/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | React App 12 | 13 | 14 | 15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /rust/mcl/mcl-sys/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::path::PathBuf; 3 | 4 | fn main() { 5 | println!("cargo:rustc-link-lib=mcl"); 6 | let bindings = bindgen::Builder::default() 7 | .header("wrapper.h") 8 | // don't insert space between -I and path 9 | .clang_arg("-I../../../../mcl/include/") 10 | .generate() 11 | .expect("Unable to generate bindings"); 12 | 13 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 14 | bindings 15 | .write_to_file(out_path.join("bindings.rs")) 16 | .expect("Couldn't write bindings"); 17 | } 18 | -------------------------------------------------------------------------------- /wasm/exception/test.js: -------------------------------------------------------------------------------- 1 | const ex = require('./index.js') 2 | 3 | ex.init().then(() => { 4 | try { 5 | console.log('nocatch') 6 | const f = ex.mod.func_nocatch 7 | console.log(`nocatch 5 -> ${f(5)}`) 8 | console.log(`nocatch -5 -> ${f(-5)}`) 9 | } catch (e) { 10 | console.log(`err ${e}`) 11 | } 12 | try { 13 | console.log('catch') 14 | const g = ex.mod.func_catch 15 | console.log(`catch 5 -> ${g(5)}`) 16 | console.log(`catch -5 -> ${g(-5)}`) 17 | } catch (e) { 18 | console.log(`err ${e}`) 19 | } 20 | }) 21 | -------------------------------------------------------------------------------- /cpp/sample/constexpr_if.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | template 7 | void put(const T& x) 8 | { 9 | printf("type=%s\n", typeid(T).name()); 10 | if constexpr (std::is_integral_v) { 11 | printf("int %d\n", x); 12 | } else if constexpr (std::is_convertible_v) { 13 | printf("str %s\n", x); 14 | } else { 15 | std::cout << "other " << x << std::endl; 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | put(123); 22 | put("abc"); 23 | put(1.2); 24 | } 25 | -------------------------------------------------------------------------------- /static-cstr/Makefile: -------------------------------------------------------------------------------- 1 | 2 | %.o: %.cpp 3 | $(CXX) $(CFLAGS) -c $< -o $@ 4 | 5 | main1.exe: main.o sub1.o sub2.o 6 | $(PRE)$(CXX) $^ -o $@ $(LDFLAGS) 7 | 8 | main2.exe: main.o sub2.o sub1.o 9 | $(PRE)$(CXX) $^ -o $@ $(LDFLAGS) 10 | 11 | main3.exe: sub1.o sub2.o main.o 12 | $(PRE)$(CXX) $^ -o $@ $(LDFLAGS) 13 | 14 | main4.exe: sub2.o sub1.o main.o 15 | $(PRE)$(CXX) $^ -o $@ $(LDFLAGS) 16 | 17 | test: main1.exe main2.exe main3.exe main4.exe 18 | ./main1.exe 19 | ./main2.exe 20 | ./main3.exe 21 | ./main4.exe 22 | 23 | clean: 24 | \rm -rf *.o *.exe 25 | -------------------------------------------------------------------------------- /neon/add.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace Xbyak_aarch64; 5 | 6 | class Generator : public CodeGenerator { 7 | public: 8 | Generator() 9 | { 10 | fadd(v0.s, v0.s, v0.s); 11 | ret(); 12 | } 13 | }; 14 | 15 | int main() { 16 | Generator gen; 17 | gen.ready(); 18 | auto f = gen.getCode(); 19 | float32x4_t a = { -3.4, 4.2, 1.5, 5.3 }; 20 | a = f(a); 21 | float v[4]; 22 | memcpy(v, &a, sizeof(v)); 23 | printf("%f %f %f %f\n", v[0], v[1], v[2], v[3]); 24 | } 25 | -------------------------------------------------------------------------------- /swig/byte-array/sample/Sample.java: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------------- 2 | * This file was automatically generated by SWIG (http://www.swig.org). 3 | * Version 3.0.12 4 | * 5 | * Do not make changes to this file unless you know what you are doing--modify 6 | * the SWIG interface file instead. 7 | * ----------------------------------------------------------------------------- */ 8 | 9 | package sample; 10 | 11 | public class Sample { 12 | public static byte[] getStr() { return SampleJNI.getStr(); } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /xbyak_split/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | CFLAGS="-I./ -ftime-report" 3 | 4 | for CXX in g++-8 clang++-10 ; do 5 | echo ${CXX} 6 | for OPT in -O0 -Ofast ; do 7 | echo ${OPT} 8 | for NAME in t1 t2 ; do 9 | echo ${NAME} 10 | for MODE in 0 1 ; do 11 | echo time ${CXX} ${CFLAGS} ${OPT} -DXBYAK_SPLIT=${MODE} -c ${NAME}.cpp \> result/${NAME}-${MODE}${OPT}-${CXX}.txt 2>&1 12 | time ${CXX} ${CFLAGS} ${OPT} -DXBYAK_SPLIT=${MODE} -c ${NAME}.cpp > result/${NAME}-${MODE}${OPT}-${CXX}.txt 2>&1 13 | done 14 | done 15 | done 16 | done 17 | -------------------------------------------------------------------------------- /wasm/exception/exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #if defined(__EMSCRIPTEN__) 4 | #define API __attribute__((used)) 5 | #elif defined(__wasm__) 6 | #define API __attribute__((visibility("default"))) 7 | #endif 8 | 9 | extern "C" { 10 | API int func_nocatch(int x); 11 | API int func_catch(int x); 12 | } 13 | 14 | int func_nocatch(int x) 15 | { 16 | if (x < 0) throw std::runtime_error("bad x"); 17 | return x * 2; 18 | } 19 | 20 | int func_catch(int x) 21 | { 22 | try { 23 | return func_nocatch(x); 24 | } catch (...) { 25 | return -1; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /extint/wasm/carry.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool add(uint64_t *z, uint64_t x, uint64_t y) 4 | { 5 | uint64_t t = x + y; 6 | *z = t; 7 | return t < x; 8 | } 9 | 10 | uint64_t adc64(uint64_t *z, uint64_t x, uint64_t y, uint64_t c) 11 | { 12 | uint64_t xc = x + c; 13 | if (xc < c) { 14 | *z = y; 15 | } else { 16 | xc += y; 17 | c = xc < y; 18 | *z = xc; 19 | } 20 | return xc; 21 | } 22 | 23 | uint32_t adc32(uint32_t *z, uint32_t x, uint32_t y, uint32_t c) 24 | { 25 | uint64_t t = uint64_t(x) + y + c; 26 | *z = uint32_t(t); 27 | return t >> 32; 28 | } 29 | -------------------------------------------------------------------------------- /go/thread/wait.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void put(int i) 7 | { 8 | std::cout << "thead=" << std::this_thread::get_id() << " i=" << i << std::endl; 9 | std::this_thread::sleep_for(std::chrono::seconds(1)); 10 | } 11 | extern "C" void waitTask(int n) 12 | { 13 | const int N = 8; 14 | std::thread th[N]; 15 | if (n > N) n = N; 16 | for (int i = 0; i < n; i++) { 17 | th[i] = std::thread(put, i); 18 | } 19 | for (int i = 0; i < n; i++) { 20 | th[i].join(); 21 | } 22 | puts("end"); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /security/cryptorec/img/tls1.3.txt: -------------------------------------------------------------------------------- 1 | @startuml 2 | title TLS1.3 3 | 4 | participant "クライアント" as C 5 | participant "サーバ" as S 6 | 7 | C -> S: ClientHello(KeyShare, signature_algorithms) 8 | S -> C: ServerHello(KeyShare) 9 | note right: ここから暗号化する 10 | S -> C: EncryptedExtensions 11 | S -> C: Certificate 12 | S -> C: CertificateVerify 13 | S -> C: Finished 14 | C -> S: Certificate 15 | C -> S: CertificateVerify 16 | C -> S: Finished 17 | S -> C: NewSessionTicket(ticket) 18 | note left: PSK取得 19 | C -> S: Application Data 20 | 21 | @enduml 22 | 23 | -------------------------------------------------------------------------------- /whole-archive/Makefile: -------------------------------------------------------------------------------- 1 | 2 | CFLAGS=-Wall -Wextra -L./ 3 | 4 | LIBS=libsub.a libinit_f.a 5 | ng: main.cpp $(LIBS) 6 | gcc -o $@ $< $(CFLAGS) -lsub -linit_f 7 | 8 | ok: main.cpp $(LIBS) 9 | gcc -o $@ $< $(CFLAGS) -lsub -Wl,--whole-archive libinit_f.a -Wl,--no-whole-archive 10 | 11 | libinit_f.a: init_f.o 12 | ar r $@ $< 13 | libsub.a: sub.o 14 | ar r $@ $< 15 | 16 | sub.o: sub.cpp 17 | init_f.o: init_f.cpp 18 | 19 | .o: .cpp 20 | gcc -c $< -fPIC $(CFLAGS) 21 | 22 | test_ng: ng 23 | ./ng 24 | 25 | test_ok: ok 26 | ./ok 27 | 28 | clean: 29 | rm -rf *.a *.so *.o ng ok 30 | -------------------------------------------------------------------------------- /xbyak_fixed/Makefile: -------------------------------------------------------------------------------- 1 | all: t2.exe t4.exe 2 | CFLAGS+=-I../../xbyak -I../../cybozulib/include -Wall -Wextra 3 | 4 | %.o: %.asm 5 | nasm -f elf64 $< 6 | 7 | t1.exe: t1.cpp 8 | $(CXX) $(CFLAGS) -o $@ $< 9 | t2.exe: t2.cpp t1-out.o 10 | $(CXX) $(CFLAGS) -o $@ $? 11 | 12 | t1-out.asm: t1.exe 13 | ./t1.exe $@ 14 | 15 | t3.exe: t3.cpp 16 | $(CXX) $(CFLAGS) -o $@ $< 17 | t4.exe: t4.cpp t3-out.o 18 | $(CXX) $(CFLAGS) -o $@ $? 19 | 20 | t3-out.asm: t3.exe 21 | ./t3.exe $@ 22 | 23 | test: t2.exe t4.exe 24 | ./t2.exe 25 | ./t4.exe 26 | 27 | clean: 28 | rm -rf *.exe *.o *.asm 29 | -------------------------------------------------------------------------------- /fma/fma_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "func.h" 5 | 6 | const int N = 5*7*8*9*1000; 7 | 8 | template 9 | void bench() 10 | { 11 | int C = 100; 12 | CYBOZU_BENCH_C("", C, funcN, N); 13 | printf("n=%d %f\n", n, cybozu::bench::g_clk.getClock()/double(N)/C); 14 | } 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | cybozu::Option opt; 19 | int n; 20 | opt.appendOpt(&n, 0, "n", "unroll n"); 21 | if (!opt.parse(argc, argv)) { 22 | opt.usage(); 23 | return 0; 24 | } 25 | benchAll(n); 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /static-cstr/sub1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifdef __GNUC__ 4 | #define MCL_ATTRIBUTE __attribute__((constructor)) 5 | #else 6 | #define MCL_ATTRIBUTE 7 | #endif 8 | 9 | namespace { 10 | 11 | static struct X1 { 12 | X1() { puts("sub1 X1 cstr"); } 13 | } x1; 14 | 15 | static void MCL_ATTRIBUTE initSub1() 16 | { 17 | puts("initSub1"); 18 | } 19 | 20 | #ifdef _MSC_VER 21 | #pragma section(".CRT$XCT", read) 22 | __declspec(allocate(".CRT$XCT")) void(*ptr_initSub1)() = initSub1; 23 | #endif 24 | 25 | static struct X2 { 26 | X2() { puts("sub1 X2 cstr"); } 27 | } x2; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /wasm/sample/Makefile: -------------------------------------------------------------------------------- 1 | MCL_JS=./src/sample_c.js 2 | 3 | EMCC_OPT+=-s WASM=1 -s NO_EXIT_RUNTIME=1 -s NODEJS_CATCH_EXIT=0 -s NODEJS_CATCH_REJECTION=0 4 | EMCC_OPT+=-s MODULARIZE=1 5 | EMCC_OPT+=-s STRICT_JS=1 6 | EMCC_OPT+=-s SINGLE_FILE=1 7 | EMCC_OPT+=-s DISABLE_EXCEPTION_CATCHING=1 8 | EMCC_OPT+=--minify 0 9 | 10 | all: $(MCL_JS) 11 | 12 | $(MCL_JS): ./src/sample.cpp 13 | emcc -o $@ src/sample.cpp $(EMCC_OPT) -fno-exceptions -fno-rtti 14 | # disable require fs, path 15 | #perl -i -pe 's@(.* = require\(.*)@//\1@' $@ 16 | 17 | clean: 18 | rm -rf $(MCL_JS) 19 | 20 | .PHONY: clean 21 | -------------------------------------------------------------------------------- /bbs/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es2020", 4 | "lib": ["es2020", "dom"], 5 | "module": "es2020", 6 | "rootDir": "./", 7 | "allowJs": true, 8 | "declaration": false, 9 | "outDir": "./browser", 10 | "stripInternal": true, 11 | "esModuleInterop": true, 12 | "forceConsistentCasingInFileNames": true, 13 | "strict": true, 14 | "skipLibCheck": true, 15 | "moduleResolution": "node" 16 | }, 17 | "include": ["./wasm/bbs.ts", "./browser/demo.ts"], 18 | "exclude": ["./wasm/index.ts", "./wasm/getRandomValues.ts"] 19 | } 20 | -------------------------------------------------------------------------------- /webpack/test2/src/Makefile: -------------------------------------------------------------------------------- 1 | all: add_c.js 2 | 3 | EMCC_OPT=-O3 -DNDEBUG 4 | EMCC_OPT+=-s WASM=1 -s NO_EXIT_RUNTIME=1 -s NODEJS_CATCH_EXIT=0 -s NODEJS_CATCH_REJECTION=0 5 | EMCC_OPT+=-s MODULARIZE=1 6 | EMCC_OPT+=-s EXPORT_NAME='createModule' 7 | EMCC_OPT+=-s STRICT_JS=1 8 | EMCC_OPT+=-s SINGLE_FILE=1 9 | EMCC_OPT+=-s MINIFY_HTML=0 10 | EMCC_OPT+=-s ABORTING_MALLOC=0 11 | EMCC_OPT+=-s FILESYSTEM=0 12 | EMCC_OPT+=--minify 0 13 | 14 | add_c.js: add.cpp Makefile 15 | emcc -o add_c.js add.cpp $(EMCC_OPT) 16 | 17 | test: add.js add_c.js 18 | node test.js 19 | 20 | clean: 21 | rm -rf add_c.js 22 | -------------------------------------------------------------------------------- /static-cstr/sub2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifdef __GNUC__ 4 | #define MCL_ATTRIBUTE __attribute__((constructor)) 5 | #else 6 | #define MCL_ATTRIBUTE 7 | #endif 8 | 9 | namespace { 10 | 11 | static struct X1 { 12 | X1() { puts("sub2 X1 cstr"); } 13 | } x1; 14 | 15 | static void MCL_ATTRIBUTE initSub2() 16 | { 17 | puts("initSub2"); 18 | } 19 | 20 | #ifdef _MSC_VER 21 | #pragma section(".CRT$XCT", read) 22 | __declspec(allocate(".CRT$XCT")) void(*ptr_initSub2)() = initSub2; 23 | #endif 24 | 25 | static struct X2 { 26 | X2() { puts("sub2 X2 cstr"); } 27 | } x2; 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /avx-512/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-g -Wall -Wextra -mavx512f -mavx512ifma -I ../../mcl/include -Ofast 2 | 3 | test: test.exe 4 | ./$< 5 | 6 | bench_test: bench_test.exe 7 | ./$< 8 | 9 | test.exe: test.o gen.o 10 | $(CXX) -o $@ $^ 11 | 12 | %.o: %.cpp 13 | $(CXX) -c -o $@ $< $(CFLAGS) 14 | 15 | %.o: %.S 16 | $(CXX) -c -o $@ $< 17 | 18 | gen.S: gen.py Makefile 19 | python3 gen.py -m gas > $@ 20 | 21 | bench.S: bench.py Makefile 22 | python3 bench.py -m gas > $@ 23 | 24 | bench_test.exe: bench_test.o bench.o 25 | $(CXX) -o $@ $^ 26 | 27 | clean: 28 | rm -rf *.o *.exe *.S 29 | 30 | .PHONY: test clean 31 | -------------------------------------------------------------------------------- /zig/run.sh: -------------------------------------------------------------------------------- 1 | zig fmt *.zig 2 | zig build 3 | zig-out/bin/bls-example 4 | #zig build-exe main.zig -I ../../bls/include/ -I ../../bls/mcl/include/ -L ../../bls-eth-go-binary/bls/lib/linux/amd64/ -l bls384_256 -l stdc++ -femit-bin=bls.exe && ./bls.exe 5 | # 6 | #zig fmt build.zig.zon 7 | #zig build --summary all 8 | #zig build-exe bls-test.zig -I ../../bls/include/ -I ../../bls/mcl/include/ -L ../../bls-eth-go-binary/bls/lib/linux/amd64/ -l bls384_256 -l stdc++ && ./bls-test 9 | #zig test bls-test.zig -I ../../bls/include/ -I ../../bls/mcl/include/ -L ../../bls-eth-go-binary/bls/lib/linux/amd64/ -l bls384_256 -l stdc++ 10 | -------------------------------------------------------------------------------- /react/test2/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | module.exports = { 4 | mode: 'development', 5 | entry: './src/index.js', 6 | /* 7 | devServer: { 8 | host: '0.0.0.0', 9 | port: 3000 10 | }, 11 | */ 12 | output: { 13 | filename: 'main.js', 14 | path: path.resolve(__dirname, 'public') 15 | }, 16 | resolve: { 17 | fallback: { 18 | crypto: false 19 | }, 20 | }, 21 | module: { 22 | rules: [ 23 | { 24 | test: /\.(js|jsx)$/, 25 | exclude: /node_modules/, 26 | loader: 'babel-loader' 27 | } 28 | ] 29 | } 30 | }; 31 | 32 | -------------------------------------------------------------------------------- /react/test1/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import mcl from 'mcl-wasm/'; 4 | 5 | class Layout extends React.Component { 6 | constructor() { 7 | super(); 8 | this.state = { msg:"initializing..." } 9 | mcl.init().then(() => { 10 | const x = new mcl.Fr() 11 | x.setStr('123') 12 | this.setState({ 13 | msg: x.getStr() 14 | }) 15 | }) 16 | } 17 | render() { 18 | return ( 19 |

{this.state.msg}

20 | ); 21 | } 22 | } 23 | 24 | const app = document.getElementById('app'); 25 | ReactDOM.render(, app); 26 | 27 | -------------------------------------------------------------------------------- /strlen_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | size_t internal_strlen(const char *s) { 7 | size_t i = 0; 8 | while (s[i]) i++; 9 | return i; 10 | } 11 | 12 | size_t (*g_p1)(const char*) = strlen; 13 | size_t (*g_p2)(const char*) = internal_strlen; 14 | 15 | int main() 16 | { 17 | const size_t N = 256; 18 | char buf[N]; 19 | memset(buf, 1, N); 20 | buf[N - 1] = '\0'; 21 | const int C = 10000; 22 | CYBOZU_BENCH_C("C strlen", C, g_p1, buf); 23 | CYBOZU_BENCH_C("inernal_strlen", C, g_p2, buf); 24 | 25 | printf("len=%zd %zd\n", g_p1(buf), g_p2(buf)); 26 | } 27 | -------------------------------------------------------------------------------- /cpp/std_function_gcc_bug.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84858 3 | g++ std_function_gcc_bug.cpp -O2 -std=c++11 && ./a.out 4 | */ 5 | #include 6 | #include 7 | #include 8 | 9 | void f() noexcept 10 | { 11 | puts("f"); 12 | } 13 | 14 | void g() 15 | { 16 | puts("g"); 17 | throw std::runtime_error("err"); 18 | } 19 | 20 | int main() 21 | { 22 | std::function a = f; 23 | a(); 24 | try{ 25 | a(); 26 | } catch (...) { 27 | } 28 | std::function b = g; 29 | try { 30 | b(); 31 | } catch (...) { 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /kakko/fac.c: -------------------------------------------------------------------------------- 1 | /* 2 | cl fac.c on Windows 3 | gcc fac.c -m32 -z execstack on Linux 4 | */ 5 | #include 6 | #include 7 | a = atoi; 8 | b = printf; 9 | c = 0x20216425; 10 | d = 0x7525203d; 11 | e = 0x0000000a; 12 | main = 0x55535756; 13 | f = 0x000000e8; 14 | g = 0x6c8b5e00; 15 | h = 0xfd831424; 16 | i = 0x8b127401; 17 | j = 0x8b18246c; 18 | k = 0x8b55046d; 19 | l = 0xd5ffe36e; 20 | m = 0x8904c483; 21 | n = 0x0cec83c5; 22 | o = 0x89eb468d; 23 | p = 0x768b2404; 24 | q = 0x43db31e7; 25 | r = 0xaf0fdf89; 26 | s = 0x245c89fb; 27 | t = 0x247c8904; 28 | u = 0x43d6ff08; 29 | v = 0xee76eb39; 30 | w = 0x5d0cc483; 31 | x = 0xc35e5f5b; 32 | -------------------------------------------------------------------------------- /swig/byte-array/SampleTest.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import sample.*; 3 | 4 | public class SampleTest { 5 | static { 6 | String lib = "sample"; 7 | String libName = System.mapLibraryName(lib); 8 | System.out.println("libName : " + libName); 9 | System.loadLibrary(lib); 10 | } 11 | static String toHex(byte[] buf) { 12 | StringBuilder sb = new StringBuilder(); 13 | for (byte b : buf) { 14 | sb.append(String.format("%02x", b)); 15 | } 16 | return sb.toString(); 17 | } 18 | public static void main(String[] argv) { 19 | byte[] buf = Sample.getStr(); 20 | String hex = toHex(buf); 21 | System.out.println(hex); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /shared/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-g -fsanitize=address 2 | MOPT= -include ./mymalloc.h 3 | test: a.c b.so mymalloc.so 4 | gcc a.c -g -o test ./b.so $(MOPT) -ldl -DUSE_DLOPEN ./mymalloc.so 5 | 6 | mymalloc.so: ./mymalloc.c 7 | gcc -O2 -fPIC -shared -o $@ -g $< 8 | 9 | prem.so: ./mymalloc.c 10 | gcc -O2 -fPIC -shared -o $@ -g $< -DMYMALLOC_PRELOAD 11 | 12 | b.so: b.c 13 | gcc -fPIC -shared -o b.so b.c $(MOPT) -DUSE_DLOPEN 14 | 15 | bb.so: b.c 16 | gcc -fPIC -shared -o bb.so b.c -g $(MOPT) 17 | 18 | t: t.c 19 | gcc -o t t.c 20 | 21 | run: t prem.so 22 | env LD_PRELOAD=./prem.so ./t 23 | 24 | clean: 25 | rm -rf test b.so bb.so t mymalloc.so prem.so 26 | 27 | -------------------------------------------------------------------------------- /rust/thread/t.rs: -------------------------------------------------------------------------------- 1 | use std::thread; 2 | 3 | fn add(a:i32, b:i32) -> i32 { 4 | println!("add a={} b={}\n", a, b); 5 | return a + b; 6 | } 7 | 8 | fn main() { 9 | let n = 5; 10 | let mut handles = vec![]; 11 | for i in 0..n { 12 | let handle = thread::spawn(move|| { 13 | add(3, i); 14 | return (i, i+1) 15 | }); 16 | handles.push(handle); 17 | } 18 | let mut sum = 0; 19 | let mut first = true; 20 | for h in handles { 21 | let v = h.join().unwrap(); 22 | if first { 23 | sum = v.0; 24 | first = false; 25 | } else { 26 | sum = sum + v.0; 27 | } 28 | println!("v={:?}", v); 29 | } 30 | println!("sum {:?}\n", sum); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /swig/byte-array/sample.i: -------------------------------------------------------------------------------- 1 | %module Sample 2 | 3 | %include 4 | 5 | %typemap(jtype) void getStr "byte[]" 6 | %typemap(jstype) void getStr "byte[]" 7 | %typemap(jni) void getStr "jbyteArray" 8 | %typemap(javaout) void getStr { return $jnicall; } 9 | %typemap(in, numinputs=0) std::string& out (std::string buf) "$1=&buf;" 10 | %typemap(argout) std::string& out { 11 | $result = JCALL1(NewByteArray, jenv, $1->size()); 12 | JCALL4(SetByteArrayRegion, jenv, $result, 0, $1->size(), (const jbyte*)$1->c_str()); 13 | } 14 | 15 | 16 | %inline %{ 17 | 18 | void getStr(std::string& out) throw(std::exception) { 19 | out = "abcd"; 20 | } 21 | 22 | %} 23 | -------------------------------------------------------------------------------- /go/pkg-test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // #cgo pkg-config: pkg-test 4 | /* 5 | #include 6 | typedef struct A { 7 | int x; 8 | } A; 9 | 10 | void add(A* a) 11 | { 12 | a->x += AAA; 13 | } 14 | 15 | void put(const A* a) 16 | { 17 | printf("%d\n", a->x); 18 | } 19 | */ 20 | import "C" 21 | import "unsafe" 22 | 23 | /* 24 | env PKG_CONFIG_PATH=`pwd` go run pkg-test.go 25 | */ 26 | 27 | type A struct { 28 | x int 29 | } 30 | 31 | func (a *A) Add() { 32 | C.add((*C.A)(unsafe.Pointer(&a.x))) 33 | } 34 | func (a *A) Put() { 35 | C.put((*C.A)(unsafe.Pointer(&a.x))) 36 | } 37 | func main() { 38 | var a A 39 | a.Put() 40 | a.Add() 41 | a.Put() 42 | } 43 | -------------------------------------------------------------------------------- /neon/Makefile: -------------------------------------------------------------------------------- 1 | SRC=add.cpp 2 | OBJ=$(SRC:.cpp=.o) 3 | DEP=$(SRC:.cpp=.d) 4 | EXE=$(SRC:.cpp=.exe) 5 | all: $(EXE) 6 | 7 | XBYAK_DIR?=../../fujitsu/xbyak_aarch64 8 | LDFLAGS=-L $(XBYAK_DIR)/lib -lxbyak_aarch64 9 | CFLAGS=-std=c++11 -g -DNDEBUG -Wall -Wextra -I $(XBYAK_DIR) 10 | #CFLAGS+=-march=armv8.2-a+sve 11 | ifneq ($(DEBUG),1) 12 | CFLAGS+=-O2 13 | endif 14 | 15 | %.o: %.cpp 16 | $(CXX) -c $< -o $@ $(CFLAGS) -MMD -MP -MF $(@:.o=.d) 17 | 18 | -include $(DEP) 19 | 20 | %.exe: %.o 21 | $(CXX) $< -o $@ $(LDFLAGS) 22 | 23 | clean: 24 | rm -rf $(EXE) $(OBJ) $(DEP) 25 | 26 | test: ./add.exe 27 | ./add.exe 28 | 29 | .PHONY: clean 30 | 31 | .SECONDARY: $(OBJ) 32 | -------------------------------------------------------------------------------- /swig/test.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct A { 4 | A(int a) 5 | : a(a) 6 | { 7 | puts("cstr"); 8 | } 9 | ~A() 10 | { 11 | puts("dstr"); 12 | } 13 | A(const A& rhs) 14 | : a(rhs.a) 15 | { 16 | puts("cp cstr"); 17 | } 18 | A& operator=(const A& rhs) 19 | { 20 | a = rhs.a; 21 | puts("cp assign"); 22 | return *this; 23 | } 24 | void addModify(int x) 25 | { 26 | puts("addModify"); 27 | a += x; 28 | } 29 | A addReturn(int x) 30 | { 31 | puts("addReturn"); 32 | A a(*this); 33 | a.a += x; 34 | return a; 35 | } 36 | void put() const 37 | { 38 | printf("put %d\n", a); 39 | } 40 | private: 41 | int a; 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /wasm/sample/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sample", 3 | "version": "1.0.0", 4 | "description": "sample", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "build": "npm run build:test_c.js && npm run build:dist", 8 | "build:test_c.js": "make", 9 | "build:dist": "tsc", 10 | "test": "tsc && node test/test.js" 11 | }, 12 | "types": "dist/index.d.ts", 13 | "engines": { 14 | "node": ">=8.9.0" 15 | }, 16 | "devDependencies": { 17 | "@types/node": "^15.14.9", 18 | "ts-loader": "^9.2.6", 19 | "ts-standard": "^10.0.0", 20 | "typescript": "^4.9.4" 21 | }, 22 | "ts-standard": { 23 | "project": "./tsconfig.json" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /security/montgomery.md: -------------------------------------------------------------------------------- 1 | # Montgomery演算 2 | 3 | ## 記号 4 | - p : prime (>= 3) 5 | - N = 2^L (L = 32 or 64を想定) 6 | (-p)とNは互いに素なのでR(-p)+aN=1となる整数R(0 < R < N)とaがある。 7 | 8 | このとき 9 | R=(-p^(-1)) % N 10 | である。 11 | 12 | ## Th 1. 任意の整数xに対してy := x + p((xR) % N)はNで割り切れる。 13 | 14 | pR+1=aNなのでpxR+x=axN。 15 | xR = ((xR) % N) + bNとすると 16 | y = x + p(xR % N) = x + p(xR - bN) = (x + pxR) - pbN = axN - pbN = (ax - pb)N。 17 | よってNで割り切れる。 18 | 19 | ## Th 2. y/N ≡ x N^(-1) % pである。 20 | 21 | y/N = ax - pbでa ≡ N^(-1) % pなので成り立つ。 22 | 23 | ## Th 3. 0 <= x < pNのときy/N - ((xN^(-1)) % p) = 0 or p。 24 | 25 | 0 <= x < pNだから0 =< y < x + pN < 2pN。よって0 < y/N < 2p。 26 | よって成り立つ。 27 | -------------------------------------------------------------------------------- /dlopen/main-dl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | void *p; 8 | puts("main-dl"); 9 | printf("malloc=%p, free=%p\n", malloc, free); 10 | p = malloc(123); 11 | #ifdef DEEP 12 | puts("deep"); 13 | void *h = dlopen("./sub.so", RTLD_LAZY | RTLD_DEEPBIND); 14 | #else 15 | void *h = dlopen("./sub.so", RTLD_LAZY); 16 | #endif 17 | if (h == NULL) { 18 | perror("dlopen"); 19 | return 1; 20 | } 21 | puts("dlsym"); 22 | void (*f)() = dlsym(h, "sub_free"); 23 | if (f == NULL) { 24 | perror("dlsym"); 25 | return 1; 26 | } 27 | puts("call"); 28 | f(p); 29 | puts("dlclose"); 30 | dlclose(h); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /malloc-free-openmp.cpp: -------------------------------------------------------------------------------- 1 | // g++ -O3 -fopenmp t.cpp 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void task(int idx) 8 | { 9 | const int size = 100; 10 | for (int i = 1; i < size; i++) { 11 | char *p = (char*)malloc(i); 12 | memset(p, idx, i); 13 | free(p); 14 | } 15 | } 16 | 17 | int main(int argc, char *argv[]) 18 | { 19 | const int threadNum = argc == 1 ? 1 : atoi(argv[1]); 20 | omp_set_num_threads(threadNum); 21 | printf("thread %d\n", omp_get_max_threads()); 22 | const int n = 1 * 2 * 3 * 4 * 20000; 23 | 24 | #pragma omp parallel for 25 | for (int i = 0; i < n; i++) { 26 | task(i); 27 | } 28 | puts("ok"); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /react/test2/src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import * as mcl from './mcl-wasm'; 4 | 5 | class Layout extends React.Component { 6 | constructor () { 7 | super() 8 | this.state = { msg:"initializing..." } 9 | mcl.init().then(() => { 10 | const x = new mcl.Fr() 11 | x.setStr('12345678') 12 | const y = new mcl.Fr() 13 | y.setByCSPRNG() 14 | this.setState({ 15 | msg: x.getStr() + ':' + y.getStr() 16 | }) 17 | }) 18 | } 19 | render () { 20 | return

{this.state.msg}

21 | } 22 | } 23 | 24 | const app = document.getElementById('app'); 25 | ReactDOM.render(, app); 26 | 27 | -------------------------------------------------------------------------------- /sve/floatformat.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | union fi { 6 | uint32_t i; 7 | float f; 8 | }; 9 | 10 | struct FloatFormat { 11 | uint32_t f:23; 12 | uint32_t e:8; 13 | uint32_t s:1; 14 | FloatFormat(uint32_t s = 0, uint32_t e = 0, uint32_t f = 0) 15 | : f(f) 16 | , e(e) 17 | , s(s) 18 | { 19 | } 20 | void set(uint32_t s_, uint32_t e_, uint32_t f_) 21 | { 22 | s = s_; 23 | e = e_; 24 | f = f_; 25 | } 26 | fi get() const 27 | { 28 | fi fi; 29 | memcpy(&fi, this, sizeof(*this)); 30 | return fi; 31 | } 32 | void put() const 33 | { 34 | fi fi = get(); 35 | printf("%08x %d %f %e\n", fi.i, fpclassify(fi.f), fi.f, fi.f); 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /avx-512/util.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../../mcl/src/avx512.hpp" 4 | 5 | typedef __attribute__((mode(TI))) unsigned int uint128_t; 6 | 7 | uint64_t getMask(size_t n) 8 | { 9 | if (n == 64) return 0xffffffffffffffff; 10 | return (uint64_t(1) << n) - 1; 11 | } 12 | 13 | template 14 | void dump(const T& v, const char *msg = 0) 15 | { 16 | if (msg) printf("%s ", msg); 17 | const uint64_t *src = (const uint64_t*)&v; 18 | for (size_t i = 0; i < sizeof(T)/8; i++) { 19 | printf("%016llx", (long long)src[i]); 20 | putchar(' '); 21 | } 22 | printf("\n"); 23 | } 24 | 25 | void set(Vec& v, const uint64_t x[8]) 26 | { 27 | memcpy(&v, x, sizeof(v)); 28 | } 29 | -------------------------------------------------------------------------------- /cpp/sample/structued_binding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | #ifdef CPP17 8 | std::set s = { 1, 5, 9, 2, 4 }; 9 | auto [i, inserted] = s.insert(2); 10 | if (inserted) { 11 | printf("inseted %d\n", *i); 12 | } else { 13 | puts("not inserted"); 14 | } 15 | std::map m = { { "abc", 3 }, { "xyz", 4 }, { "qqq", 2 } }; 16 | for (const auto& [key, val] : m) { 17 | printf("%s %d\n", key.c_str(), val); 18 | } 19 | #else 20 | std::set s = { 1, 5, 9, 2, 4 }; 21 | auto i = s.insert(22); 22 | if (i.second) { 23 | printf("inseted %d\n", *i.first); 24 | } else { 25 | puts("not inserted"); 26 | } 27 | #endif 28 | } 29 | -------------------------------------------------------------------------------- /dlopen/main2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | void *p; 9 | puts("main2"); 10 | printf("clock=%p\n", clock); 11 | printf("clock()=%d\n", (int)clock()); 12 | #ifdef DEEP 13 | puts("deep"); 14 | void *h = dlopen("./sub3.so", RTLD_LAZY | RTLD_DEEPBIND); 15 | #else 16 | void *h = dlopen("./sub3.so", RTLD_LAZY); 17 | #endif 18 | if (h == NULL) { 19 | perror("dlopen"); 20 | return 1; 21 | } 22 | void (*f)(void) = dlsym(h, "sub3"); 23 | if (f == NULL) { 24 | perror("dlsym"); 25 | return 1; 26 | } 27 | puts("call sub3 in sub3.c"); 28 | f(); 29 | puts("dlclose"); 30 | dlclose(h); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /fma/gen-fma.py: -------------------------------------------------------------------------------- 1 | from s_xbyak import * 2 | 3 | def gen_func(n): 4 | with FuncProc(f'func{n}'): 5 | with StackFrame(1, vNum=n+1, vType=T_ZMM) as sf: 6 | c = sf.p[0] 7 | lp = Label() 8 | for i in range(n): 9 | vxorps(Zmm(i), Zmm(i), Zmm(i)) 10 | align(32) 11 | L(lp) 12 | for i in range(n): 13 | vfmadd231ps(Zmm(i), Zmm(i), Zmm(i)) 14 | sub(c, 1) 15 | jnz(lp) 16 | 17 | def main(): 18 | parser = getDefaultParser() 19 | param = parser.parse_args() 20 | 21 | N=16 22 | 23 | init(param) 24 | segment('text') 25 | 26 | for i in range(1, N+1): 27 | gen_func(i) 28 | 29 | term() 30 | 31 | if __name__ == '__main__': 32 | main() 33 | -------------------------------------------------------------------------------- /inv/Makefile: -------------------------------------------------------------------------------- 1 | MCL_DIR?=../../mcl 2 | CLANG?=clang++ 3 | CXX=$(CLANG) 4 | CFLAGS=-g -Wall -Wextra -O2 -DNDEBUG 5 | CFLAGS+=-I $(MCL_DIR)/include 6 | LDFLAGS+=-L $(MCL_DIR)/lib -lmcl 7 | 8 | SRC=inv_test.cpp 9 | LL=inv.ll inv-org.ll 10 | LL_OBJ=$(LL:.ll=.o) 11 | OBJ=$(SRC:.cpp=.o) $(LL) 12 | 13 | test: inv_test.exe 14 | ./$< 15 | 16 | %.exe: %.o $(LL) 17 | $(CXX) -o $@ $^ $(LDFLAGS) 18 | 19 | %.o: %.cpp 20 | $(CXX) -c -o $@ $< $(CFLAGS) 21 | 22 | %.o: %.S 23 | $(CXX) -c -o $@ $< 24 | 25 | #%.s: %.ll 26 | # $(CXX) -O2 -S -o $@ $< 27 | 28 | inv.ll: gen_inv.py 29 | python3 gen_inv.py > inv.ll 30 | 31 | clean: 32 | rm -rf *.o *.exe *.S inv.ll 33 | 34 | .PHONY: test clean 35 | 36 | .SECONDARY: $(OBJ) 37 | -------------------------------------------------------------------------------- /fib.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | std::stack g_stack; 5 | void push(int x) 6 | { 7 | g_stack.push(x); 8 | } 9 | int pop() 10 | { 11 | int v = g_stack.top(); 12 | g_stack.pop(); 13 | return v; 14 | } 15 | 16 | int I, N, R, R1, R2; 17 | 18 | // input : N 19 | // output : R 20 | void fib() 21 | { 22 | if (N <= 1) { 23 | R = 1; 24 | return; 25 | } 26 | push(N); 27 | N = N - 1; 28 | fib(); 29 | R1 = R; 30 | N = pop(); 31 | N = N - 2; 32 | push(R1); 33 | fib(); 34 | R2 = R; 35 | R1 = pop(); 36 | R = R1 + R2; 37 | } 38 | 39 | int main() 40 | { 41 | for (I = 0; I <= 10; I++) { 42 | printf("fib(%d)=", I); 43 | N = I; 44 | fib(); 45 | printf("%d\n", R); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /omp/custom.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | -fopenmp 3 | */ 4 | #include 5 | #include 6 | 7 | struct X { 8 | int x; 9 | X() : x(0) {} 10 | void add(const X& other) 11 | { 12 | x += other.x; 13 | } 14 | }; 15 | 16 | void aggregate(X& out, const X *x, size_t n) 17 | { 18 | out.x = 0; 19 | #pragma omp declare reduction(Xadd: X: omp_out.add(omp_in)) initializer(omp_priv=omp_orig) 20 | #pragma omp parallel for reduction(Xadd:out) 21 | for (size_t i = 0; i < n; i ++) { 22 | out.add(x[i]); 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | X sum; 29 | const size_t n = 10000; 30 | X x[n]; 31 | for (int i = 0; i < n; i++) { 32 | x[i].x = i; 33 | } 34 | aggregate(sum, x, n); 35 | printf("sum=%d\n", sum.x); 36 | } 37 | -------------------------------------------------------------------------------- /rc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #ifdef _MSC_VER 4 | #include 5 | #else 6 | #include 7 | #endif 8 | 9 | void put(__m128 x) 10 | { 11 | float f[4]; 12 | memcpy(f, &x, sizeof(f)); 13 | printf("%5g %5g %5g %5g\n", f[0], f[1], f[2], f[3]); 14 | } 15 | 16 | int main() 17 | { 18 | const float a[] = { 1.1f, 1.8f, -2.2f, -2.8f }; 19 | __m128 x = _mm_loadu_ps(a); 20 | printf(" "); 21 | put(x); 22 | for (int i = 0; i < 4; i++) { 23 | const int rcMask = 3 << 13; 24 | int cw = _mm_getcsr(); 25 | cw &= ~rcMask; 26 | cw |= i << 13; 27 | printf("%04x ", cw); 28 | __m128i y = _mm_cvtps_epi32(x); 29 | _mm_setcsr(cw); 30 | put(_mm_cvtepi32_ps(y)); 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /react/test2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "test2", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "build": "webpack" 9 | }, 10 | "author": "herumi (https://github.com/herumi/)", 11 | "license": "ISC", 12 | "devDependencies": { 13 | "@babel/core": "^7.16.0", 14 | "@babel/preset-env": "^7.16.0", 15 | "@babel/preset-react": "^7.16.0", 16 | "babel-loader": "^8.2.3", 17 | "react": "^17.0.1", 18 | "react-dom": "^17.0.1", 19 | "webpack": "^5.4.0", 20 | "webpack-cli": "^4.2.0" 21 | }, 22 | "dependencies": { 23 | "mcl-wasm": "^1.0.3" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /dll/gen.py: -------------------------------------------------------------------------------- 1 | from gen_x86asm import * 2 | import argparse 3 | 4 | def gen_test(): 5 | with FuncProc('fff'): 6 | with StackFrame(2) as sf: 7 | x = sf.p[0] 8 | y = sf.p[1] 9 | L1 = Label() 10 | L2 = Label() 11 | lea(eax, ptr(x + y)) 12 | jmp(L2) 13 | L(L1) 14 | mov(rax, x) 15 | add(rax, y) 16 | ret() 17 | L(L2) 18 | jmp(L1) 19 | 20 | parser = argparse.ArgumentParser() 21 | parser.add_argument('-win', '--win', help='output win64 abi', action='store_true') 22 | parser.add_argument('-m', '--mode', help='output asm syntax', default='nasm') 23 | param = parser.parse_args() 24 | 25 | setWin64ABI(param.win) 26 | init(param.mode) 27 | 28 | segment('text') 29 | 30 | gen_test() 31 | 32 | termOutput() 33 | -------------------------------------------------------------------------------- /node/wasm3/index.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | async function initWasm() { 4 | const fs = require('fs') 5 | const wasm = fs.readFileSync('./add.wasm') 6 | const memory = await new WebAssembly.Memory({initial:100}) 7 | const imports = { 8 | env : { 9 | memory: memory, 10 | } 11 | } 12 | const ret = await WebAssembly.instantiate(wasm, imports) 13 | return ret.instance.exports 14 | } 15 | 16 | let mie = {}; 17 | 18 | (async () => { 19 | console.log('async') 20 | mie.func = await initWasm() 21 | await test() 22 | // mie.mem = await new Memory(mie.func.memory.buffer) 23 | })() 24 | 25 | const test = () => { 26 | console.log('test') 27 | const ret = mie.func.add(3, 5) 28 | console.log(`ret=${ret}`) 29 | } 30 | -------------------------------------------------------------------------------- /sve/faddv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace Xbyak_aarch64; 4 | 5 | struct Code : CodeGenerator { 6 | Code() 7 | { 8 | ptrue(p0.s); 9 | ld1w(z0.s, p0, ptr(x0)); 10 | faddv(s0, p0, z0.s); 11 | ret(); 12 | } 13 | }; 14 | 15 | int main() 16 | try 17 | { 18 | const int N = 16; 19 | float d[N]; 20 | for (int i = 0; i < N; i++) { 21 | d[i] = (i + 1) / 10.0f; 22 | } 23 | Code c; 24 | auto f = c.getCode(); 25 | c.ready(); 26 | float sum1 = f(d); 27 | float sum2 = 0; 28 | for (int i = 0; i < N; i++) { 29 | sum2 += d[i]; 30 | } 31 | printf("sum1=%f sum2=%f\n", sum1, sum2); 32 | } catch (std::exception& e) { 33 | printf("err %s\n", e.what()); 34 | return 1; 35 | } 36 | -------------------------------------------------------------------------------- /vinsertf128.cpp: -------------------------------------------------------------------------------- 1 | #define XBYAK_NO_OP_NAMES 2 | #include 3 | 4 | MIE_ALIGN(32) char buf[512]; 5 | 6 | double *u = (double*)(buf + 1); 7 | 8 | MIE_ALIGN(32) double a[4]; 9 | 10 | struct Code : Xbyak::CodeGenerator { 11 | Code() 12 | { 13 | mov(rax, (size_t)u); 14 | vmovups(xm0, ptr [rax]); 15 | vinsertf128(ym0, ym0, ptr [rax + 16], 1); 16 | mov(rax, (size_t)a); 17 | vmovaps(ptr [rax], ym0); 18 | ret(); 19 | } 20 | } code; 21 | 22 | void (*f)() = code.getCode(); 23 | 24 | int main() 25 | try 26 | { 27 | u[0] = 1; 28 | u[1] = 2; 29 | u[2] = 3; 30 | u[3] = 5; 31 | f(); 32 | printf("%f %f %f %f\n", a[3], a[2], a[1], a[0]); 33 | } catch (std::exception& e) { 34 | printf("err %s\n", e.what()); 35 | } 36 | -------------------------------------------------------------------------------- /addnc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct AddNC : Xbyak::CodeGenerator { 4 | AddNC() 5 | { 6 | using namespace Xbyak; 7 | util::StackFrame sf(this, 3); 8 | const Reg64& z = sf.p[0]; 9 | const Reg64& x = sf.p[1]; 10 | const Reg64& y = sf.p[2]; 11 | mov(rax, ptr [x]); 12 | add(rax, ptr [y]); 13 | mov(ptr [z], rax); 14 | for (int i = 1; i < 3; i++) { 15 | mov(rax, ptr [x + i * 8]); 16 | adc(rax, ptr [y + i * 8]); 17 | mov(ptr [z + i * 8], rax); 18 | } 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | AddNC code; 25 | void (*addNC)(uint64_t*, const uint64_t*, const uint64_t*) = code.getCode(); 26 | uint64_t x[4], y[4], z[4]; 27 | addNC(z, x, y); 28 | } 29 | -------------------------------------------------------------------------------- /go/seccon2013/exec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define XBYAK_NO_OP_NAMES 5 | #include 6 | 7 | struct A : Xbyak::CodeGenerator { 8 | explicit A(const std::string& s) 9 | { 10 | for (size_t i = 0; i < s.size(); i += 2) { 11 | uint8_t v = cybozu::hextoi(s.c_str() + i, 2); 12 | // fprintf(stderr, "%02x ", v); 13 | db(v); 14 | } 15 | // fprintf(stderr, "\n"); 16 | } 17 | }; 18 | 19 | int main(int argc, char *argv[]) 20 | try 21 | { 22 | if (argc != 2) { 23 | puts("exec hexbinary"); 24 | return 1; 25 | } 26 | const std::string s = argv[1]; 27 | A a(s); 28 | a.getCode()(); 29 | } catch (std::exception& e) { 30 | fprintf(stderr, "err %s\n", e.what()); 31 | return 1; 32 | } 33 | -------------------------------------------------------------------------------- /llvm/gen_lp.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append('../../mcl-ff') 3 | from s_xbyak_llvm import * 4 | 5 | def main(): 6 | r = Int(32) 7 | n = Int(32) 8 | zero = Imm(0, 32) 9 | with Function('sum', r, n): 10 | entry = Label() 11 | lp = Label() 12 | body = Label() 13 | end = Label() 14 | 15 | L(entry) 16 | br(lp) 17 | L(lp) 18 | phi_i = i = phi(zero, entry) 19 | phi_s = s = phi(zero, entry) 20 | cond = icmp(ult, i, n) 21 | br(cond, body, end) 22 | 23 | L(body) 24 | s = add(s, i) 25 | i = add(i, 1) 26 | phi_i.link(i, body) 27 | phi_s.link(s, body) 28 | br(lp) 29 | 30 | L(end) 31 | r = phi((zero, entry), (s, lp)) 32 | ret(r) 33 | 34 | term() 35 | 36 | if __name__ == '__main__': 37 | main() 38 | -------------------------------------------------------------------------------- /sve/low_func.s: -------------------------------------------------------------------------------- 1 | .align 16 2 | .global get_zcr_el1 3 | .global _get_zcr_el1 4 | get_zcr_el1: 5 | _get_zcr_el1: 6 | # mrs x0, zcr_el1 7 | .inst 0xd5381200 8 | ret 9 | 10 | .align 16 11 | .global get_fpcr 12 | .global _get_fpcr 13 | get_fpcr: 14 | _get_fpcr: 15 | mrs x0, fpcr 16 | ret 17 | 18 | .align 16 19 | .global set_fpcr 20 | .global _set_fpcr 21 | set_fpcr: 22 | _set_fpcr: 23 | msr fpcr, x0 24 | ret 25 | 26 | .align 16 27 | .global get_id_aa64isar0_el1 28 | .global _get_id_aa64isar0_el1 29 | get_id_aa64isar0_el1: 30 | _get_id_aa64isar0_el1: 31 | mrs x0, id_aa64isar0_el1 32 | ret 33 | 34 | .align 16 35 | .global get_id_aa64pfr0_el1 36 | .global _get_id_aa64pfr0_el1 37 | get_id_aa64pfr0_el1: 38 | _get_id_aa64pfr0_el1: 39 | mrs x0, id_aa64pfr0_el1 40 | ret 41 | -------------------------------------------------------------------------------- /codec-calc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int calc(int a, int b, int s) 5 | { 6 | const int Q = 1 << s, Q2 = Q * 2, Q3 = Q * 3; 7 | assert(0 <= s && s <= 16 && 0 <= b && b < a && a < Q * 4); 8 | int n = 0; 9 | for (;;) { 10 | if (a < Q2) { 11 | n = n * 2; 12 | } else if (b >= Q2) { 13 | n = n * 2 + 1; 14 | b -= Q2; 15 | a -= Q2; 16 | } else if (b >= Q && a < Q3) { 17 | b -= Q; 18 | a -= Q; 19 | } else { 20 | break; 21 | } 22 | b = b * 2; 23 | a = a * 2 + 1; 24 | } 25 | return n; 26 | } 27 | 28 | int main() 29 | { 30 | int s = 3; 31 | for (int a = 1; a < (1 << s) * 4; a++) { 32 | printf("a=%2d ", a); 33 | for (int b = 0; b < a; b++) { 34 | printf("%2d ", calc(a, b, s)); 35 | } 36 | printf("\n"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /debug/aarch64/script.txt: -------------------------------------------------------------------------------- 1 | define flags 2 | printf "QQQ %c %c %c %c\n", ($cpsr >> 31) & 1 ? 'N' : '-', ($cpsr >> 30) & 1 ? 'Z' : '-', ($cpsr >> 29) & 1 ? 'C' : '-', ($cpsr >> 28) & 1 ? 'V' : '-' 3 | end 4 | 5 | define step1 6 | c 7 | printf "QQQ %08x %08x\n", $w0, $w1 8 | si 9 | printf "QQQ %08x\n", $w0 10 | flags 11 | end 12 | 13 | b func 14 | set verbose off 15 | display/i $pc 16 | start 17 | step1 18 | step1 19 | step1 20 | step1 21 | step1 22 | step1 23 | step1 24 | step1 25 | step1 26 | step1 27 | step1 28 | step1 29 | step1 30 | step1 31 | step1 32 | step1 33 | step1 34 | step1 35 | step1 36 | step1 37 | step1 38 | step1 39 | step1 40 | step1 41 | step1 42 | step1 43 | step1 44 | step1 45 | step1 46 | step1 47 | step1 48 | step1 49 | step1 50 | step1 51 | step1 52 | step1 53 | 54 | -------------------------------------------------------------------------------- /gf256/Makefile: -------------------------------------------------------------------------------- 1 | ARCH?=$(shell uname -m) 2 | CYBOZU_DIR?=../../cybozulib/include 3 | XBYAK_DIR?=../../xbyak/ 4 | 5 | CFLAGS+=-DNDEBUG -O2 -Wall -Wextra -I $(CYBOZU_DIR) -I $(XBYAK_DIR) 6 | test_emu: gf256_test.exe 7 | sde -spr -- ./$< 8 | 9 | ifeq ($(ARCH),x86_64) 10 | ASM_OBJ+=gf256_gfni.o 11 | endif 12 | 13 | test: gf256_test.exe 14 | ./$< 15 | 16 | update: gf256_gfni.S gf256_gfni.asm 17 | 18 | gf256_test.exe: gf256_test.o $(ASM_OBJ) 19 | $(CXX) -o $@ $^ $(CFLAGS) 20 | 21 | %.o: %.cpp gf256.hpp mul.h 22 | $(CXX) -c -o $@ $< $(CFLAGS) 23 | 24 | %.o: %.S 25 | $(CXX) -c -o $@ $< $(CFLAGS) 26 | 27 | gf256_gfni.S: gen_asm.py 28 | python3 gen_asm.py -m gas > $@ 29 | 30 | gf256_gfni.asm: gen_asm.py 31 | python3 gen_asm.py -m masm > $@ 32 | 33 | clean: 34 | $(RM) *.exe *.o *.S 35 | -------------------------------------------------------------------------------- /static-var/var.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifdef _MSC_VER 4 | #ifdef DLL_EXPORT 5 | #define DLL_API __declspec(dllexport) 6 | #else 7 | #define DLL_API __declspec(dllimport) 8 | #endif 9 | #else 10 | #define DLL_API 11 | #endif 12 | template 13 | struct X { 14 | static int x; 15 | static const char *get() 16 | { 17 | static const char *m = "abc"; 18 | printf("m=%p %s\n", m, m); 19 | printf("&x=%p\n", &x); 20 | return m; 21 | } 22 | void inc() 23 | { 24 | x++; 25 | } 26 | }; 27 | 28 | //DLL_API 29 | template 30 | int X::x = 5; 31 | 32 | template struct DLL_API X; // VC ok 33 | 34 | extern "C" { 35 | 36 | DLL_API void put(); 37 | DLL_API void incX(); 38 | 39 | } 40 | 41 | struct XX { 42 | DLL_API static int x; 43 | }; 44 | -------------------------------------------------------------------------------- /safegcd/Makefile: -------------------------------------------------------------------------------- 1 | MCL_DIR?=../../mcl 2 | include $(MCL_DIR)/common.mk 3 | SRC=invmod.cpp invmod_gmp.cpp 4 | 5 | OBJ=$(SRC:.cpp=.o) 6 | DEP=$(SRC:.cpp=.d) 7 | EXE=$(SRC:.cpp=.exe) 8 | 9 | all: $(EXE) 10 | 11 | test: invmod.exe 12 | ./$< 13 | 14 | test_gmp: invmod_gmp.exe 15 | ./$< 16 | 17 | CFLAGS=-I $(MCL_DIR)/include -Wall -Wextra -g 18 | LDFLAGS=-lmcl -L $(MCL_DIR)/lib -lgmp -lgmpxx 19 | ifeq ($(DEBUG),1) 20 | MODE?=address 21 | CFLAGS+=-fsanitize=$(MODE) 22 | LDFLAGS+=-fsanitize=$(MODE) 23 | else 24 | CFLAGS+=-O2 -DNDEBUG 25 | endif 26 | 27 | %.exe: %.o 28 | $(PRE)$(CXX) $< -o $@ $(LDFLAGS) 29 | 30 | %.o: %.cpp 31 | $(PRE)$(CXX) $(CFLAGS) -c $< -o $@ -MMD -MP -MF $(@:.o=.d) 32 | 33 | clean: 34 | $(RM) $(OBJ) $(DEP) $(EXE) 35 | 36 | -include $(DEP) 37 | .SECONDARY: $(OBJ) 38 | -------------------------------------------------------------------------------- /wasm/exception/Makefile: -------------------------------------------------------------------------------- 1 | TARGET=exception_c.js 2 | 3 | all: $(TARGET) 4 | 5 | CFLAGS=-O2 -DNDEUBG -Wall -Wextra 6 | 7 | EMCC_OPT+=-s WASM=1 8 | #EMCC_OPT+=-s NO_EXIT_RUNTIME=1 9 | #EMCC_OPT+=-s NODEJS_CATCH_EXIT=0 10 | #EMCC_OPT+=-s NODEJS_CATCH_REJECTION=0 11 | EMCC_OPT+=-s MODULARIZE=1 12 | EMCC_OPT+=-s SINGLE_FILE=1 13 | EMCC_OPT+=-s ABORTING_MALLOC=0 14 | 15 | MODE?=2 16 | # MODE = 1 and MODE = 2 makes the same binary 17 | ifeq ($(MODE),1) 18 | # catch C++ exception in JS 19 | EMCC_OPT+=-sNO_DISABLE_EXCEPTION_CATCHING 20 | endif 21 | ifeq ($(MODE),2) 22 | # catch C++ exception in JS 23 | EMCC_OPT+=-fexceptions 24 | endif 25 | exception_c.js: exception.cpp 26 | emcc -o $@ $< $(EMCC_OPT) 27 | 28 | 29 | test: $(TARGET) 30 | node test.js 31 | 32 | clean: 33 | $(RM) -rf $(TARGET) 34 | -------------------------------------------------------------------------------- /dlopen/readme.md: -------------------------------------------------------------------------------- 1 | # test of address sanitizer for dlopen 2 | 3 | `main.c` call a function in `sub.c`. 4 | 5 | test|main.c|sub.c|gcc|clang| 6 | -|-|-|-|-| 7 | test1| |static|ok|ok| 8 | test2| ASan|static+ASan|ok|ok| 9 | test3| |shared|ok|ok| 10 | test4| |shared+ASan|err|err| 11 | tset5|LD_PRELOAD|shared+ASan|ok|ok| 12 | test6|dlopen|sahred|ok|ok| 13 | test7|dlopen+ASan|shared+ASan|ok|ok| 14 | test8|dlopen+RTLD_DEEPBIND+ASan|sahred+ASan|ok|err| 15 | 16 | - [gcc-result](gcc-result.txt) 17 | - [clang-result](clang-result.txt) 18 | 19 | ## dlopen + `RTLD_DEEPBIND` 20 | 21 | ``` 22 | make testA1 23 | make testA2 24 | ``` 25 | 26 | - dlopen without `RTLD_DEEPBIND` 27 | - `clock()` in sub2.c is called from `sub3()` in sub3.c 28 | - dlopen with `RTLD_DEEPBIND` 29 | - The original `clock()` is called from `sub3()` in sub3.c 30 | -------------------------------------------------------------------------------- /go/callback/callback.go: -------------------------------------------------------------------------------- 1 | package main 2 | /* 3 | #include "lib.h" 4 | #cgo CFLAGS: -I . 5 | #cgo LDFLAGS: -L./ -llib 6 | int wrapCallbackCgo(int in); 7 | */ 8 | import "C" 9 | 10 | import ( 11 | "fmt" 12 | "unsafe" 13 | ) 14 | 15 | type CallbackIF interface { 16 | run(int) int 17 | } 18 | 19 | var s_callbackIF CallbackIF 20 | 21 | func setCallbackGo(f CallbackIF) { 22 | s_callbackIF = f 23 | C.setCallbackC(C.FuncType(unsafe.Pointer(C.wrapCallbackCgo))) 24 | } 25 | 26 | // this function can't be put in sub.go 27 | //export wrapCallbackGo 28 | func wrapCallbackGo(x int) int { 29 | fmt.Printf(" wrapCallbackGo x=%d\n", x) 30 | ret := s_callbackIF.run(x + 1) 31 | fmt.Printf(" wrapCallbackGo ret=%d\n", ret) 32 | return ret 33 | } 34 | 35 | func callCallbackGo(x int) { 36 | C.callCallbackC(C.int(x)) 37 | } 38 | -------------------------------------------------------------------------------- /sve/fexpa.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | struct FexpaTbl { 7 | static const size_t N = 64; 8 | uint32_t tbl[N]; 9 | union fi { 10 | float f; 11 | uint32_t i; 12 | }; 13 | 14 | static inline uint32_t mask(int n) 15 | { 16 | if (n == 32) return uint32_t(-1); 17 | return (1u << n) - 1; 18 | } 19 | FexpaTbl() 20 | { 21 | for (size_t i = 0; i < N; i++) { 22 | fi fi; 23 | fi.f = (float)pow(2.0, i / float(N)); 24 | tbl[i] = fi.i & mask(23); 25 | } 26 | } 27 | float calc(float f) const 28 | { 29 | fi fi; 30 | fi.f = f; 31 | uint32_t v = fi.i; 32 | fi.i = ((v >> 6) & mask(8)) << 23; 33 | fi.i |= tbl[v & 63]; 34 | return fi.f; 35 | } 36 | }; 37 | 38 | float fexpaEmu(float x) 39 | { 40 | static FexpaTbl tbl; 41 | return tbl.calc(x); 42 | } 43 | -------------------------------------------------------------------------------- /call_user.c: -------------------------------------------------------------------------------- 1 | /* 2 | make -C /lib/modules/`uname -r`/build M=`pwd` 3 | see kernel/kmod.c 4 | */ 5 | #include 6 | #include 7 | 8 | char usercmd[256] = "/bin/ls"; 9 | 10 | static int __init call_user_init(void) 11 | { 12 | int ret; 13 | char *argv[] = { usercmd, NULL }; 14 | char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL }; 15 | ret = call_usermodehelper(usercmd, argv, envp, UMH_WAIT_PROC); 16 | if (ret) { 17 | printk(KERN_INFO "call_user_init fail=%d\n", ret); 18 | } 19 | return ret; 20 | } 21 | 22 | static void __exit call_user_exit(void) 23 | { 24 | printk(KERN_INFO "call_user_exit\n"); 25 | } 26 | 27 | module_init(call_user_init); 28 | module_exit(call_user_exit); 29 | 30 | MODULE_AUTHOR("MITSUNARI Shigeo"); 31 | MODULE_LICENSE("GPL"); 32 | 33 | -------------------------------------------------------------------------------- /dec-sub.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define XBYAK_NO_OP_NAMES 3 | #include 4 | 5 | const size_t N = 100000000; 6 | 7 | struct Code : Xbyak::CodeGenerator { 8 | explicit Code(bool useDec) 9 | { 10 | puts(useDec ? "use dec" : "use sub"); 11 | mov(eax, N); 12 | L("@@"); 13 | if (useDec) { 14 | sub(eax, 1); 15 | } else { 16 | dec(eax); 17 | } 18 | jnz("@b"); 19 | ret(); 20 | } 21 | }; 22 | 23 | int main(int argc, char *argv[]) 24 | { 25 | int mode = argc == 1 ? -1 : atoi(argv[1]); 26 | for (int i = 0; i < 2; i++) { 27 | if (mode >= 0 && mode != i) continue; 28 | Code c(i == 0); 29 | void (*f)() = c.getCode(); 30 | Xbyak::util::Clock clk; 31 | clk.begin(); 32 | f(); 33 | clk.end(); 34 | printf("%.2fclk\n", clk.getClock() / double(N)); 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /local-label.txt: -------------------------------------------------------------------------------- 1 | Xbyakのラベルの扱いの拡張案 2 | 3 | 現状の仕様 4 | 5 | L("AAA"); グローバルラベル 6 | 7 | L(".AAA"); ローカルラベル 8 | 9 | jne(".AAA"); 10 | 11 | inLocalLabel(); outLocalLabel(); 12 | で囲まれた中のローカルラベルはその外側のラベルは見えない。 13 | 14 | 拡張案 15 | ジャンプ命令の第2(3?)引数にint型の引数を追加する。 16 | 17 | inLocalLabel(); 18 | L(".AAA"); // L1 19 | 20 | inLocalLabel(); 21 | L(".AAA"); // L2 22 | 23 | inLocalLabel(); 24 | L(".AAA"); // L3 25 | 26 | jmp(".AAA"); // 今までどおり 27 | jmp(".AAA", 1); // 1段外のスコープのラベル(L2)になる 28 | jmp(".AAA", 2); // L1になる 29 | 30 | outLocalLabel(); 31 | outLocalLabel(); 32 | outLocalLabel(); 33 | 34 | inLocalLabel()が今のスタックの段数を返すようにすればより自由度が高まるか。 35 | 36 | 現状は 37 | jne(const std::string& label, LabelType type = T_AUTO); 38 | これをどうやって拡張しよう。 39 | 40 | 案) 第2引数がマイナスのときは上記拡張になるとする。 41 | -1なら1段外、-2なら2段外。このときはT_NEAR固定でよいか。 42 | -------------------------------------------------------------------------------- /atomic/readme.md: -------------------------------------------------------------------------------- 1 | # 生成コード 2 | ## x64 3 | 4 | ``` 5 | store0: 6 | movl $5, (%rdi) 7 | ret 8 | 9 | load0: 10 | movl (%rdi), %eax 11 | ret 12 | 13 | store1: 14 | movl $3, (%rdi) 15 | mfence 16 | ret 17 | 18 | load1: 19 | movl (%rdi), %eax 20 | ret 21 | 22 | store2: 23 | movl $7, (%rdi) 24 | ret 25 | 26 | load2: 27 | movl (%rdi), %eax 28 | ret 29 | ``` 30 | ## aarch64 31 | ``` 32 | store0: 33 | mov w1, 5 34 | str w1, [x0] 35 | ret 36 | 37 | load0: 38 | ldr w0, [x0] 39 | ret 40 | 41 | store1: 42 | mov w1, 3 43 | stlr w1, [x0] 44 | ret 45 | 46 | load1: 47 | ldar w0, [x0] 48 | ret 49 | 50 | store2: 51 | mov w1, 7 52 | stlr w1, [x0] 53 | ret 54 | 55 | load2: 56 | ldar w0, [x0] 57 | ret 58 | ``` 59 | -------------------------------------------------------------------------------- /node/wasm3/Makefile: -------------------------------------------------------------------------------- 1 | all=add.wasm 2 | VER?=-8 3 | VER_OK?=-7 4 | 5 | CPPFLAGS=-O3 -m32 -fno-builtin --target=wasm32-unknown-unknown-wasm -fvisibility=hidden -ffreestanding -fno-exceptions -Wall -Wextra -fno-threadsafe-statics -nodefaultlibs -nostdlib -fno-use-cxa-atexit -fno-unwind-tables -fno-rtti -nostdinc++ 6 | add.wasm: add.cpp sub.c 7 | clang++$(VER) -O3 -c -emit-llvm $(CPPFLAGS) add.cpp 8 | clang$(VER) -O3 -c -emit-llvm $(CPPFLAGS) sub.c 9 | llvm-link$(VER) -o wasm.bc add.bc sub.bc 10 | opt$(VER) -O3 wasm.bc -o wasm.bc 11 | llc$(VER_OK) -O3 -filetype=obj -o wasm.o wasm.bc 12 | wasm-ld$(VER_OK) --no-entry --no-threads --allow-undefined --strip-all --import-memory -o add.wasm wasm.o 13 | 14 | test: add.wasm 15 | node index.js 16 | 17 | dis: add.wasm 18 | wasm-dis add.wasm 19 | 20 | clean: 21 | rm -rf add.wasm add.js *.o *.bc 22 | -------------------------------------------------------------------------------- /sve/call.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace Xbyak_aarch64; 4 | 5 | void put(int x) 6 | { 7 | printf("x=%d\n", x); 8 | } 9 | 10 | struct Code : CodeGenerator { 11 | /* 12 | void f(int x, void put(int)) 13 | { 14 | put(x); 15 | put(x + 5); 16 | } 17 | */ 18 | Code() 19 | { 20 | const auto& x = x19; 21 | const auto& func = x20; 22 | stp(x29, x30, pre_ptr(sp, -32)); 23 | stp(x, func, ptr(sp, 16)); 24 | 25 | mov(x, x0); 26 | mov(func, x1); 27 | 28 | blr(func); 29 | 30 | mov(x0, x); 31 | add(x0, x0, 5); 32 | 33 | blr(func); 34 | 35 | ldp(x, func, ptr(sp, 16)); 36 | ldp(x29, x30, post_ptr(sp, 32)); 37 | ret(); 38 | } 39 | }; 40 | 41 | int main() 42 | { 43 | Code c; 44 | c.ready(); 45 | auto f = c.getCode(); 46 | f(123, put); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /swig/make_wrap.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | call set-java-path.bat 3 | set JAVA_INCLUDE=%JAVA_DIR%\include 4 | set SWIG=..\..\swig\swigwin-3.0.2\swig.exe 5 | 6 | set MODULE_NAME=test 7 | set JAVA_NAME=Test 8 | set IF_NAME=%MODULE_NAME% 9 | set PACKAGE_NAME=mcl.%MODULE_NAME% 10 | set PACKAGE_DIR=%PACKAGE_NAME:.=\% 11 | 12 | echo [[run swig]] 13 | mkdir %PACKAGE_DIR% 14 | echo %SWIG% -java -package %PACKAGE_NAME% -outdir %PACKAGE_DIR% -c++ -Wall %IF_NAME%.i 15 | %SWIG% -java -package %PACKAGE_NAME% -outdir %PACKAGE_DIR% -c++ -Wall %IF_NAME%.i 16 | echo [[make dll]] 17 | mkdir ..\bin 18 | cl /MD /DNOMINMAX /DNDEBUG /LD /Ox /EHsc %IF_NAME%_wrap.cxx -I%JAVA_INCLUDE% -I%JAVA_INCLUDE%\win32 /link /LIBPATH:../../cybozulib_ext/mpir/lib /OUT:./%IF_NAME%_wrap.dll 19 | 20 | call run-%MODULE_NAME%.bat 21 | 22 | echo [[make jar]] 23 | %JAVA_DIR%\bin\jar cvf %MODULE_NAME%.jar mcl 24 | -------------------------------------------------------------------------------- /sve/Makefile: -------------------------------------------------------------------------------- 1 | CXX?=clang++ 2 | INC_DIR?=../../fujitsu/xbyak_aarch64/ 3 | LIB_DIR?=$(INC_DIR)/lib 4 | CPPFLAGS=$(OPT) -Wall -I $(INC_DIR) -I ../../cybozulib/include -g 5 | ifneq ($(DEBUG),1) 6 | CPPFLAGS+=-O3 -DNDEBUG 7 | endif 8 | LDFLAGS=-s -L $(LIB_DIR) -lxbyak_aarch64 -g 9 | SRC=double-vec.cpp int-vec.cpp float-vec.cpp float-test.cpp exp_v.cpp log.cpp ldr.cpp softmax.cpp 10 | OBJ=$(SRC:.cpp=.o) 11 | EXE=$(SRC:.cpp=.exe) 12 | DEP=$(SRC:.cpp=.d) 13 | all: $(EXE) 14 | 15 | -include $(DEP) 16 | 17 | %.o: %.cpp 18 | $(CXX) -c $< $(CPPFLAGS) -MMD -MP -MF $(@:.o=.d) 19 | 20 | %.exe: %.o 21 | $(CXX) -o $@ $< $(LDFLAGS) 22 | 23 | emu: 24 | aarch64-linux-gnu-g++ float-test.cpp -I ../../fujitsu/xbyak_aarch64/ 25 | env QEMU_LD_PREFIX=/usr/aarch64-linux-gnu qemu-aarch64 -cpu max,sve512=on ./a.out 26 | 27 | clean: 28 | rm -rf $(EXE) *.o *.d 29 | 30 | .SECONDARY: $(OBJ) $(DEP) 31 | -------------------------------------------------------------------------------- /sve/direct-jmp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace Xbyak_aarch64; 4 | 5 | void put(const void *p) 6 | { 7 | printf("p=%p\n", p); 8 | } 9 | 10 | struct Code : CodeGenerator { 11 | Code() 12 | { 13 | stp(x29, x30, pre_ptr(sp, -32)); 14 | 15 | const size_t addr = (size_t)put; 16 | const size_t mask = 0xffff; 17 | mov(x0, (addr >> 48) & mask); 18 | lsl(x0, x0, 16); 19 | mov(x1, (addr >> 32) & mask); 20 | orr(x0, x0, x1); 21 | lsl(x0, x0, 16); 22 | mov(x1, (addr >> 16) & mask); 23 | orr(x0, x0, x1); 24 | lsl(x0, x0, 16); 25 | mov(x1, (addr >> 0) & mask); 26 | orr(x0, x0, x1); 27 | blr(x0); 28 | 29 | ldp(x29, x30, post_ptr(sp, 32)); 30 | ret(); 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | Code c; 37 | c.ready(); 38 | auto f = c.getCode(); 39 | printf("put=%p\n", put); 40 | f(); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /tfns/Makefile: -------------------------------------------------------------------------------- 1 | MCL_DIR?=../../mcl/ 2 | include $(MCL_DIR)common.mk 3 | 4 | ALL_SRC=tfns_test.cpp 5 | 6 | TARGET=tfns 7 | all: $(TARGET) 8 | 9 | CFLAGS+=-I $(MCL_DIR)include -I ./ 10 | ifeq ($(OS),mac) 11 | CFLAGS+=-Xpreprocessor -fopenmp 12 | LDFLAGS+=-lomp 13 | else 14 | CFLAGS+=-fopenmp 15 | LDFLAGS+=-fopenmp 16 | endif 17 | 18 | MCL_LIB=$(MCL_DIR)lib/libmcl.a 19 | 20 | $(MCL_LIB): 21 | $(MAKE) -C $(MCL_DIR) lib/libmcl.a 22 | 23 | %.o: %.cpp tfns.hpp 24 | $(PRE)$(CXX) $(CFLAGS) -c $< -o $@ -MMD -MP -MF $(@:.o=.d) 25 | 26 | tfns_test: tfns_test.o $(MCL_LIB) 27 | $(PRE)$(CXX) $< -o $@ $(MCL_LIB) $(LDFLAGS) -lpthread 28 | 29 | test: tfns_test 30 | ./tfns_test 31 | 32 | clean: 33 | rm -rf $(TARGET) *.o tfns_test *.d 34 | 35 | DEPEND_FILE=$(ALL_SRC:.cpp=.d) 36 | -include $(DEPEND_FILE) 37 | 38 | # don't remove these files automatically 39 | .SECONDARY: $(ALL_SRC:.cpp=.o) 40 | 41 | -------------------------------------------------------------------------------- /go/finalizer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /* 4 | #include 5 | #include 6 | struct A; 7 | static int c = 0; 8 | struct A *createA() 9 | { 10 | struct A *p = calloc(10, 1); 11 | printf("create A %p %d\n", p, c++); 12 | return p; 13 | } 14 | 15 | void destroyA(struct A *a) 16 | { 17 | printf("destroy A %p %d\n", a, --c); 18 | free(a); 19 | } 20 | */ 21 | import "C" 22 | import "fmt" 23 | import "runtime" 24 | import "time" 25 | 26 | type A struct { 27 | self *C.struct_A 28 | } 29 | 30 | func destroyA(a *A) { 31 | C.destroyA(a.self) 32 | } 33 | func newA() *A { 34 | a := new(A) 35 | a.self = C.createA() 36 | runtime.SetFinalizer(a, destroyA) 37 | return a 38 | } 39 | 40 | func main() { 41 | var a *A 42 | for i := 0; i < 10; i++ { 43 | a = newA() 44 | } 45 | fmt.Println("runtime GC", a) 46 | runtime.GC() 47 | fmt.Println("sleep") 48 | time.Sleep(2 * time.Second) 49 | } 50 | -------------------------------------------------------------------------------- /combination2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // see optimized version combination.cpp 6 | 7 | // return the next bit pattern of n-bits which contains k zeros. 8 | uint64_t nextCombination(uint64_t a) 9 | { 10 | uint64_t b = a ^ (a + 1); 11 | uint64_t c = a - b / 2; 12 | return c - (c & -c) / (b + 1); 13 | } 14 | 15 | void putB(uint64_t n, uint64_t a) 16 | { 17 | for (uint64_t i = 0; i < n; i++) { 18 | printf("%c ", 1 & (a >> (n - 1 - i)) ? '1' : '0'); 19 | } 20 | printf("\n"); 21 | } 22 | 23 | int main(int argc, char *argv[]) 24 | { 25 | if (argc < 3) { 26 | fprintf(stderr, "conv n k\n"); 27 | return 1; 28 | } 29 | const int n = atoi(argv[1]); 30 | const int k = atoi(argv[2]); 31 | uint64_t a = ((uint64_t(1) << (n - k)) - 1) << k; 32 | int c = 1; 33 | do { 34 | printf("% 8d: ", c++); 35 | putB(n, a); 36 | } while (a = nextCombination(a)); 37 | } 38 | -------------------------------------------------------------------------------- /sanitize/Makefile: -------------------------------------------------------------------------------- 1 | CLANG?=clang++-14 2 | SRC=list_err.cpp strlen_err.cpp substr_err.cpp 3 | EXE=$(SRC:cpp=exe) 4 | IGNORE?=0 5 | 6 | CFLAGS=-g -fsanitize=memory -fsanitize-memory-track-origins 7 | ifeq ($(IGNORE),1) 8 | CFLAGS+=-fsanitize-ignorelist=ignorelist.txt 9 | endif 10 | 11 | %.exe: %.cpp 12 | $(CLANG) -o $@ $< $(CFLAGS) 13 | 14 | test: $(EXE) 15 | ./list_err.exe 16 | ./strlen_err.exe 17 | ./substr_err.exe 18 | 19 | clone_LLVM: 20 | git clone --depth=1 https://github.com/llvm/llvm-project 21 | cd llvm-project 22 | mkdir build 23 | libc: build 24 | # configure cmake 25 | cd build ; cmake -GNinja ../llvm \ 26 | -DCMAKE_BUILD_TYPE=Release \ 27 | -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi" \ 28 | -DCMAKE_C_COMPILER=clang \ 29 | -DCMAKE_CXX_COMPILER=clang++ \ 30 | -DLLVM_USE_SANITIZER=MemoryWithOrigins 31 | # build the libraries 32 | cd build ; cmake --build . -- cxx cxxabi 33 | 34 | clean: 35 | rm -f $(EXE) 36 | -------------------------------------------------------------------------------- /mul128.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | inline uint64_t mulUnit(uint64_t *pH, uint64_t x, uint64_t y) 5 | { 6 | const uint64_t mask = 0xffffffff; 7 | uint64_t v = (x & mask) * (y & mask); 8 | uint64_t L = uint32_t(v); 9 | uint64_t H = v >> 32; 10 | uint64_t ad = (x & mask) * uint32_t(y >> 32); 11 | uint64_t bc = uint32_t(x >> 32) * (y & mask); 12 | H += uint32_t(ad); 13 | H += uint32_t(bc); 14 | L |= H << 32; 15 | H >>= 32; 16 | H += ad >> 32; 17 | H += bc >> 32; 18 | H += (x >> 32) * (y >> 32); 19 | *pH = H; 20 | return L; 21 | } 22 | 23 | typedef __attribute__((mode(TI))) unsigned int uint128; 24 | 25 | int main() 26 | { 27 | uint64_t x = 0x1234567890123; 28 | uint64_t y = 0x23429834723487; 29 | uint64_t L, H; 30 | L = mulUnit(&H, x, y); 31 | printf("%016lx:%016lx\n", H, L); 32 | uint128 z = uint128(x) * uint128(y); 33 | printf("%016lx:%016lx\n", uint64_t(z >> 64), uint64_t(z)); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /slow_sin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | cl /EHsc /Ox /Ob2 /fp:fast /Zi slow_sin.cpp 3 | cl /EHsc /Ox /Ob2 /fp:fast /arch:AVX /Feslow_sin_avx.exe /Zi slow_sin.cpp 4 | 5 | Core i7-2600 3.4GHz / Windows 7 Ultimate 6 | 7 | VS2012(x64) VS2013(x64) 8 | slow_sin 1.33 1.34 9 | slow_sin_avx 1.33 6.27 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | struct A { 17 | float a[8]; 18 | A() 19 | { 20 | const float x = log(2.0f); 21 | for (int i = 0; i < 8; i++) { 22 | a[i] = x; 23 | } 24 | } 25 | }; 26 | 27 | const A a; 28 | 29 | int main() 30 | { 31 | double (*f)(double) = sin; 32 | double begin = clock(); 33 | double x = 1.0; 34 | double y = 0; 35 | x = 1; 36 | y = 0; 37 | for (int i = 0; i < 100000000; i++) { 38 | y += f(x); 39 | x += 1e-6; 40 | } 41 | double end = clock(); 42 | printf("x=%f, %.2fsec\n", x, (end - begin) /double(CLOCKS_PER_SEC)); 43 | } 44 | -------------------------------------------------------------------------------- /sve/perm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // void perm(float dst[16], const float src[16], const uint32_t idx[16]); 4 | struct Code : Xbyak::CodeGenerator { 5 | Code() 6 | { 7 | Xbyak::util::StackFrame sf(this, 3); 8 | vmovups(zm1, ptr[sf.p[1]]); 9 | vmovups(zm2, ptr[sf.p[2]]); 10 | vpermps(zm0, zm1, zm2); 11 | vmovups(ptr[sf.p[0]], zm0); 12 | } 13 | }; 14 | 15 | void put(const char *msg, const float x[16]) 16 | { 17 | printf("%s", msg); 18 | for (int i = 0; i < 16; i++) { 19 | printf(" %.2f", x[i]); 20 | } 21 | printf("\n"); 22 | } 23 | 24 | int main() 25 | { 26 | Code c; 27 | auto perm = c.getCode(); 28 | float in[16], out[16]; 29 | uint32_t idx[16]; 30 | for (int i = 0; i < 16; i++) { 31 | in[i] = i * 0.1f; 32 | } 33 | for (int i = 0; i < 16; i++) { 34 | idx[i] = 15 - i; 35 | } 36 | put("in ", in); 37 | perm(out, idx, in); 38 | put("out", out); 39 | } 40 | -------------------------------------------------------------------------------- /avx-512/mask-load.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | g++ -O3 -mavx512f -mavx512vl -mavx512bw mask-load.cpp 3 | */ 4 | #include 5 | #include 6 | #ifdef _MSC_VER 7 | #include 8 | #else 9 | #include 10 | #endif 11 | 12 | __m128i load(const void *p, size_t n) 13 | { 14 | #if !defined(__AVX512BW__) || !defined(__AVX512VL__) 15 | #error "-mavx512bw -mavx512vl" 16 | #endif 17 | if (n == 16) { 18 | return _mm_loadu_si128((const __m128i*)p); 19 | } else { 20 | __mmask16 k = _mm512_kmov((1 << n) - 1); 21 | return _mm_maskz_loadu_epi8(k, p); 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | const int N = 16; 28 | char buf[N]; 29 | for (int i = 0; i < N; i++) { 30 | buf[i] = char(i + 1); 31 | } 32 | for (int i = 0; i < N + 1; i++) { 33 | __m128i x = load(buf, i); 34 | printf("%2d ", i); 35 | unsigned char b[N]; 36 | memcpy(b, &x, N); 37 | for (int j = 0; j < N; j++) { 38 | printf("%02x", b[j]); 39 | } 40 | printf("\n"); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /swig/TestTest.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import mcl.test.*; 3 | 4 | public class TestTest { 5 | static { 6 | System.loadLibrary("test_wrap"); 7 | } 8 | public static void main(String argv[]) { 9 | try { 10 | System.out.println(":::A(5)"); 11 | A a0 = new A(5); 12 | System.out.println(":::A(a0)"); 13 | A a1 = new A(a0); 14 | System.out.println(":::addModify"); 15 | a1.put(); 16 | a1.addModify(5); 17 | a1.put(); 18 | System.out.println(":::addReturn"); 19 | A a3 = a1.addReturn(4); 20 | a1.put(); 21 | a3.put(); 22 | System.out.println(":::addModify"); 23 | a3.addModify(2); 24 | a1.put(); 25 | a3.put(); 26 | System.out.println(":::assign"); 27 | a3 = a1; 28 | a1.put(); 29 | a3.put(); 30 | System.out.println(":::AddRetrun with new"); 31 | A a4 = new A(a0.addReturn(4)); 32 | a1.put(); 33 | a3.put(); 34 | } catch (RuntimeException e) { 35 | System.out.println("unknown exception :" + e); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /sve/ff.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "floatformat.hpp" 7 | 8 | int main() 9 | { 10 | printf("sizeof ff=%zd\n", sizeof(FloatFormat)); 11 | const struct { 12 | uint32_t s; 13 | uint32_t e; 14 | uint32_t f; 15 | } tbl[] = { 16 | { 0, 0, 0 }, 17 | { 0, 127, 0 }, 18 | { 0, 0, 1 }, 19 | { 0, 128, 0 }, 20 | { 0, 255, 0 }, 21 | { 0, 255, 1 }, 22 | { 0, 255, 1 << 22 }, 23 | { 0, 255, 1 << 21 }, 24 | }; 25 | printf("NAN=%d\n", FP_NAN); 26 | printf("INFINITE=%d\n", FP_INFINITE); 27 | printf("ZERO=%d\n", FP_ZERO); 28 | printf("SUBNORMAL=%d\n", FP_SUBNORMAL); 29 | printf("NORMAL=%d\n", FP_NORMAL); 30 | FloatFormat ff; 31 | for (size_t i = 0; i < sizeof(tbl)/sizeof(tbl[0]); i++) { 32 | uint32_t s = tbl[i].s; 33 | uint32_t e = tbl[i].e; 34 | uint32_t f = tbl[i].f; 35 | ff.set(s, e, f); 36 | ff.put(); 37 | ff.set(1 - s, e, f); 38 | ff.put(); 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /vc_iterator.cpp: -------------------------------------------------------------------------------- 1 | #define _ALLOW_KEYWORD_MACROS 2 | #define private public 3 | #include 4 | #include 5 | 6 | typedef std::vector IntVec; 7 | 8 | int main() 9 | { 10 | IntVec iv; 11 | iv.push_back(123); 12 | IntVec::iterator i = iv.begin(); 13 | 14 | printf("sizeof(i)=%d\n", (int)sizeof(i)); 15 | printf("sizeof(_Ptr)=%d\n", (int)sizeof(i._Ptr)); 16 | printf("*_Ptr=%d\n", *i._Ptr); 17 | 18 | printf("ptr &*i =%p\n", &*i); 19 | printf("ptr i =%p\n", &i); 20 | printf("ptr proxy=%p\n", &i._Myproxy); 21 | printf("ptr next =%p\n", &i._Mynextiter); 22 | printf("ptr _Ptr =%p\n", &i._Ptr); 23 | 24 | printf("offset proxy=%d\n", (int)offsetof(IntVec::iterator, _Myproxy)); 25 | printf("offset next =%d\n", (int)offsetof(IntVec::iterator, _Mynextiter)); 26 | printf("offset _Ptr =%d\n", (int)offsetof(IntVec::iterator, _Ptr)); 27 | 28 | printf("val proxy=%x\n", i._Myproxy); 29 | printf("val next =%x\n", i._Mynextiter); 30 | printf("val _Ptr =%x\n", i._Ptr); 31 | 32 | } -------------------------------------------------------------------------------- /go/callback/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | import ( 3 | "fmt" 4 | "crypto/rand" 5 | ) 6 | 7 | type RandCallback struct { 8 | } 9 | 10 | func (r *RandCallback) read(buf []byte) bool { 11 | n, err := rand.Read(buf) 12 | if n != len(buf) || err != nil { 13 | return false 14 | } 15 | return true 16 | } 17 | 18 | type Add struct { 19 | x int 20 | } 21 | 22 | func (self *Add) run(x int) int { 23 | fmt.Printf(" Add.read (self=%x) x=%d\n", self.x, x) 24 | return x + 1 25 | } 26 | 27 | func main() { 28 | fmt.Printf("callback test\n") 29 | setCallbackGo(new(Add)) 30 | /* 31 | callCallbackGo 32 | -> C.callCallbackC 33 | -> s_callbackC ; function pointer of C 34 | -> C.wrapCallbackCgo ; defined in sub.go 35 | -> wrapCallbackGo ; defined in callback.go 36 | -> s_callbackIF 37 | -> Add.run 38 | */ 39 | callCallbackGo(5) 40 | buf := make([]byte, 7) 41 | fmt.Printf("buf=%x\n", buf) 42 | rng := new(RandCallback) 43 | rng.read(buf) 44 | fmt.Printf("buf=%x\n", buf) 45 | } 46 | 47 | -------------------------------------------------------------------------------- /rop/test-x64.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace Xbyak; 4 | 5 | void putVal(int x) 6 | { 7 | printf("x=%d\n", x); 8 | } 9 | 10 | struct Code : CodeGenerator { 11 | Code(bool stop) 12 | { 13 | Label clearEaxL, incEaxL, putValL; 14 | if (stop) int3(); 15 | const Label* tbl[] = { 16 | &clearEaxL, 17 | &incEaxL, 18 | &putValL, 19 | }; 20 | const size_t n = sizeof(tbl)/sizeof(tbl[0]); 21 | for (size_t i = 0; i < n; i++) { 22 | mov(rax, *tbl[n - 1 - i]); 23 | push(rax); 24 | } 25 | // exec 26 | ret(); 27 | 28 | L(incEaxL); 29 | inc(eax); 30 | ret(); 31 | L(clearEaxL); 32 | xor_(eax, eax); 33 | ret(); 34 | L(putValL); 35 | #ifdef XBYAK64_GCC 36 | mov(rdi, rax); 37 | #else 38 | mov(rcx, rax); 39 | #endif 40 | mov(rax, (size_t)putVal); 41 | call(rax); 42 | ret(); 43 | } 44 | }; 45 | 46 | int main(int argc, char *[]) 47 | { 48 | Code c(argc > 1); 49 | auto f = c.getCode(); 50 | f(); 51 | puts("end"); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /signal_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void handler(int x) 9 | { 10 | printf("handler x=%d\n", x); 11 | exit(1); 12 | } 13 | 14 | void run() 15 | { 16 | puts("exec 'push es'"); 17 | // 'push es' is invalid on x64 18 | __asm__ volatile(".byte 0x06"); 19 | puts("not here"); 20 | } 21 | 22 | void setHandler() 23 | { 24 | struct sigaction sa; 25 | sa.sa_handler = handler; 26 | sigfillset(&sa.sa_mask); 27 | sa.sa_flags = 0; 28 | if (sigaction(SIGILL, &sa, NULL)) { 29 | perror("sigaction"); 30 | return; 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | setHandler(); 37 | pid_t pid = fork(); 38 | if (pid < 0) { 39 | perror("fork"); 40 | return 1; 41 | } 42 | if (pid == 0) { 43 | printf("child %d\n", pid); 44 | _exit(0); 45 | } 46 | printf("parent %d\n", pid); 47 | run(); 48 | waitpid(pid, NULL, 0); 49 | puts("parent end"); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /rop/test-aarch64.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace Xbyak_aarch64; 4 | 5 | void putVal(int x) 6 | { 7 | printf("x=%d\n", x); 8 | } 9 | 10 | struct Code : CodeGenerator { 11 | Code(bool stop) 12 | { 13 | Label tblL, incL, addL; 14 | if (stop) brk(0); 15 | int saveByte = 16; 16 | stp(x29, x30, pre_ptr(sp, -saveByte)); 17 | mov(x0, 0); 18 | #if 1 19 | adr(x1, incL); 20 | blr(x1); 21 | #else 22 | adr(x2, addL); 23 | blr(x2); 24 | adr(x2, incL); 25 | blr(x2); 26 | #endif 27 | // exec 28 | ldp(x29, x30, post_ptr(sp, saveByte)); 29 | ret(); 30 | 31 | L(incL); 32 | add(x0, x0, 1); 33 | ret(); 34 | L(addL); 35 | add(x0, x0, 5); 36 | ret(); 37 | L(tblL); 38 | putL(incL); 39 | putL(addL); 40 | 41 | ready(); 42 | } 43 | }; 44 | 45 | int main(int argc, char *[]) 46 | { 47 | Code c(argc > 1); 48 | puts("start"); 49 | auto f = c.getCode(); 50 | int v = f(); 51 | printf("v=%d\n", v); 52 | puts("end"); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /const_div/constdiv.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | class ConstDiv: 4 | def __init__(self, d, M=2**32-1): 5 | self.d = d 6 | self.M = M 7 | M_d = M - ((M+1)%d) 8 | a = math.ceil(math.log2(d)) 9 | while True: 10 | A = 1 << a 11 | c = (A + d - 1) // d 12 | e = d * c - A 13 | if e * M_d < A: 14 | self.a = a 15 | self.A = A 16 | self.c = c 17 | self.e = e 18 | return 19 | a += 1 20 | 21 | def divd(self, x): 22 | return (x * self.c) >> self.a 23 | 24 | def __str__(self): 25 | return f'd={self.d} c={self.c} c.len={self.c.bit_length()} a={self.a} e={self.e}' 26 | 27 | 28 | M=2**9-1 29 | 30 | def findMax(M): 31 | print(f'{M=} len={M.bit_length()}') 32 | maxc = 0 33 | maxcd = None 34 | for d in range(1, M+1): 35 | cd = ConstDiv(d, M) 36 | if cd.c > maxc: 37 | maxc = cd.c 38 | maxcd = cd 39 | 40 | print(f'{maxcd}') 41 | 42 | 43 | for i in range(8, 18): 44 | M=2**i-1 45 | findMax(M) 46 | -------------------------------------------------------------------------------- /extint/wasm/multi3.ll: -------------------------------------------------------------------------------- 1 | define private i64 @mul32x32L(i32 %r2, i32 %r3) 2 | { 3 | %r4 = zext i32 %r2 to i64 4 | %r5 = zext i32 %r3 to i64 5 | %r6 = mul i64 %r4, %r5 6 | ret i64 %r6 7 | } 8 | define i128 @__multi3(i64 %r2, i64 %r3) 9 | { 10 | %r4 = lshr i64 %r2, 32 11 | %r5 = trunc i64 %r4 to i32 12 | %r6 = trunc i64 %r2 to i32 13 | %r7 = lshr i64 %r3, 32 14 | %r8 = trunc i64 %r7 to i32 15 | %r9 = trunc i64 %r3 to i32 16 | %r10 = call i64 @mul32x32L(i32 %r5, i32 %r9) 17 | %r11 = call i64 @mul32x32L(i32 %r6, i32 %r9) 18 | %r12 = zext i64 %r11 to i96 19 | %r13 = zext i64 %r10 to i96 20 | %r14 = shl i96 %r13, 32 21 | %r15 = add i96 %r14, %r12 22 | %r16 = call i64 @mul32x32L(i32 %r5, i32 %r8) 23 | %r17 = call i64 @mul32x32L(i32 %r6, i32 %r8) 24 | %r18 = zext i64 %r17 to i96 25 | %r19 = zext i64 %r16 to i96 26 | %r20 = shl i96 %r19, 32 27 | %r21 = add i96 %r20, %r18 28 | %r22 = zext i96 %r15 to i128 29 | %r23 = zext i96 %r21 to i128 30 | %r24 = shl i128 %r23, 32 31 | %r25 = add i128 %r24, %r22 32 | ret i128 %r25 33 | } 34 | -------------------------------------------------------------------------------- /text2sound/text2sound.js: -------------------------------------------------------------------------------- 1 | /* 2 | how to use : cscript test2sound.js text-file 3 | */ 4 | var isWScript = true 5 | if (WScript.FullName.indexOf('cscript.exe') >= 0) { 6 | isWScript = false 7 | } 8 | 9 | var args = WScript.Arguments 10 | 11 | if (args.length == 0) { 12 | WScript.Echo('ERR cscript text2sound.js text-file') 13 | WScript.Quit() 14 | } 15 | var fs = new ActiveXObject("Scripting.FileSystemObject") 16 | var textFile = args.Item(0) 17 | //WScript.Echo('open ' + textFile) 18 | var ifs = fs.OpenTextFile(textFile) 19 | var text = ifs.ReadAll().split(/\r\n/) 20 | ifs.Close() 21 | 22 | var v = WScript.CreateObject("SAPI.SpVoice") 23 | // v.Rate = 0 // slow if < 0, 0:normal, fast if > 0 24 | 25 | for (var i = 0; i < text.length; i++) { 26 | var line = text[i] 27 | if (line == '@pause') { 28 | v.Pause() 29 | WScript.Echo('pause : type return key') 30 | if (!isWScript) { 31 | WScript.StdIn.ReadLine() 32 | } 33 | v.Resume() 34 | } else { 35 | v.Speak(line) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /slow-pti/runcmd.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | 4 | def to_str(ss): 5 | return " ".join(ss) 6 | 7 | def run_local_command(args, putMsg=False): 8 | ''' 9 | run a command at localhost. 10 | args :: [str] - command line arguments. 11 | The head item must be full-path executable. 12 | putMsg :: bool - put debug message. 13 | return :: str - standard output of the command. 14 | ''' 15 | 16 | if putMsg: 17 | print "run_command:", to_str(args) 18 | p = subprocess.Popen(args, stdout=subprocess.PIPE, 19 | stderr=sys.stderr, close_fds=True) 20 | f = p.stdout 21 | s = f.read().strip() 22 | ret = p.wait() 23 | print 'ret', ret 24 | # if ret != 0: 25 | # raise Exception("command error %s %d\n" % (args, ret)) 26 | if putMsg: 27 | print "run_command_result:", s 28 | return s 29 | 30 | #print run_local_command(['/usr/bin/lsof', '-i', 'TCP:10200']) 31 | print run_local_command(sys.argv[1:]) 32 | 33 | -------------------------------------------------------------------------------- /montgomery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "montgomery.hpp" 4 | 5 | void test(const mpz_class& p) 6 | { 7 | std::cout << "p=0x" << std::hex << p << std::endl; 8 | Montgomery mont(p); 9 | 10 | mpz_class x1("123456789012345678"); 11 | mpz_class y1("987654321092342342"); 12 | for (int i = 0; i < 100; i++) { 13 | mpz_class z1 = (x1 * y1) % p; 14 | mpz_class x2, y2, z2; 15 | mont.toMont(x2, x1); 16 | mont.toMont(y2, y1); 17 | mont.mul(z2, x2, y2); 18 | mont.fromMont(z2, z2); 19 | if (z1 != z2) { 20 | puts("ERR"); 21 | std::cout << z1 << std::endl; 22 | std::cout << z2 << std::endl; 23 | return; 24 | } 25 | x1 = z1; 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | const char *pTbl[] = { 32 | "0x2523648240000001ba344d80000000086121000000000013a700000000000013", 33 | "0xfffffffffffffffffffffffffffffffffffffffeffffee37", 34 | }; 35 | for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(pTbl); i++) { 36 | const mpz_class p(pTbl[i]); 37 | test(p); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /static-cstr/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifndef PRIORITY 4 | #define PRIORITY 101 5 | #endif 6 | 7 | #ifdef __GNUC__ 8 | #define CSTR __attribute__((constructor(PRIORITY))) 9 | #define INIT_PRIORITY(x) __attribute__((init_priority(x))) 10 | #else 11 | #define CSTR 12 | #define INIT_PRIORITY(x) 13 | #endif 14 | 15 | namespace { 16 | 17 | static struct X1 { 18 | X1() { puts("X1 cstr"); } 19 | } x1 INIT_PRIORITY(1030); 20 | 21 | static void CSTR initMain() 22 | { 23 | puts("initMain"); 24 | } 25 | 26 | #ifdef _MSC_VER 27 | #pragma warning(default:5247) 28 | #pragma warning(default:5248) 29 | #pragma section(".CRT$XCT", read) 30 | __declspec(allocate(".CRT$XCT")) void(*ptr_initMain)() = initMain; 31 | #endif 32 | 33 | static struct X2 { 34 | X2() { puts("X2 cstr"); } 35 | } x2 INIT_PRIORITY(1010); 36 | 37 | } 38 | 39 | int main() 40 | { 41 | puts("main"); 42 | puts(""); 43 | } 44 | 45 | namespace { 46 | 47 | static struct X3 { 48 | X3() { puts("X3 cstr"); } 49 | } x3 INIT_PRIORITY(1020); 50 | 51 | } 52 | -------------------------------------------------------------------------------- /shared/mymalloc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ASLR off 3 | sudo sh -c "echo 0 > /proc/sys/kernel/randomize_va_space" 4 | ASLR on 5 | sudo sh -c "echo 2 > /proc/sys/kernel/randomize_va_space" 6 | */ 7 | #pragma once 8 | #define _GNU_SOURCE 9 | #include 10 | int mie_posix_memalign(void **memptr, size_t alignment, size_t size); 11 | void *mie_aligned_alloc(size_t alignment, size_t size); 12 | void *mie_memalign(size_t alignment, size_t size); 13 | void *mie_malloc(size_t size); 14 | void mie_free(void *ptr); 15 | void *mie_calloc(size_t n, size_t size); 16 | void *mie_realloc(void *ptr, size_t size); 17 | char *mie_strdup(const char *ptr); 18 | size_t mie_malloc_usable_size(void *p); 19 | void mie_init(); 20 | #define posix_memalign mie_posix_memalign 21 | #define aligned_alloc mie_aligned_alloc 22 | #define memalign mie_memalign 23 | #define malloc mie_malloc 24 | #define free mie_free 25 | #define calloc mie_calloc 26 | #define realloc mie_realloc 27 | #define strdup mie_strdup 28 | #define malloc_usable_size mie_malloc_usable_size 29 | 30 | -------------------------------------------------------------------------------- /slow-pti/loop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void add(long *x, const long *y) 5 | { 6 | x[0] += x[1] + y[0]; 7 | x[1] += y[1] + x[0]; 8 | } 9 | 10 | int main(int argc, char *[]) 11 | { 12 | long x[2] = { argc, argc }; 13 | long y[2] = { argc, argc }; 14 | void (*f)(long *x, const long *y); 15 | if (argc) f = add; 16 | clock_t begin, end; 17 | begin = clock(); 18 | for (int i = argc; i < 1000000000; i++) { 19 | x[0] += x[1] + y[0]; 20 | x[1] += y[1] + x[0]; 21 | } 22 | end = clock(); 23 | printf("inline %ld %ld %f\n", x[0], x[1], (end - begin) / double(CLOCKS_PER_SEC)); 24 | begin = clock(); 25 | for (int i = argc; i < 1000000000; i++) { 26 | add(x, y); 27 | } 28 | end = clock(); 29 | printf("call fct %ld %ld %f\n", x[0], x[1], (end - begin) / double(CLOCKS_PER_SEC)); 30 | begin = clock(); 31 | for (int i = argc; i < 1000000000; i++) { 32 | f(x, y); 33 | } 34 | end = clock(); 35 | printf("call fct ptr %ld %ld %f\n", x[0], x[1], (end - begin) / double(CLOCKS_PER_SEC)); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /avx-512/ifma-test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "util.hpp" 3 | 4 | using namespace Xbyak::util; 5 | 6 | int main() 7 | try 8 | { 9 | Xbyak::util::Cpu cpu; 10 | if (!cpu.has(Xbyak::util::Cpu::tAVX512_IFMA)) { 11 | printf("AVX512_IFMA is not supported\n"); 12 | return 1; 13 | } 14 | const int N = 8; 15 | const uint64_t _a[N] = { 16 | 0x12345678abcdefab, 0x45678abcdefab, 17 | }; 18 | const uint64_t _b[N] = { 19 | 0xffeeddccbbaa8877, 0xeddccbbaa8877, 20 | }; 21 | const uint64_t _c[N] = { 22 | 0xffffaabbccddeeff, 0xffffaabbccddeeff, 23 | }; 24 | Vec a, b, c, z; 25 | set(a, _a); 26 | set(b, _b); 27 | set(c, _c); 28 | z = vmulL(a, b, c); // low(c+a*b) 29 | dump(a, "a"); 30 | dump(b, "b"); 31 | dump(c, "c"); 32 | dump(z, "z"); 33 | const uint64_t mask52 = getMask(52); 34 | uint128_t ia = _a[0]; 35 | uint128_t ib = _b[0]; 36 | uint128_t ic = _c[0]; 37 | uint64_t iz = (mask52 & ((mask52 & ia) * (mask52 & ib))) + ic; 38 | dump(iz, "i"); 39 | } catch (std::exception& e) { 40 | printf("err %s\n", e.what()); 41 | } 42 | -------------------------------------------------------------------------------- /unsigned_mul.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | uint32_t neg(uint32_t a, uint32_t mask) 5 | { 6 | return (a ^ mask) - mask; 7 | } 8 | 9 | uint32_t mul(uint32_t a, uint32_t b) 10 | { 11 | uint32_t sa = int(a) >> 31; 12 | uint32_t sb = int(b) >> 31; 13 | uint32_t aa = neg(a, sa); 14 | uint32_t bb = neg(b, sb); 15 | aa *= bb; 16 | sa ^= sb; 17 | return neg(aa, sa); 18 | } 19 | 20 | int main() 21 | { 22 | int a = 9; 23 | int b = 50; 24 | uint32_t a1 = a; 25 | uint32_t a2 = -a; 26 | uint32_t b1 = b; 27 | uint32_t b2 = -b; 28 | uint32_t c11 = a1 * b1; 29 | uint32_t c12 = a1 * b2; 30 | uint32_t c21 = a2 * b1; 31 | uint32_t c22 = a2 * b2; 32 | printf("11 %08x x %08x = %08x (%08x) %d %d\n", a1, b1, c11, -c11, a * b, mul(a, b)); 33 | printf("12 %08x x %08x = %08x (%08x) %d %d\n", a1, b2, c12, -c12, a * (-b), mul(a, -b)); 34 | printf("21 %08x x %08x = %08x (%08x) %d %d\n", a2, b1, c21, -c21, (-a) * b, mul(-a, b)); 35 | printf("22 %08x x %08x = %08x (%08x) %d %d\n", a2, b2, c22, -c22, (-a) * (-b), mul(-a, -b)); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /make/depend.md: -------------------------------------------------------------------------------- 1 | # Makefileでの依存関係の書き方 2 | 3 | ## ソースファイルに対応する依存ファイル 4 | ``` 5 | SRC=a.cpp b.cpp main.cpp 6 | DEP=$(SRC:.cpp=.d) 7 | ``` 8 | * cppに対応するdファイルとする 9 | 10 | ## コンパイラに依存関係を出力させる 11 | ``` 12 | %.o: %.cpp 13 | $(CXX) -c $< -o $@ $(CFLAGS) -MMD -MP -MF $(@:.o=.d) 14 | ``` 15 | * `-MMD -MP`で依存関係の出力 16 | * -MFでファイル名を指定(なくてもよい) 17 | 18 | ## 生成された依存ファイルを取り込む 19 | ``` 20 | -include $(DEP) 21 | ``` 22 | 23 | ## 依存ファイルを消去しないようにする 24 | ``` 25 | .SECONDARY: $(DEP) 26 | ``` 27 | * 自動生成された中間ファイルが自動的に削除されて困ることがあるので明示する(簡単なものなら無くてもよい) 28 | 29 | ## 例 30 | 31 | ``` 32 | SRC=a.cpp b.cpp main.cpp 33 | OBJ=$(SRC:.cpp=.o) 34 | DEP=$(SRC:.cpp=.d) 35 | 36 | TARGET=main 37 | CFLAGS+=-O3 -Wall -Wextra -DNDEUBG -g 38 | 39 | all: $(TARGET) 40 | 41 | %.o: %.cpp 42 | $(CXX) -c $< -o $@ $(CFLAGS) -MMD -MP -MF $(@:.o=.d) 43 | 44 | -include $(DEP) 45 | 46 | $(TARGET): $(OBJ) 47 | $(CC) -o $@ $? $(LDFLAGS) 48 | 49 | clean: 50 | $(RM) -rf $(OBJ) $(DEP) $(TARGET) 51 | 52 | .PHONY: clean 53 | 54 | .SECONDARY: $(DEP) 55 | ``` 56 | -------------------------------------------------------------------------------- /thread-local.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define XBYAK_NO_OP_NAMES 4 | #include "xbyak/xbyak.h" 5 | #include 6 | 7 | thread_local int a = 5; 8 | 9 | int b = 3; 10 | 11 | struct Code1 : public Xbyak::CodeGenerator { 12 | Code1() 13 | { 14 | const void *addr = (void*)0xffffffe0; 15 | putSeg(fs); inc(dword [addr]); 16 | putSeg(fs); mov(rax, ptr [addr]); 17 | ret(); 18 | } 19 | }; 20 | 21 | struct Code2 : public Xbyak::CodeGenerator { 22 | Code2() 23 | { 24 | mov(rax, (size_t)&b); 25 | inc(dword [rax]); 26 | mov(eax, ptr [rax]); 27 | ret(); 28 | } 29 | }; 30 | 31 | void ff() 32 | { 33 | printf("ff %d\n", a); 34 | } 35 | 36 | int main() 37 | { 38 | std::thread t(ff); 39 | printf("%d\n", a); 40 | Code1 c1; 41 | Code2 c2; 42 | int (*f)() = c1.getCode(); 43 | int (*g)() = c2.getCode(); 44 | printf("f=%d\n", f()); 45 | printf("g=%d\n", g()); 46 | CYBOZU_BENCH_C("f", 100000, f); 47 | CYBOZU_BENCH_C("g", 100000, g); 48 | t.join(); 49 | printf("a=%d, b=%d\n", a, b); 50 | } 51 | -------------------------------------------------------------------------------- /gf256/gen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mul.h" 3 | 4 | void gen_inv() 5 | { 6 | printf("static const uint8_t invTbl[] = {\n"); 7 | printf("0x00, "); 8 | for (int i = 1; i < 256; i++) { 9 | for (int j = 1; j < 256; j++) { 10 | if (mul(i, j) == 1) { 11 | printf("0x%02x, ", j); 12 | break; 13 | } 14 | } 15 | if ((i % 16) == 15) putchar('\n'); 16 | } 17 | puts("};"); 18 | } 19 | 20 | void gen_exp_log() 21 | { 22 | uint8_t logTbl[256] = {}; 23 | uint8_t g = 3; // generator of K-{0} 24 | uint8_t x = 1; 25 | puts("static const uint8_t expTbl[] = {"); 26 | for (int i = 0; i < 255; i++) { 27 | printf("0x%02x, ", x); // expTbl[i] = g^i 28 | logTbl[x] = i; // logTbl[expTbl[i]] = i 29 | x = mul(x, g); 30 | if ((i % 16) == 15) putchar('\n'); 31 | } 32 | puts("};"); 33 | puts("static const uint8_t logTbl[] = {"); 34 | for (int i = 1; i < 256; i++) { 35 | printf("0x%02x, ", logTbl[i]); 36 | if ((i % 16) == 15) putchar('\n'); 37 | } 38 | puts("};"); 39 | } 40 | 41 | int main() 42 | { 43 | gen_inv(); 44 | gen_exp_log(); 45 | } 46 | -------------------------------------------------------------------------------- /slow_sin2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | cl /EHsc /Ox /Ob2 /fp:fast /arch:AVX /Zi slow_sin2.cpp 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define XBYAK_NO_OP_NAMES 10 | #include 11 | #ifdef _MSC_VER 12 | #include 13 | #endif 14 | 15 | const struct Code : Xbyak::CodeGenerator { 16 | Code() 17 | { 18 | vinsertf128(ym0, ym0, xm0, 1); 19 | // vzeroupper(); 20 | ret(); 21 | } 22 | } code; 23 | 24 | void (*call_vinsertf128)() = code.getCode(); 25 | 26 | int main() 27 | { 28 | double (*f)(double) = sin; 29 | printf("f=%p\n", f); 30 | double x = 1.0; 31 | CYBOZU_BENCH("before vinsertf128", x = f, x); 32 | printf("x=%f\n", x); 33 | 34 | #if 1 35 | call_vinsertf128(); 36 | #else 37 | { 38 | puts("a"); 39 | __m128d x;// = _mm_setzero_si128(); 40 | __m256d y;// = _mm256_setzero_si256(); 41 | y = _mm256_insertf128_pd(y, x, 1); 42 | printf("%d\n", *(int*)&y); 43 | } 44 | #endif 45 | CYBOZU_BENCH("after vinsertf128", x = f, x); 46 | printf("x=%f\n", x); 47 | } 48 | -------------------------------------------------------------------------------- /sve/softmax.cpp: -------------------------------------------------------------------------------- 1 | #define FMATH_USE_LOG_TBL 2 | #include "fmath-sve.hpp" 3 | #include "log.hpp" 4 | #include 5 | #include 6 | #include 7 | 8 | // 0 : -0.31326166 9 | // 1 : -1.31326163 10 | float logsoftmax1(float x0, float x1) 11 | { 12 | float maxv = std::max(x0, x1); 13 | float sum = 0; 14 | sum += std::exp(x0 - maxv) + std::exp(x1 - maxv); 15 | sum = std::log(sum); 16 | float ret = x0 - maxv - sum; 17 | printf("0 : %.8f\n", ret); 18 | printf("1 : %.8f\n", x1 - maxv - sum); 19 | return ret; 20 | } 21 | 22 | // 0 : -0.31326175 23 | // 1 : -1.31326175 24 | float logsoftmax2(float x0, float x1) 25 | { 26 | float maxv = std::max(x0, x1); 27 | float sum = 0; 28 | sum += fmath::expf(x0 - maxv) + fmath::expf(x1 - maxv); 29 | sum = fmath::logf(sum); 30 | float ret = x0 - maxv - sum; 31 | printf("0 : %.8f\n", ret); 32 | printf("1 : %.8f\n", x1 - maxv - sum); 33 | return ret; 34 | } 35 | 36 | 37 | int main() 38 | { 39 | float ok = logsoftmax1(90, 89); 40 | float my = logsoftmax2(90, 89); 41 | printf("ok - my=%e\n", ok - my); 42 | } 43 | -------------------------------------------------------------------------------- /inline/inline_call.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace Xbyak; 5 | using namespace Xbyak::util; 6 | 7 | void (*f1)(); 8 | void (*f2)(); 9 | 10 | const int N = 1000; 11 | 12 | struct Code : CodeGenerator { 13 | Code() 14 | { 15 | gen1(); 16 | gen2(); 17 | } 18 | void gen1() 19 | { 20 | f1 = getCurr(); 21 | mov(ecx, N); 22 | xor_(eax, eax); 23 | Label lp; 24 | L(lp); 25 | add(eax, 3); 26 | imul(eax, eax); 27 | dec(ecx); 28 | jnz(lp); 29 | ret(); 30 | } 31 | void gen2() 32 | { 33 | f2 = getCurr(); 34 | mov(ecx, N); 35 | xor_(eax, eax); 36 | Label lp, funcL; 37 | L(lp); 38 | call(funcL); 39 | dec(ecx); 40 | jnz(lp); 41 | ret(); 42 | align(32); 43 | L(funcL); 44 | add(eax, 3); 45 | imul(eax, eax); 46 | ret(); 47 | } 48 | }; 49 | 50 | int main() 51 | try 52 | { 53 | Code c; 54 | CYBOZU_BENCH_C("f1", 1000, f1); 55 | CYBOZU_BENCH_C("f2", 1000, f2); 56 | } catch (std::exception& e) { 57 | printf("err %s\n", e.what()); 58 | return 1; 59 | } 60 | 61 | -------------------------------------------------------------------------------- /wasm/Makefile: -------------------------------------------------------------------------------- 1 | all: low_func32.js 2 | ALL_SRC=mul_test.cpp 3 | TARGET=mul_test.exe 4 | all: $(TARGET) 5 | 6 | CFLAGS=-O3 -DNDEUBG -Wall -Wextra -I ../../mcl/include 7 | LDFLAGS=-lgmp -lgmpxx 8 | 9 | EMCC_OPT=-I ./mcl/include -I ./mcl/src -Wall -Wextra 10 | EMCC_OPT+=-O3 -DNDEBUG 11 | EMCC_OPT+=-s WASM=1 -s NO_EXIT_RUNTIME=1 -s NODEJS_CATCH_EXIT=0 -s NODEJS_CATCH_REJECTION=0 12 | EMCC_OPT+=-s MODULARIZE=1 13 | EMCC_OPT+=-s STRICT_JS=1 14 | EMCC_OPT+=-s SINGLE_FILE=1 15 | EMCC_OPT+=--minify 0 16 | EMCC_OPT+=-DCYBOZU_MINIMUM_EXCEPTION 17 | EMCC_OPT+=-s ABORTING_MALLOC=0 18 | 19 | low_func32.js: low_func32.hpp low_func32.cpp 20 | emcc -o $@ low_func32.cpp $(EMCC_OPT) -fno-exceptions 21 | 22 | mul.hpp: mk_mul.py 23 | python3 mk_mul.py > mul.hpp 24 | 25 | %.o: %.cpp 26 | $(PRE)$(CXX) $(CFLAGS) -c $< -o $@ -MMD -MP -MF $(@:.o=.d) 27 | 28 | %.exe: %.o 29 | $(PRE)$(CXX) $< -o $@ $(LDFLAGS) 30 | 31 | test: mul_test.exe 32 | ./mul_test.exe 33 | 34 | clean: 35 | $(RM) -rf *.exe *.o *.d 36 | 37 | DEPEND_FILE=$(addsuffix .d,$(basename $(ALL_SRC))) 38 | -include $(DEPEND_FILE) 39 | 40 | -------------------------------------------------------------------------------- /xbyak_split/t2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define XBYAK_ENABLE_OMITTED_OPERAND 3 | #include "xbyak/xbyak.h" 4 | #define CYBOZU_TEST_DISABLE_AUTO_RUN 5 | #include "cybozu/test.hpp" 6 | 7 | using namespace Xbyak; 8 | 9 | #ifdef _MSC_VER 10 | #pragma warning(disable : 4245) 11 | #pragma warning(disable : 4312) 12 | #endif 13 | class Sample : public CodeGenerator { 14 | void operator=(const Sample&); 15 | public: 16 | #include "nm.cpp" 17 | }; 18 | 19 | 20 | class ErrorSample : public CodeGenerator { 21 | void operator=(const ErrorSample&); 22 | public: 23 | void gen() 24 | { 25 | #ifndef XBYAK_NO_EXCEPTION 26 | CYBOZU_TEST_EXCEPTION(mov(ptr[eax],1), std::exception); 27 | CYBOZU_TEST_EXCEPTION(test(ptr[eax],1), std::exception); 28 | CYBOZU_TEST_EXCEPTION(adc(ptr[eax],1), std::exception); 29 | CYBOZU_TEST_EXCEPTION(setz(eax), std::exception); 30 | #endif 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | // the size of Operand exceeds 32 bit. 37 | CYBOZU_TEST_EQUAL(sizeof(Xbyak::Operand), 8u); 38 | Sample s; 39 | s.gen(); 40 | ErrorSample es; 41 | es.gen(); 42 | } 43 | -------------------------------------------------------------------------------- /xbyak_split/t3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define XBYAK_ENABLE_OMITTED_OPERAND 3 | #include "xbyak/xbyak.h" 4 | #define CYBOZU_TEST_DISABLE_AUTO_RUN 5 | #include "cybozu/test.hpp" 6 | 7 | using namespace Xbyak; 8 | 9 | #ifdef _MSC_VER 10 | #pragma warning(disable : 4245) 11 | #pragma warning(disable : 4312) 12 | #endif 13 | class Sample : public CodeGenerator { 14 | void operator=(const Sample&); 15 | public: 16 | #include "nm-cut.cpp" 17 | }; 18 | 19 | 20 | class ErrorSample : public CodeGenerator { 21 | void operator=(const ErrorSample&); 22 | public: 23 | void gen() 24 | { 25 | #ifndef XBYAK_NO_EXCEPTION 26 | CYBOZU_TEST_EXCEPTION(mov(ptr[eax],1), std::exception); 27 | CYBOZU_TEST_EXCEPTION(test(ptr[eax],1), std::exception); 28 | CYBOZU_TEST_EXCEPTION(adc(ptr[eax],1), std::exception); 29 | CYBOZU_TEST_EXCEPTION(setz(eax), std::exception); 30 | #endif 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | // the size of Operand exceeds 32 bit. 37 | CYBOZU_TEST_EQUAL(sizeof(Xbyak::Operand), 8u); 38 | Sample s; 39 | s.gen(); 40 | ErrorSample es; 41 | es.gen(); 42 | } 43 | -------------------------------------------------------------------------------- /shared/a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //#define USE_DLOPEN 7 | 8 | #ifndef USE_DLOPEN 9 | void sub_free(void*); 10 | #endif 11 | 12 | const char *soName = "./b.so"; 13 | 14 | void test_realloc() 15 | { 16 | puts("test_realloc"); 17 | char *p = malloc(1); 18 | p = realloc(p, 3); 19 | free(p); 20 | } 21 | int main() 22 | { 23 | mie_init(); 24 | printf("malloc=%p, free=%p\n", malloc, free); 25 | puts("main"); 26 | test_realloc(); 27 | char *p = malloc(7); 28 | #ifdef USE_DLOPEN 29 | puts("dlopen"); 30 | void *h = dlopen(soName, RTLD_LAZY); 31 | if (h == NULL) { 32 | perror("dlopen"); 33 | return 1; 34 | } 35 | puts("dlsym"); 36 | void (*f)() = dlsym(h, "sub_free"); 37 | if (f == NULL) { 38 | perror("dlsym"); 39 | return 1; 40 | } 41 | puts("call"); 42 | f(p); 43 | puts("dlclose"); 44 | dlclose(h); 45 | #else 46 | sub_free(p); 47 | #endif 48 | { 49 | char *q = malloc(3); 50 | printf("malloc size=%zd\n", malloc_usable_size(q)); 51 | q[3] = 'a'; 52 | free(q); 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /xbyak_split/t4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define XBYAK_ENABLE_OMITTED_OPERAND 3 | #include "xbyak/xbyak_decl_only.h" 4 | #define CYBOZU_TEST_DISABLE_AUTO_RUN 5 | #include "cybozu/test.hpp" 6 | 7 | using namespace Xbyak; 8 | 9 | #ifdef _MSC_VER 10 | #pragma warning(disable : 4245) 11 | #pragma warning(disable : 4312) 12 | #endif 13 | class Sample : public CodeGenerator { 14 | void operator=(const Sample&); 15 | public: 16 | #include "nm-cut.cpp" 17 | }; 18 | 19 | 20 | class ErrorSample : public CodeGenerator { 21 | void operator=(const ErrorSample&); 22 | public: 23 | void gen() 24 | { 25 | #ifndef XBYAK_NO_EXCEPTION 26 | CYBOZU_TEST_EXCEPTION(mov(ptr[eax],1), std::exception); 27 | CYBOZU_TEST_EXCEPTION(test(ptr[eax],1), std::exception); 28 | CYBOZU_TEST_EXCEPTION(adc(ptr[eax],1), std::exception); 29 | CYBOZU_TEST_EXCEPTION(setz(eax), std::exception); 30 | #endif 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | // the size of Operand exceeds 32 bit. 37 | CYBOZU_TEST_EQUAL(sizeof(Xbyak::Operand), 8u); 38 | Sample s; 39 | s.gen(); 40 | ErrorSample es; 41 | es.gen(); 42 | } 43 | -------------------------------------------------------------------------------- /ecdsa-test.py: -------------------------------------------------------------------------------- 1 | import ecdsa 2 | import hashlib 3 | 4 | msg = b"hello" 5 | sec = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1, hashfunc=hashlib.sha256) 6 | pub = sec.verifying_key 7 | sig = sec.sign(msg) 8 | print(f'secHex = "{sec.to_string().hex()}"') 9 | print(f'pubHex = "{pub.to_string().hex()}"') 10 | print(f'sigHex = "{sig.hex()}"') 11 | print(pub.verify(sig, msg)) 12 | 13 | secHex = "b1aa6282b14e5ffbf6d12f783612f804e6a20d1a9734ffbb6c9923c670ee8da2" 14 | pubHex = "0a09ff142d94bc3f56c5c81b75ea3b06b082c5263fbb5bd88c619fc6393dda3da53e0e930892cdb7799eea8fd45b9fff377d838f4106454289ae8a080b111f8d" 15 | sigHex = "50839a97404c24ec39455b996e4888477fd61bcf0ffb960c7ffa3bef104501919671b8315bb5c1611d422d49cbbe7e80c6b463215bfad1c16ca73172155bf31a" 16 | 17 | print('static') 18 | sec = ecdsa.SigningKey.from_string(bytes.fromhex(secHex), curve=ecdsa.SECP256k1, hashfunc=hashlib.sha256) 19 | pub = sec.verifying_key 20 | sig = bytes.fromhex(sigHex) 21 | print(pub.verify(sig, msg)) 22 | print(f'secHex = "{sec.to_string().hex()}"') 23 | print(f'pubHex = "{pub.to_string().hex()}"') 24 | print(f'sigHex = "{sig.hex()}"') 25 | -------------------------------------------------------------------------------- /swig/byte-array/Makefile: -------------------------------------------------------------------------------- 1 | include ../../../mcl/common.mk 2 | ifeq ($(UNAME_S),Darwin) 3 | JAVA_INC=-I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ 4 | else 5 | JAVA_INC=-I/usr/lib/jvm/default-java/include 6 | #JAVA_INC=-I/usr/lib/jvm/java-7-openjdk-amd64/include 7 | CFLAGS+=-z noexecstack 8 | LDFLAGS+=-lrt 9 | endif 10 | CFLAGS+=$(JAVA_INC) $(JAVA_INC)/linux -Wno-strict-aliasing 11 | CFLAGS+=-std=c++03 12 | SAMPLE_LIB=libsample.$(LIB_SUF) 13 | 14 | PACKAGE_NAME=sample 15 | PACKAGE_DIR=$(subst .,/,$(PACKAGE_NAME)) 16 | 17 | MCLJAVA_LIB=libmcljava.$(LIB_SUF) 18 | JAVA_EXE=LD_LIBRARY_PATH=./ java -classpath ./ 19 | 20 | sample_wrap.cxx: sample.i 21 | $(MKDIR) $(PACKAGE_DIR) 22 | swig -java -package $(PACKAGE_NAME) -outdir $(PACKAGE_DIR) -c++ -Wall sample.i 23 | 24 | $(SAMPLE_LIB): sample_wrap.cxx $(MCL_LIB) 25 | $(PRE)$(CXX) $< -o $@ $(CFLAGS) $(MCL_LIB) $(LDFLAGS) -shared 26 | 27 | %.class: %.java 28 | javac $< 29 | 30 | test: $(SAMPLE_LIB) SampleTest.class 31 | $(JAVA_EXE) SampleTest 32 | 33 | clean: 34 | rm -rf *.class $(SAMPLE_LIB) *.class *_wrap.cxx sample 35 | 36 | -------------------------------------------------------------------------------- /memory-speed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | template 7 | void f(int& c, const T& v, size_t pos) 8 | { 9 | c += v[pos]; 10 | } 11 | void test(size_t shiftSize, bool random) 12 | { 13 | const size_t dataSize = (size_t(1) << shiftSize) / 8; 14 | printf("dataSize=%.2fMiB\n", dataSize * 8 / 1024. / 1024.); 15 | cybozu::AlignedArray v; 16 | v.resize(dataSize); 17 | int c = 0; 18 | if (random) { 19 | cybozu::XorShift rg; 20 | CYBOZU_BENCH("random", f, c, v, rg.get64() & (dataSize - 1)); 21 | } else { 22 | int pos = 0; 23 | CYBOZU_BENCH("seq", f, c, v, pos++ & (dataSize - 1)); 24 | } 25 | printf("c=%d\n", c); 26 | } 27 | int main(int argc, char *argv[]) 28 | { 29 | size_t shiftSize; 30 | bool random; 31 | cybozu::Option opt; 32 | opt.appendOpt(&shiftSize, 10, "s", "data size : (1< 16 | #include 17 | #include 18 | 19 | int main() 20 | { 21 | std::string s = "abc"; 22 | printf("len=%zd\n", strlen(s.c_str())); 23 | } 24 | -------------------------------------------------------------------------------- /peterson.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #ifdef _MSC_VER 5 | #include 6 | #else 7 | #include 8 | #endif 9 | 10 | static void lock(volatile int *my, volatile int *other, volatile int *turn, int other_id) 11 | { 12 | *my = 1; 13 | *turn = other_id; 14 | _mm_mfence(); 15 | while (*other && *turn == other_id) { 16 | } 17 | } 18 | 19 | static void unlock(volatile int *my) 20 | { 21 | *my = 0; 22 | } 23 | 24 | const int COUNT = 5000000; 25 | static volatile int counter = 0; 26 | 27 | void task(volatile int *my, volatile int *other, volatile int *turn, int id) 28 | { 29 | for (int i = 0; i < COUNT; i++) { 30 | lock(my, other, turn, id); 31 | counter++; 32 | unlock(my); 33 | } 34 | } 35 | 36 | int main() 37 | { 38 | { 39 | volatile int f0 = 0; 40 | volatile int f1 = 0; 41 | volatile int turn = 0; 42 | std::thread t0(task, &f0, &f1, &turn, 0); 43 | std::thread t1(task, &f1, &f0, &turn, 1); 44 | t0.join(); 45 | t1.join(); 46 | } 47 | 48 | printf("counter = %d\n", counter); 49 | printf("difference = %d\n", COUNT * 2 - counter); 50 | } 51 | -------------------------------------------------------------------------------- /extint/mul256.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | unsigned _ExtInt(WidthA + WidthB) lossless_mul(unsigned _ExtInt(WidthA) a, unsigned _ExtInt(WidthB) b) { 6 | return static_cast(a) 7 | * static_cast(b); 8 | } 9 | 10 | 11 | void mul256(uint64_t *pz, const uint64_t *px, const uint64_t *py) 12 | { 13 | unsigned _ExtInt(256) x = *(unsigned _ExtInt(256)*)px; 14 | unsigned _ExtInt(256) y = *(unsigned _ExtInt(256)*)py; 15 | unsigned _ExtInt(512) z = lossless_mul<256, 256>(x, y); 16 | *(unsigned _ExtInt(512)*)pz = z; 17 | } 18 | 19 | void dump(const uint64_t *x, size_t n) 20 | { 21 | printf("0x"); 22 | for (size_t i = 0; i < n; i++) { 23 | printf("%016lx", x[n - 1 - i]); 24 | } 25 | printf("\n"); 26 | } 27 | 28 | int main() 29 | { 30 | uint64_t x[4] = { 1, 2, 3, 4 }; 31 | uint64_t y[4] = { uint64_t(-1), uint64_t(-2), uint64_t(-3), uint64_t(-4) }; 32 | uint64_t z[8]; 33 | mul256(z, x, y); 34 | dump(x, 4); 35 | dump(y, 4); 36 | dump(z, 8); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /motoko/package-set.dhall: -------------------------------------------------------------------------------- 1 | -- The upstream seems outdated, hence we define what we need directly below under additions 2 | -- let upstream = https://github.com/dfinity/vessel-package-set/releases/download/mo-0.6.7-20210818/package-set.dhall sha256:c4bd3b9ffaf6b48d21841545306d9f69b57e79ce3b1ac5e1f63b068ca4f89957 3 | 4 | let Package = 5 | { name : Text, version : Text, repo : Text, dependencies : List Text } 6 | 7 | let upstream = [] : List Package 8 | 9 | let 10 | -- This is where you can add your own packages to the package-set 11 | additions = [ 12 | { name = "iterext" 13 | , version = "v2.0.0" 14 | , repo = "https://github.com/timohanke/motoko-iterext.git" 15 | , dependencies = [ "base" ] : List Text 16 | } 17 | ] 18 | 19 | let 20 | -- This is where you can override existing packages in the package-set 21 | overrides = [ 22 | { name = "base" 23 | , version = "dfx-0.8.4" 24 | , repo = "https://github.com/dfinity/motoko-base.git" 25 | , dependencies = [] : List Text 26 | } 27 | ] 28 | 29 | -- in upstream # additions # overrides 30 | in upstream # additions # overrides 31 | -------------------------------------------------------------------------------- /python/ecc/org/ecdsa-test.py: -------------------------------------------------------------------------------- 1 | # sample of https://pypi.org/project/ecdsa/ 2 | import ecdsa 3 | import hashlib 4 | 5 | msg = b"hello" 6 | sec = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1, hashfunc=hashlib.sha256) 7 | pub = sec.verifying_key 8 | sig = sec.sign(msg) 9 | print(f'secHex = "{sec.to_string().hex()}"') 10 | print(f'pubHex = "{pub.to_string().hex()}"') 11 | print(f'sigHex = "{sig.hex()}"') 12 | print(pub.verify(sig, msg)) 13 | 14 | secHex = "b1aa6282b14e5ffbf6d12f783612f804e6a20d1a9734ffbb6c9923c670ee8da2" 15 | pubHex = "0a09ff142d94bc3f56c5c81b75ea3b06b082c5263fbb5bd88c619fc6393dda3da53e0e930892cdb7799eea8fd45b9fff377d838f4106454289ae8a080b111f8d" 16 | sigHex = "50839a97404c24ec39455b996e4888477fd61bcf0ffb960c7ffa3bef104501919671b8315bb5c1611d422d49cbbe7e80c6b463215bfad1c16ca73172155bf31a" 17 | 18 | print('static') 19 | sec = ecdsa.SigningKey.from_string(bytes.fromhex(secHex), curve=ecdsa.SECP256k1, hashfunc=hashlib.sha256) 20 | pub = sec.verifying_key 21 | sig = bytes.fromhex(sigHex) 22 | print(pub.verify(sig, msg)) 23 | print(f'secHex = "{sec.to_string().hex()}"') 24 | print(f'pubHex = "{pub.to_string().hex()}"') 25 | print(f'sigHex = "{sig.hex()}"') 26 | -------------------------------------------------------------------------------- /sve/gather-test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace Xbyak_aarch64; 7 | 8 | struct Code : CodeGenerator { 9 | explicit Code() 10 | : CodeGenerator(4096) 11 | { 12 | // func(dst, src, offset) 13 | const auto& dst = x0; 14 | const auto& src = x1; 15 | const auto& offset = x2; 16 | ptrue(p0.s); 17 | ld1w(z0.s, p0/T_z, ptr(offset)); 18 | ld1w(z1.s, p0, ptr(src, z0.s, SXTW)); 19 | st1w(z1.s, p0/T_z, ptr(dst)); 20 | ret(); 21 | } 22 | }; 23 | 24 | int main() 25 | try 26 | { 27 | Code c; 28 | c.ready(); 29 | auto func = c.getCode(); 30 | const size_t N = 16; 31 | uint32_t dst[N] = {}; 32 | 33 | uint32_t src[N]; 34 | uint32_t offset[N]; 35 | for (size_t i = 0; i < N; i++) { 36 | src[i] = 0x01010101 * i; 37 | } 38 | for (size_t i = 0; i < N; i++) { 39 | offset[i] = i * 4; 40 | } 41 | func(dst, src, offset); 42 | for (size_t i = 0; i < N; i++) { 43 | printf("i=%zd %08x\n", i, dst[i]); 44 | } 45 | } catch (std::exception& e) { 46 | printf("err %s\n", e.what()); 47 | return 1; 48 | } 49 | -------------------------------------------------------------------------------- /go/share/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-Wall -Wextra -O3 -I./include 2 | 3 | add.o: add.cpp 4 | gcc $(CFLAGS) -fPIC -c -o $@ -g $< 5 | 6 | lib/libadd.so: add.cpp 7 | gcc $(CFLAGS) -fPIC -shared -o $@ -g $< 8 | 9 | lib/libadd.a: add.o 10 | ar r lib/libadd.a add.o 11 | 12 | # linux 13 | test: test.cpp lib/libadd.so 14 | gcc -o test $(CFLAGS) test.cpp -ladd -L./lib 15 | 16 | run: test 17 | # env LD_RUN_PATH="./lib" ./test 18 | ./test 19 | 20 | # make dll by gcc 21 | mingw0: 22 | gcc $(CFLAGS) -shared -o lib/add.so add.cpp -Wl,--out-implib,lib/libadd.a 23 | gcc -o test $(CFLAGS) test.cpp -ladd -L./lib 24 | env PATH=$(PATH):./lib ./test.exe 25 | 26 | mingw1: 27 | gcc $(CFLAGS) -shared -o lib/add.dll add.cpp -Wl,--out-implib,lib/libadd.a 28 | gcc -o test $(CFLAGS) test.cpp -ladd -L./lib 29 | env PATH=$(PATH):./lib ./test.exe 30 | 31 | # make dll by vc 32 | lib/add.dll: add.cpp 33 | cd lib 34 | cl /LD ../add.cpp -I../include 35 | 36 | mingw2: 37 | gcc -o test $(CFLAGS) test.cpp -ladd -L./lib 38 | env PATH=$(PATH):./lib ./test.exe 39 | 40 | test_go: 41 | env PATH=$(PATH):./lib CGO_CFLAGS="-DAAA=5" go run test.go 42 | 43 | clean: 44 | rm -rf test lib/add.* *.dll lib/libadd.a *.exe 45 | -------------------------------------------------------------------------------- /go/unsafe.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /* 4 | #include 5 | #include 6 | typedef struct A { 7 | uint64_t x[2]; 8 | }A ; 9 | void putA(const A *a) 10 | { 11 | printf("a=%p\n", a); 12 | printf("x=%d %d\n", (int)a->x[0], (int)a->x[1]); 13 | } 14 | typedef struct B { 15 | uint64_t y[2]; 16 | }B ; 17 | void putB(const B *b) 18 | { 19 | printf("b=%p\n", b); 20 | printf("y=%d %d\n", (int)b->y[0], (int)b->y[1]); 21 | } 22 | void putC(const void *p) 23 | { 24 | putB((const B*)p); 25 | } 26 | */ 27 | import "C" 28 | import "unsafe" 29 | 30 | type A struct { 31 | x [2]uint64 32 | } 33 | 34 | func (a *A) Put() { 35 | C.putA((*C.A)(unsafe.Pointer(&a.x[0]))) 36 | } 37 | 38 | type B struct { 39 | y [2]uint64 40 | } 41 | 42 | func (b *B) Put() { 43 | C.putB((*C.B)(unsafe.Pointer(&b.y[0]))) 44 | } 45 | func (b *B) PutC() { 46 | // C.putC((*C.uint8_t)(unsafe.Pointer(&b.y[0]))) 47 | C.putC((unsafe.Pointer(&b.y[0]))) 48 | } 49 | 50 | func main() { 51 | v := make([]A, 2) 52 | v[0].Put() 53 | v[0].x[0] = 2 54 | v[0].x[1] = 5 55 | v[0].Put() 56 | w := *(*[]B)(unsafe.Pointer(&v)) 57 | w[0].Put() 58 | ww := new(B) 59 | ww.y[0] = 5 60 | ww.y[1] = 8 61 | ww.PutC() 62 | } 63 | -------------------------------------------------------------------------------- /codec-calc-ans.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int calc(int a, int b, int s) 5 | { 6 | const int Q = 1 << s, Q2 = Q * 2, Q3 = Q * 3; 7 | assert(0 <= s && s <= 16 && 0 <= b && b < a && a < Q * 4); 8 | int n = 0; 9 | for (;;) { 10 | if (a < Q2) { 11 | n = n * 2; 12 | } else if (b >= Q2) { 13 | n = n * 2 + 1; 14 | b -= Q2; 15 | a -= Q2; 16 | } else if (b >= Q && a < Q3) { 17 | b -= Q; 18 | a -= Q; 19 | } else { 20 | break; 21 | } 22 | b = b * 2; 23 | a = a * 2 + 1; 24 | } 25 | return n; 26 | } 27 | 28 | static inline int bsr(int x) 29 | { 30 | assert(x > 0); 31 | union { 32 | int i; 33 | float f; 34 | } ui; 35 | ui.f = (float)x; 36 | return (ui.i >> 23) - 127; /* equal to "bsr eax, x" */ 37 | } 38 | 39 | int calc2(int a, int b, int) 40 | { 41 | return a >> (bsr(a ^ b) + 1); 42 | // return b >> (bsr(a ^ b) + 1); // same 43 | } 44 | 45 | int main() 46 | { 47 | int s = 3; 48 | for (int a = 1; a < (1 << s) * 4; a++) { 49 | printf("a=%2d ", a); 50 | for (int b = 0; b < a; b++) { 51 | printf("%2d ", calc(a, b, s)); 52 | if (calc(a, b, s) != calc2(a, b, s)) puts("ERR"); 53 | } 54 | printf("\n"); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /go/seccon2013/crypt300.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef mcl::FpT<> Fp; 5 | struct tagZn; 6 | typedef mcl::FpT Zn; 7 | typedef mcl::EcT Ec; 8 | 9 | // return n such that nP == Q 10 | int GetDLP(const Ec& P, const Ec& Q) 11 | { 12 | Ec R(P); 13 | for (int i = 1; i < 10000000; i++) { 14 | if (R == Q) { 15 | return i; 16 | } 17 | R += P; 18 | } 19 | throw cybozu::Exception("GetDLP") << P << Q; 20 | } 21 | int main() 22 | try 23 | { 24 | Fp::setModulo("7654319"); 25 | Zn::setModulo("7654319"); 26 | Ec::setParam("1234577", "3213242"); 27 | const Ec base(Fp("5234568"), Fp("2287747")); 28 | const Ec Pub(Fp("2366653"), Fp("1424308")); 29 | const Ec O1(Fp("5081741"), Fp("6744615")); 30 | const Ec O2(Fp("610619"), Fp("6218")); 31 | Ec R(base); 32 | const int secretKey = GetDLP(base, Pub); 33 | printf("secretKey=%d\n", secretKey); 34 | const int r = GetDLP(base, O1); 35 | printf("r=%d\n", r); 36 | Ec plain; 37 | Ec::mul(plain, Pub, r); 38 | Ec::sub(plain, O2, plain); 39 | plain.normalize(); 40 | std::cout << plain.x + plain.y << std::endl; 41 | } catch (std::exception& e) { 42 | printf("err=%s\n", e.what()); 43 | return 1; 44 | } 45 | -------------------------------------------------------------------------------- /xbyak_fixed/t1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace Xbyak; 7 | using namespace Xbyak::util; 8 | 9 | template 10 | void put(OutputStream& os, const void *src, size_t n) 11 | { 12 | const uint8_t *p = (const uint8_t*)src; 13 | while (n > 0) { 14 | size_t remain = (n >= 16) ? 16 : n; 15 | os.write("db ", 3); 16 | for (size_t i = 0; i < remain; i++) { 17 | char buf[] = "0x__,"; 18 | cybozu::itohex(buf + 2, 2, *p++); 19 | os.write(buf, 5); 20 | } 21 | os.write("\n", 1); 22 | n -= remain; 23 | } 24 | } 25 | 26 | struct Code : CodeGenerator { 27 | Code() 28 | { 29 | StackFrame sf(this, 2); 30 | mov(rax, sf.p[0]); 31 | add(rax, sf.p[1]); 32 | } 33 | }; 34 | 35 | int main(int argc, char *argv[]) 36 | { 37 | if (argc == 1) { 38 | fprintf(stderr, "t1 out-file\n"); 39 | return 1; 40 | } 41 | std::ofstream ofs(argv[1], std::ios::binary); 42 | Code c; 43 | 44 | std::string header = 45 | "segment .text\n" 46 | "global fff:\n" 47 | "fff:\n"; 48 | ofs.write(header.c_str(), header.size()); 49 | put(ofs, c.getCode(), c.getSize()); 50 | } 51 | -------------------------------------------------------------------------------- /p1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | D=2 N=100 3 | c=338 4 | D=3 N=1000 5 | c=6388 6 | D=4 N=10000 7 | c=87320 8 | D=5 N=100000 9 | c=973935 10 | D=6 N=1000000 11 | c=12881436 12 | D=7 N=10000000 13 | c=150248026 14 | D=8 N=100000000 15 | c=1721663629 16 | */ 17 | #include 18 | #include 19 | 20 | constexpr size_t ipow(size_t n) 21 | { 22 | size_t r = 1; 23 | for (size_t i = 0; i < n; i++) { 24 | r *= 10; 25 | } 26 | return r; 27 | } 28 | 29 | template 30 | int count() 31 | { 32 | constexpr size_t N = ipow(D); 33 | printf("D=%zd N=%zd\n", D, N); 34 | static int tbl[N] = {}; 35 | cybozu::XorShift rg; 36 | size_t x = 0; 37 | size_t remain = N - 1; 38 | int c = 0; 39 | while (remain > 0) { 40 | int d = rg() % 10; 41 | x *= 10; 42 | x %= N; 43 | x += d; 44 | tbl[x]++; 45 | if (tbl[x] == 1) { 46 | remain--; 47 | } 48 | c++; 49 | } 50 | return c; 51 | } 52 | 53 | int main() 54 | { 55 | printf("c=%d\n", count<2>()); 56 | printf("c=%d\n", count<3>()); 57 | printf("c=%d\n", count<4>()); 58 | printf("c=%d\n", count<5>()); 59 | printf("c=%d\n", count<6>()); 60 | printf("c=%d\n", count<7>()); 61 | printf("c=%d\n", count<8>()); 62 | } 63 | 64 | -------------------------------------------------------------------------------- /sma_test.cpp: -------------------------------------------------------------------------------- 1 | #include "sma.hpp" 2 | #include 3 | #include 4 | #include 5 | 6 | struct V { 7 | uint64_t v; 8 | uint64_t t; 9 | }; 10 | void test(uint64_t interval, const V* tbl, size_t tblNum, uint64_t expectNum, uint64_t expectTotal) 11 | { 12 | SMAverage sma(interval); 13 | for (size_t i = 0; i < tblNum; i++) { 14 | sma.append(tbl[i].v, tbl[i].t); 15 | } 16 | const SMAverage::ValVec& vv = sma.getValVec(); 17 | CYBOZU_TEST_EQUAL(vv.size(), expectNum); 18 | CYBOZU_TEST_EQUAL(sma.getTotalByte(), expectTotal); 19 | } 20 | 21 | CYBOZU_TEST_AUTO(append) 22 | { 23 | const V tbl[] = { 24 | { 1, 0 }, 25 | { 2, 0 }, 26 | { 3, 0 }, 27 | { 4, 1 }, 28 | { 5, 1 }, 29 | { 6, 3 }, 30 | { 7, 5 }, 31 | { 8, 7 }, 32 | { 9, 9 }, 33 | }; 34 | test(10, tbl, 9, 9, 45); // all 35 | test(9, tbl, 9, 9, 45); // all 36 | test(8, tbl, 9, 6, 39); // tbl[3..8] 37 | test(7, tbl, 9, 4, 30); // tbl[5..8] 38 | test(6, tbl, 9, 4, 30); // tbl[5..8] 39 | test(5, tbl, 9, 3, 24); // tbl[6..8] 40 | test(4, tbl, 9, 3, 24); // tbl[6..8] 41 | test(3, tbl, 9, 2, 17); // tbl[7..8] 42 | test(2, tbl, 9, 2, 17); // tbl[7..8] 43 | test(1, tbl, 9, 1, 9); // tbl[8] 44 | } 45 | -------------------------------------------------------------------------------- /bbs/webpack.demo.config.js: -------------------------------------------------------------------------------- 1 | import path from 'path' 2 | import { fileURLToPath } from 'url' 3 | 4 | const __filename = fileURLToPath(import.meta.url) 5 | const __dirname = path.dirname(__filename) 6 | 7 | export default { 8 | mode: 'production', 9 | entry: './browser/demo.ts', 10 | target: 'web', 11 | module: { 12 | rules: [ 13 | { 14 | test: /\.tsx?$/, 15 | use: { 16 | loader: 'ts-loader', 17 | options: { 18 | compilerOptions: { 19 | module: 'commonjs', 20 | target: 'es2020', 21 | lib: ['es2020', 'dom'], 22 | allowJs: true, 23 | esModuleInterop: true, 24 | moduleResolution: 'node' 25 | } 26 | } 27 | }, 28 | exclude: /node_modules/, 29 | }, 30 | ], 31 | }, 32 | resolve: { 33 | extensions: ['.tsx', '.ts', '.js'], 34 | }, 35 | output: { 36 | filename: 'demo.js', 37 | path: path.resolve(__dirname, 'browser'), 38 | library: 'demo', 39 | libraryTarget: 'window', 40 | globalObject: 'window', 41 | }, 42 | optimization: { 43 | minimize: false, 44 | }, 45 | } -------------------------------------------------------------------------------- /crash-gdb.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | a sample code to crash gdb 7.7 3 | g++ crash-gdb.cpp -std=c++11 && gdb ./a.out 4 | clang++ crash-gdb.cpp -std=c++11 && gdb ./a.out 5 | 6 | g++ 4.6.3 -std=c++0x 7 | g++ 4.8.2 -std=c++11 8 | clang 3.5-1ubuntu1 -std=c++11 9 | 10 | gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04 ; not crash 11 | gdb (Ubuntu 7.7-Oubuntu3.1) 7.7 ; crash 12 | gdb 7.8.50.20140612-cvs ; not crash 13 | */ 14 | #include 15 | #include 16 | 17 | struct BaseHolder { }; 18 | 19 | template 20 | struct Holder : public BaseHolder { 21 | Func func; 22 | explicit Holder(Func&& func) 23 | : func(std::forward(func)) 24 | { 25 | } 26 | }; 27 | 28 | struct Runner { 29 | BaseHolder *holder_; 30 | template 31 | explicit Runner(Func && func) 32 | : holder_(new Holder(func)) { } 33 | ~Runner() { delete holder_; } 34 | }; 35 | 36 | template 37 | void f(T &) 38 | { 39 | auto g =[&](){ }; 40 | Runner{g}; 41 | } 42 | 43 | int main() 44 | { 45 | int a = 0; 46 | f(a); 47 | #ifdef __clang__ 48 | printf("clang %d %d\n", __clang_major__, __clang_minor__); 49 | #elif defined(__GNUC__) 50 | printf("gcc %d %d\n", __GNUC__, __GNUC_MINOR__); 51 | #endif 52 | } 53 | -------------------------------------------------------------------------------- /vzeroupper.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | see Intel manual Sytem Programming Guide 3 | Chapter 19 Performance-Monitoring Events 4 | perf -e r08c1 ./a.out ; OTHER_ASSISTS.AVX_TO_SSE 5 | perf -e r10c1 ./a.out ; OTHER_ASSISTS.SSE_TO_AVX 6 | */ 7 | #include 8 | #define XBYAK_NO_OP_NAMES 9 | #include 10 | #include 11 | 12 | const size_t N = 10000; 13 | 14 | struct Code : public Xbyak::CodeGenerator { 15 | explicit Code(int mode) 16 | { 17 | mov(eax, N); 18 | L("@@"); 19 | vaddpd(ym0, ym0); 20 | 21 | if (mode & 1) vzeroupper(); 22 | 23 | addpd(xm0, xm0); 24 | 25 | if (mode & 2) vzeroupper(); 26 | 27 | vaddpd(ym0, ym0); 28 | sub(eax, 1); 29 | jnz("@b"); 30 | ret(); 31 | } 32 | }; 33 | 34 | int main(int argc, char *argv[]) 35 | { 36 | int sel = argc == 1 ? -1 : atoi(argv[1]); 37 | for (int mode = 0; mode < 4; mode++) { 38 | if (sel >= 0 && sel != mode) continue; 39 | Code c(mode); 40 | void (*f)() = c.getCode(); 41 | Xbyak::util::Clock clk; 42 | const size_t C = 1000; 43 | clk.begin(); 44 | for (size_t i = 0; i < C; i++) { 45 | f(); 46 | } 47 | clk.end(); 48 | printf("mode=%d %7.3fclk\n", mode, (double)clk.getClock () / C / N); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /go/go-in-c/lib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lib.h" 3 | 4 | extern void GoFunc1(); 5 | extern void GoFunc2(const void *buf, int n); 6 | extern void GoCallF(void *buf, int n); 7 | 8 | void fill(void *buf, int n) 9 | { 10 | char *p = (char*)buf; 11 | for (int i = 0; i < n; i++) { 12 | p[i] = i; 13 | } 14 | } 15 | void ggg(void f(void *, int), void *buf, int n) 16 | { 17 | puts("ggg callback"); 18 | f(buf, n); 19 | } 20 | void hhh() 21 | { 22 | GoFunc1(); 23 | } 24 | int fff() 25 | { 26 | return 123; 27 | } 28 | 29 | void putput(const void *buf, int n) 30 | { 31 | printf("putput buf=%p, n=%d\n", buf, n); 32 | if (buf == 0) return; 33 | const unsigned char *p = (const unsigned char *)buf; 34 | for (int i = 0; i < n; i++) { 35 | printf("%02x ", p[i]); 36 | } 37 | printf("\n"); 38 | GoFunc2(buf, n); 39 | } 40 | 41 | #define S_BUF_SIZE 8 42 | static char s_buf[S_BUF_SIZE]; 43 | 44 | void callGoCallF() 45 | { 46 | GoCallF(s_buf, S_BUF_SIZE); 47 | } 48 | 49 | void putBuf() 50 | { 51 | printf("putBuf "); 52 | for (int i = 0; i < S_BUF_SIZE; i++) { 53 | printf("%02x ", (unsigned char)s_buf[i]); 54 | } 55 | printf("\n"); 56 | } 57 | 58 | FuncType s_f; 59 | void setCallback(FuncType f) 60 | { 61 | s_f = f; 62 | } 63 | -------------------------------------------------------------------------------- /python/test.js: -------------------------------------------------------------------------------- 1 | const HEADERS = { 2 | "Content-Type": "application/json; charset=utf-8", 3 | } 4 | const URL = 'http://localhost:8000/' 5 | 6 | const getValue = name => { return document.getElementsByName(name)[0].value } 7 | const setText = (name, val) => { document.getElementsByName(name)[0].innerText = val } 8 | 9 | const printTable = (js) => { 10 | console.log(`json=${JSON.stringify(js)}`) 11 | setText('result', JSON.stringify(js)) 12 | } 13 | 14 | const onAdd = () => { 15 | const key = getValue('key') 16 | const value = getValue('value') 17 | console.log(`ADD key=${key}, value=${value}`) 18 | const js = {} 19 | js[key] = value 20 | fetch(URL + 'add', { 21 | method: 'POST', 22 | headers: HEADERS, 23 | body: JSON.stringify(js) 24 | }).then(res => res.json()) 25 | .then(js => { 26 | printTable(js) 27 | }) 28 | .catch(err => console.error(err)) 29 | } 30 | 31 | const onDel = () => { 32 | const key = getValue('key') 33 | const js = {} 34 | js[key] = '' 35 | fetch(URL + 'del', { 36 | method: 'POST', 37 | headers: HEADERS, 38 | body: JSON.stringify(js) 39 | }).then(res => res.json()) 40 | .then(js => { 41 | printTable(js) 42 | }) 43 | .catch(err => console.error(err)) 44 | } 45 | -------------------------------------------------------------------------------- /swig/common.mk: -------------------------------------------------------------------------------- 1 | # common definition for Makefile 2 | # for GNU c++ 3 | #CCACHE=$(shell eval ls /usr/local/bin/ccache 2>/dev/null) 4 | 5 | #CXX = g++ 6 | #CC = gcc 7 | #LD = g++ 8 | CP = cp -f 9 | AR = ar r 10 | MKDIR=mkdir -p 11 | RM=rm -f 12 | CFLAGS = -O3 -fomit-frame-pointer -DNDEBUG -msse2 -mfpmath=sse -march=native 13 | CFLAGS_WARN=-Wall -Wextra -Wformat=2 -Wcast-qual -Wcast-align -Wwrite-strings -Wfloat-equal -Wpointer-arith #-Wswitch-enum -Wstrict-aliasing=2 14 | AS = nasm 15 | AFLAGS = -f elf -D__unix__ 16 | 17 | # for only 64-bit 18 | BIT=-m64 19 | #BIT=-m32 20 | #ifeq ($(shell uname -s),x86_64) 21 | #BIT=-m64 22 | #endif 23 | #ifeq ($(shell uname -s),Darwin) 24 | #BIT=-m64 25 | #endif 26 | ifeq ($(shell uname -s),Cygwin) 27 | # install mingw64-x86_64-gcc-g++ 28 | CXX=x86_64-w64-mingw32-g++ 29 | LD=x86_64-w64-mingw32-g++ 30 | AR=x86_64-w64-mingw32-ar r 31 | #LDFLAGS+=-L/usr/x86_64-w64-mingw32/sys-root/mingw/lib 32 | endif 33 | 34 | ifeq ($(DBG),on) 35 | CFLAGS += -O0 -g3 -UNDEBUG 36 | LDFLAGS += -g3 37 | endif 38 | 39 | .SUFFIXES: .cpp 40 | .cpp.o: 41 | $(CXX) -c $< -o $@ $(CFLAGS) $(CFLAGS_WARN) $(CFLAGS_ALWAYS) $(INC_DIR) $(BIT) 42 | 43 | .c.o: 44 | $(CC) -c $< -o $@ $(CFLAGS) $(CFLAGS_WARN) $(CFLAGS_ALWAYS) $(INC_DIR) $(BIT) 45 | 46 | -------------------------------------------------------------------------------- /hash/MurmurHash3.h: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // MurmurHash3 was written by Austin Appleby, and is placed in the public 3 | // domain. The author hereby disclaims copyright to this source code. 4 | 5 | #ifndef _MURMURHASH3_H_ 6 | #define _MURMURHASH3_H_ 7 | 8 | //----------------------------------------------------------------------------- 9 | // Platform-specific functions and macros 10 | 11 | // Microsoft Visual Studio 12 | 13 | #if defined(_MSC_VER) 14 | 15 | typedef unsigned char uint8_t; 16 | typedef unsigned long uint32_t; 17 | typedef unsigned __int64 uint64_t; 18 | 19 | // Other compilers 20 | 21 | #else // defined(_MSC_VER) 22 | 23 | #include 24 | 25 | #endif // !defined(_MSC_VER) 26 | 27 | //----------------------------------------------------------------------------- 28 | 29 | void MurmurHash3_x86_32 ( const void * key, int len, uint32_t seed, void * out ); 30 | 31 | void MurmurHash3_x86_128 ( const void * key, int len, uint32_t seed, void * out ); 32 | 33 | void MurmurHash3_x64_128 ( const void * key, int len, uint32_t seed, void * out ); 34 | 35 | //----------------------------------------------------------------------------- 36 | 37 | #endif // _MURMURHASH3_H_ 38 | -------------------------------------------------------------------------------- /extract-jpeg.py: -------------------------------------------------------------------------------- 1 | # extract JPEG file from google1.pdf 2 | import sys 3 | 4 | if len(sys.argv) != 3: 5 | print "extract-jpeg.py " 6 | sys.exit(1) 7 | 8 | data = open(sys.argv[1], 'rb').read() 9 | fo = open(sys.argv[2], 'wb') 10 | 11 | SOI = 0xd8 12 | EOI = 0xd9 13 | APP0 = 0xe0 14 | COMMENT = 0xfe 15 | 16 | inJpeg = False 17 | 18 | size = len(data) 19 | print size 20 | 21 | i = 0 22 | app0pos = 0 23 | while i < size: 24 | c0 = ord(data[i]) 25 | if inJpeg: 26 | if c0 == 0xff: 27 | c1 = ord(data[i + 1]) 28 | if c1 == EOI: 29 | print "found EOI at", i 30 | fo.write(data[app0pos:i + 2]) 31 | break 32 | H = ord(data[i + 2]) 33 | L = ord(data[i + 3]) 34 | len = H * 256 + L 35 | if c1 == COMMENT: 36 | print "found comment({0}, {1}). skip len={2}".format(hex(H), hex(L), len) 37 | i += len + 2 38 | print "next pos", i 39 | continue 40 | if c1 == APP0: 41 | print "found segment 0xff {0} at {1} len={2}".format(hex(c1), hex(i), len) 42 | app0pos = i 43 | i += 1 44 | else: 45 | if c0 == 0xff: 46 | c1 = ord(data[i + 1]) 47 | if c1 == SOI: 48 | print "found SOI at {0}".format(i) 49 | fo.write(data[i]) 50 | fo.write(data[i + 1]) 51 | i += 2 52 | inJpeg = True 53 | continue 54 | i += 1 55 | -------------------------------------------------------------------------------- /pptx2pdf.vbs: -------------------------------------------------------------------------------- 1 | 2 | ' convert pptx to pdf 3 | 4 | Option Explicit 5 | Dim g_fs 6 | set g_fs = wscript.createObject("scripting.fileSystemObject") 7 | 8 | ParseAndRun wscript.arguments 9 | wscript.quit 10 | 11 | sub ParseAndRun(args) 12 | Dim inName, outName 13 | if args.unnamed.Count = 0 then 14 | wscript.echo "pptx2pdf.vbs inName [outFileName]" 15 | wscript.quit 16 | end if 17 | 18 | inName = g_fs.getAbsolutePathName(args.unnamed(0)) 19 | Dim suf 20 | suf = lcase(g_fs.getExtensionName(inName)) 21 | if suf <> "ppt" and suf <> "pptx" then 22 | wscript.echo "bad suffix=" & suf 23 | wscript.quit 24 | end if 25 | 26 | if args.unnamed.Count < 2 then 27 | outName = g_fs.getParentFolderName(inName) + "\" + g_fs.getBaseName(inName) + ".pdf" 28 | else 29 | outName = g_fs.getAbsolutePathName(args.unnamed(1)) 30 | end if 31 | 32 | pptx2pdf inName, outName 33 | end sub 34 | 35 | sub pptx2pdf(inName, outName) 36 | on error resume next 37 | 38 | Dim app, presen 39 | set app = createObject("powerPoint.application") 40 | 41 | set presen = app.presentations.open(inName) ', ReadOnly = true 42 | presen.saveAs outName, 32, true 43 | app.quit 44 | if err.number <> 0 then 45 | wscript.echo "ERR:" & err.number & " desc:" & err.description 46 | end if 47 | end sub 48 | -------------------------------------------------------------------------------- /sve/low_func.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | inline uint32_t get_fpcr(void) 10 | { 11 | uint64_t x; 12 | asm __volatile__("mrs %[x], fpcr":[x]"=r"(x)); 13 | return x; 14 | } 15 | inline void set_fpcr(uint32_t x) 16 | { 17 | uint64_t xx = x; 18 | asm __volatile__("msr fpcr, %[x]"::[x]"r"(xx)); 19 | } 20 | 21 | struct Type_id_aa64isar0_el1 { 22 | int resv0:4; 23 | int aes:4; 24 | int sha1:4; 25 | int sha2:4; 26 | int crc32:4; 27 | int atomic:4; 28 | int resv1:4; 29 | int rdm:4; 30 | int resv2:12; 31 | int dp:4; 32 | int resv3:16; 33 | }; 34 | 35 | Type_id_aa64isar0_el1 get_id_aa64isar0_el1(void) 36 | { 37 | Type_id_aa64isar0_el1 x; 38 | asm __volatile__("mrs %[x], id_aa64isar0_el1":[x]"=r"(x)); 39 | return x; 40 | } 41 | 42 | struct Type_id_aa64pfr0_el1 { 43 | int el0:4; 44 | int el1:4; 45 | int el2:4; 46 | int el3:4; 47 | int fp:4; 48 | int advsimd:4; 49 | int gic:4; 50 | int ras:4; 51 | int sve:4; 52 | int resv0:28; 53 | }; 54 | 55 | Type_id_aa64pfr0_el1 get_id_aa64pfr0_el1(void) 56 | { 57 | Type_id_aa64pfr0_el1 x; 58 | asm __volatile__("mrs %[x], id_aa64pfr0_el1":[x]"=r"(x)); 59 | return x; 60 | } 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | -------------------------------------------------------------------------------- /sve/simple-inv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace Xbyak_aarch64; 4 | 5 | struct Code : public CodeGenerator { 6 | Code() 7 | { 8 | ptrue(p0.s); 9 | ld1w(z0.s, p0/T_z, ptr(x1)); 10 | 11 | #if 1 12 | fcpy(z2.s, p0, 1.0); 13 | frecpe(z0.s, z0.s); // y = approx(1/x) 14 | movprfx(z0.s, p0, z2.s); 15 | fmls(z0.s, p0, z0.s, z1.s); // z0 = 1 - xy ; nam if x = inf 16 | fmad(z0.s, p0, z0.s, z0.s); 17 | fmad(z0.s, p0, z1.s, z1.s); 18 | #else 19 | frecpe(z1.s, z0.s); 20 | frecps(z2.s, z0.s, z1.s); 21 | fmul(z1.s, z1.s, z2.s); 22 | 23 | frecps(z2.s, z0.s, z1.s); 24 | fmul(z0.s, z1.s, z2.s); 25 | #endif 26 | 27 | st1w(z0.s, p0, ptr(x0)); 28 | ret(); 29 | } 30 | }; 31 | 32 | int main() 33 | try 34 | { 35 | Code c; 36 | c.ready(); 37 | auto f = c.getCode(); 38 | float xs[16]; 39 | float ys[16]; 40 | xs[0] = INFINITY; 41 | f(ys, xs); 42 | printf("%f %f\n", xs[0], ys[0]); 43 | for (float x = 0.1; x < 10; x += 0.8) { 44 | xs[0] = x; 45 | f(ys, xs); 46 | printf("%f %f\n", xs[0], ys[0]); 47 | } 48 | 49 | 50 | puts("top code"); 51 | const uint32_t *pc = (const uint32_t*)f; 52 | printf("%08x\n", *pc); 53 | } catch (std::exception& e) { 54 | printf("err %s\n", e.what()); 55 | } 56 | -------------------------------------------------------------------------------- /cpp/sample/fold.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int initValue = 100; 4 | #if CPP17 5 | 6 | template 7 | auto sumR(Args... args) 8 | { 9 | return (args + ...); 10 | } 11 | 12 | template 13 | auto sumL(Args... args) 14 | { 15 | return (... + args); 16 | } 17 | template 18 | auto sumRI(Args... args) 19 | { 20 | return (args + ... + initValue); 21 | } 22 | 23 | template 24 | auto sumLI(Args... args) 25 | { 26 | return (initValue + ... + args); 27 | } 28 | 29 | struct A { 30 | int a; 31 | A(int a) : a(a) {} 32 | }; 33 | 34 | A operator+(const A& lhs, const A& rhs) 35 | { 36 | printf("lhs=%d, rhs=%d\n", lhs.a, rhs.a); 37 | return A(lhs.a + rhs.a); 38 | } 39 | 40 | #else 41 | 42 | auto sum() 43 | { 44 | return initValue; 45 | } 46 | 47 | template 48 | auto sum(Arg head, Args... tails) 49 | { 50 | return head + sum(tails...); 51 | } 52 | 53 | #endif 54 | 55 | int main() 56 | { 57 | #ifdef CPP17 58 | printf("sumR =%d\n", sumR(A(1), A(2), A(3)).a); 59 | printf("sumL =%d\n", sumL(A(1), A(2), A(3)).a); 60 | printf("sumRI=%d\n", sumRI(A(1), A(2), A(3)).a); 61 | printf("sumLI=%d\n", sumLI(A(1), A(2), A(3)).a); 62 | #else 63 | printf("sum=%d\n", sum(1, 2, 5, 4)); 64 | #endif 65 | } 66 | --------------------------------------------------------------------------------