blob: 45fd441348e8fe690901f153fd5772d42df7b235 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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 Scullfbc938a2018-08-20 14:09:28 +010017#pragma once
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
19#include <stddef.h>
20#include <stdint.h>
21
Andrew Scull6386f252018-12-06 13:29:10 +000022#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
23
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010024int memcmp(const void *a, const void *b, size_t n);
25
Andrew Scull3c351e92020-01-28 11:26:05 +000026int strncmp(const char *a, const char *b, size_t n);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010027
Andrew Walbran318f5732018-11-20 16:23:42 +000028#define ctz(x) __builtin_ctz(x)
29
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000030/* 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 Scullad897042020-01-20 16:01:03 +000042#define is_aligned(v, a) (((uintptr_t)(v) & ((a)-1)) == 0)
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000043#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 Scullad897042020-01-20 16:01:03 +000051#define align_up(v, a) (((uintptr_t)(v) + ((a)-1)) & ~((a)-1))
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000052#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 Scullad897042020-01-20 16:01:03 +000060#define align_down(v, a) ((uintptr_t)(v) & ~((a)-1))
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000061#endif
62
Andrew Walbran4a53ba62019-03-05 17:26:12 +000063#ifndef be16toh
Andrew Scullf12b35d2018-07-16 12:12:59 +010064#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010065
Andrew Scullf12b35d2018-07-16 12:12:59 +010066#define be16toh(v) __builtin_bswap16(v)
67#define be32toh(v) __builtin_bswap32(v)
68#define be64toh(v) __builtin_bswap64(v)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010069
Andrew Scullf12b35d2018-07-16 12:12:59 +010070#define htobe16(v) __builtin_bswap16(v)
71#define htobe32(v) __builtin_bswap32(v)
72#define htobe64(v) __builtin_bswap64(v)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010073
Andrew Scullf12b35d2018-07-16 12:12:59 +010074#define le16toh(v) (v)
75#define le32toh(v) (v)
76#define le64toh(v) (v)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010077
Andrew Scullf12b35d2018-07-16 12:12:59 +010078#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 Scullcbefbdb2019-01-11 16:36:26 +0000100#else
101
102/*
103 * __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ &&
104 * __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
105 */
Andrew Scullf12b35d2018-07-16 12:12:59 +0100106
107#error "Unsupported byte order"
108
109#endif
Andrew Walbran4a53ba62019-03-05 17:26:12 +0000110#endif