Skip to content

Commit d5195d1

Browse files
committed
Merge branch 'feature/endian_h_compat_header' into 'master'
newlib: add endian.h header file for compatibility (Github PR) See merge request espressif/esp-idf!8236
2 parents 09d4767 + 1173ce3 commit d5195d1

File tree

1 file changed

+212
-0
lines changed
  • components/newlib/platform_include

1 file changed

+212
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2+
// Copyright 2020 Francesco Giancane <[email protected]>
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/*
17+
* This is a compatibility header for <endian.h>.
18+
* In xtensa-newlib distribution it is located in <machine/endian.h>
19+
* but most program expect to be plain <endian.h>.
20+
*/
21+
22+
#ifndef XTENSA_COMPAT_ENDIAN_H_INCLUDED
23+
#define XTENSA_COMPAT_ENDIAN_H_INCLUDED
24+
#include <machine/endian.h>
25+
26+
/*
27+
* All the code below is a rework of
28+
* https://github.com/freebsd/freebsd/blob/master/sys/sys/endian.h
29+
* to import symbols defining non-standard endian handling functions.
30+
* The aforementioned source code license terms are included here.
31+
* For further license info, please look at https://github.com/freebsd/freebsd
32+
*/
33+
34+
/*-
35+
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
36+
*
37+
* Copyright (c) 2002 Thomas Moestl <[email protected]>
38+
* All rights reserved.
39+
*
40+
* Redistribution and use in source and binary forms, with or without
41+
* modification, are permitted provided that the following conditions
42+
* are met:
43+
* 1. Redistributions of source code must retain the above copyright
44+
* notice, this list of conditions and the following disclaimer.
45+
* 2. Redistributions in binary form must reproduce the above copyright
46+
* notice, this list of conditions and the following disclaimer in the
47+
* documentation and/or other materials provided with the distribution.
48+
*
49+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59+
* SUCH DAMAGE.
60+
*
61+
* $FreeBSD$
62+
*/
63+
64+
/*
65+
* General byte order swapping functions.
66+
*/
67+
#define bswap16(x) __bswap16(x)
68+
#define bswap32(x) __bswap32(x)
69+
#define bswap64(x) __bswap64(x)
70+
71+
/*
72+
* Host to big endian, host to little endian, big endian to host, and little
73+
* endian to host byte order functions as detailed in byteorder(9).
74+
*/
75+
#if _BYTE_ORDER == _LITTLE_ENDIAN
76+
#define htobe16(x) bswap16((x))
77+
#define htobe32(x) bswap32((x))
78+
#define htobe64(x) bswap64((x))
79+
#define htole16(x) ((uint16_t)(x))
80+
#define htole32(x) ((uint32_t)(x))
81+
#define htole64(x) ((uint64_t)(x))
82+
83+
#define be16toh(x) bswap16((x))
84+
#define be32toh(x) bswap32((x))
85+
#define be64toh(x) bswap64((x))
86+
#define le16toh(x) ((uint16_t)(x))
87+
#define le32toh(x) ((uint32_t)(x))
88+
#define le64toh(x) ((uint64_t)(x))
89+
#else /* _BYTE_ORDER != _LITTLE_ENDIAN */
90+
#define htobe16(x) ((uint16_t)(x))
91+
#define htobe32(x) ((uint32_t)(x))
92+
#define htobe64(x) ((uint64_t)(x))
93+
#define htole16(x) bswap16((x))
94+
#define htole32(x) bswap32((x))
95+
#define htole64(x) bswap64((x))
96+
97+
#define be16toh(x) ((uint16_t)(x))
98+
#define be32toh(x) ((uint32_t)(x))
99+
#define be64toh(x) ((uint64_t)(x))
100+
#define le16toh(x) bswap16((x))
101+
#define le32toh(x) bswap32((x))
102+
#define le64toh(x) bswap64((x))
103+
#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
104+
105+
/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
106+
107+
static __inline uint16_t
108+
be16dec(const void *pp)
109+
{
110+
uint8_t const *p = (uint8_t const *)pp;
111+
112+
return ((p[0] << 8) | p[1]);
113+
}
114+
115+
static __inline uint32_t
116+
be32dec(const void *pp)
117+
{
118+
uint8_t const *p = (uint8_t const *)pp;
119+
120+
return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
121+
}
122+
123+
static __inline uint64_t
124+
be64dec(const void *pp)
125+
{
126+
uint8_t const *p = (uint8_t const *)pp;
127+
128+
return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
129+
}
130+
131+
static __inline uint16_t
132+
le16dec(const void *pp)
133+
{
134+
uint8_t const *p = (uint8_t const *)pp;
135+
136+
return ((p[1] << 8) | p[0]);
137+
}
138+
139+
static __inline uint32_t
140+
le32dec(const void *pp)
141+
{
142+
uint8_t const *p = (uint8_t const *)pp;
143+
144+
return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
145+
}
146+
147+
static __inline uint64_t
148+
le64dec(const void *pp)
149+
{
150+
uint8_t const *p = (uint8_t const *)pp;
151+
152+
return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
153+
}
154+
155+
static __inline void
156+
be16enc(void *pp, uint16_t u)
157+
{
158+
uint8_t *p = (uint8_t *)pp;
159+
160+
p[0] = (u >> 8) & 0xff;
161+
p[1] = u & 0xff;
162+
}
163+
164+
static __inline void
165+
be32enc(void *pp, uint32_t u)
166+
{
167+
uint8_t *p = (uint8_t *)pp;
168+
169+
p[0] = (u >> 24) & 0xff;
170+
p[1] = (u >> 16) & 0xff;
171+
p[2] = (u >> 8) & 0xff;
172+
p[3] = u & 0xff;
173+
}
174+
175+
static __inline void
176+
be64enc(void *pp, uint64_t u)
177+
{
178+
uint8_t *p = (uint8_t *)pp;
179+
180+
be32enc(p, (uint32_t)(u >> 32));
181+
be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
182+
}
183+
184+
static __inline void
185+
le16enc(void *pp, uint16_t u)
186+
{
187+
uint8_t *p = (uint8_t *)pp;
188+
189+
p[0] = u & 0xff;
190+
p[1] = (u >> 8) & 0xff;
191+
}
192+
193+
static __inline void
194+
le32enc(void *pp, uint32_t u)
195+
{
196+
uint8_t *p = (uint8_t *)pp;
197+
198+
p[0] = u & 0xff;
199+
p[1] = (u >> 8) & 0xff;
200+
p[2] = (u >> 16) & 0xff;
201+
p[3] = (u >> 24) & 0xff;
202+
}
203+
204+
static __inline void
205+
le64enc(void *pp, uint64_t u)
206+
{
207+
uint8_t *p = (uint8_t *)pp;
208+
209+
le32enc(p, (uint32_t)(u & 0xffffffffU));
210+
le32enc(p + 4, (uint32_t)(u >> 32));
211+
}
212+
#endif /* XTENSA_COMPAT_ENDIAN_H_INCLUDED */

0 commit comments

Comments
 (0)