LCOV - code coverage report
Current view: top level - src/crypto - common.h (source / functions) Hit Total Coverage
Test: bitcoincash_test.info Lines: 6 8 75.0 %
Date: 2018-04-13 15:12:50 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright (c) 2014 The Bitcoin Core developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #ifndef BITCOIN_CRYPTO_COMMON_H
       6             : #define BITCOIN_CRYPTO_COMMON_H
       7             : 
       8             : #if defined(HAVE_CONFIG_H)
       9             : #include "config/bitcoin-config.h"
      10             : #endif
      11             : 
      12             : #include <cstdint>
      13             : #include <cstring>
      14             : 
      15             : #include "compat/endian.h"
      16             : 
      17             : static inline uint16_t ReadLE16(const uint8_t *ptr) {
      18             :     uint16_t x;
      19           0 :     memcpy((char *)&x, ptr, 2);
      20             :     return le16toh(x);
      21             : }
      22             : 
      23             : static inline uint32_t ReadLE32(const uint8_t *ptr) {
      24             :     uint32_t x;
      25           0 :     memcpy((char *)&x, ptr, 4);
      26             :     return le32toh(x);
      27             : }
      28             : 
      29             : static inline uint64_t ReadLE64(const uint8_t *ptr) {
      30             :     uint64_t x;
      31    65029480 :     memcpy((char *)&x, ptr, 8);
      32             :     return le64toh(x);
      33             : }
      34             : 
      35             : static inline void WriteLE16(uint8_t *ptr, uint16_t x) {
      36         357 :     uint16_t v = htole16(x);
      37         357 :     memcpy(ptr, (char *)&v, 2);
      38             : }
      39             : 
      40             : static inline void WriteLE32(uint8_t *ptr, uint32_t x) {
      41          16 :     uint32_t v = htole32(x);
      42          16 :     memcpy(ptr, (char *)&v, 4);
      43             : }
      44             : 
      45             : static inline void WriteLE64(uint8_t *ptr, uint64_t x) {
      46             :     uint64_t v = htole64(x);
      47             :     memcpy(ptr, (char *)&v, 8);
      48             : }
      49             : 
      50             : static inline uint32_t ReadBE32(const uint8_t *ptr) {
      51             :     uint32_t x;
      52             :     memcpy((char *)&x, ptr, 4);
      53             :     return be32toh(x);
      54             : }
      55             : 
      56             : static inline uint64_t ReadBE64(const uint8_t *ptr) {
      57             :     uint64_t x;
      58             :     memcpy((char *)&x, ptr, 8);
      59             :     return be64toh(x);
      60             : }
      61             : 
      62             : static inline void WriteBE32(uint8_t *ptr, uint32_t x) {
      63             :     uint32_t v = htobe32(x);
      64             :     memcpy(ptr, (char *)&v, 4);
      65             : }
      66             : 
      67             : static inline void WriteBE64(uint8_t *ptr, uint64_t x) {
      68             :     uint64_t v = htobe64(x);
      69             :     memcpy(ptr, (char *)&v, 8);
      70             : }
      71             : 
      72             : /**
      73             :  * Return the smallest number n such that (x >> n) == 0 (or 64 if the highest
      74             :  * bit in x is set.
      75             :  */
      76             : uint64_t static inline CountBits(uint64_t x) {
      77             : #ifdef HAVE_DECL___BUILTIN_CLZL
      78             :     if (sizeof(unsigned long) >= sizeof(uint64_t)) {
      79      526266 :         return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
      80             :     }
      81             : #endif
      82             : #ifdef HAVE_DECL___BUILTIN_CLZLL
      83             :     if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
      84             :         return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
      85             :     }
      86             : #endif
      87             :     int ret = 0;
      88             :     while (x) {
      89             :         x >>= 1;
      90             :         ++ret;
      91             :     }
      92             :     return ret;
      93             : }
      94             : 
      95             : #endif // BITCOIN_CRYPTO_COMMON_H

Generated by: LCOV version 1.12