reduce float code size; remote AddFloatAsSmallset
diff --git a/inc/qcbor.h b/inc/qcbor.h
index d7f9ec9..5125a67 100644
--- a/inc/qcbor.h
+++ b/inc/qcbor.h
@@ -984,41 +984,6 @@
QCBOREncode_AddFloatAsHalf_2((pCtx), NULL, (nLabel), (fNum))
-/*
- @brief Add a dynamically sized floating point number to the encoded output
-
- @param[in] pCtx The encoding context to add the float to.
- @param[in] szLabel The string map label for this integer value.
- @param[in] nLabel The integer map label for this integer value.
- @param[in] fNum The float to add.
-
- This will selectively encode the single-precision floating point number as either
- single-precision or half-precision. It will always encode infinity, NaN and 0
- has half precision. If no precision will be lost in the conversion to half-precision
- then it will be performed, otherwise it will not be performed.
-
- This reduces the size of encoded messages a lot, maybe even half if most values are
- 0, infinity or NaN.
-
- Half-precision floating point numbers take up 2 bytes, half that of single-precision.
-
- These will always be decoded into a float as standard C doesn't have a widely used
- standard representation for half-precision floats yet.
-
- This works the same as QCBOREncode_AddInt64_2() except it is for single and half-precision floats.
-
- */
-
-void QCBOREncode_AddFloatAsSmallest_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, float fNum);
-
-#define QCBOREncode_AddFloatAsSmallest(pCtx, fNum) \
- QCBOREncode_AddFloatAsSmallest_2((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (fNum))
-
-#define QCBOREncode_AddFloatAsSmallestToMap(pCtx, szLabel, fNum) \
- QCBOREncode_AddFloatAsSmallest_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (fNum))
-
-#define QCBOREncode_AddFloatAsSmallestToMapN(pCtx, nLabel, fNum) \
- QCBOREncode_AddFloatAsSmallest_2((pCtx), NULL, (nLabel), (fNum))
/**
diff --git a/src/ieee754.c b/src/ieee754.c
index e221f3d..aba7f32 100644
--- a/src/ieee754.c
+++ b/src/ieee754.c
@@ -403,20 +403,20 @@
// Optimizer will re organize so there is only one call to IEEE754_FloatToHalf()
if(uSingle == 0) {
// Value is 0.0000, not a a subnormal
- result.uTag = IEEE754_UNION_IS_HALF;
- result.u16 = IEEE754_FloatToHalf(f);
+ result.uSize = IEEE754_UNION_IS_HALF;
+ result.uValue = IEEE754_FloatToHalf(f);
} else if(nSingleExponent == SINGLE_EXPONENT_INF_OR_NAN) {
// NaN, +/- infinity
- result.uTag = IEEE754_UNION_IS_HALF;
- result.u16 = IEEE754_FloatToHalf(f);
+ result.uSize = IEEE754_UNION_IS_HALF;
+ result.uValue = IEEE754_FloatToHalf(f);
} else if((nSingleExponent >= HALF_EXPONENT_MIN) && nSingleExponent <= HALF_EXPONENT_MAX && (!(uSingleSignificand & uDroppedSingleBits))) {
// Normal number in exponent range and precision won't be lost
- result.uTag = IEEE754_UNION_IS_HALF;
- result.u16 = IEEE754_FloatToHalf(f);
+ result.uSize = IEEE754_UNION_IS_HALF;
+ result.uValue = IEEE754_FloatToHalf(f);
} else {
// Subnormal, exponent out of range, or precision will be lost
- result.uTag = IEEE754_UNION_IS_SINGLE;
- result.u32 = uSingle;
+ result.uSize = IEEE754_UNION_IS_SINGLE;
+ result.uValue = uSingle;
}
return result;
@@ -439,24 +439,24 @@
// The various cases
if(d == 0.0) { // Take care of positive and negative zero
// Value is 0.0000, not a a subnormal
- result.uTag = IEEE754_UNION_IS_HALF;
- result.u16 = IEEE754_DoubleToHalf(d);
+ result.uSize = IEEE754_UNION_IS_HALF;
+ result.uValue = IEEE754_DoubleToHalf(d);
} else if(nDoubleExponent == DOUBLE_EXPONENT_INF_OR_NAN) {
// NaN, +/- infinity
- result.uTag = IEEE754_UNION_IS_HALF;
- result.u16 = IEEE754_DoubleToHalf(d);
+ result.uSize = IEEE754_UNION_IS_HALF;
+ result.uValue = IEEE754_DoubleToHalf(d);
} else if(bAllowHalfPrecision && (nDoubleExponent >= HALF_EXPONENT_MIN) && nDoubleExponent <= HALF_EXPONENT_MAX && (!(uDoubleSignificand & uDroppedDoubleBits))) {
// Can convert to half without precision loss
- result.uTag = IEEE754_UNION_IS_HALF;
- result.u16 = IEEE754_DoubleToHalf(d);
+ result.uSize = IEEE754_UNION_IS_HALF;
+ result.uValue = IEEE754_DoubleToHalf(d);
} else if((nDoubleExponent >= SINGLE_EXPONENT_MIN) && nDoubleExponent <= SINGLE_EXPONENT_MAX && (!(uDoubleSignificand & uDroppedSingleBits))) {
// Can convert to single without precision loss
- result.uTag = IEEE754_UNION_IS_SINGLE;
- result.u32 = CopyFloatToUint32((float)d);
+ result.uSize = IEEE754_UNION_IS_SINGLE;
+ result.uValue = CopyFloatToUint32((float)d);
} else {
// Can't convert without precision loss
- result.uTag = IEEE754_UNION_IS_DOUBLE;
- result.u64 = uDouble;
+ result.uSize = IEEE754_UNION_IS_DOUBLE;
+ result.uValue = uDouble;
}
return result;
diff --git a/src/ieee754.h b/src/ieee754.h
index 124d095..5cd36db 100644
--- a/src/ieee754.h
+++ b/src/ieee754.h
@@ -141,18 +141,14 @@
-
-#define IEEE754_UNION_IS_HALF 0
-#define IEEE754_UNION_IS_SINGLE 1
-#define IEEE754_UNION_IS_DOUBLE 2
+// Both tags the value and gives the size
+#define IEEE754_UNION_IS_HALF 2
+#define IEEE754_UNION_IS_SINGLE 4
+#define IEEE754_UNION_IS_DOUBLE 8
typedef struct {
- uint8_t uTag; // One of IEEE754_IS_xxxx
- union {
- uint16_t u16;
- uint32_t u32;
- uint64_t u64;
- };
+ uint8_t uSize; // One of IEEE754_IS_xxxx
+ uint64_t uValue;
} IEEE754_union;
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 93f7203..3d88739 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -561,17 +561,7 @@
static void QCBOREncode_AddFUnionAsSmallest_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, IEEE754_union uNum)
{
- switch(uNum.uTag) {
- case IEEE754_UNION_IS_HALF:
- QCBOREncode_AddType7_2(me, szLabel, nLabel, sizeof(uint16_t), uNum.u16);
- break;
- case IEEE754_UNION_IS_SINGLE:
- QCBOREncode_AddType7_2(me, szLabel, nLabel, sizeof(uint32_t), uNum.u32);
- break;
- case IEEE754_UNION_IS_DOUBLE:
- QCBOREncode_AddType7_2(me, szLabel, nLabel, sizeof(uint64_t), uNum.u64);
- break;
- }
+ QCBOREncode_AddType7_2(me, szLabel, nLabel, uNum.uSize, uNum.uValue);
}
void QCBOREncode_AddFloatAsSmallest_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, float fNum)