1 /**********************************************************************
2 * Copyright (c) 2013, 2014 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
7 #ifndef _SECP256K1_UTIL_H_
8 #define _SECP256K1_UTIL_H_
10 #if defined HAVE_CONFIG_H
11 #include "libsecp256k1-config.h"
19 void (*fn)(const char *text, void* data);
24 #define TEST_FAILURE(msg) do { \
25 fprintf(stderr, "%s\n", msg); \
29 #define TEST_FAILURE(msg) do { \
30 fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
35 #ifdef HAVE_BUILTIN_EXPECT
36 #define EXPECT(x,c) __builtin_expect((x),(c))
38 #define EXPECT(x,c) (x)
42 #define CHECK(cond) do { \
43 if (EXPECT(!(cond), 0)) { \
44 TEST_FAILURE("test condition failed"); \
48 #define CHECK(cond) do { \
49 if (EXPECT(!(cond), 0)) { \
50 TEST_FAILURE("test condition failed: " #cond); \
55 /* Like assert(), but when VERIFY is defined, and side-effect safe. */
57 #define VERIFY_CHECK CHECK
58 #define VERIFY_SETUP(stmt) do { stmt; } while(0)
60 #define VERIFY_CHECK(cond) do { (void)(cond); } while(0)
61 #define VERIFY_SETUP(stmt)
64 static SECP256K1_INLINE void *checked_malloc(const callback_t* cb, size_t size) {
65 void *ret = malloc(size);
67 cb->fn("Out of memory", cb->data);
72 /* Macro for restrict, when available and not in a VERIFY build. */
73 #if defined(SECP256K1_BUILD) && defined(VERIFY)
74 # define SECP256K1_RESTRICT
76 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
77 # if SECP256K1_GNUC_PREREQ(3,0)
78 # define SECP256K1_RESTRICT __restrict__
79 # elif (defined(_MSC_VER) && _MSC_VER >= 1400)
80 # define SECP256K1_RESTRICT __restrict
82 # define SECP256K1_RESTRICT
85 # define SECP256K1_RESTRICT restrict
90 # define I64FORMAT "I64d"
91 # define I64uFORMAT "I64u"
93 # define I64FORMAT "lld"
94 # define I64uFORMAT "llu"
97 #if defined(HAVE___INT128)
98 # if defined(__GNUC__)
99 # define SECP256K1_GNUC_EXT __extension__
101 # define SECP256K1_GNUC_EXT
103 SECP256K1_GNUC_EXT typedef unsigned __int128 uint128_t;