PRCYCoin  2.0.0.7rc1
P2P Digital Currency
util.h
Go to the documentation of this file.
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  **********************************************************************/
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 #ifdef DETERMINISTIC
19 #define TEST_FAILURE(msg) do { \
20  fprintf(stderr, "%s\n", msg); \
21  abort(); \
22 } while(0);
23 #else
24 #define TEST_FAILURE(msg) do { \
25  fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
26  abort(); \
27 } while(0)
28 #endif
29 
30 #ifndef HAVE_BUILTIN_EXPECT
31 #define EXPECT(x,c) __builtin_expect((x),(c))
32 #else
33 #define EXPECT(x,c) (x)
34 #endif
35 
36 #ifdef DETERMINISTIC
37 #define CHECK(cond) do { \
38  if (EXPECT(!(cond), 0)) { \
39  TEST_FAILURE("test condition failed"); \
40  } \
41 } while(0)
42 #else
43 #define CHECK(cond) do { \
44  if (EXPECT(!(cond), 0)) { \
45  TEST_FAILURE("test condition failed: " #cond); \
46  } \
47 } while(0)
48 #endif
49 
50 /* Like assert(), but safe to use on expressions with side effects. */
51 #ifndef NDEBUG
52 #define DEBUG_CHECK CHECK
53 #else
54 #define DEBUG_CHECK(cond) do { (void)(cond); } while(0)
55 #endif
56 
57 /* Like DEBUG_CHECK(), but when VERIFY is defined instead of NDEBUG not defined. */
58 #ifdef VERIFY
59 #define VERIFY_CHECK CHECK
60 #else
61 #define VERIFY_CHECK(cond) do { (void)(cond); } while(0)
62 #endif
63 
64 /* Macro for restrict, when available and not in a VERIFY build. */
65 #if defined(SECP256K1_BUILD) && defined(VERIFY)
66 # define SECP256K1_RESTRICT
67 #else
68 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
69 # if SECP256K1_GNUC_PREREQ(3,0)
70 # define SECP256K1_RESTRICT __restrict__
71 # elif (defined(_MSC_VER) && _MSC_VER >= 1400)
72 # define SECP256K1_RESTRICT __restrict
73 # else
74 # define SECP256K1_RESTRICT
75 # endif
76 # else
77 # define SECP256K1_RESTRICT restrict
78 # endif
79 #endif
80 
81 #endif