diff --git a/lib/lattice/rng/rng-state.h b/lib/lattice/rng/rng-state.h index b7d00a68..170763e5 100644 --- a/lib/lattice/rng/rng-state.h +++ b/lib/lattice/rng/rng-state.h @@ -5,7 +5,7 @@ // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, @@ -19,7 +19,8 @@ // Code within namespace sha256 are from Stephan Brumme. // see http://create.stephan-brumme.com/disclaimer.html -#pragma once +#ifndef RNG_STATE_RNG_STATE_H +#define RNG_STATE_RNG_STATE_H #include "show.h" @@ -126,10 +127,9 @@ inline void splitTwoUint32(uint32_t& a, uint32_t& b, const uint64_t x) assert(x == patchTwoUint32(a, b)); } -inline void exportRngState(std::vector& v, const RngState& rs) +inline void exportRngState(uint32_t* v, const RngState& rs) { assert(22 == RNG_STATE_NUM_OF_INT32); - v.resize(RNG_STATE_NUM_OF_INT32); splitTwoUint32(v[0], v[1], rs.numBytes); for (int i = 0; i < 8; ++i) { v[2 + i] = rs.hash[i]; @@ -144,9 +144,8 @@ inline void exportRngState(std::vector& v, const RngState& rs) v[21] = rs.gaussianAvail; } -inline void importRngState(RngState& rs, const std::vector& v) +inline void importRngState(RngState& rs, const uint32_t* v) { - assert(RNG_STATE_NUM_OF_INT32 == v.size()); assert(22 == RNG_STATE_NUM_OF_INT32); rs.numBytes = patchTwoUint32(v[0], v[1]); for (int i = 0; i < 8; ++i) { @@ -156,12 +155,24 @@ inline void importRngState(RngState& rs, const std::vector& v) for (int i = 0; i < 3; ++i) { rs.cache[i] = patchTwoUint32(v[12 + i * 2], v[12 + i * 2 + 1]); } - uint64_t* p = (uint64_t*)&rs.gaussian; - *p = patchTwoUint32(v[18], v[19]); + uint64_t g = patchTwoUint32(v[18], v[19]); + rs.gaussian = reinterpret_cast(g); rs.cacheAvail = v[20]; rs.gaussianAvail = v[21]; } +inline void exportRngState(std::vector& v, const RngState& rs) +{ + v.resize(RNG_STATE_NUM_OF_INT32); + exportRngState(v.data(), rs); +} + +inline void importRngState(RngState& rs, const std::vector& v) +{ + assert(RNG_STATE_NUM_OF_INT32 == v.size()); + importRngState(rs, v.data()); +} + inline std::ostream& operator<<(std::ostream& os, const RngState& rs) { std::vector v(RNG_STATE_NUM_OF_INT32); @@ -568,3 +579,5 @@ inline double gRandGen(RngState& rs, const double sigma, const double center) #ifdef CURRENT_DEFAULT_NAMESPACE_NAME } #endif + +#endif diff --git a/lib/lattice/rng/show.h b/lib/lattice/rng/show.h index 60953875..a44b3a86 100644 --- a/lib/lattice/rng/show.h +++ b/lib/lattice/rng/show.h @@ -5,7 +5,7 @@ // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, @@ -16,7 +16,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#pragma once +#ifndef RNG_STATE_SHOW_H +#define RNG_STATE_SHOW_H #include #include @@ -105,12 +106,12 @@ T& reads(T& x, const std::string& str) return x; } -void fdisplay(FILE* fp, const std::string& str) +inline void fdisplay(FILE* fp, const std::string& str) { fprintf(fp, "%s", str.c_str()); } -void fdisplayln(FILE* fp, const std::string& str) +inline void fdisplayln(FILE* fp, const std::string& str) { fprintf(fp, "%s\n", str.c_str()); } @@ -118,3 +119,5 @@ void fdisplayln(FILE* fp, const std::string& str) #ifdef CURRENT_DEFAULT_NAMESPACE_NAME } #endif + +#endif diff --git a/lib/lattice/rng/sprng-sha256.h b/lib/lattice/rng/sprng-sha256.h index 7732fd8d..ec4c2020 100644 --- a/lib/lattice/rng/sprng-sha256.h +++ b/lib/lattice/rng/sprng-sha256.h @@ -5,7 +5,7 @@ // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or +// the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, @@ -16,7 +16,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#pragma once +#ifndef RNG_STATE_SPRNG_SHA256_H +#define RNG_STATE_SPRNG_SHA256_H #include "rng-state.h" @@ -108,3 +109,5 @@ inline bool operator==(const SprngSha256& ss1, const SprngSha256& ss2) #ifdef CURRENT_DEFAULT_NAMESPACE_NAME } #endif + +#endif