diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2018-10-28 13:56:05 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2018-10-28 13:56:05 +0800 |
commit | 05dc2da2e222b691c662910ff902a72342067364 (patch) | |
tree | ddb2885c98a44dafe3a35dd60ae74ce2506a320f | |
parent | 309a2c0db4c51e83dfe05668d114db2b63e72723 (diff) | |
download | tangerine-mcl-05dc2da2e222b691c662910ff902a72342067364.tar.gz tangerine-mcl-05dc2da2e222b691c662910ff902a72342067364.tar.zst tangerine-mcl-05dc2da2e222b691c662910ff902a72342067364.zip |
update cybozulib
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | include/cybozu/random_generator.hpp | 34 | ||||
-rw-r--r-- | include/cybozu/xorshift.hpp | 113 |
3 files changed, 91 insertions, 58 deletions
@@ -328,6 +328,8 @@ make_tbl: update_xbyak: cp -a ../xbyak/xbyak/xbyak.h ../xbyak/xbyak/xbyak_util.h ../xbyak/xbyak/xbyak_mnemonic.h src/xbyak/ +update_cybozulib: + cp -a $(addprefix ../cybozulib/,$(wildcard include/cybozu/*.hpp)) include/cybozu/ clean: $(RM) $(LIB_DIR)/*.a $(EXE_DIR)/*.$(LIB_SUF) $(OBJ_DIR)/*.o $(OBJ_DIR)/*.d $(EXE_DIR)/*.exe $(GEN_EXE) $(ASM_OBJ) $(LIB_OBJ) $(BN256_OBJ) $(BN384_OBJ) $(BN512_OBJ) $(LLVM_SRC) $(FUNC_LIST) src/*.ll lib/*.a diff --git a/include/cybozu/random_generator.hpp b/include/cybozu/random_generator.hpp index 2309698..ff4a78d 100644 --- a/include/cybozu/random_generator.hpp +++ b/include/cybozu/random_generator.hpp @@ -54,11 +54,9 @@ public: } throw cybozu::Exception("randomgenerator"); } - void read_inner(void *buf, size_t byteSize) + bool read_inner(void *buf, size_t byteSize) { - if (CryptGenRandom(prov_, static_cast<DWORD>(byteSize), static_cast<BYTE*>(buf)) == 0) { - throw cybozu::Exception("randomgenerator:read") << byteSize; - } + return CryptGenRandom(prov_, static_cast<DWORD>(byteSize), static_cast<BYTE*>(buf)) != 0; } ~RandomGenerator() { @@ -71,12 +69,15 @@ public: @note bufNum is not byte size */ template<class T> - void read(T *buf, size_t bufNum) + void read(bool *pb, T *buf, size_t bufNum) { cybozu::AutoLockCs al(cs_); const size_t byteSize = sizeof(T) * bufNum; if (byteSize > bufSize) { - read_inner(buf, byteSize); + if (!read_inner(buf, byteSize)) { + *pb = false; + return; + } } else { if (pos_ + byteSize > bufSize) { read_inner(buf_, bufSize); @@ -85,6 +86,14 @@ public: memcpy(buf, buf_ + pos_, byteSize); pos_ += byteSize; } + *pb = true; + } + template<class T> + void read(T *buf, size_t bufNum) + { + bool b; + read(&b, buf, bufNum); + if (!b) throw cybozu::Exception("RandomGenerator:read") << bufNum; } private: HCRYPTPROV prov_; @@ -107,12 +116,17 @@ private: @note bufNum is not byte size */ template<class T> - void read(T *buf, size_t bufNum) + void read(bool *pb, T *buf, size_t bufNum) { const size_t byteSize = sizeof(T) * bufNum; - if (::fread(buf, 1, (int)byteSize, fp_) != byteSize) { - throw cybozu::Exception("randomgenerator:read") << byteSize; - } + *pb = ::fread(buf, 1, (int)byteSize, fp_) == byteSize; + } + template<class T> + void read(T *buf, size_t bufNum) + { + bool b; + read(&b, buf, bufNum); + if (!b) throw cybozu::Exception("RandomGenerator:read") << bufNum; } #endif private: diff --git a/include/cybozu/xorshift.hpp b/include/cybozu/xorshift.hpp index 69edaa9..08c6a04 100644 --- a/include/cybozu/xorshift.hpp +++ b/include/cybozu/xorshift.hpp @@ -7,9 +7,47 @@ @author MITSUNARI Shigeo */ #include <cybozu/inttype.hpp> +#include <assert.h> namespace cybozu { +namespace xorshift_local { + +/* + U is uint32_t or uint64_t +*/ +template<class U, class Gen> +void read_local(void *p, size_t n, Gen& gen, U (Gen::*f)()) +{ + uint8_t *dst = static_cast<uint8_t*>(p); + const size_t uSize = sizeof(U); + assert(uSize == 4 || uSize == 8); + union ua { + U u; + uint8_t a[uSize]; + }; + + while (n >= uSize) { + ua ua; + ua.u = (gen.*f)(); + for (size_t i = 0; i < uSize; i++) { + dst[i] = ua.a[i]; + } + dst += uSize; + n -= uSize; + } + assert(n < uSize); + if (n > 0) { + ua ua; + ua.u = (gen.*f)(); + for (size_t i = 0; i < n; i++) { + dst[i] = ua.a[i]; + } + } +} + +} // xorshift_local + class XorShift { uint32_t x_, y_, z_, w_; public: @@ -38,25 +76,18 @@ public: return (uint64_t(a) << 32) | b; } template<class T> - void read(T *x, size_t n) - { - const size_t size = sizeof(T) * n; - uint8_t *p8 = static_cast<uint8_t*>(x); - for (size_t i = 0; i < size; i++) { - p8[i] = static_cast<uint8_t>(get32()); - } - } - void read(uint32_t *x, size_t n) + void read(bool *pb, T *p, size_t n) { - for (size_t i = 0; i < n; i++) { - x[i] = get32(); - } + xorshift_local::read_local(p, n * sizeof(T), *this, &XorShift::get32); + *pb = true; } - void read(uint64_t *x, size_t n) + template<class T> + size_t read(T *p, size_t n) { - for (size_t i = 0; i < n; i++) { - x[i] = get64(); - } + bool b; + read(&b, p, n); + (void)b; + return n; } }; @@ -90,25 +121,18 @@ public: return s_[1] + s0; } template<class T> - void read(T *x, size_t n) + void read(bool *pb, T *p, size_t n) { - const size_t size = sizeof(T) * n; - uint8_t *p8 = static_cast<uint8_t*>(x); - for (size_t i = 0; i < size; i++) { - p8[i] = static_cast<uint8_t>(get32()); - } + xorshift_local::read_local(p, n * sizeof(T), *this, &XorShift128Plus::get64); + *pb = true; } - void read(uint32_t *x, size_t n) - { - for (size_t i = 0; i < n; i++) { - x[i] = get32(); - } - } - void read(uint64_t *x, size_t n) + template<class T> + size_t read(T *p, size_t n) { - for (size_t i = 0; i < n; i++) { - x[i] = get64(); - } + bool b; + read(&b, p, n); + (void)b; + return n; } }; @@ -147,25 +171,18 @@ public: return result; } template<class T> - void read(T *x, size_t n) + void read(bool *pb, T *p, size_t n) { - const size_t size = sizeof(T) * n; - uint8_t *p8 = static_cast<uint8_t*>(x); - for (size_t i = 0; i < size; i++) { - p8[i] = static_cast<uint8_t>(get32()); - } - } - void read(uint32_t *x, size_t n) - { - for (size_t i = 0; i < n; i++) { - x[i] = get32(); - } + xorshift_local::read_local(p, n * sizeof(T), *this, &Xoroshiro128Plus::get64); + *pb = true; } - void read(uint64_t *x, size_t n) + template<class T> + size_t read(T *p, size_t n) { - for (size_t i = 0; i < n; i++) { - x[i] = get64(); - } + bool b; + read(&b, p, n); + (void)b; + return n; } }; |