110ff414cSEd Maste /* 210ff414cSEd Maste * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com> 310ff414cSEd Maste * 410ff414cSEd Maste * libcbor is free software; you can redistribute it and/or modify 510ff414cSEd Maste * it under the terms of the MIT license. See LICENSE for details. 610ff414cSEd Maste */ 710ff414cSEd Maste 810ff414cSEd Maste #ifndef LIBCBOR_FLOATS_CTRLS_H 910ff414cSEd Maste #define LIBCBOR_FLOATS_CTRLS_H 1010ff414cSEd Maste 1110ff414cSEd Maste #include "cbor/cbor_export.h" 1210ff414cSEd Maste #include "cbor/common.h" 1310ff414cSEd Maste 1410ff414cSEd Maste #ifdef __cplusplus 1510ff414cSEd Maste extern "C" { 1610ff414cSEd Maste #endif 1710ff414cSEd Maste 1810ff414cSEd Maste /* 1910ff414cSEd Maste * ============================================================================ 2010ff414cSEd Maste * Float manipulation 2110ff414cSEd Maste * ============================================================================ 2210ff414cSEd Maste */ 2310ff414cSEd Maste 2410ff414cSEd Maste /** Is this a ctrl value? 2510ff414cSEd Maste * 26*5d3e7166SEd Maste * @param item A float or ctrl item 2710ff414cSEd Maste * @return Is this a ctrl value? 2810ff414cSEd Maste */ 29*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT bool cbor_float_ctrl_is_ctrl( 30*5d3e7166SEd Maste const cbor_item_t *item); 3110ff414cSEd Maste 3210ff414cSEd Maste /** Get the float width 3310ff414cSEd Maste * 34*5d3e7166SEd Maste * @param item A float or ctrl item 3510ff414cSEd Maste * @return The width. 3610ff414cSEd Maste */ 37*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_float_width 38*5d3e7166SEd Maste cbor_float_get_width(const cbor_item_t *item); 3910ff414cSEd Maste 4010ff414cSEd Maste /** Get a half precision float 4110ff414cSEd Maste * 4210ff414cSEd Maste * The item must have the corresponding width 4310ff414cSEd Maste * 44*5d3e7166SEd Maste * @param item A half precision float 4510ff414cSEd Maste * @return half precision value 4610ff414cSEd Maste */ 47*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT float cbor_float_get_float2( 48*5d3e7166SEd Maste const cbor_item_t *item); 4910ff414cSEd Maste 5010ff414cSEd Maste /** Get a single precision float 5110ff414cSEd Maste * 5210ff414cSEd Maste * The item must have the corresponding width 5310ff414cSEd Maste * 54*5d3e7166SEd Maste * @param item A single precision float 5510ff414cSEd Maste * @return single precision value 5610ff414cSEd Maste */ 57*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT float cbor_float_get_float4( 58*5d3e7166SEd Maste const cbor_item_t *item); 5910ff414cSEd Maste 6010ff414cSEd Maste /** Get a double precision float 6110ff414cSEd Maste * 6210ff414cSEd Maste * The item must have the corresponding width 6310ff414cSEd Maste * 64*5d3e7166SEd Maste * @param item A double precision float 6510ff414cSEd Maste * @return double precision value 6610ff414cSEd Maste */ 67*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT double cbor_float_get_float8( 68*5d3e7166SEd Maste const cbor_item_t *item); 6910ff414cSEd Maste 7010ff414cSEd Maste /** Get the float value represented as double 7110ff414cSEd Maste * 7210ff414cSEd Maste * Can be used regardless of the width. 7310ff414cSEd Maste * 74*5d3e7166SEd Maste * @param item Any float 7510ff414cSEd Maste * @return double precision value 7610ff414cSEd Maste */ 77*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT double cbor_float_get_float( 78*5d3e7166SEd Maste const cbor_item_t *item); 7910ff414cSEd Maste 8010ff414cSEd Maste /** Get value from a boolean ctrl item 8110ff414cSEd Maste * 82*5d3e7166SEd Maste * @param item A ctrl item 8310ff414cSEd Maste * @return boolean value 8410ff414cSEd Maste */ 85*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT bool cbor_get_bool(const cbor_item_t *item); 8610ff414cSEd Maste 8710ff414cSEd Maste /** Constructs a new ctrl item 8810ff414cSEd Maste * 8910ff414cSEd Maste * The width cannot be changed once the item is created 9010ff414cSEd Maste * 91*5d3e7166SEd Maste * @return Reference to the new ctrl item. The item's reference count is 92*5d3e7166SEd Maste * initialized to one. 93*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 9410ff414cSEd Maste */ 95*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_ctrl(void); 9610ff414cSEd Maste 9710ff414cSEd Maste /** Constructs a new float item 9810ff414cSEd Maste * 9910ff414cSEd Maste * The width cannot be changed once the item is created 10010ff414cSEd Maste * 101*5d3e7166SEd Maste * @return Reference to the new float item. The item's reference count is 102*5d3e7166SEd Maste * initialized to one. 103*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 10410ff414cSEd Maste */ 105*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float2(void); 10610ff414cSEd Maste 10710ff414cSEd Maste /** Constructs a new float item 10810ff414cSEd Maste * 10910ff414cSEd Maste * The width cannot be changed once the item is created 11010ff414cSEd Maste * 111*5d3e7166SEd Maste * @return Reference to the new float item. The item's reference count is 112*5d3e7166SEd Maste * initialized to one. 113*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 11410ff414cSEd Maste */ 115*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float4(void); 11610ff414cSEd Maste 11710ff414cSEd Maste /** Constructs a new float item 11810ff414cSEd Maste * 11910ff414cSEd Maste * The width cannot be changed once the item is created 12010ff414cSEd Maste * 121*5d3e7166SEd Maste * @return Reference to the new float item. The item's reference count is 122*5d3e7166SEd Maste * initialized to one. 123*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 12410ff414cSEd Maste */ 125*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float8(void); 12610ff414cSEd Maste 12710ff414cSEd Maste /** Constructs new null ctrl item 12810ff414cSEd Maste * 129*5d3e7166SEd Maste * @return Reference to the new null item. The item's reference count is 130*5d3e7166SEd Maste * initialized to one. 131*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 13210ff414cSEd Maste */ 133*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_null(void); 13410ff414cSEd Maste 13510ff414cSEd Maste /** Constructs new undef ctrl item 13610ff414cSEd Maste * 137*5d3e7166SEd Maste * @return Reference to the new undef item. The item's reference count is 138*5d3e7166SEd Maste * initialized to one. 139*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 14010ff414cSEd Maste */ 141*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_undef(void); 14210ff414cSEd Maste 14310ff414cSEd Maste /** Constructs new boolean ctrl item 14410ff414cSEd Maste * 14510ff414cSEd Maste * @param value The value to use 146*5d3e7166SEd Maste * @return Reference to the new boolean item. The item's reference count is 147*5d3e7166SEd Maste * initialized to one. 148*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 14910ff414cSEd Maste */ 150*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_bool(bool value); 15110ff414cSEd Maste 15210ff414cSEd Maste /** Assign a control value 15310ff414cSEd Maste * 15410ff414cSEd Maste * \rst 15510ff414cSEd Maste * .. warning:: It is possible to produce an invalid CBOR value by assigning a 15610ff414cSEd Maste * invalid value using this mechanism. Please consult the standard before use. 15710ff414cSEd Maste * \endrst 15810ff414cSEd Maste * 159*5d3e7166SEd Maste * @param item A ctrl item 16010ff414cSEd Maste * @param value The simple value to assign. Please consult the standard for 16110ff414cSEd Maste * allowed values 16210ff414cSEd Maste */ 16310ff414cSEd Maste CBOR_EXPORT void cbor_set_ctrl(cbor_item_t *item, uint8_t value); 16410ff414cSEd Maste 16510ff414cSEd Maste /** Assign a boolean value to a boolean ctrl item 16610ff414cSEd Maste * 167*5d3e7166SEd Maste * @param item A ctrl item 16810ff414cSEd Maste * @param value The simple value to assign. 16910ff414cSEd Maste */ 17010ff414cSEd Maste CBOR_EXPORT void cbor_set_bool(cbor_item_t *item, bool value); 17110ff414cSEd Maste 17210ff414cSEd Maste /** Assigns a float value 17310ff414cSEd Maste * 174*5d3e7166SEd Maste * @param item A half precision float 17510ff414cSEd Maste * @param value The value to assign 17610ff414cSEd Maste */ 17710ff414cSEd Maste CBOR_EXPORT void cbor_set_float2(cbor_item_t *item, float value); 17810ff414cSEd Maste 17910ff414cSEd Maste /** Assigns a float value 18010ff414cSEd Maste * 181*5d3e7166SEd Maste * @param item A single precision float 18210ff414cSEd Maste * @param value The value to assign 18310ff414cSEd Maste */ 18410ff414cSEd Maste CBOR_EXPORT void cbor_set_float4(cbor_item_t *item, float value); 18510ff414cSEd Maste 18610ff414cSEd Maste /** Assigns a float value 18710ff414cSEd Maste * 188*5d3e7166SEd Maste * @param item A double precision float 18910ff414cSEd Maste * @param value The value to assign 19010ff414cSEd Maste */ 19110ff414cSEd Maste CBOR_EXPORT void cbor_set_float8(cbor_item_t *item, double value); 19210ff414cSEd Maste 19310ff414cSEd Maste /** Reads the control value 19410ff414cSEd Maste * 195*5d3e7166SEd Maste * @param item A ctrl item 19610ff414cSEd Maste * @return the simple value 19710ff414cSEd Maste */ 198*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_ctrl_value(const cbor_item_t *item); 19910ff414cSEd Maste 20010ff414cSEd Maste /** Constructs a new float 20110ff414cSEd Maste * 20210ff414cSEd Maste * @param value the value to use 203*5d3e7166SEd Maste * @return Reference to the new float item. The item's reference count is 204*5d3e7166SEd Maste * initialized to one. 205*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 20610ff414cSEd Maste */ 207*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float2(float value); 20810ff414cSEd Maste 20910ff414cSEd Maste /** Constructs a new float 21010ff414cSEd Maste * 21110ff414cSEd Maste * @param value the value to use 212*5d3e7166SEd Maste * @return Reference to the new float item. The item's reference count is 213*5d3e7166SEd Maste * initialized to one. 214*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 21510ff414cSEd Maste */ 216*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float4(float value); 21710ff414cSEd Maste 21810ff414cSEd Maste /** Constructs a new float 21910ff414cSEd Maste * 22010ff414cSEd Maste * @param value the value to use 221*5d3e7166SEd Maste * @return Reference to the new float item. The item's reference count is 222*5d3e7166SEd Maste * initialized to one. 223*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 22410ff414cSEd Maste */ 225*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float8(double value); 22610ff414cSEd Maste 22710ff414cSEd Maste /** Constructs a ctrl item 22810ff414cSEd Maste * 22910ff414cSEd Maste * @param value the value to use 230*5d3e7166SEd Maste * @return Reference to the new ctrl item. The item's reference count is 231*5d3e7166SEd Maste * initialized to one. 232*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 23310ff414cSEd Maste */ 234*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_ctrl(uint8_t value); 23510ff414cSEd Maste 23610ff414cSEd Maste #ifdef __cplusplus 23710ff414cSEd Maste } 23810ff414cSEd Maste #endif 23910ff414cSEd Maste 24010ff414cSEd Maste #endif // LIBCBOR_FLOATS_CTRLS_H 241