blob: ab8f895291283062788696f05174600f64d2e1ec [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 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scullfbc938a2018-08-20 14:09:28 +01009#pragma once
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010010
11#include <stddef.h>
12#include <stdint.h>
13
Andrew Scull6386f252018-12-06 13:29:10 +000014#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
15
Karl Meakin2ad6b662024-07-29 20:45:40 +010016/* NOLINTNEXTLINE(readability-redundant-declaration) */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010017int memcmp(const void *a, const void *b, size_t n);
18
Karl Meakin2ad6b662024-07-29 20:45:40 +010019/* NOLINTNEXTLINE(readability-redundant-declaration) */
Andrew Scull3c351e92020-01-28 11:26:05 +000020int strncmp(const char *a, const char *b, size_t n);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010021
Andrew Walbran318f5732018-11-20 16:23:42 +000022#define ctz(x) __builtin_ctz(x)
23
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000024/**
25 * Check whether the value `v` is aligned to the boundary `a`,
26 * with `a` power of 2.
27 */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000028#define is_aligned(v, a) __builtin_is_aligned((v), (a))
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000029
30/**
31 * Align up the value `v` to the boundary `a`, with `a` power of 2.
32 */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000033#define align_up(v, a) __builtin_align_up((v), (a))
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000034
35/**
36 * Align down the value `v` to the boundary `a`, with `a` power of 2.
37 */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000038#define align_down(v, a) __builtin_align_down((v), (a))
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +000039
J-Alves586f1fb2024-11-13 11:53:54 +000040/*
Karl Meakin04f86c92025-03-11 15:47:29 +000041 * Calculate the sum `a + b` and write the result to `*res`.
42 * Returns whether the operation overflowed.
J-Alves586f1fb2024-11-13 11:53:54 +000043 */
44#define add_overflow(a, b, res) __builtin_add_overflow((a), (b), (res))
45
46/*
Karl Meakin04f86c92025-03-11 15:47:29 +000047 * Calculate product `a * b` and write the result to `*res`.
48 * Returns whether the operation overflowed.
49 */
50#define mul_overflow(a, b, res) __builtin_mul_overflow((a), (b), (res))
51
52/*
J-Alves586f1fb2024-11-13 11:53:54 +000053 * Round up a value to align with a given size and
54 * check whether overflow happens.
55 * The rounded value is '*res', return false on success and true on overflow.
56 */
57#define align_up_overflow(v, size, res) \
58 __extension__({ \
Karl Meakin33e77092025-03-19 18:03:24 +000059 typeof(v) __v = (v); \
60 typeof(size) __size = (size); \
61 typeof(res) __res = (res); \
J-Alves586f1fb2024-11-13 11:53:54 +000062 *__res = align_up(__v, __size); \
63 __v > *__res; \
64 })
65
66/*
67 * Add a with b, then round up the result to align with a given size and
68 * check whether overflow happens.
69 * The rounded value is '*res', return false on success and true on overflow.
70 */
71#define add_with_round_up_overflow(a, b, size, res) \
72 (__extension__({ \
Karl Meakin33e77092025-03-19 18:03:24 +000073 typeof(a) __add_res = 0; \
J-Alves586f1fb2024-11-13 11:53:54 +000074 add_overflow((a), (b), &__add_res) || \
75 align_up_overflow(__add_res, (size), (res)); \
76 }))
77
Andrew Walbran4a53ba62019-03-05 17:26:12 +000078#ifndef be16toh
Andrew Scullf12b35d2018-07-16 12:12:59 +010079#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010080
Andrew Scullf12b35d2018-07-16 12:12:59 +010081#define be16toh(v) __builtin_bswap16(v)
82#define be32toh(v) __builtin_bswap32(v)
83#define be64toh(v) __builtin_bswap64(v)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010084
Andrew Scullf12b35d2018-07-16 12:12:59 +010085#define htobe16(v) __builtin_bswap16(v)
86#define htobe32(v) __builtin_bswap32(v)
87#define htobe64(v) __builtin_bswap64(v)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010088
Andrew Scullf12b35d2018-07-16 12:12:59 +010089#define le16toh(v) (v)
90#define le32toh(v) (v)
91#define le64toh(v) (v)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010092
Andrew Scullf12b35d2018-07-16 12:12:59 +010093#define htole16(v) (v)
94#define htole32(v) (v)
95#define htole64(v) (v)
96
97#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
98
99#define be16toh(v) (v)
100#define be32toh(v) (v)
101#define be64toh(v) (v)
102
103#define htobe16(v) (v)
104#define htobe32(v) (v)
105#define htobe64(v) (v)
106
107#define le16toh(v) __builtin_bswap16(v)
108#define le32toh(v) __builtin_bswap32(v)
109#define le64toh(v) __builtin_bswap64(v)
110
111#define htole16(v) __builtin_bswap16(v)
112#define htole32(v) __builtin_bswap32(v)
113#define htole64(v) __builtin_bswap64(v)
114
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000115#else
116
117/*
118 * __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ &&
119 * __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
120 */
Andrew Scullf12b35d2018-07-16 12:12:59 +0100121
122#error "Unsupported byte order"
123
124#endif
Andrew Walbran4a53ba62019-03-05 17:26:12 +0000125#endif