/src/libcbor/src/cbor/floats_ctrls.c
Line | Count | Source |
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 | | |
12 | 67.0k | cbor_float_width cbor_float_get_width(const cbor_item_t *item) { |
13 | 67.0k | CBOR_ASSERT(cbor_isa_float_ctrl(item)); |
14 | 67.0k | return item->metadata.float_ctrl_metadata.width; |
15 | 67.0k | } |
16 | | |
17 | 67.0k | uint8_t cbor_ctrl_value(const cbor_item_t *item) { |
18 | 67.0k | CBOR_ASSERT(cbor_isa_float_ctrl(item)); |
19 | 67.0k | CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_0); |
20 | 67.0k | return item->metadata.float_ctrl_metadata.ctrl; |
21 | 67.0k | } |
22 | | |
23 | 0 | bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item) { |
24 | 0 | CBOR_ASSERT(cbor_isa_float_ctrl(item)); |
25 | 0 | return cbor_float_get_width(item) == CBOR_FLOAT_0; |
26 | 0 | } |
27 | | |
28 | 0 | float cbor_float_get_float2(const cbor_item_t *item) { |
29 | 0 | CBOR_ASSERT(cbor_is_float(item)); |
30 | 0 | CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_16); |
31 | 0 | return *(float *)item->data; |
32 | 0 | } |
33 | | |
34 | 0 | float cbor_float_get_float4(const cbor_item_t *item) { |
35 | 0 | CBOR_ASSERT(cbor_is_float(item)); |
36 | 0 | CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_32); |
37 | 0 | return *(float *)item->data; |
38 | 0 | } |
39 | | |
40 | 0 | double cbor_float_get_float8(const cbor_item_t *item) { |
41 | 0 | CBOR_ASSERT(cbor_is_float(item)); |
42 | 0 | CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_64); |
43 | 0 | return *(double *)item->data; |
44 | 0 | } |
45 | | |
46 | 0 | double cbor_float_get_float(const cbor_item_t *item) { |
47 | 0 | CBOR_ASSERT(cbor_is_float(item)); |
48 | | // cppcheck-suppress missingReturn |
49 | 0 | switch (cbor_float_get_width(item)) { |
50 | 0 | case CBOR_FLOAT_0: |
51 | 0 | return NAN; |
52 | 0 | case CBOR_FLOAT_16: |
53 | 0 | return cbor_float_get_float2(item); |
54 | 0 | case CBOR_FLOAT_32: |
55 | 0 | return cbor_float_get_float4(item); |
56 | 0 | case CBOR_FLOAT_64: |
57 | 0 | return cbor_float_get_float8(item); |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | 0 | bool cbor_get_bool(const cbor_item_t *item) { |
62 | 0 | CBOR_ASSERT(cbor_is_bool(item)); |
63 | 0 | return item->metadata.float_ctrl_metadata.ctrl == CBOR_CTRL_TRUE; |
64 | 0 | } |
65 | | |
66 | 14.4k | void cbor_set_float2(cbor_item_t *item, float value) { |
67 | 14.4k | CBOR_ASSERT(cbor_is_float(item)); |
68 | 14.4k | CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_16); |
69 | 14.4k | *((float *)item->data) = value; |
70 | 14.4k | } |
71 | | |
72 | 7.55k | void cbor_set_float4(cbor_item_t *item, float value) { |
73 | 7.55k | CBOR_ASSERT(cbor_is_float(item)); |
74 | 7.55k | CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_32); |
75 | 7.55k | *((float *)item->data) = value; |
76 | 7.55k | } |
77 | | |
78 | 3.89k | void cbor_set_float8(cbor_item_t *item, double value) { |
79 | 3.89k | CBOR_ASSERT(cbor_is_float(item)); |
80 | 3.89k | CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_64); |
81 | 3.89k | *((double *)item->data) = value; |
82 | 3.89k | } |
83 | | |
84 | 188k | void cbor_set_ctrl(cbor_item_t *item, uint8_t value) { |
85 | 188k | CBOR_ASSERT(cbor_isa_float_ctrl(item)); |
86 | 188k | CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_0); |
87 | 188k | item->metadata.float_ctrl_metadata.ctrl = value; |
88 | 188k | } |
89 | | |
90 | 0 | void cbor_set_bool(cbor_item_t *item, bool value) { |
91 | 0 | CBOR_ASSERT(cbor_is_bool(item)); |
92 | 0 | item->metadata.float_ctrl_metadata.ctrl = |
93 | 0 | value ? CBOR_CTRL_TRUE : CBOR_CTRL_FALSE; |
94 | 0 | } |
95 | | |
96 | 188k | cbor_item_t *cbor_new_ctrl(void) { |
97 | 188k | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t)); |
98 | 188k | _CBOR_NOTNULL(item); |
99 | | |
100 | 188k | *item = (cbor_item_t){ |
101 | 188k | .type = CBOR_TYPE_FLOAT_CTRL, |
102 | 188k | .data = NULL, |
103 | 188k | .refcount = 1, |
104 | 188k | .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_0, |
105 | 188k | .ctrl = CBOR_CTRL_NONE}}}; |
106 | 188k | return item; |
107 | 188k | } |
108 | | |
109 | 14.4k | cbor_item_t *cbor_new_float2(void) { |
110 | 14.4k | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 4); |
111 | 14.4k | _CBOR_NOTNULL(item); |
112 | | |
113 | 14.4k | *item = (cbor_item_t){ |
114 | 14.4k | .type = CBOR_TYPE_FLOAT_CTRL, |
115 | 14.4k | .data = (unsigned char *)item + sizeof(cbor_item_t), |
116 | 14.4k | .refcount = 1, |
117 | 14.4k | .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_16}}}; |
118 | 14.4k | return item; |
119 | 14.4k | } |
120 | | |
121 | 7.55k | cbor_item_t *cbor_new_float4(void) { |
122 | 7.55k | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 4); |
123 | 7.55k | _CBOR_NOTNULL(item); |
124 | | |
125 | 7.55k | *item = (cbor_item_t){ |
126 | 7.55k | .type = CBOR_TYPE_FLOAT_CTRL, |
127 | 7.55k | .data = (unsigned char *)item + sizeof(cbor_item_t), |
128 | 7.55k | .refcount = 1, |
129 | 7.55k | .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_32}}}; |
130 | 7.55k | return item; |
131 | 7.55k | } |
132 | | |
133 | 3.89k | cbor_item_t *cbor_new_float8(void) { |
134 | 3.89k | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 8); |
135 | 3.89k | _CBOR_NOTNULL(item); |
136 | | |
137 | 3.89k | *item = (cbor_item_t){ |
138 | 3.89k | .type = CBOR_TYPE_FLOAT_CTRL, |
139 | 3.89k | .data = (unsigned char *)item + sizeof(cbor_item_t), |
140 | 3.89k | .refcount = 1, |
141 | 3.89k | .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_64}}}; |
142 | 3.89k | return item; |
143 | 3.89k | } |
144 | | |
145 | 5.11k | cbor_item_t *cbor_new_null(void) { |
146 | 5.11k | cbor_item_t *item = cbor_new_ctrl(); |
147 | 5.11k | _CBOR_NOTNULL(item); |
148 | 5.11k | cbor_set_ctrl(item, CBOR_CTRL_NULL); |
149 | 5.11k | return item; |
150 | 5.11k | } |
151 | | |
152 | 121k | cbor_item_t *cbor_new_undef(void) { |
153 | 121k | cbor_item_t *item = cbor_new_ctrl(); |
154 | 121k | _CBOR_NOTNULL(item); |
155 | 121k | cbor_set_ctrl(item, CBOR_CTRL_UNDEF); |
156 | 121k | return item; |
157 | 121k | } |
158 | | |
159 | 61.6k | cbor_item_t *cbor_build_bool(bool value) { |
160 | 61.6k | return cbor_build_ctrl(value ? CBOR_CTRL_TRUE : CBOR_CTRL_FALSE); |
161 | 61.6k | } |
162 | | |
163 | 0 | cbor_item_t *cbor_build_float2(float value) { |
164 | 0 | cbor_item_t *item = cbor_new_float2(); |
165 | 0 | _CBOR_NOTNULL(item); |
166 | 0 | cbor_set_float2(item, value); |
167 | 0 | return item; |
168 | 0 | } |
169 | | |
170 | 0 | cbor_item_t *cbor_build_float4(float value) { |
171 | 0 | cbor_item_t *item = cbor_new_float4(); |
172 | 0 | _CBOR_NOTNULL(item); |
173 | 0 | cbor_set_float4(item, value); |
174 | 0 | return item; |
175 | 0 | } |
176 | | |
177 | 0 | cbor_item_t *cbor_build_float8(double value) { |
178 | 0 | cbor_item_t *item = cbor_new_float8(); |
179 | 0 | _CBOR_NOTNULL(item); |
180 | 0 | cbor_set_float8(item, value); |
181 | 0 | return item; |
182 | 0 | } |
183 | | |
184 | 61.6k | cbor_item_t *cbor_build_ctrl(uint8_t value) { |
185 | 61.6k | cbor_item_t *item = cbor_new_ctrl(); |
186 | 61.6k | _CBOR_NOTNULL(item); |
187 | 61.6k | cbor_set_ctrl(item, value); |
188 | 61.6k | return item; |
189 | 61.6k | } |