PRCYCoin  2.0.0.7rc1
P2P Digital Currency
util.h
Go to the documentation of this file.
1 /**********************************************************************
2  * Copyright (c) 2013-2015 Pieter Wuille, Gregory Maxwell *
3  * Distributed under the MIT software license, see the accompanying *
4  * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5  **********************************************************************/
6 
7 #ifndef SECP256K1_UTIL_H
8 #define SECP256K1_UTIL_H
9 
10 #if defined HAVE_CONFIG_H
11 #include "libsecp256k1-config.h"
12 #endif
13 
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include <stdio.h>
17 
18 typedef struct {
19  void (*fn)(const char *text, void* data);
20  const void* data;
22 
23 static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback * const cb, const char * const text) {
24  cb->fn(text, (void*)cb->data);
25 }
26 
27 #ifdef DETERMINISTIC
28 #define TEST_FAILURE(msg) do { \
29  fprintf(stderr, "%s\n", msg); \
30  abort(); \
31 } while(0);
32 #else
33 #define TEST_FAILURE(msg) do { \
34  fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
35  abort(); \
36 } while(0)
37 #endif
38 
39 #ifdef HAVE_BUILTIN_EXPECT
40 #define EXPECT(x,c) __builtin_expect((x),(c))
41 #else
42 #define EXPECT(x,c) (x)
43 #endif
44 
45 #ifdef DETERMINISTIC
46 #define CHECK(cond) do { \
47  if (EXPECT(!(cond), 0)) { \
48  TEST_FAILURE("test condition failed"); \
49  } \
50 } while(0)
51 #else
52 #define CHECK(cond) do { \
53  if (EXPECT(!(cond), 0)) { \
54  TEST_FAILURE("test condition failed: " #cond); \
55  } \
56 } while(0)
57 #endif
58 
59 /* Like assert(), but when VERIFY is defined, and side-effect safe. */
60 #if defined(COVERAGE)
61 #define VERIFY_CHECK(check)
62 #define VERIFY_SETUP(stmt)
63 #elif defined(VERIFY)
64 #define VERIFY_CHECK CHECK
65 #define VERIFY_SETUP(stmt) do { stmt; } while(0)
66 #else
67 #define VERIFY_CHECK(cond) do { (void)(cond); } while(0)
68 #define VERIFY_SETUP(stmt)
69 #endif
70 
71 static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
72  void *ret = malloc(size);
73  if (ret == NULL) {
74  secp256k1_callback_call(cb, "Out of memory");
75  }
76  return ret;
77 }
78 
79 static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void *ptr, size_t size) {
80  void *ret = realloc(ptr, size);
81  if (ret == NULL) {
82  secp256k1_callback_call(cb, "Out of memory");
83  }
84  return ret;
85 }
86 
87 /* Extract the sign of an int64, take the abs and return a uint64, constant time. */
88 SECP256K1_INLINE static int secp256k1_sign_and_abs64(uint64_t *out, int64_t in) {
89  uint64_t mask0, mask1;
90  int ret;
91  ret = in < 0;
92  mask0 = ret + ~((uint64_t)0);
93  mask1 = ~mask0;
94  *out = (uint64_t)in;
95  *out = (*out & mask0) | ((~*out + 1) & mask1);
96  return ret;
97 }
98 
99 SECP256K1_INLINE static int secp256k1_clz64_var(uint64_t x) {
100  int ret;
101  if (!x) {
102  return 64;
103  }
104 # if defined(HAVE_BUILTIN_CLZLL)
105  ret = __builtin_clzll(x);
106 # else
107  /*FIXME: debruijn fallback. */
108  for (ret = 0; ((x & (1ULL << 63)) == 0); x <<= 1, ret++);
109 # endif
110  return ret;
111 }
112 
113 /* Macro for restrict, when available and not in a VERIFY build. */
114 #if defined(SECP256K1_BUILD) && defined(VERIFY)
115 # define SECP256K1_RESTRICT
116 #else
117 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
118 # if SECP256K1_GNUC_PREREQ(3,0)
119 # define SECP256K1_RESTRICT __restrict__
120 # elif (defined(_MSC_VER) && _MSC_VER >= 1400)
121 # define SECP256K1_RESTRICT __restrict
122 # else
123 # define SECP256K1_RESTRICT
124 # endif
125 # else
126 # define SECP256K1_RESTRICT restrict
127 # endif
128 #endif
129 
130 #if defined(_WIN32)
131 # define I64FORMAT "I64d"
132 # define I64uFORMAT "I64u"
133 #else
134 # define I64FORMAT "lld"
135 # define I64uFORMAT "llu"
136 #endif
137 
138 #if defined(HAVE___INT128)
139 # if defined(__GNUC__)
140 # define SECP256K1_GNUC_EXT __extension__
141 # else
142 # define SECP256K1_GNUC_EXT
143 # endif
144 SECP256K1_GNUC_EXT typedef unsigned __int128 uint128_t;
145 #endif
146 
147 #endif /* SECP256K1_UTIL_H */
uint128_t
unsigned __int128 uint128_t
Definition: scalar_4x64_impl.h:10
secp256k1_callback::data
const void * data
Definition: util.h:24
secp256k1_callback::fn
void(* fn)(const char *text, void *data)
Definition: util.h:23
secp256k1_callback
Definition: util.h:18
SECP256K1_INLINE
#define SECP256K1_INLINE
Definition: secp256k1.h:23