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