]> Git Repo - secp256k1.git/blame - src/util.h
Store z-ratios in the 'x' coord they'll recover
[secp256k1.git] / src / util.h
CommitLineData
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
abe2d3e8
DR
7#ifndef SECP256K1_UTIL_H
8#define SECP256K1_UTIL_H
d06e61cb 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
18typedef struct {
19 void (*fn)(const char *text, void* data);
05732c5a 20 const void* data;
dd891e0e 21} secp256k1_callback;
995c5487 22
dd891e0e 23static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback * const cb, const char * const text) {
05732c5a
LD
24 cb->fn(text, (void*)cb->data);
25}
26
f49b2ef8
PW
27#ifdef DETERMINISTIC
28#define TEST_FAILURE(msg) do { \
29 fprintf(stderr, "%s\n", msg); \
30 abort(); \
31} while(0);
32#else
1c7fa133
PW
33#define TEST_FAILURE(msg) do { \
34 fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
35 abort(); \
36} while(0)
f49b2ef8 37#endif
1c7fa133 38
402878ae 39#ifdef HAVE_BUILTIN_EXPECT
1c7fa133
PW
40#define EXPECT(x,c) __builtin_expect((x),(c))
41#else
42#define EXPECT(x,c) (x)
43#endif
44
f49b2ef8
PW
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
1c7fa133
PW
52#define CHECK(cond) do { \
53 if (EXPECT(!(cond), 0)) { \
54 TEST_FAILURE("test condition failed: " #cond); \
55 } \
56} while(0)
f49b2ef8 57#endif
1c7fa133 58
995c5487 59/* Like assert(), but when VERIFY is defined, and side-effect safe. */
a724d729
AP
60#if defined(COVERAGE)
61#define VERIFY_CHECK(check)
62#define VERIFY_SETUP(stmt)
63#elif defined(VERIFY)
1c7fa133 64#define VERIFY_CHECK CHECK
72ae443a 65#define VERIFY_SETUP(stmt) do { stmt; } while(0)
1c7fa133 66#else
9974d869 67#define VERIFY_CHECK(cond) do { (void)(cond); } while(0)
72ae443a 68#define VERIFY_SETUP(stmt)
1c7fa133
PW
69#endif
70
dd891e0e 71static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
a5759c57 72 void *ret = malloc(size);
995c5487 73 if (ret == NULL) {
dd891e0e 74 secp256k1_callback_call(cb, "Out of memory");
995c5487 75 }
a5759c57
PW
76 return ret;
77}
78
548de42e
AP
79static 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
be82e92f
PW
87/* Macro for restrict, when available and not in a VERIFY build. */
88#if defined(SECP256K1_BUILD) && defined(VERIFY)
89# define SECP256K1_RESTRICT
90#else
91# if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
92# if SECP256K1_GNUC_PREREQ(3,0)
93# define SECP256K1_RESTRICT __restrict__
94# elif (defined(_MSC_VER) && _MSC_VER >= 1400)
95# define SECP256K1_RESTRICT __restrict
96# else
97# define SECP256K1_RESTRICT
98# endif
99# else
100# define SECP256K1_RESTRICT restrict
101# endif
102#endif
103
f735446c
GM
104#if defined(_WIN32)
105# define I64FORMAT "I64d"
106# define I64uFORMAT "I64u"
107#else
108# define I64FORMAT "lld"
109# define I64uFORMAT "llu"
110#endif
111
4be8d6fc 112#if defined(HAVE___INT128)
f735446c
GM
113# if defined(__GNUC__)
114# define SECP256K1_GNUC_EXT __extension__
115# else
116# define SECP256K1_GNUC_EXT
117# endif
118SECP256K1_GNUC_EXT typedef unsigned __int128 uint128_t;
4be8d6fc
GM
119#endif
120
abe2d3e8 121#endif /* SECP256K1_UTIL_H */
This page took 0.049451 seconds and 4 git commands to generate.