update cybozulib

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

@ -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

@ -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:

@ -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)
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, &XorShift::get32);
*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;
}
};
@ -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());
xorshift_local::read_local(p, n * sizeof(T), *this, &Xoroshiro128Plus::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;
}
};

Loading…
Cancel
Save