Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 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 | * https://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 | |
Andrew Scull | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 17 | #pragma once |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
| 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
| 21 | |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 22 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 23 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 24 | int memcmp(const void *a, const void *b, size_t n); |
| 25 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 26 | int strcmp(const char *a, const char *b); |
| 27 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 28 | #define ctz(x) __builtin_ctz(x) |
| 29 | |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 30 | /* Compatibility with old compilers */ |
| 31 | #ifndef __has_builtin |
| 32 | #define __has_builtin(x) 0 |
| 33 | #endif |
| 34 | |
| 35 | /** |
| 36 | * Check whether the value `v` is aligned to the boundary `a`, |
| 37 | * with `a` power of 2. |
| 38 | */ |
| 39 | #if __has_builtin(__builtin_is_aligned) |
| 40 | #define is_aligned(v, a) __builtin_is_aligned((v), (a)) |
| 41 | #else |
Andrew Scull | ad89704 | 2020-01-20 16:01:03 +0000 | [diff] [blame^] | 42 | #define is_aligned(v, a) (((uintptr_t)(v) & ((a)-1)) == 0) |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | /** |
| 46 | * Align up the value `v` to the boundary `a`, with `a` power of 2. |
| 47 | */ |
| 48 | #if __has_builtin(__builtin_align_up) |
| 49 | #define align_up(v, a) __builtin_align_up((v), (a)) |
| 50 | #else |
Andrew Scull | ad89704 | 2020-01-20 16:01:03 +0000 | [diff] [blame^] | 51 | #define align_up(v, a) (((uintptr_t)(v) + ((a)-1)) & ~((a)-1)) |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | /** |
| 55 | * Align down the value `v` to the boundary `a`, with `a` power of 2. |
| 56 | */ |
| 57 | #if __has_builtin(__builtin_align_down) |
| 58 | #define align_down(v, a) __builtin_align_down((v), (a)) |
| 59 | #else |
Andrew Scull | ad89704 | 2020-01-20 16:01:03 +0000 | [diff] [blame^] | 60 | #define align_down(v, a) ((uintptr_t)(v) & ~((a)-1)) |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Andrew Walbran | 4a53ba6 | 2019-03-05 17:26:12 +0000 | [diff] [blame] | 63 | #ifndef be16toh |
Andrew Scull | f12b35d | 2018-07-16 12:12:59 +0100 | [diff] [blame] | 64 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 65 | |
Andrew Scull | f12b35d | 2018-07-16 12:12:59 +0100 | [diff] [blame] | 66 | #define be16toh(v) __builtin_bswap16(v) |
| 67 | #define be32toh(v) __builtin_bswap32(v) |
| 68 | #define be64toh(v) __builtin_bswap64(v) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 69 | |
Andrew Scull | f12b35d | 2018-07-16 12:12:59 +0100 | [diff] [blame] | 70 | #define htobe16(v) __builtin_bswap16(v) |
| 71 | #define htobe32(v) __builtin_bswap32(v) |
| 72 | #define htobe64(v) __builtin_bswap64(v) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 73 | |
Andrew Scull | f12b35d | 2018-07-16 12:12:59 +0100 | [diff] [blame] | 74 | #define le16toh(v) (v) |
| 75 | #define le32toh(v) (v) |
| 76 | #define le64toh(v) (v) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 77 | |
Andrew Scull | f12b35d | 2018-07-16 12:12:59 +0100 | [diff] [blame] | 78 | #define htole16(v) (v) |
| 79 | #define htole32(v) (v) |
| 80 | #define htole64(v) (v) |
| 81 | |
| 82 | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 83 | |
| 84 | #define be16toh(v) (v) |
| 85 | #define be32toh(v) (v) |
| 86 | #define be64toh(v) (v) |
| 87 | |
| 88 | #define htobe16(v) (v) |
| 89 | #define htobe32(v) (v) |
| 90 | #define htobe64(v) (v) |
| 91 | |
| 92 | #define le16toh(v) __builtin_bswap16(v) |
| 93 | #define le32toh(v) __builtin_bswap32(v) |
| 94 | #define le64toh(v) __builtin_bswap64(v) |
| 95 | |
| 96 | #define htole16(v) __builtin_bswap16(v) |
| 97 | #define htole32(v) __builtin_bswap32(v) |
| 98 | #define htole64(v) __builtin_bswap64(v) |
| 99 | |
Andrew Scull | cbefbdb | 2019-01-11 16:36:26 +0000 | [diff] [blame] | 100 | #else |
| 101 | |
| 102 | /* |
| 103 | * __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ && |
| 104 | * __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__ |
| 105 | */ |
Andrew Scull | f12b35d | 2018-07-16 12:12:59 +0100 | [diff] [blame] | 106 | |
| 107 | #error "Unsupported byte order" |
| 108 | |
| 109 | #endif |
Andrew Walbran | 4a53ba6 | 2019-03-05 17:26:12 +0000 | [diff] [blame] | 110 | #endif |