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
) {
43 return _cvtss_sh((float)val
, 3);
45 static inline double decode_half(unsigned short half
) {
46 return _cvtsh_ss(half
);
49 /* software implementation of float-to-fp16 conversions */
50 static inline unsigned short encode_half(double val
) {
53 memcpy(&v
, &val
, sizeof(v
));
55 exp1
= (v
>> 52) & 0x7ff;
56 mant
= v
<< 12 >> 12 >> (53 - 11); /* keep only the 11 most significant bits of the mantissa */
62 } else if (exp1
>= 16) {
63 /* overflow, as largest number */
66 } else if (exp1
>= -14) {
68 } else if (exp1
>= -24) {
71 mant
>>= -(exp1
+ 14);
74 /* underflow, make zero */
78 /* safe cast here as bit operations above guarantee not to overflow */
79 return (unsigned short)(sign
| ((exp1
+ 15) << 10) | mant
);
82 /* this function was copied & adapted from RFC 7049 Appendix D */
83 static inline double decode_half(unsigned short half
) {
84 int exp1
= (half
>> 10) & 0x1f;
85 int mant
= half
& 0x3ff;
87 if (exp1
== 0) val
= ldexp(mant
, -24);
88 else if (exp1
!= 31) val
= ldexp(mant
+ 1024, exp1
- 25);
89 else val
= mant
== 0 ? INFINITY
: NAN
;
90 return (half
& 0x8000) ? -val
: val
;
93 #endif /* CBOR_NO_HALF_FLOAT_TYPE */
95 #ifndef CBOR_INTERNAL_API
96 # define CBOR_INTERNAL_API
99 #ifndef CBOR_PARSER_MAX_RECURSIONS
100 # define CBOR_PARSER_MAX_RECURSIONS 1024
105 * Encoded in the high 3 bits of the descriptor byte
106 * See http://tools.ietf.org/html/rfc7049#section-2.1
108 typedef enum CborMajorTypes
{
109 UnsignedIntegerType
= 0U,
110 NegativeIntegerType
= 1U,
114 MapType
= 5U, /* a.k.a. object */
120 * CBOR simple and floating point types
121 * Encoded in the low 8 bits of the descriptor byte when the
124 typedef enum CborSimpleTypes
{
129 SimpleTypeInNextByte
= 24, /* not really a simple type */
130 HalfPrecisionFloat
= 25, /* ditto */
131 SinglePrecisionFloat
= 26, /* ditto */
132 DoublePrecisionFloat
= 27, /* ditto */
137 SmallValueBitLength
= 5U,
138 SmallValueMask
= (1U << SmallValueBitLength
) - 1, /* 31 */
143 IndefiniteLength
= 31U,
145 MajorTypeShift
= SmallValueBitLength
,
146 MajorTypeMask
= (int)(~0U << MajorTypeShift
),
148 BreakByte
= (unsigned)Break
| (SimpleTypesType
<< MajorTypeShift
)
151 CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC
_cbor_value_extract_number(const uint8_t **ptr
, const uint8_t *end
, uint64_t *len
);
152 CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC
_cbor_value_prepare_string_iteration(CborValue
*it
);
153 CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC
_cbor_value_get_string_chunk(const CborValue
*value
, const void **bufferptr
,
154 size_t *len
, CborValue
*next
);
157 #endif /* CBORINTERNAL_P_H */