From f171ec0c7d084b6bb163d1466edd814cf4dcbc93 Mon Sep 17 00:00:00 2001 From: Olivier Langlois Date: Thu, 10 Oct 2013 12:35:51 -0400 Subject: Make util phexdigit array reusable class template base_uint had its own private lookup table. This is saving 256 bytes per instantiation. The result is not spectacular as bitcoin-qt has only shrinked of about 1Kb but it is still valid improvement. Also, I have replaced a for loop with a memset() call. Made CBigNum::SetHex() use the new HexDigit() function. Signed-off-by: Olivier Langlois --- src/uint256.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/uint256.h') diff --git a/src/uint256.h b/src/uint256.h index 45ab8abb5e..562426a0d8 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -14,7 +14,12 @@ typedef long long int64; typedef unsigned long long uint64; +extern const signed char p_util_hexdigit[256]; // defined in util.cpp +inline signed char HexDigit(char c) +{ + return p_util_hexdigit[(unsigned char)c]; +} inline int Testuint256AdHoc(std::vector vArg); @@ -305,8 +310,7 @@ public: void SetHex(const char* psz) { - for (int i = 0; i < WIDTH; i++) - pn[i] = 0; + memset(pn,0,sizeof(pn)); // skip leading spaces while (isspace(*psz)) @@ -317,19 +321,18 @@ public: psz += 2; // hex string to uint - static const unsigned char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 }; const char* pbegin = psz; - while (phexdigit[(unsigned char)*psz] || *psz == '0') + while (::HexDigit(*psz) != -1) psz++; psz--; unsigned char* p1 = (unsigned char*)pn; unsigned char* pend = p1 + WIDTH * 4; while (psz >= pbegin && p1 < pend) { - *p1 = phexdigit[(unsigned char)*psz--]; + *p1 = ::HexDigit(*psz--); if (psz >= pbegin) { - *p1 |= (phexdigit[(unsigned char)*psz--] << 4); + *p1 |= ((unsigned char)::HexDigit(*psz--) << 4); p1++; } } -- cgit v1.2.3