Skip to content

Commit c45469b

Browse files
committed
half/full-FP support changes
- Make half float encode/decode conditional - Change defaults for CBOR_WITHOUT_OPEN_MEMSTREAM, CBOR_NO_HALF_FLOAT_TYPE and CBOR_NO_FLOATING_POINT to y so that half float, float and open memstream support along with newlib libc does not get compiled in - src/cborpretty.c, src/cbortojson.c and src/cborvalidation.c conditionally include math.h and half float type support - Conditionally include math.h in src/compilersupport_p.h to avoid newlib libc from getting compiled in - Conditionally compile src/cborparser_dup_string.c if newlib libc is compiled in Signed-off-by: Vipul Rahane <[email protected]>
1 parent ece8b46 commit c45469b

File tree

9 files changed

+34
-20
lines changed

9 files changed

+34
-20
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ endif
101101
# version using funopen or fopencookie
102102
ifeq ($(open_memstream-pass),)
103103
ifeq ($(funopen-pass)$(fopencookie-pass),)
104-
CFLAGS += -DWITHOUT_OPEN_MEMSTREAM
104+
CFLAGS += -DCBOR_WITHOUT_OPEN_MEMSTREAM
105105
$(warning warning: funopen and fopencookie unavailable, open_memstream can not be implemented and conversion to JSON will not work properly!)
106106
else
107107
TINYCBOR_SOURCES += src/open_memstream.c

src/cbor.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ struct CborEncoder
208208
{
209209
cbor_encoder_writer *writer;
210210
void *writer_arg;
211-
#ifndef NO_DFLT_WRITER
211+
#ifndef CBOR_NO_DFLT_WRITER
212212
struct cbor_buf_writer wr;
213213
#endif
214214
size_t remaining;
@@ -219,7 +219,7 @@ typedef struct CborEncoder CborEncoder;
219219

220220
static const size_t CborIndefiniteLength = SIZE_MAX;
221221

222-
#ifndef NO_DFLT_WRITER
222+
#ifndef CBOR_NO_DFLT_WRITER
223223
CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags);
224224
#endif
225225
CBOR_API void cbor_encoder_cust_writer_init(CborEncoder *encoder, struct cbor_encoder_writer *w, int flags);
@@ -267,7 +267,7 @@ enum CborParserIteratorFlags
267267

268268
struct CborParser
269269
{
270-
#ifndef NO_DFLT_READER
270+
#ifndef CBOR_NO_DFLT_READER
271271
struct cbor_buf_reader br;
272272
#endif
273273
struct cbor_decoder_reader *d;
@@ -288,7 +288,7 @@ struct CborValue
288288
};
289289
typedef struct CborValue CborValue;
290290

291-
#ifndef NO_DFLT_READER
291+
#ifndef CBOR_NO_DFLT_READER
292292
CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it);
293293
#endif
294294
CBOR_API CborError cbor_parser_cust_reader_init(struct cbor_decoder_reader *r, int flags, CborParser *parser, CborValue *it);

src/cborencoder.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
* Structure used to encode to CBOR.
197197
*/
198198

199-
#ifndef NO_DFLT_WRITER
199+
#ifndef CBOR_NO_DFLT_WRITER
200200
/**
201201
* Initializes a CborEncoder structure \a encoder by pointing it to buffer \a
202202
* buffer of size \a size. The \a flags field is currently unused and must be
@@ -223,7 +223,7 @@ void cbor_encoder_cust_writer_init(CborEncoder *encoder, struct cbor_encoder_wri
223223
}
224224

225225

226-
#ifndef NO_FLOAT_SUPPORT
226+
#ifndef CBOR_NO_FLOATING_POINT
227227
static inline void put16(void *where, uint16_t v)
228228
{
229229
v = cbor_htons(v);
@@ -242,7 +242,7 @@ static inline bool isOomError(CborError err)
242242
return true;
243243
}
244244

245-
#ifndef NO_FLOAT_SUPPORT
245+
#ifndef CBOR_NO_FLOATING_POINT
246246
static inline void put32(void *where, uint32_t v)
247247
{
248248
v = cbor_htonl(v);
@@ -363,7 +363,7 @@ CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value)
363363
return encode_number(encoder, value, SimpleTypesType << MajorTypeShift);
364364
}
365365

366-
#ifndef NO_FLOAT_SUPPORT
366+
#ifndef CBOR_NO_FLOATING_POINT
367367
/**
368368
* Appends the floating-point value of type \a fpType and pointed to by \a
369369
* value to the CBOR stream provided by \a encoder. The value of \a fpType must
@@ -453,7 +453,7 @@ static CborError create_container(CborEncoder *encoder, CborEncoder *container,
453453
{
454454
CborError err;
455455
container->writer = encoder->writer;
456-
#ifndef NO_DFLT_WRITER
456+
#ifndef CBOR_NO_DFLT_WRITER
457457
container->wr.end = encoder->wr.end;
458458
#endif
459459
saturated_decrement(encoder);
@@ -540,7 +540,7 @@ CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *
540540
if (containerEncoder->remaining != 1)
541541
return containerEncoder->remaining == 0 ? CborErrorTooManyItems : CborErrorTooFewItems;
542542

543-
#ifndef NO_DFLT_WRITER
543+
#ifndef CBOR_NO_DFLT_WRITER
544544
if (!encoder->wr.end) {
545545
return CborErrorOutOfMemory;
546546
}

src/cborparser.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ static CborError preparse_next_value(CborValue *it)
303303
static CborError advance_internal(CborValue *it)
304304
{
305305
uint64_t length;
306-
CborError err = _cbor_value_extract_number(it->parser, &it->offset, &length);
306+
CborError err;
307+
err = _cbor_value_extract_number(it->parser, &it->offset, &length);
307308
cbor_assert(err == CborNoError);
308309

309310
if (it->type == CborByteStringType || it->type == CborTextStringType) {
@@ -342,7 +343,7 @@ uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
342343
return value->parser->d->get64(value->parser->d, value->offset + 1);
343344
}
344345

345-
#ifndef NO_DFLT_READER
346+
#ifndef CBOR_NO_DFLT_READER
346347
/**
347348
* Initializes the CBOR parser for parsing \a size bytes beginning at \a
348349
* buffer. Parsing will use flags set in \a flags. The iterator to the first

src/cborpretty.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
#include "cborinternal_p.h"
3434
#include "utf8_p.h"
3535

36-
#include <float.h>
3736
#include <inttypes.h>
37+
#include <float.h>
38+
#ifndef CBOR_NO_FLOATING_POINT
3839
#include <math.h>
40+
#endif
3941
#include <string.h>
4042

4143
/**
@@ -457,7 +459,7 @@ static CborError value_to_pretty(CborStreamFunction stream, void *out, CborValue
457459
err = stream(out, val ? "true" : "false");
458460
break;
459461
}
460-
#ifndef NO_FLOAT_SUPPORT
462+
#ifndef CBOR_NO_FLOATING_POINT
461463
case CborDoubleType: {
462464
const char *suffix;
463465
double val;
@@ -468,12 +470,14 @@ static CborError value_to_pretty(CborStreamFunction stream, void *out, CborValue
468470
cbor_value_get_float(it, &f);
469471
val = f;
470472
suffix = flags & CborPrettyNumericEncodingIndicators ? "_2" : "f";
473+
#ifndef CBOR_NO_HALF_FLOAT_TYPE
471474
} else if (false) {
472475
uint16_t f16;
473476
case CborHalfFloatType:
474477
cbor_value_get_half_float(it, &f16);
475478
val = decode_half(f16);
476479
suffix = flags & CborPrettyNumericEncodingIndicators ? "_1" : "f16";
480+
#endif
477481
} else {
478482
cbor_value_get_double(it, &val);
479483
suffix = "";

src/cbortojson.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636

3737
#include <float.h>
3838
#include <inttypes.h>
39+
#ifndef CBOR_NO_FLOATING_POINT
3940
#include <math.h>
41+
#endif
4042
#include <stdio.h>
4143
#include <stdlib.h>
4244
#include <string.h>
@@ -401,7 +403,7 @@ static CborError stringify_map_key(char **key, CborValue *it, int flags, CborTyp
401403
{
402404
(void)flags; /* unused */
403405
(void)type; /* unused */
404-
#ifdef WITHOUT_OPEN_MEMSTREAM
406+
#ifdef CBOR_WITHOUT_OPEN_MEMSTREAM
405407
(void)key; /* unused */
406408
(void)it; /* unused */
407409
return CborErrorJsonNotImplemented;
@@ -592,7 +594,7 @@ static CborError value_to_json(FILE *out, CborValue *it, int flags, CborType typ
592594
return CborErrorIO;
593595
break;
594596
}
595-
#ifndef NO_FLOAT_SUPPORT
597+
#ifndef CBOR_NO_FLOATING_POINT
596598
case CborDoubleType: {
597599
double val;
598600
if (false) {
@@ -601,12 +603,14 @@ static CborError value_to_json(FILE *out, CborValue *it, int flags, CborType typ
601603
status->flags = TypeWasNotNative;
602604
cbor_value_get_float(it, &f);
603605
val = f;
606+
#ifndef CBOR_NO_HALF_FLOAT_TYPE
604607
} else if (false) {
605608
uint16_t f16;
606609
case CborHalfFloatType:
607610
status->flags = TypeWasNotNative;
608611
cbor_value_get_half_float(it, &f16);
609612
val = decode_half(f16);
613+
#endif
610614
} else {
611615
cbor_value_get_double(it, &val);
612616
}

src/cborvalidation.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939

4040
#include <string.h>
4141

42+
#include <float.h>
4243
#ifndef CBOR_NO_FLOATING_POINT
43-
# include <float.h>
44-
# include <math.h>
44+
#include <math.h>
4545
#endif
4646

4747

src/compilersupport_p.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ extern "C" {
4141
# include <assert.h>
4242
#endif
4343
#include <float.h>
44+
#ifndef CBOR_NO_FLOATING_TYPE
4445
#include <math.h>
46+
#endif
4547
#include <stddef.h>
4648
#include <stdint.h>
4749
#include <string.h>
@@ -203,6 +205,7 @@ static inline bool add_check_overflow(size_t v1, size_t v2, size_t *r)
203205
#endif
204206
}
205207

208+
#ifndef CBOR_NO_HALF_FLOAT_TYPE
206209
static inline unsigned short encode_half(double val)
207210
{
208211
#ifdef __F16C__
@@ -255,6 +258,8 @@ static inline double decode_half(unsigned short half)
255258
#endif
256259
}
257260

261+
#endif
262+
258263
#ifdef __cplusplus
259264
}
260265
#endif

src/open_memstream.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define _DEFAULT_SOURCE 1
2727
#define _GNU_SOURCE 1
2828

29-
#ifndef WITHOUT_OPEN_MEMSTREAM
29+
#ifndef CBOR_WITHOUT_OPEN_MEMSTREAM
3030

3131
#include <sys/types.h>
3232
#include <errno.h>

0 commit comments

Comments
 (0)