Path: blob/main/contrib/libcbor/src/cbor/floats_ctrls.c
39507 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#include "floats_ctrls.h"8#include <math.h>9#include "assert.h"1011cbor_float_width cbor_float_get_width(const cbor_item_t *item) {12CBOR_ASSERT(cbor_isa_float_ctrl(item));13return item->metadata.float_ctrl_metadata.width;14}1516uint8_t cbor_ctrl_value(const cbor_item_t *item) {17CBOR_ASSERT(cbor_isa_float_ctrl(item));18CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_0);19return item->metadata.float_ctrl_metadata.ctrl;20}2122bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item) {23CBOR_ASSERT(cbor_isa_float_ctrl(item));24return cbor_float_get_width(item) == CBOR_FLOAT_0;25}2627float cbor_float_get_float2(const cbor_item_t *item) {28CBOR_ASSERT(cbor_is_float(item));29CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_16);30return *(float *)item->data;31}3233float cbor_float_get_float4(const cbor_item_t *item) {34CBOR_ASSERT(cbor_is_float(item));35CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_32);36return *(float *)item->data;37}3839double cbor_float_get_float8(const cbor_item_t *item) {40CBOR_ASSERT(cbor_is_float(item));41CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_64);42return *(double *)item->data;43}4445double cbor_float_get_float(const cbor_item_t *item) {46CBOR_ASSERT(cbor_is_float(item));47// cppcheck-suppress missingReturn48switch (cbor_float_get_width(item)) {49case CBOR_FLOAT_0:50return NAN;51case CBOR_FLOAT_16:52return cbor_float_get_float2(item);53case CBOR_FLOAT_32:54return cbor_float_get_float4(item);55case CBOR_FLOAT_64:56return cbor_float_get_float8(item);57}58}5960bool cbor_get_bool(const cbor_item_t *item) {61CBOR_ASSERT(cbor_is_bool(item));62return item->metadata.float_ctrl_metadata.ctrl == CBOR_CTRL_TRUE;63}6465void cbor_set_float2(cbor_item_t *item, float value) {66CBOR_ASSERT(cbor_is_float(item));67CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_16);68*((float *)item->data) = value;69}7071void cbor_set_float4(cbor_item_t *item, float value) {72CBOR_ASSERT(cbor_is_float(item));73CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_32);74*((float *)item->data) = value;75}7677void cbor_set_float8(cbor_item_t *item, double value) {78CBOR_ASSERT(cbor_is_float(item));79CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_64);80*((double *)item->data) = value;81}8283void cbor_set_ctrl(cbor_item_t *item, uint8_t value) {84CBOR_ASSERT(cbor_isa_float_ctrl(item));85CBOR_ASSERT(cbor_float_get_width(item) == CBOR_FLOAT_0);86item->metadata.float_ctrl_metadata.ctrl = value;87}8889void cbor_set_bool(cbor_item_t *item, bool value) {90CBOR_ASSERT(cbor_is_bool(item));91item->metadata.float_ctrl_metadata.ctrl =92value ? CBOR_CTRL_TRUE : CBOR_CTRL_FALSE;93}9495cbor_item_t *cbor_new_ctrl(void) {96cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t));97_CBOR_NOTNULL(item);9899*item = (cbor_item_t){100.type = CBOR_TYPE_FLOAT_CTRL,101.data = NULL,102.refcount = 1,103.metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_0,104.ctrl = CBOR_CTRL_NONE}}};105return item;106}107108cbor_item_t *cbor_new_float2(void) {109cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 4);110_CBOR_NOTNULL(item);111112*item = (cbor_item_t){113.type = CBOR_TYPE_FLOAT_CTRL,114.data = (unsigned char *)item + sizeof(cbor_item_t),115.refcount = 1,116.metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_16}}};117return item;118}119120cbor_item_t *cbor_new_float4(void) {121cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 4);122_CBOR_NOTNULL(item);123124*item = (cbor_item_t){125.type = CBOR_TYPE_FLOAT_CTRL,126.data = (unsigned char *)item + sizeof(cbor_item_t),127.refcount = 1,128.metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_32}}};129return item;130}131132cbor_item_t *cbor_new_float8(void) {133cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 8);134_CBOR_NOTNULL(item);135136*item = (cbor_item_t){137.type = CBOR_TYPE_FLOAT_CTRL,138.data = (unsigned char *)item + sizeof(cbor_item_t),139.refcount = 1,140.metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_64}}};141return item;142}143144cbor_item_t *cbor_new_null(void) {145cbor_item_t *item = cbor_new_ctrl();146_CBOR_NOTNULL(item);147cbor_set_ctrl(item, CBOR_CTRL_NULL);148return item;149}150151cbor_item_t *cbor_new_undef(void) {152cbor_item_t *item = cbor_new_ctrl();153_CBOR_NOTNULL(item);154cbor_set_ctrl(item, CBOR_CTRL_UNDEF);155return item;156}157158cbor_item_t *cbor_build_bool(bool value) {159return cbor_build_ctrl(value ? CBOR_CTRL_TRUE : CBOR_CTRL_FALSE);160}161162cbor_item_t *cbor_build_float2(float value) {163cbor_item_t *item = cbor_new_float2();164_CBOR_NOTNULL(item);165cbor_set_float2(item, value);166return item;167}168169cbor_item_t *cbor_build_float4(float value) {170cbor_item_t *item = cbor_new_float4();171_CBOR_NOTNULL(item);172cbor_set_float4(item, value);173return item;174}175176cbor_item_t *cbor_build_float8(double value) {177cbor_item_t *item = cbor_new_float8();178_CBOR_NOTNULL(item);179cbor_set_float8(item, value);180return item;181}182183cbor_item_t *cbor_build_ctrl(uint8_t value) {184cbor_item_t *item = cbor_new_ctrl();185_CBOR_NOTNULL(item);186cbor_set_ctrl(item, value);187return item;188}189190191