diff options
| author | Greg Hackmann <ghackmann@google.com> | 2015-04-16 20:43:21 +0000 |
|---|---|---|
| committer | Android Git Automerger <android-git-automerger@android.com> | 2015-04-16 20:43:21 +0000 |
| commit | dd27606bce21b55d409385cfca5ed160cbbce6db (patch) | |
| tree | 9f60ba86e72f8f0001279fa0d1f17b2d9b5a089a | |
| parent | d4a09619028b7832b2ee51a4b7874db91a49c6a3 (diff) | |
| parent | 618c2edba1a4723e2673d09615a03368a43c9568 (diff) | |
| download | system_core-dd27606bce21b55d409385cfca5ed160cbbce6db.zip system_core-dd27606bce21b55d409385cfca5ed160cbbce6db.tar.gz system_core-dd27606bce21b55d409385cfca5ed160cbbce6db.tar.bz2 | |
am 618c2edb: am 47578c68: am 88e03449: Merge "cutils: add OS-independent endian.h"
* commit '618c2edba1a4723e2673d09615a03368a43c9568':
cutils: add OS-independent endian.h
| -rw-r--r-- | include/cutils/endian.h | 52 | ||||
| -rw-r--r-- | libcutils/tests/Android.mk | 1 | ||||
| -rw-r--r-- | libcutils/tests/EndianTest.cpp | 60 |
3 files changed, 113 insertions, 0 deletions
diff --git a/include/cutils/endian.h b/include/cutils/endian.h new file mode 100644 index 0000000..3d27ce6 --- /dev/null +++ b/include/cutils/endian.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CUTILS_ENDIAN_H +#define __CUTILS_ENDIAN_H + +#if defined(__linux__) && !defined(TEST_CUTILS_ENDIAN_H) +#include <endian.h> +#else + +#if !defined(__BYTE_ORDER__) +/* gcc and clang predefine __BYTE_ORDER__, so this should never happen */ +#error Compiler does not define __BYTE_ORDER__ +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define htobe16(x) (x) +#define htobe32(x) (x) +#define htobe64(x) (x) +#define htole16(x) __builtin_bswap16(x) +#define htole32(x) __builtin_bswap32(x) +#define htole64(x) __builtin_bswap64(x) +#else +#define htobe16(x) __builtin_bswap16(x) +#define htobe32(x) __builtin_bswap32(x) +#define htobe64(x) __builtin_bswap64(x) +#define htole16(x) (x) +#define htole32(x) (x) +#define htole64(x) (x) +#endif /* __BYTE_ORDER__ */ + +#define be16toh(x) htobe16(x) +#define le16toh(x) htole16(x) +#define be32toh(x) htobe32(x) +#define le32toh(x) htole32(x) +#define be64toh(x) htobe64(x) +#define le64toh(x) htole64(x) + +#endif /* defined(__linux__) */ + +#endif /* __CUTILS_ENDIAN_H */ diff --git a/libcutils/tests/Android.mk b/libcutils/tests/Android.mk index cf70345..577b61d 100644 --- a/libcutils/tests/Android.mk +++ b/libcutils/tests/Android.mk @@ -16,6 +16,7 @@ LOCAL_PATH := $(call my-dir) test_src_files := \ test_str_parms.cpp \ + EndianTest.cpp test_target_only_src_files := \ MemsetTest.cpp \ diff --git a/libcutils/tests/EndianTest.cpp b/libcutils/tests/EndianTest.cpp new file mode 100644 index 0000000..a484da7 --- /dev/null +++ b/libcutils/tests/EndianTest.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdint.h> + +#define TEST_CUTILS_ENDIAN_H +#include <cutils/endian.h> +#include <gtest/gtest.h> + +static const uint16_t host16 = 0x1122; +static const uint32_t host32 = 0x11223344; +static const uint64_t host64 = 0x1122334455667788; +static const uint16_t swapped16 = 0x2211; +static const uint32_t swapped32 = 0x44332211; +static const uint64_t swapped64 = 0x8877665544332211; +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +static const uint16_t le16 = swapped16; +static const uint32_t le32 = swapped32; +static const uint64_t le64 = swapped64; +static const uint16_t be16 = native16; +static const uint32_t be32 = native32; +static const uint64_t be64 = native64; +#else +static const uint16_t le16 = host16; +static const uint32_t le32 = host32; +static const uint64_t le64 = host64; +static const uint16_t be16 = swapped16; +static const uint32_t be32 = swapped32; +static const uint64_t be64 = swapped64; +#endif + +TEST(endian, endian) { + EXPECT_EQ(le16, htole16(host16)); + EXPECT_EQ(host16, le16toh(htole16(host16))); + EXPECT_EQ(be16, htobe16(host16)); + EXPECT_EQ(host16, be16toh(htobe16(host16))); + + EXPECT_EQ(le32, htole32(host32)); + EXPECT_EQ(host32, le32toh(htole32(host32))); + EXPECT_EQ(be32, htobe32(host32)); + EXPECT_EQ(host32, be32toh(htobe32(host32))); + + EXPECT_EQ(le64, htole64(host64)); + EXPECT_EQ(host64, le64toh(htole64(host64))); + EXPECT_EQ(be64, htobe64(host64)); + EXPECT_EQ(host64, be64toh(htobe64(host64))); +} |
