]>
Commit | Line | Data |
---|---|---|
71712b27 GM |
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 | **********************************************************************/ | |
0a433ea2 | 6 | |
d06e61cb PW |
7 | #ifndef _SECP256K1_UTIL_H_ |
8 | #define _SECP256K1_UTIL_H_ | |
9 | ||
1c7fa133 PW |
10 | #if defined HAVE_CONFIG_H |
11 | #include "libsecp256k1-config.h" | |
12 | #endif | |
13 | ||
01097ddf | 14 | #include <stdlib.h> |
1c7fa133 PW |
15 | #include <stdint.h> |
16 | #include <stdio.h> | |
17 | ||
995c5487 PW |
18 | typedef struct { |
19 | void (*fn)(const char *text, void* data); | |
20 | void* data; | |
21 | } callback_t; | |
22 | ||
f49b2ef8 PW |
23 | #ifdef DETERMINISTIC |
24 | #define TEST_FAILURE(msg) do { \ | |
25 | fprintf(stderr, "%s\n", msg); \ | |
26 | abort(); \ | |
27 | } while(0); | |
28 | #else | |
1c7fa133 PW |
29 | #define TEST_FAILURE(msg) do { \ |
30 | fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \ | |
31 | abort(); \ | |
32 | } while(0) | |
f49b2ef8 | 33 | #endif |
1c7fa133 | 34 | |
402878ae | 35 | #ifdef HAVE_BUILTIN_EXPECT |
1c7fa133 PW |
36 | #define EXPECT(x,c) __builtin_expect((x),(c)) |
37 | #else | |
38 | #define EXPECT(x,c) (x) | |
39 | #endif | |
40 | ||
f49b2ef8 PW |
41 | #ifdef DETERMINISTIC |
42 | #define CHECK(cond) do { \ | |
43 | if (EXPECT(!(cond), 0)) { \ | |
44 | TEST_FAILURE("test condition failed"); \ | |
45 | } \ | |
46 | } while(0) | |
47 | #else | |
1c7fa133 PW |
48 | #define CHECK(cond) do { \ |
49 | if (EXPECT(!(cond), 0)) { \ | |
50 | TEST_FAILURE("test condition failed: " #cond); \ | |
51 | } \ | |
52 | } while(0) | |
f49b2ef8 | 53 | #endif |
1c7fa133 | 54 | |
995c5487 | 55 | /* Like assert(), but when VERIFY is defined, and side-effect safe. */ |
1c7fa133 PW |
56 | #ifdef VERIFY |
57 | #define VERIFY_CHECK CHECK | |
58 | #else | |
9974d869 | 59 | #define VERIFY_CHECK(cond) do { (void)(cond); } while(0) |
1c7fa133 PW |
60 | #endif |
61 | ||
995c5487 | 62 | static SECP256K1_INLINE void *checked_malloc(const callback_t* cb, size_t size) { |
a5759c57 | 63 | void *ret = malloc(size); |
995c5487 PW |
64 | if (ret == NULL) { |
65 | cb->fn("Out of memory", cb->data); | |
66 | } | |
a5759c57 PW |
67 | return ret; |
68 | } | |
69 | ||
be82e92f PW |
70 | /* Macro for restrict, when available and not in a VERIFY build. */ |
71 | #if defined(SECP256K1_BUILD) && defined(VERIFY) | |
72 | # define SECP256K1_RESTRICT | |
73 | #else | |
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 | |
79 | # else | |
80 | # define SECP256K1_RESTRICT | |
81 | # endif | |
82 | # else | |
83 | # define SECP256K1_RESTRICT restrict | |
84 | # endif | |
85 | #endif | |
86 | ||
f735446c GM |
87 | #if defined(_WIN32) |
88 | # define I64FORMAT "I64d" | |
89 | # define I64uFORMAT "I64u" | |
90 | #else | |
91 | # define I64FORMAT "lld" | |
92 | # define I64uFORMAT "llu" | |
93 | #endif | |
94 | ||
4be8d6fc | 95 | #if defined(HAVE___INT128) |
f735446c GM |
96 | # if defined(__GNUC__) |
97 | # define SECP256K1_GNUC_EXT __extension__ | |
98 | # else | |
99 | # define SECP256K1_GNUC_EXT | |
100 | # endif | |
101 | SECP256K1_GNUC_EXT typedef unsigned __int128 uint128_t; | |
4be8d6fc GM |
102 | #endif |
103 | ||
d06e61cb | 104 | #endif |