libcbor  0.5.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
floats_ctrls.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2017 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 "assert.h"
10 #include <math.h>
11 
13 {
14  assert(cbor_isa_float_ctrl(item));
15  return item->metadata.float_ctrl_metadata.width;
16 }
17 
18 uint8_t cbor_ctrl_value(const cbor_item_t *item)
19 {
20  assert(cbor_isa_float_ctrl(item));
21  assert(cbor_float_get_width(item) == CBOR_FLOAT_0);
22  return item->metadata.float_ctrl_metadata.ctrl;
23 }
24 
26 {
27  assert(cbor_isa_float_ctrl(item));
28  return cbor_float_get_width(item) == CBOR_FLOAT_0;
29 }
30 
32 {
33  assert(cbor_is_float(item));
34  assert(cbor_float_get_width(item) == CBOR_FLOAT_16);
35  return *(float *) item->data;
36 }
37 
39 {
40  assert(cbor_is_float(item));
41  assert(cbor_float_get_width(item) == CBOR_FLOAT_32);
42  return *(float *) item->data;
43 }
44 
46 {
47  assert(cbor_is_float(item));
48  assert(cbor_float_get_width(item) == CBOR_FLOAT_64);
49  return *(double *) item->data;
50 }
51 
52 double cbor_float_get_float(const cbor_item_t * item)
53 {
54  assert(cbor_is_float(item));
55  switch(cbor_float_get_width(item)) {
56  case CBOR_FLOAT_0: return NAN;
57  case CBOR_FLOAT_16: return cbor_float_get_float2(item);
58  case CBOR_FLOAT_32: return cbor_float_get_float4(item);
59  case CBOR_FLOAT_64: return cbor_float_get_float8(item);
60  }
61  return NAN; /* Compiler complaints */
62 }
63 
64 void cbor_set_float2(cbor_item_t *item, float value)
65 {
66  assert(cbor_is_float(item));
67  assert(cbor_float_get_width(item) == CBOR_FLOAT_16);
68  *((float *) item->data) = value;
69 }
70 
71 void cbor_set_float4(cbor_item_t *item, float value)
72 {
73  assert(cbor_is_float(item));
74  assert(cbor_float_get_width(item) == CBOR_FLOAT_32);
75  *((float *) item->data) = value;
76 }
77 
78 void cbor_set_float8(cbor_item_t *item, double value)
79 {
80  assert(cbor_is_float(item));
81  assert(cbor_float_get_width(item) == CBOR_FLOAT_64);
82  *((double *) item->data) = value;
83 }
84 
85 void cbor_set_ctrl(cbor_item_t *item, uint8_t value)
86 {
87  assert(cbor_isa_float_ctrl(item));
88  assert(cbor_float_get_width(item) == CBOR_FLOAT_0);
89  item->metadata.float_ctrl_metadata.ctrl = value;
90 }
91 
92 bool cbor_ctrl_is_bool(const cbor_item_t *item)
93 {
94  assert(cbor_is_bool(item));
96 }
97 
99 {
100  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
101  *item = (cbor_item_t) {
103  .data = NULL,
104  .refcount = 1,
105  .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_0, .ctrl = CBOR_CTRL_NONE}}
106  };
107  return item;
108 }
109 
111 {
112  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 4);
113  *item = (cbor_item_t) {
115  .data = (unsigned char *) item + sizeof(cbor_item_t),
116  .refcount = 1,
117  .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_16}}
118  };
119  return item;
120 }
121 
123 {
124  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 4);
125  *item = (cbor_item_t) {
127  .data = (unsigned char *) item + sizeof(cbor_item_t),
128  .refcount = 1,
129  .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_32}}
130  };
131  return item;
132 }
133 
135 {
136  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 8);
137  *item = (cbor_item_t) {
139  .data = (unsigned char *) item + sizeof(cbor_item_t),
140  .refcount = 1,
141  .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_64}}
142  };
143  return item;
144 }
145 
147 {
148  cbor_item_t *item = cbor_new_ctrl();
150  return item;
151 }
152 
154 {
155  cbor_item_t *item = cbor_new_ctrl();
157  return item;
158 }
159 
161 {
163 }
164 
166 {
167  cbor_item_t *item = cbor_new_float2();
168  cbor_set_float2(item, value);
169  return item;
170 }
171 
173 {
174  cbor_item_t *item = cbor_new_float4();
175  cbor_set_float4(item, value);
176  return item;
177 }
178 
180 {
181  cbor_item_t *item = cbor_new_float8();
182  cbor_set_float8(item, value);
183  return item;
184 }
185 
187 {
188  cbor_item_t *item = cbor_new_ctrl();
189  cbor_set_ctrl(item, value);
190  return item;
191 }
struct cbor_item_t cbor_item_t
The item handle.
void cbor_set_float4(cbor_item_t *item, float value)
Assigns a float value.
Definition: floats_ctrls.c:71
void cbor_set_float2(cbor_item_t *item, float value)
Assigns a float value.
Definition: floats_ctrls.c:64
struct _cbor_float_ctrl_metadata float_ctrl_metadata
Definition: data.h:145
cbor_float_width
Possible widths of CBOR_TYPE_FLOAT_CTRL items.
Definition: data.h:54
double cbor_float_get_float(const cbor_item_t *item)
Get the float value represented as double.
Definition: floats_ctrls.c:52
union cbor_item_metadata metadata
Discriminated by type.
Definition: data.h:151
Internal use - ctrl and special values.
Definition: data.h:55
cbor_item_t * cbor_build_float2(float value)
Constructs a new float.
Definition: floats_ctrls.c:165
cbor_item_t * cbor_new_undef()
Constructs new under ctrl item.
Definition: floats_ctrls.c:153
cbor_item_t * cbor_build_ctrl(uint8_t value)
Constructs a ctrl item.
Definition: floats_ctrls.c:186
cbor_item_t * cbor_build_bool(bool value)
Constructs new boolean ctrl item.
Definition: floats_ctrls.c:160
cbor_type type
Major type discriminator.
Definition: data.h:155
7 - decimals and special values (true, false, nil, ...)
Definition: data.h:32
#define _CBOR_MALLOC
Definition: common.h:84
size_t refcount
Reference count - initialize to 0.
Definition: data.h:153
void cbor_set_ctrl(cbor_item_t *item, uint8_t value)
Assign a control value.
Definition: floats_ctrls.c:85
cbor_item_t * cbor_build_float4(float value)
Constructs a new float.
Definition: floats_ctrls.c:172
Double.
Definition: data.h:58
Half float.
Definition: data.h:56
cbor_item_t * cbor_new_ctrl()
Constructs a new ctrl item.
Definition: floats_ctrls.c:98
cbor_item_t * cbor_new_float4()
Constructs a new float item.
Definition: floats_ctrls.c:122
bool cbor_is_float(const cbor_item_t *item)
Is the item an a floating point number?
Definition: common.c:87
float cbor_float_get_float4(const cbor_item_t *item)
Get a single precision float.
Definition: floats_ctrls.c:38
uint8_t cbor_ctrl_value(const cbor_item_t *item)
Reads the control value.
Definition: floats_ctrls.c:18
cbor_item_t * cbor_new_float8()
Constructs a new float item.
Definition: floats_ctrls.c:134
cbor_float_width cbor_float_get_width(const cbor_item_t *item)
Get the float width.
Definition: floats_ctrls.c:12
bool cbor_isa_float_ctrl(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:53
void cbor_set_float8(cbor_item_t *item, double value)
Assigns a float value.
Definition: floats_ctrls.c:78
float cbor_float_get_float2(const cbor_item_t *item)
Get a half precision float.
Definition: floats_ctrls.c:31
cbor_item_t * cbor_build_float8(double value)
Constructs a new float.
Definition: floats_ctrls.c:179
cbor_item_t * cbor_new_null()
Constructs new null ctrl item.
Definition: floats_ctrls.c:146
cbor_float_width width
Definition: data.h:121
double cbor_float_get_float8(const cbor_item_t *item)
Get a double precision float.
Definition: floats_ctrls.c:45
bool cbor_is_bool(const cbor_item_t *item)
Is the item an a boolean?
Definition: common.c:71
cbor_item_t * cbor_new_float2()
Constructs a new float item.
Definition: floats_ctrls.c:110
bool cbor_ctrl_is_bool(const cbor_item_t *item)
Is this ctrl item a boolean?
Definition: floats_ctrls.c:92
unsigned char * data
Raw data block - interpretation depends on metadata.
Definition: data.h:157
bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item)
Is this a ctrl value?
Definition: floats_ctrls.c:25
The item handle.
Definition: data.h:149
Single float.
Definition: data.h:57