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
59 #define VERIFY_CHECK(cond) do { (void)(cond); } while(0)
62 static SECP256K1_INLINE void *checked_malloc(const callback_t* cb, size_t size) {
63 void *ret = malloc(size);
65 cb->fn("Out of memory", cb->data);
70 /* Macro for restrict, when available and not in a VERIFY build. */
71 #if defined(SECP256K1_BUILD) && defined(VERIFY)
72 # define SECP256K1_RESTRICT
74 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
75 # if SECP256K1_GNUC_PREREQ(3,0)
76 # define SECP256K1_RESTRICT __restrict__
77 # elif (defined(_MSC_VER) && _MSC_VER >= 1400)
78 # define SECP256K1_RESTRICT __restrict
80 # define SECP256K1_RESTRICT
83 # define SECP256K1_RESTRICT restrict
88 # define I64FORMAT "I64d"
89 # define I64uFORMAT "I64u"
91 # define I64FORMAT "lld"
92 # define I64uFORMAT "llu"
95 #if defined(HAVE___INT128)
96 # if defined(__GNUC__)
97 # define SECP256K1_GNUC_EXT __extension__
99 # define SECP256K1_GNUC_EXT
101 SECP256K1_GNUC_EXT typedef unsigned __int128 uint128_t;