1 /****************************************************************************
3 ** Copyright (C) 2017 Intel Corporation
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
12 ** The above copyright notice and this permission notice shall be included in
13 ** all copies or substantial portions of the Software.
15 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 ****************************************************************************/
25 #ifndef CBORINTERNAL_P_H
26 #define CBORINTERNAL_P_H
28 #include "compilersupport_p.h"
30 #ifndef CBOR_NO_FLOATING_POINT
34 # ifndef CBOR_NO_HALF_FLOAT_TYPE
35 # define CBOR_NO_HALF_FLOAT_TYPE 1
39 #ifndef CBOR_NO_HALF_FLOAT_TYPE
41 # include <immintrin.h>
42 static inline unsigned short encode_half(double val
)
44 return _cvtss_sh((float)val
, 3);
46 static inline double decode_half(unsigned short half
)
48 return _cvtsh_ss(half
);
51 /* software implementation of float-to-fp16 conversions */
52 static inline unsigned short encode_half(double val
)
56 memcpy(&v
, &val
, sizeof(v
));
58 exp
= (v
>> 52) & 0x7ff;
59 mant
= v
<< 12 >> 12 >> (53-11); /* keep only the 11 most significant bits of the mantissa */
65 } else if (exp
>= 16) {
66 /* overflow, as largest number */
69 } else if (exp
>= -14) {
71 } else if (exp
>= -24) {
77 /* underflow, make zero */
81 /* safe cast here as bit operations above guarantee not to overflow */
82 return (unsigned short)(sign
| ((exp
+ 15) << 10) | mant
);
85 /* this function was copied & adapted from RFC 7049 Appendix D */
86 static inline double decode_half(unsigned short half
)
88 int exp
= (half
>> 10) & 0x1f;
89 int mant
= half
& 0x3ff;
91 if (exp
== 0) val
= ldexp(mant
, -24);
92 else if (exp
!= 31) val
= ldexp(mant
+ 1024, exp
- 25);
93 else val
= mant
== 0 ? INFINITY
: NAN
;
94 return half
& 0x8000 ? -val
: val
;
97 #endif /* CBOR_NO_HALF_FLOAT_TYPE */
99 #ifndef CBOR_INTERNAL_API
100 # define CBOR_INTERNAL_API
103 #ifndef CBOR_PARSER_MAX_RECURSIONS
104 # define CBOR_PARSER_MAX_RECURSIONS 1024
109 * Encoded in the high 3 bits of the descriptor byte
110 * See http://tools.ietf.org/html/rfc7049#section-2.1
112 typedef enum CborMajorTypes
{
113 UnsignedIntegerType
= 0U,
114 NegativeIntegerType
= 1U,
118 MapType
= 5U, /* a.k.a. object */
124 * CBOR simple and floating point types
125 * Encoded in the low 8 bits of the descriptor byte when the
128 typedef enum CborSimpleTypes
{
133 SimpleTypeInNextByte
= 24, /* not really a simple type */
134 HalfPrecisionFloat
= 25, /* ditto */
135 SinglePrecisionFloat
= 26, /* ditto */
136 DoublePrecisionFloat
= 27, /* ditto */
141 SmallValueBitLength
= 5U,
142 SmallValueMask
= (1U << SmallValueBitLength
) - 1, /* 31 */
147 IndefiniteLength
= 31U,
149 MajorTypeShift
= SmallValueBitLength
,
150 MajorTypeMask
= (int) (~0U << MajorTypeShift
),
152 BreakByte
= (unsigned)Break
| (SimpleTypesType
<< MajorTypeShift
)
155 CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC
_cbor_value_extract_number(const uint8_t **ptr
, const uint8_t *end
, uint64_t *len
);
156 CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC
_cbor_value_prepare_string_iteration(CborValue
*it
);
157 CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC
_cbor_value_get_string_chunk(const CborValue
*value
, const void **bufferptr
,
158 size_t *len
, CborValue
*next
);
161 #endif /* CBORINTERNAL_P_H */