diff --git a/include/endian.h b/include/endian.h new file mode 100644 index 00000000000..5622cff78af --- /dev/null +++ b/include/endian.h @@ -0,0 +1,151 @@ +/**************************************************************************** + * include/endian.h + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __INCLUDE_ENDIAN_H +#define __INCLUDE_ENDIAN_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Preprocessor definitions + ****************************************************************************/ + +/* The endian.h header must define at least the following macros for use in + * determining host byte order for integer types: + * + * BYTE_ORDER This macro will have a value equal to one of the *_ENDIAN + * macros in this header. + * LITTLE_ENDIAN If BYTE_ORDER == LITTLE_ENDIAN, the host byte order is + * from least significant to most significant. + * BIG_ENDIAN If BYTE_ORDER == BIG_ENDIAN, the host byte order is from + * most significant to least significant. + */ + +#define LITTLE_ENDIAN 1234 +#define BIG_ENDIAN 4321 + +/* Common byte swapping macros */ + +#define __SWAP_UINT16_ISMACRO 1 +#undef __SWAP_UINT32_ISMACRO + +#ifdef __SWAP_UINT16_ISMACRO +# define __swap_uint16(n) \ + (uint16_t)(((((uint16_t)(n)) & 0x00ff) << 8) | \ + ((((uint16_t)(n)) >> 8) & 0x00ff)) +#endif + +#ifdef __SWAP_UINT32_ISMACRO +# define __swap_uint32(n) \ + (uint32_t)(((((uint32_t)(n)) & 0x000000ffUL) << 24) | \ + ((((uint32_t)(n)) & 0x0000ff00UL) << 8) | \ + ((((uint32_t)(n)) & 0x00ff0000UL) >> 8) | \ + ((((uint32_t)(n)) & 0xff000000UL) >> 24)) +#endif + +/* Endian-specific definitions */ + +#ifdef CONFIG_ENDIAN_BIG +/* Big-endian byte order */ + +# define BYTE_ORDER BIG_ENDIAN + +/* Big-endian byte order macros */ + +# define htobe16(n) (n) +# define htole16(n) __swap_uint16((uint16_t)n) +# define be16toh(n) (n) +# define le16toh(n) __swap_uint16((uint16_t)n) + +# define htobe32(n) (n) +# define htole32(n) __swap_uint32((uint32_t)n) +# define be32toh(n) (n) +# define le32toh(n) __swap_uint32(n) + +# ifdef CONFIG_HAVE_LONG_LONG +# define htobe64(n) (n) +# define htole64(n) __swap_uint64((uint64_t)n) +# define be64toh(n) (n) +# define le64toh(n) __swap_uint64(uint64_t)n) +# endif + +#else +/* Little-endian byte order */ + +# define BYTE_ORDER LITTLE_ENDIAN + +/* Little-endian byte order macros */ + +# define htobe16(n) __swap_uint16((uint16_t)n) +# define htole16(n) (n) +# define be16toh(n) __swap_uint16((uint16_t)n) +# define le16toh(n) (n) + +# define htobe32(n) __swap_uint32((uint32_t)n) +# define htole32(n) (n) +# define be32toh(n) __swap_uint32((uint32_t)n) +# define le32toh(n) (n) + +# ifdef CONFIG_HAVE_LONG_LONG +# define htobe64(n) __swap_uint64(uint64_t)n) +# define htole64(n) (n) +# define be64toh(n) __swap_uint64(uint64_t)n) +# define le64toh(n) (n) +# endif +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifndef __SWAP_UINT16_ISMACRO +uint16_t __swap_uint16(uint16_t n); +#endif + +#ifndef __SWAP_UINT32_ISMACRO +uint32_t __swap_uint32(uint32_t n); +#endif + +#if CONFIG_HAVE_LONG_LONG +uint64_t __swap_uint64(uint64_t n); +#endif + +#endif /* __INCLUDE_ENDIAN_H */ diff --git a/libs/libc/Makefile b/libs/libc/Makefile index c659bac101c..ccc8ece9559 100644 --- a/libs/libc/Makefile +++ b/libs/libc/Makefile @@ -57,6 +57,7 @@ include aio/Make.defs include audio/Make.defs include dirent/Make.defs include dllfcn/Make.defs +include endian/Make.defs include fixedmath/Make.defs include hex2bin/Make.defs include inttypes/Make.defs diff --git a/libs/libc/README.txt b/libs/libc/README.txt index 546135a9332..c7127893e03 100644 --- a/libs/libc/README.txt +++ b/libs/libc/README.txt @@ -30,6 +30,7 @@ we have: audio - This part of the audio system: nuttx/audio/audio.h dllfcn - dllfcn.h + endian - endian.h hex2bin - hex2bin.h libgen - libgen.h locale - locale.h diff --git a/libs/libc/endian/Kconfig b/libs/libc/endian/Kconfig new file mode 100644 index 00000000000..f72f3c094ce --- /dev/null +++ b/libs/libc/endian/Kconfig @@ -0,0 +1,4 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# diff --git a/libs/libc/endian/Make.defs b/libs/libc/endian/Make.defs new file mode 100644 index 00000000000..5e6d8735d0d --- /dev/null +++ b/libs/libc/endian/Make.defs @@ -0,0 +1,43 @@ +############################################################################ +# libs/libc/endian/Make.defs +# +# Copyright (C) 2018 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +# Add the endian.h C files to the build + +CSRCS += lib_swap16.c lib_swap32.c lib_swap64.c + +# Add the endian directory to the build + +DEPPATH += --dep-path endian +VPATH += :endian diff --git a/libs/libc/endian/lib_swap16.c b/libs/libc/endian/lib_swap16.c new file mode 100644 index 00000000000..befca216193 --- /dev/null +++ b/libs/libc/endian/lib_swap16.c @@ -0,0 +1,53 @@ +/**************************************************************************** + * libs/libc/endian/lib_swap16.c + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifndef __SWAP_UINT16_ISMACRO +uint16_t __swap_uint16(uint16_t n) +{ + return (uint16_t)(((((uint16_t)(n)) & 0x00ff) << 8) | \ + ((((uint16_t)(n)) >> 8) & 0x00ff)); +} +#endif diff --git a/libs/libc/endian/lib_swap32.c b/libs/libc/endian/lib_swap32.c new file mode 100644 index 00000000000..226e50e9f43 --- /dev/null +++ b/libs/libc/endian/lib_swap32.c @@ -0,0 +1,55 @@ +/**************************************************************************** + * libs/libc/endian/lib_swap32.c + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifndef __SWAP_UINT32_ISMACRO +uint32_t __swap_uint32(uint32_t n) +{ + return (uint32_t)(((((uint32_t)(n)) & 0x000000ffUL) << 24) | \ + ((((uint32_t)(n)) & 0x0000ff00UL) << 8) | \ + ((((uint32_t)(n)) & 0x00ff0000UL) >> 8) | \ + ((((uint32_t)(n)) & 0xff000000UL) >> 24)); +} +#endif diff --git a/libs/libc/endian/lib_swap64.c b/libs/libc/endian/lib_swap64.c new file mode 100644 index 00000000000..7b3461b7a6f --- /dev/null +++ b/libs/libc/endian/lib_swap64.c @@ -0,0 +1,61 @@ +/**************************************************************************** + * libs/libc/endian/lib_swap64.c + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_HAVE_LONG_LONG +uint64_t __swap_uint64(uint64_t n) +{ + return (uint64_t)(((((uint64_t)(n)) & 0x00000000000000ffULL) << 56) | \ + ((((uint64_t)(n)) & 0x000000000000ff00ULL) << 40) | \ + ((((uint64_t)(n)) & 0x0000000000ff0000ULL) << 24) | \ + ((((uint64_t)(n)) & 0x00000000ff000000ULL) << 8) | \ + ((((uint64_t)(n)) & 0x000000ff00000000ULL) >> 8) | \ + ((((uint64_t)(n)) & 0x0000ff0000000000ULL) >> 24) | \ + ((((uint64_t)(n)) & 0x00ff000000000000ULL) >> 40) | \ + ((((uint64_t)(n)) & 0xff00000000000000ULL) >> 56)); +} +#endif