Rework theremin script
[RRG-proxmark3.git] / client / deps / tinycbor / cborinternal_p.h
blobd971fa478bed4ae407ef1e2524caa97c1dfecad0
1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 Intel Corporation
4 **
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
21 ** THE SOFTWARE.
23 ****************************************************************************/
25 #ifndef CBORINTERNAL_P_H
26 #define CBORINTERNAL_P_H
28 #include "compilersupport_p.h"
30 #ifndef CBOR_NO_FLOATING_POINT
31 # include <float.h>
32 # include <math.h>
33 #else
34 # ifndef CBOR_NO_HALF_FLOAT_TYPE
35 # define CBOR_NO_HALF_FLOAT_TYPE 1
36 # endif
37 #endif
39 #ifndef CBOR_NO_HALF_FLOAT_TYPE
40 # ifdef __F16C__
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);
48 # else
49 /* software implementation of float-to-fp16 conversions */
50 static inline unsigned short encode_half(double val) {
51 uint64_t v;
52 int sign, exp1, mant;
53 memcpy(&v, &val, sizeof(v));
54 sign = v >> 63 << 15;
55 exp1 = (v >> 52) & 0x7ff;
56 mant = v << 12 >> 12 >> (53 - 11); /* keep only the 11 most significant bits of the mantissa */
57 exp1 -= 1023;
58 if (exp1 == 1024) {
59 /* infinity or NaN */
60 exp1 = 16;
61 mant >>= 1;
62 } else if (exp1 >= 16) {
63 /* overflow, as largest number */
64 exp1 = 15;
65 mant = 1023;
66 } else if (exp1 >= -14) {
67 /* regular normal */
68 } else if (exp1 >= -24) {
69 /* subnormal */
70 mant |= 1024;
71 mant >>= -(exp1 + 14);
72 exp1 = -15;
73 } else {
74 /* underflow, make zero */
75 return 0;
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;
86 double val;
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;
92 # endif
93 #endif /* CBOR_NO_HALF_FLOAT_TYPE */
95 #ifndef CBOR_INTERNAL_API
96 # define CBOR_INTERNAL_API
97 #endif
99 #ifndef CBOR_PARSER_MAX_RECURSIONS
100 # define CBOR_PARSER_MAX_RECURSIONS 1024
101 #endif
104 * CBOR Major types
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,
111 ByteStringType = 2U,
112 TextStringType = 3U,
113 ArrayType = 4U,
114 MapType = 5U, /* a.k.a. object */
115 TagType = 6U,
116 SimpleTypesType = 7U
117 } CborMajorTypes;
120 * CBOR simple and floating point types
121 * Encoded in the low 8 bits of the descriptor byte when the
122 * Major Type is 7.
124 typedef enum CborSimpleTypes {
125 FalseValue = 20,
126 TrueValue = 21,
127 NullValue = 22,
128 UndefinedValue = 23,
129 SimpleTypeInNextByte = 24, /* not really a simple type */
130 HalfPrecisionFloat = 25, /* ditto */
131 SinglePrecisionFloat = 26, /* ditto */
132 DoublePrecisionFloat = 27, /* ditto */
133 Break = 31
134 } CborSimpleTypes;
136 enum {
137 SmallValueBitLength = 5U,
138 SmallValueMask = (1U << SmallValueBitLength) - 1, /* 31 */
139 Value8Bit = 24U,
140 Value16Bit = 25U,
141 Value32Bit = 26U,
142 Value64Bit = 27U,
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 */