update cybozulib

dev
MITSUNARI Shigeo 6 years ago
parent 309a2c0db4
commit 05dc2da2e2
  1. 2
      Makefile
  2. 34
      include/cybozu/random_generator.hpp
  3. 113
      include/cybozu/xorshift.hpp

@ -328,6 +328,8 @@ make_tbl:
update_xbyak: update_xbyak:
cp -a ../xbyak/xbyak/xbyak.h ../xbyak/xbyak/xbyak_util.h ../xbyak/xbyak/xbyak_mnemonic.h src/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: 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 $(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

@ -54,11 +54,9 @@ public:
} }
throw cybozu::Exception("randomgenerator"); 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) { return CryptGenRandom(prov_, static_cast<DWORD>(byteSize), static_cast<BYTE*>(buf)) != 0;
throw cybozu::Exception("randomgenerator:read") << byteSize;
}
} }
~RandomGenerator() ~RandomGenerator()
{ {
@ -71,12 +69,15 @@ public:
@note bufNum is not byte size @note bufNum is not byte size
*/ */
template<class T> template<class T>
void read(T *buf, size_t bufNum) void read(bool *pb, T *buf, size_t bufNum)
{ {
cybozu::AutoLockCs al(cs_); cybozu::AutoLockCs al(cs_);
const size_t byteSize = sizeof(T) * bufNum; const size_t byteSize = sizeof(T) * bufNum;
if (byteSize > bufSize) { if (byteSize > bufSize) {
read_inner(buf, byteSize); if (!read_inner(buf, byteSize)) {
*pb = false;
return;
}
} else { } else {
if (pos_ + byteSize > bufSize) { if (pos_ + byteSize > bufSize) {
read_inner(buf_, bufSize); read_inner(buf_, bufSize);
@ -85,6 +86,14 @@ public:
memcpy(buf, buf_ + pos_, byteSize); memcpy(buf, buf_ + pos_, byteSize);
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: private:
HCRYPTPROV prov_; HCRYPTPROV prov_;
@ -107,12 +116,17 @@ private:
@note bufNum is not byte size @note bufNum is not byte size
*/ */
template<class T> 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; const size_t byteSize = sizeof(T) * bufNum;
if (::fread(buf, 1, (int)byteSize, fp_) != byteSize) { *pb = ::fread(buf, 1, (int)byteSize, fp_) == byteSize;
throw cybozu::Exception("randomgenerator:read") << 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 #endif
private: private:

@ -7,9 +7,47 @@
@author MITSUNARI Shigeo @author MITSUNARI Shigeo
*/ */
#include <cybozu/inttype.hpp> #include <cybozu/inttype.hpp>
#include <assert.h>
namespace cybozu { 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 { class XorShift {
uint32_t x_, y_, z_, w_; uint32_t x_, y_, z_, w_;
public: public:
@ -38,25 +76,18 @@ public:
return (uint64_t(a) << 32) | b; return (uint64_t(a) << 32) | b;
} }
template<class T> 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++) { xorshift_local::read_local(p, n * sizeof(T), *this, &XorShift::get32);
x[i] = 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++) { bool b;
x[i] = get64(); read(&b, p, n);
} (void)b;
return n;
} }
}; };
@ -90,25 +121,18 @@ public:
return s_[1] + s0; return s_[1] + s0;
} }
template<class T> 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; xorshift_local::read_local(p, n * sizeof(T), *this, &XorShift128Plus::get64);
uint8_t *p8 = static_cast<uint8_t*>(x); *pb = true;
for (size_t i = 0; i < size; i++) {
p8[i] = static_cast<uint8_t>(get32());
}
} }
void read(uint32_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] = get32();
}
}
void read(uint64_t *x, size_t n)
{ {
for (size_t i = 0; i < n; i++) { bool b;
x[i] = get64(); read(&b, p, n);
} (void)b;
return n;
} }
}; };
@ -147,25 +171,18 @@ public:
return result; return result;
} }
template<class T> 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; xorshift_local::read_local(p, n * sizeof(T), *this, &Xoroshiro128Plus::get64);
uint8_t *p8 = static_cast<uint8_t*>(x); *pb = true;
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();
}
} }
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++) { bool b;
x[i] = get64(); read(&b, p, n);
} (void)b;
return n;
} }
}; };

Loading…
Cancel
Save