libcbor 0.12.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
Loading...
Searching...
No Matches
floats_ctrls.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3 *
4 * libcbor is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
6 */
7
8#include "floats_ctrls.h"
9#include <math.h>
10#include "assert.h"
11
16
22
27
31 return *(float*)item->data;
32}
33
37 return *(float*)item->data;
38}
39
43 return *(double*)item->data;
44}
45
46double cbor_float_get_float(const cbor_item_t* item) {
48 switch (cbor_float_get_width(item)) {
49 case CBOR_FLOAT_0:
50 return NAN;
51 case CBOR_FLOAT_16:
52 return cbor_float_get_float2(item);
53 case CBOR_FLOAT_32:
54 return cbor_float_get_float4(item);
55 case CBOR_FLOAT_64:
56 return cbor_float_get_float8(item);
57 default:
59 return 0;
60 }
61}
62
67
68void cbor_set_float2(cbor_item_t* item, float value) {
71 *((float*)item->data) = value;
72}
73
74void cbor_set_float4(cbor_item_t* item, float value) {
77 *((float*)item->data) = value;
78}
79
80void cbor_set_float8(cbor_item_t* item, double value) {
83 *((double*)item->data) = value;
84}
85
91
92void cbor_set_bool(cbor_item_t* item, bool value) {
96}
97
99 cbor_item_t* item = _cbor_malloc(sizeof(cbor_item_t));
100 _CBOR_NOTNULL(item);
101
102 *item = (cbor_item_t){
103 .type = CBOR_TYPE_FLOAT_CTRL,
104 .data = NULL,
105 .refcount = 1,
106 .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_0,
107 .ctrl = CBOR_CTRL_NONE}}};
108 return item;
109}
110
112 cbor_item_t* item = _cbor_malloc(sizeof(cbor_item_t) + 4);
113 _CBOR_NOTNULL(item);
114
115 *item = (cbor_item_t){
116 .type = CBOR_TYPE_FLOAT_CTRL,
117 .data = (unsigned char*)item + sizeof(cbor_item_t),
118 .refcount = 1,
119 .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_16}}};
120 return item;
121}
122
124 cbor_item_t* item = _cbor_malloc(sizeof(cbor_item_t) + 4);
125 _CBOR_NOTNULL(item);
126
127 *item = (cbor_item_t){
128 .type = CBOR_TYPE_FLOAT_CTRL,
129 .data = (unsigned char*)item + sizeof(cbor_item_t),
130 .refcount = 1,
131 .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_32}}};
132 return item;
133}
134
136 cbor_item_t* item = _cbor_malloc(sizeof(cbor_item_t) + 8);
137 _CBOR_NOTNULL(item);
138
139 *item = (cbor_item_t){
140 .type = CBOR_TYPE_FLOAT_CTRL,
141 .data = (unsigned char*)item + sizeof(cbor_item_t),
142 .refcount = 1,
143 .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_64}}};
144 return item;
145}
146
148 cbor_item_t* item = cbor_new_ctrl();
149 _CBOR_NOTNULL(item);
151 return item;
152}
153
155 cbor_item_t* item = cbor_new_ctrl();
156 _CBOR_NOTNULL(item);
158 return item;
159}
160
164
167 _CBOR_NOTNULL(item);
168 cbor_set_float2(item, value);
169 return item;
170}
171
174 _CBOR_NOTNULL(item);
175 cbor_set_float4(item, value);
176 return item;
177}
178
181 _CBOR_NOTNULL(item);
182 cbor_set_float8(item, value);
183 return item;
184}
185
187 cbor_item_t* item = cbor_new_ctrl();
188 _CBOR_NOTNULL(item);
189 cbor_set_ctrl(item, value);
190 return item;
191}
_cbor_malloc_t _cbor_malloc
Definition allocators.c:10
bool cbor_is_float(const cbor_item_t *item)
Is the item an a floating point number?
Definition common.c:74
bool cbor_is_bool(const cbor_item_t *item)
Is the item an a boolean?
Definition common.c:60
bool cbor_isa_float_ctrl(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:50
#define _CBOR_UNREACHABLE
Definition common.h:99
#define _CBOR_NOTNULL(cbor_item)
Definition common.h:111
#define CBOR_ASSERT(e)
Definition common.h:73
@ CBOR_TYPE_FLOAT_CTRL
7 - decimals and special values (true, false, nil, ...)
Definition data.h:39
@ CBOR_CTRL_UNDEF
Definition data.h:88
@ CBOR_CTRL_NULL
Definition data.h:87
@ CBOR_CTRL_FALSE
Definition data.h:85
@ CBOR_CTRL_NONE
Definition data.h:84
@ CBOR_CTRL_TRUE
Definition data.h:86
cbor_float_width
Possible widths of CBOR_TYPE_FLOAT_CTRL items.
Definition data.h:66
@ CBOR_FLOAT_32
Single float.
Definition data.h:71
@ CBOR_FLOAT_16
Half float.
Definition data.h:69
@ CBOR_FLOAT_64
Double.
Definition data.h:73
@ CBOR_FLOAT_0
Internal use - ctrl and special values.
Definition data.h:67
cbor_float_width cbor_float_get_width(const cbor_item_t *item)
Get the float width.
cbor_item_t * cbor_build_ctrl(uint8_t value)
Constructs a ctrl item.
cbor_item_t * cbor_new_undef(void)
Constructs new undef ctrl item.
bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item)
Is this a ctrl value?
double cbor_float_get_float8(const cbor_item_t *item)
Get a double precision float.
cbor_item_t * cbor_new_float2(void)
Constructs a new float item.
cbor_item_t * cbor_new_float8(void)
Constructs a new float item.
void cbor_set_ctrl(cbor_item_t *item, uint8_t value)
Assign a control value.
void cbor_set_float4(cbor_item_t *item, float value)
Assigns a float value.
void cbor_set_float8(cbor_item_t *item, double value)
Assigns a float value.
cbor_item_t * cbor_new_ctrl(void)
Constructs a new ctrl item.
cbor_item_t * cbor_build_bool(bool value)
Constructs new boolean ctrl item.
cbor_item_t * cbor_build_float4(float value)
Constructs a new float.
cbor_item_t * cbor_new_null(void)
Constructs new null ctrl item.
uint8_t cbor_ctrl_value(const cbor_item_t *item)
Reads the control value.
void cbor_set_float2(cbor_item_t *item, float value)
Assigns a float value.
float cbor_float_get_float2(const cbor_item_t *item)
Get a half precision float.
cbor_item_t * cbor_build_float8(double value)
Constructs a new float.
cbor_item_t * cbor_new_float4(void)
Constructs a new float item.
void cbor_set_bool(cbor_item_t *item, bool value)
Assign a boolean value to a boolean ctrl item.
float cbor_float_get_float4(const cbor_item_t *item)
Get a single precision float.
double cbor_float_get_float(const cbor_item_t *item)
Get the float value represented as double.
cbor_item_t * cbor_build_float2(float value)
Constructs a new float.
bool cbor_get_bool(const cbor_item_t *item)
Get value from a boolean ctrl item.
cbor_float_width width
Definition data.h:142
The item handle.
Definition data.h:171
unsigned char * data
Raw data block - interpretation depends on metadata.
Definition data.h:179
size_t refcount
Reference count - initialize to 0.
Definition data.h:175
union cbor_item_metadata metadata
Discriminated by type.
Definition data.h:173
struct _cbor_float_ctrl_metadata float_ctrl_metadata
Definition data.h:167