Skip to content

Commit da8e35e

Browse files
Merge assert_p.h and math_support_p.h into compilersupport_p.h
And stop the unnecessary "must include assert_p.h last". Just use a different macro name, that we control. Signed-off-by: Thiago Macieira <[email protected]>
1 parent 2c22d71 commit da8e35e

9 files changed

+52
-116
lines changed

src/assert_p.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/cborencoder.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@
3232
#include "cborinternal_p.h"
3333
#include "compilersupport_p.h"
3434

35-
#include <assert.h>
3635
#include <stdlib.h>
3736
#include <string.h>
3837

39-
#include "assert_p.h" /* Always include last */
40-
4138
/**
4239
* \defgroup CborEncoding Encoding to CBOR
4340
* \brief Group of functions used to encode data to CBOR.
@@ -136,7 +133,7 @@
136133
* Finally, the example below illustrates expands on the one above and also
137134
* deals with dynamically growing the buffer if the initial allocation wasn't
138135
* big enough. Note the two places where the error checking was replaced with
139-
* an assertion, showing where the author assumes no error can occur.
136+
* an cbor_assertion, showing where the author assumes no error can occur.
140137
*
141138
* \code
142139
* uint8_t *encode_string_array(const char **strings, int n, size_t *bufsize)
@@ -156,7 +153,7 @@
156153
*
157154
* cbor_encoder_init(&encoder, &buf, size, 0);
158155
* err = cbor_encoder_create_array(&encoder, &arrayEncoder, n);
159-
* assert(err); // can't fail, the buffer is always big enough
156+
* cbor_assert(err); // can't fail, the buffer is always big enough
160157
*
161158
* for (i = 0; i < n; ++i) {
162159
* err = cbor_encode_text_stringz(&arrayEncoder, strings[i]);
@@ -165,7 +162,7 @@
165162
* }
166163
*
167164
* err = cbor_encoder_close_container_checked(&encoder, &arrayEncoder);
168-
* assert(err); // shouldn't fail!
165+
* cbor_assert(err); // shouldn't fail!
169166
*
170167
* more_bytes = cbor_encoder_get_extra_bytes_needed(encoder);
171168
* if (more_size) {
@@ -378,7 +375,7 @@ CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value)
378375
CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value)
379376
{
380377
uint8_t buf[1 + sizeof(uint64_t)];
381-
assert(fpType == CborHalfFloatType || fpType == CborFloatType || fpType == CborDoubleType);
378+
cbor_assert(fpType == CborHalfFloatType || fpType == CborFloatType || fpType == CborDoubleType);
382379
buf[0] = fpType;
383380

384381
unsigned size = 2U << (fpType - CborHalfFloatType);

src/cborencoder_close_container_checked.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
#include "cborinternal_p.h"
3333
#include "compilersupport_p.h"
3434

35-
#include <assert.h>
36-
37-
#include "assert_p.h" /* Always include last */
38-
3935
/**
4036
* \addtogroup CborEncoding
4137
* @{

src/cborparser.c

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@
3232
#include "cborinternal_p.h"
3333
#include "compilersupport_p.h"
3434

35-
#include <assert.h>
3635
#include <string.h>
3736

38-
#include "assert_p.h" /* Always include last */
39-
4037
#ifndef CBOR_PARSER_MAX_RECURSIONS
4138
# define CBOR_PARSER_MAX_RECURSIONS 1024
4239
#endif
@@ -281,7 +278,7 @@ static CborError preparse_value(CborValue *it)
281278
case 29:
282279
case 30:
283280
case Break:
284-
assert(false); /* these conditions can't be reached */
281+
cbor_assert(false); /* these conditions can't be reached */
285282
return CborErrorUnexpectedBreak;
286283
}
287284
return CborNoError;
@@ -323,11 +320,11 @@ static CborError advance_internal(CborValue *it)
323320
{
324321
uint64_t length;
325322
CborError err = _cbor_value_extract_number(&it->ptr, it->parser->end, &length);
326-
assert(err == CborNoError);
323+
cbor_assert(err == CborNoError);
327324

328325
if (it->type == CborByteStringType || it->type == CborTextStringType) {
329-
assert(length == (size_t)length);
330-
assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
326+
cbor_assert(length == (size_t)length);
327+
cbor_assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
331328
it->ptr += length;
332329
}
333330

@@ -346,16 +343,16 @@ static CborError advance_internal(CborValue *it)
346343
*/
347344
uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
348345
{
349-
assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
350-
value->type == CborFloatType || value->type == CborDoubleType);
346+
cbor_assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
347+
value->type == CborFloatType || value->type == CborDoubleType);
351348

352349
/* since the additional information can only be Value32Bit or Value64Bit,
353350
* we just need to test for the one bit those two options differ */
354-
assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
351+
cbor_assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
355352
if ((*value->ptr & 1) == (Value32Bit & 1))
356353
return get32(value->ptr + 1);
357354

358-
assert((*value->ptr & SmallValueMask) == Value64Bit);
355+
cbor_assert((*value->ptr & SmallValueMask) == Value64Bit);
359356
return get64(value->ptr + 1);
360357
}
361358

@@ -440,8 +437,8 @@ CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborPa
440437
*/
441438
CborError cbor_value_advance_fixed(CborValue *it)
442439
{
443-
assert(it->type != CborInvalidType);
444-
assert(is_fixed_type(it->type));
440+
cbor_assert(it->type != CborInvalidType);
441+
cbor_assert(is_fixed_type(it->type));
445442
if (!it->remaining)
446443
return CborErrorAdvancePastEOF;
447444
return advance_internal(it);
@@ -488,7 +485,7 @@ static CborError advance_recursive(CborValue *it, int nestingLevel)
488485
*/
489486
CborError cbor_value_advance(CborValue *it)
490487
{
491-
assert(it->type != CborInvalidType);
488+
cbor_assert(it->type != CborInvalidType);
492489
if (!it->remaining)
493490
return CborErrorAdvancePastEOF;
494491
return advance_recursive(it, 0);
@@ -552,7 +549,7 @@ CborError cbor_value_skip_tag(CborValue *it)
552549
CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
553550
{
554551
CborError err;
555-
assert(cbor_value_is_container(it));
552+
cbor_assert(cbor_value_is_container(it));
556553
*recursed = *it;
557554

558555
if (it->flags & CborIteratorFlag_UnknownLength) {
@@ -567,7 +564,7 @@ CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
567564
} else {
568565
uint64_t len;
569566
err = _cbor_value_extract_number(&recursed->ptr, recursed->parser->end, &len);
570-
assert(err == CborNoError);
567+
cbor_assert(err == CborNoError);
571568

572569
recursed->remaining = (uint32_t)len;
573570
if (recursed->remaining != len || len == UINT32_MAX) {
@@ -608,8 +605,8 @@ CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
608605
*/
609606
CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
610607
{
611-
assert(cbor_value_is_container(it));
612-
assert(recursed->type == CborInvalidType);
608+
cbor_assert(cbor_value_is_container(it));
609+
cbor_assert(recursed->type == CborInvalidType);
613610
it->ptr = recursed->ptr;
614611
return preparse_next_value(it);
615612
}
@@ -790,7 +787,7 @@ CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
790787
*/
791788
CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
792789
{
793-
assert(cbor_value_is_integer(value));
790+
cbor_assert(cbor_value_is_integer(value));
794791
uint64_t v = _cbor_value_extract_int64_helper(value);
795792

796793
/* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
@@ -829,7 +826,7 @@ CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
829826
*/
830827
CborError cbor_value_get_int_checked(const CborValue *value, int *result)
831828
{
832-
assert(cbor_value_is_integer(value));
829+
cbor_assert(cbor_value_is_integer(value));
833830
uint64_t v = _cbor_value_extract_int64_helper(value);
834831

835832
/* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
@@ -965,7 +962,7 @@ static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
965962
static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
966963
bool *result, CborValue *next, IterateFunction func)
967964
{
968-
assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
965+
cbor_assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
969966

970967
size_t total;
971968
CborError err;
@@ -1220,7 +1217,7 @@ CborError cbor_value_text_string_equals(const CborValue *value, const char *stri
12201217
*/
12211218
CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
12221219
{
1223-
assert(cbor_value_is_map(map));
1220+
cbor_assert(cbor_value_is_map(map));
12241221
size_t len = strlen(string);
12251222
CborError err = cbor_value_enter_container(map, element);
12261223
if (err)
@@ -1331,7 +1328,7 @@ CborError cbor_value_map_find_value(const CborValue *map, const char *string, Cb
13311328
*/
13321329
CborError cbor_value_get_half_float(const CborValue *value, void *result)
13331330
{
1334-
assert(cbor_value_is_half_float(value));
1331+
cbor_assert(cbor_value_is_half_float(value));
13351332

13361333
/* size has been computed already */
13371334
uint16_t v = get16(value->ptr + 1);

src/cborparser_dup_string.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#endif
3030

3131
#include "cbor.h"
32+
#include "compilersupport_p.h"
3233
#include <stdlib.h>
3334

3435
/**
@@ -91,8 +92,8 @@
9192
*/
9293
CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next)
9394
{
94-
assert(buffer);
95-
assert(buflen);
95+
cbor_assert(buffer);
96+
cbor_assert(buflen);
9697
*buflen = SIZE_MAX;
9798
CborError err = _cbor_value_copy_string(value, NULL, buflen, NULL);
9899
if (err)

src/cborpretty.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "cbor.h"
3232
#include "cborinternal_p.h"
3333
#include "compilersupport_p.h"
34-
#include "math_support_p.h"
3534

3635
#include <float.h>
3736
#include <inttypes.h>

src/cbortojson.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "cbor.h"
3434
#include "cborjson.h"
3535
#include "compilersupport_p.h"
36-
#include "math_support_p.h"
3736

3837
#include <float.h>
3938
#include <inttypes.h>
@@ -186,7 +185,7 @@ static CborError dump_bytestring_base16(char **result, CborValue *it)
186185
/* let cbor_value_copy_byte_string know we have an extra byte for the terminating NUL */
187186
++n;
188187
err = cbor_value_copy_byte_string(it, buffer + n - 1, &n, it);
189-
assert(err == CborNoError);
188+
cbor_assert(err == CborNoError);
190189

191190
for (i = 0; i < n; ++i) {
192191
uint8_t byte = buffer[n + i];
@@ -216,7 +215,7 @@ static CborError generic_dump_base64(char **result, CborValue *it, const char al
216215
/* let cbor_value_copy_byte_string know we have an extra byte for the terminating NUL */
217216
++n;
218217
err = cbor_value_copy_byte_string(it, in, &n, it);
219-
assert(err == CborNoError);
218+
cbor_assert(err == CborNoError);
220219

221220
uint_least32_t val = 0;
222221
for (i = 0; n - i >= 3; i += 3) {

src/compilersupport_p.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#endif
3636
#include <assert.h>
3737
#include <float.h>
38+
#include <math.h>
3839
#include <stddef.h>
3940
#include <stdint.h>
4041
#include <string.h>
@@ -61,6 +62,12 @@
6162
# define inline CBOR_INLINE
6263
#endif
6364

65+
#ifdef NDEBUG
66+
# define cbor_assert(cond) do { if (!(cond)) unreachable(); } while (0)
67+
#else
68+
# define cbor_assert(cond) assert(cond)
69+
#endif
70+
6471
#ifndef STRINGIFY
6572
#define STRINGIFY(x) STRINGIFY2(x)
6673
#endif
@@ -230,5 +237,21 @@ static inline unsigned short encode_half(double val)
230237
#endif
231238
}
232239

240+
/* this function was copied & adapted from RFC 7049 Appendix D */
241+
static inline double decode_half(unsigned short half)
242+
{
243+
#ifdef __F16C__
244+
return _cvtsh_ss(half);
245+
#else
246+
int exp = (half >> 10) & 0x1f;
247+
int mant = half & 0x3ff;
248+
double val;
249+
if (exp == 0) val = ldexp(mant, -24);
250+
else if (exp != 31) val = ldexp(mant + 1024, exp - 25);
251+
else val = mant == 0 ? INFINITY : NAN;
252+
return half & 0x8000 ? -val : val;
253+
#endif
254+
}
255+
233256
#endif /* COMPILERSUPPORT_H */
234257

src/math_support_p.h

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)