getArray supports big endian

2merge^2
MITSUNARI Shigeo 4 years ago
parent 580d574c7a
commit 71ae0bb970
  1. 5
      include/mcl/conversion.hpp
  2. 22
      include/mcl/gmp_util.hpp

@ -24,15 +24,18 @@ namespace mcl { namespace fp {
template<class D, class S> template<class D, class S>
bool setArrayAsLE(D *dst, size_t dstN, const S *src, size_t srcN) bool setArrayAsLE(D *dst, size_t dstN, const S *src, size_t srcN)
{ {
char assert_D_is_unsigned[D(-1) < 0 ? -1 : 1];
char assert_S_is_unsigned[S(-1) < 0 ? -1 : 1]; char assert_S_is_unsigned[S(-1) < 0 ? -1 : 1];
(void)assert_D_is_unsigned;
(void)assert_S_is_unsigned; (void)assert_S_is_unsigned;
if (sizeof(D) * dstN < sizeof(S) * srcN) return false; if (sizeof(D) * dstN < sizeof(S) * srcN) return false;
size_t pos = 0; size_t pos = 0;
size_t i = 0; size_t i = 0;
while (i < dstN) { while (i < dstN) {
if (sizeof(D) < sizeof(S)) { if (sizeof(D) < sizeof(S)) {
S s = src[pos++]; S s = (pos < srcN) ? src[pos++] : 0;
for (size_t j = 0; j < sizeof(S); j += sizeof(D)) { for (size_t j = 0; j < sizeof(S); j += sizeof(D)) {
assert(i < dstN);
dst[i++] = D(s); dst[i++] = D(s);
s >>= sizeof(D) * 8; s >>= sizeof(D) * 8;
} }

@ -16,6 +16,8 @@
#endif #endif
#include <mcl/randgen.hpp> #include <mcl/randgen.hpp>
#include <mcl/config.hpp> #include <mcl/config.hpp>
#include <mcl/conversion.hpp>
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(push) #pragma warning(push)
#pragma warning(disable : 4616) #pragma warning(disable : 4616)
@ -61,24 +63,18 @@ void setArray(bool *pb, mpz_class& z, const T *buf, size_t n)
buf[0, size) = x buf[0, size) = x
buf[size, maxSize) with zero buf[size, maxSize) with zero
*/ */
template<class T, class U>
bool getArray_(T *buf, size_t maxSize, const U *x, int xn)//const mpz_srcptr x)
{
const size_t bufByteSize = sizeof(T) * maxSize;
if (xn < 0) return false;
size_t xByteSize = sizeof(*x) * xn;
if (xByteSize > bufByteSize) return false;
memcpy(buf, x, xByteSize);
memset((char*)buf + xByteSize, 0, bufByteSize - xByteSize);
return true;
}
template<class T> template<class T>
void getArray(bool *pb, T *buf, size_t maxSize, const mpz_class& x) void getArray(bool *pb, T *buf, size_t maxSize, const mpz_class& x)
{ {
#ifdef MCL_USE_VINT #ifdef MCL_USE_VINT
*pb = getArray_(buf, maxSize, x.getUnit(), (int)x.getUnitSize()); *pb = fp::setArrayAsLE(buf, maxSize, x.getUnit(), x.getUnitSize());
#else #else
*pb = getArray_(buf, maxSize, x.get_mpz_t()->_mp_d, x.get_mpz_t()->_mp_size); int n = x.get_mpz_t()->_mp_size;
if (n < 0) {
*pb = false;
return;
}
*pb = fp::setArrayAsLE(buf, maxSize, x.get_mpz_t()->_mp_d, n);
#endif #endif
} }
inline void set(mpz_class& z, uint64_t x) inline void set(mpz_class& z, uint64_t x)

Loading…
Cancel
Save