diff options
author | Thomas Heijligen <thomas.heijligen@secunet.com> | 2022-04-21 13:29:33 +0200 |
---|---|---|
committer | Thomas Heijligen <src@posteo.de> | 2022-05-08 17:37:45 +0000 |
commit | 7228ce007cc42030393b3a33f28b6115e41591b1 (patch) | |
tree | f05d46d98809e752e6d711061ef20f8b4d84b4fe | |
parent | 1866deb06d775b9d006c1ef21c99f439677f0f76 (diff) | |
download | flashrom-7228ce007cc42030393b3a33f28b6115e41591b1.tar.gz flashrom-7228ce007cc42030393b3a33f28b6115e41591b1.tar.bz2 flashrom-7228ce007cc42030393b3a33f28b6115e41591b1.zip |
platform/swap: move swap inline functions & macros into an own header
These inline functions and macros are only used in
platform/endian_(big|little).c and do not need to be compiled into every
object which includes `platform.h`.
Change-Id: Ib2326e6a4eb5e000a0eace857d040372e2e9e561
Signed-off-by: Thomas Heijligen <thomas.heijligen@secunet.com>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/63825
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
-rw-r--r-- | include/platform.h | 53 | ||||
-rw-r--r-- | include/platform/swap.h | 80 | ||||
-rw-r--r-- | platform/endian_big.c | 1 | ||||
-rw-r--r-- | platform/endian_little.c | 1 |
4 files changed, 82 insertions, 53 deletions
diff --git a/include/platform.h b/include/platform.h index d15082f0..3cde2eda 100644 --- a/include/platform.h +++ b/include/platform.h @@ -25,59 +25,6 @@ #include <stddef.h> #include <stdint.h> -/* swap bytes */ -/* OpenBSD has conflicting definitions for swapX and __swapX */ -static inline uint8_t ___swap8(const uint8_t value) -{ - return (value & (uint8_t)0xffU); -} - -static inline uint16_t ___swap16(const uint16_t value) -{ - return ((value & (uint16_t)0x00ffU) << 8) | - ((value & (uint16_t)0xff00U) >> 8); -} - -static inline uint32_t ___swap32(const uint32_t value) -{ - return ((value & (uint32_t)0x000000ffUL) << 24) | - ((value & (uint32_t)0x0000ff00UL) << 8) | - ((value & (uint32_t)0x00ff0000UL) >> 8) | - ((value & (uint32_t)0xff000000UL) >> 24); -} - -static inline uint64_t ___swap64(const uint64_t value) -{ - return ((value & (uint64_t)0x00000000000000ffULL) << 56) | - ((value & (uint64_t)0x000000000000ff00ULL) << 40) | - ((value & (uint64_t)0x0000000000ff0000ULL) << 24) | - ((value & (uint64_t)0x00000000ff000000ULL) << 8) | - ((value & (uint64_t)0x000000ff00000000ULL) >> 8) | - ((value & (uint64_t)0x0000ff0000000000ULL) >> 24) | - ((value & (uint64_t)0x00ff000000000000ULL) >> 40) | - ((value & (uint64_t)0xff00000000000000ULL) >> 56); -} - -/* - * macro to return the same value as passed. - * - * `___return_same(cpu_to_le, 8)` - * expands to - * `uint8_t cpu_to_le8 (const uint8_t value) { return value; }` - */ -#define ___return_same(name, bits) \ - uint##bits##_t name##bits (const uint##bits##_t value) { return value; } - -/* - * macro to return the swapped value as passed. - * - * `___return_swapped(cpu_to_be, 8)` - * expands to - * `uint8_t cpu_to_be8 (const uint8_t value) { return ___swap8 (value); }` - */ -#define ___return_swapped(name, bits) \ - uint##bits##_t name##bits (const uint##bits##_t value) { return ___swap##bits (value); } - /* convert cpu native endian to little endian */ uint8_t cpu_to_le8 (uint8_t value); uint16_t cpu_to_le16(uint16_t value); diff --git a/include/platform/swap.h b/include/platform/swap.h new file mode 100644 index 00000000..12f31065 --- /dev/null +++ b/include/platform/swap.h @@ -0,0 +1,80 @@ +/* + * This file is part of the flashrom project. + * + * Copyright (C) 2009 Carl-Daniel Hailfinger + * Copyright (C) 2022 secunet Security Networks AG + * (written by Thomas Heijligen <thomas.heijligen@secunet.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +/* + * inline functions and macros shared by endian conversion functions + */ + +#ifndef ___SWAP_H___ +#define ___SWAP_H___ 1 + +#include <stdint.h> + +/* swap bytes */ +/* OpenBSD has conflicting definitions for swapX, _swap_X and __swapX */ +static inline uint8_t ___swap8(const uint8_t value) +{ + return (value & (uint8_t)0xffU); +} + +static inline uint16_t ___swap16(const uint16_t value) +{ + return ((value & (uint16_t)0x00ffU) << 8) | + ((value & (uint16_t)0xff00U) >> 8); +} + +static inline uint32_t ___swap32(const uint32_t value) +{ + return ((value & (uint32_t)0x000000ffUL) << 24) | + ((value & (uint32_t)0x0000ff00UL) << 8) | + ((value & (uint32_t)0x00ff0000UL) >> 8) | + ((value & (uint32_t)0xff000000UL) >> 24); +} + +static inline uint64_t ___swap64(const uint64_t value) +{ + return ((value & (uint64_t)0x00000000000000ffULL) << 56) | + ((value & (uint64_t)0x000000000000ff00ULL) << 40) | + ((value & (uint64_t)0x0000000000ff0000ULL) << 24) | + ((value & (uint64_t)0x00000000ff000000ULL) << 8) | + ((value & (uint64_t)0x000000ff00000000ULL) >> 8) | + ((value & (uint64_t)0x0000ff0000000000ULL) >> 24) | + ((value & (uint64_t)0x00ff000000000000ULL) >> 40) | + ((value & (uint64_t)0xff00000000000000ULL) >> 56); +} + +/* + * macro to return the same value as passed. + * + * `___return_same(cpu_to_le, 8)` + * expands to + * `uint8_t cpu_to_le8 (const uint8_t value) { return value; }` + */ +#define ___return_same(name, bits) \ + uint##bits##_t name##bits (const uint##bits##_t value) { return value; } + +/* + * macro to return the swapped value as passed. + * + * `___return_swapped(cpu_to_be, 8)` + * expands to + * `uint8_t cpu_to_be8 (const uint8_t value) { return ___swap8 (value); }` + */ +#define ___return_swapped(name, bits) \ + uint##bits##_t name##bits (const uint##bits##_t value) { return ___swap##bits (value); } + +#endif /* !___SWAP_H___ */ diff --git a/platform/endian_big.c b/platform/endian_big.c index 6541a831..45655340 100644 --- a/platform/endian_big.c +++ b/platform/endian_big.c @@ -16,6 +16,7 @@ */ #include "platform.h" +#include "platform/swap.h" /* convert cpu native endian to little endian */ ___return_swapped(cpu_to_le, 8) diff --git a/platform/endian_little.c b/platform/endian_little.c index aeb9d573..690f3839 100644 --- a/platform/endian_little.c +++ b/platform/endian_little.c @@ -16,6 +16,7 @@ */ #include "platform.h" +#include "platform/swap.h" /* convert cpu native endian to little endian */ ___return_same(cpu_to_le, 8) |