Add compile option to disable floating point usage (#114)
This commit adds the option to disable floating point processing in
QCBOR. To disable, the USEFULBUF_DISABLE_ALL_FLOAT preprocessor macro
needs to be defined.
e.g.:
$ make CMD_LINE="-DUSEFULBUF_DISABLE_ALL_FLOAT"
This removes the capability (and the code) of decoding floating point
types. The type is still recognised, so a meaningful
QCBOR_ERR_ALL_FLOAT_DISABLED error is returned when a floating point value
is encountered in a decoded qcbor. From the encoding interface the
floating point encoding functions are removed.
Change-Id: I371769246f7d83354607de9bce1e7998b8c536a1
Signed-off-by: Mate Toth-Pal <mate.toth-pal@arm.com>
diff --git a/src/ieee754.c b/src/ieee754.c
index 216cd00..a8079f8 100644
--- a/src/ieee754.c
+++ b/src/ieee754.c
@@ -2,6 +2,7 @@
ieee754.c -- floating-point conversion between half, double & single-precision
Copyright (c) 2018-2020, Laurence Lundblade. All rights reserved.
+ Copyright (c) 2021, Arm Limited. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
@@ -10,6 +11,12 @@
Created on 7/23/18
=============================================================================*/
+/*
+ Include before QCBOR_DISABLE_PREFERRED_FLOAT is checked as
+ QCBOR_DISABLE_PREFERRED_FLOAT might be defined in qcbor/qcbor_common.h
+ */
+#include "qcbor/qcbor_common.h"
+
#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
#include "ieee754.h"
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 2be12d9..4a20613 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -1,6 +1,7 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
Copyright (c) 2018-2021, Laurence Lundblade.
+ Copyright (c) 2021, Arm Limited.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -839,6 +840,7 @@
* @param[out] pDecodedItem The filled in decoded item.
*
* @retval QCBOR_ERR_HALF_PRECISION_DISABLED
+ * @retval QCBOR_ERR_ALL_FLOAT_DISABLED
* @retval QCBOR_ERR_BAD_TYPE_7
*/
@@ -867,11 +869,11 @@
*/
pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uArgument);
pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
-#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
- uReturn = QCBOR_ERR_HALF_PRECISION_DISABLED;
#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
+ uReturn = FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS);
break;
case SINGLE_PREC_FLOAT: /* 26 */
+#ifndef USEFULBUF_DISABLE_ALL_FLOAT
/* Single precision is normally returned as a double since
* double is widely supported, there is no loss of precision,
* it makes it easy for the caller in most cases and it can
@@ -899,11 +901,16 @@
*/
#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
}
+#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
+ uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS);
break;
case DOUBLE_PREC_FLOAT: /* 27 */
+#ifndef USEFULBUF_DISABLE_ALL_FLOAT
pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uArgument);
pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
+#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
+ uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS);
break;
case CBOR_SIMPLEV_FALSE: /* 20 */
@@ -1068,6 +1075,7 @@
* @retval QCBOR_ERR_STRING_ALLOCATE
* @retval QCBOR_ERR_STRING_TOO_LONG
* @retval QCBOR_ERR_HALF_PRECISION_DISABLED
+ * @retval QCBOR_ERR_ALL_FLOAT_DISABLED
* @retval QCBOR_ERR_BAD_TYPE_7
* @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
*
@@ -1179,6 +1187,7 @@
* @retval QCBOR_ERR_STRING_ALLOCATE
* @retval QCBOR_ERR_STRING_TOO_LONG
* @retval QCBOR_ERR_HALF_PRECISION_DISABLED
+ * @retval QCBOR_ERR_ALL_FLOAT_DISABLED
* @retval QCBOR_ERR_BAD_TYPE_7
* @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
* @retval QCBOR_ERR_NO_STRING_ALLOCATOR
@@ -1412,6 +1421,7 @@
* @retval QCBOR_ERR_STRING_ALLOCATE
* @retval QCBOR_ERR_STRING_TOO_LONG
* @retval QCBOR_ERR_HALF_PRECISION_DISABLED
+ * @retval QCBOR_ERR_ALL_FLOAT_DISABLED
* @retval QCBOR_ERR_BAD_TYPE_7
* @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
* @retval QCBOR_ERR_NO_STRING_ALLOCATOR
@@ -1494,6 +1504,7 @@
* @retval QCBOR_ERR_STRING_ALLOCATE
* @retval QCBOR_ERR_STRING_TOO_LONG
* @retval QCBOR_ERR_HALF_PRECISION_DISABLED
+ * @retval QCBOR_ERR_ALL_FLOAT_DISABLED
* @retval QCBOR_ERR_BAD_TYPE_7
* @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
* @retval QCBOR_ERR_NO_STRING_ALLOCATOR
@@ -1733,6 +1744,7 @@
* @retval QCBOR_ERR_STRING_ALLOCATE
* @retval QCBOR_ERR_STRING_TOO_LONG
* @retval QCBOR_ERR_HALF_PRECISION_DISABLED
+ * @retval QCBOR_ERR_ALL_FLOAT_DISABLED
* @retval QCBOR_ERR_BAD_TYPE_7
* @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
* @retval QCBOR_ERR_NO_STRING_ALLOCATOR
@@ -1893,6 +1905,7 @@
*
* @retval QCBOR_ERR_DATE_OVERFLOW
* @retval QCBOR_ERR_FLOAT_DATE_DISABLED
+ * @retval QCBOR_ERR_ALL_FLOAT_DISABLED
* @retval QCBOR_ERR_BAD_TAG_CONTENT
*
* The epoch date tag defined in QCBOR allows for floating-point
@@ -1907,7 +1920,9 @@
{
QCBORError uReturn = QCBOR_SUCCESS;
+#ifndef USEFULBUF_DISABLE_ALL_FLOAT
pDecodedItem->val.epochDate.fSecondsFraction = 0;
+#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
switch (pDecodedItem->uDataType) {
@@ -1994,6 +2009,7 @@
*
* @retval QCBOR_ERR_DATE_OVERFLOW
* @retval QCBOR_ERR_FLOAT_DATE_DISABLED
+ * @retval QCBOR_ERR_ALL_FLOAT_DISABLED
* @retval QCBOR_ERR_BAD_TAG_CONTENT
*
* This is much simpler than the other epoch date format because
@@ -5051,6 +5067,7 @@
+#ifndef USEFULBUF_DISABLE_ALL_FLOAT
static QCBORError ConvertDouble(const QCBORItem *pItem,
uint32_t uConvertTypes,
double *pdValue)
@@ -5373,6 +5390,7 @@
pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uConvertTypes, pdValue);
}
+#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 7c248ef..7f9c169 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -1,6 +1,7 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
Copyright (c) 2018-2021, Laurence Lundblade.
+ Copyright (c) 2021, Arm Limited.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -690,6 +691,7 @@
}
+#ifndef USEFULBUF_DISABLE_ALL_FLOAT
/*
* Public functions for adding a double. See qcbor/qcbor_encode.h
*/
@@ -740,6 +742,7 @@
QCBOREncode_AddFloatNoPreferred(me, fNum);
#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
}
+#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
diff --git a/src/qcbor_err_to_str.c b/src/qcbor_err_to_str.c
index e96ed70..4879f91 100644
--- a/src/qcbor_err_to_str.c
+++ b/src/qcbor_err_to_str.c
@@ -3,6 +3,7 @@
Copyright (c) 2020, Patrick Uiterwijk. All rights reserved.
Copyright (c) 2020, Laurence Lundblade.
+ Copyright (c) 2021, Arm Limited. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
@@ -60,6 +61,7 @@
_ERR_TO_STR(ERR_HALF_PRECISION_DISABLED)
_ERR_TO_STR(ERR_HW_FLOAT_DISABLED)
_ERR_TO_STR(ERR_FLOAT_EXCEPTION)
+ _ERR_TO_STR(ERR_ALL_FLOAT_DISABLED)
default:
return "Unidentified error";