2018-03-07 20:57:14 +08:00
|
|
|
/*
|
2022-07-13 09:21:24 +08:00
|
|
|
* Copyright (C) 2018-2022 Intel Corporation.
|
2018-03-07 20:57:14 +08:00
|
|
|
*
|
2018-05-26 01:49:13 +08:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2018-03-07 20:57:14 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef UTIL_H
|
|
|
|
#define UTIL_H
|
2019-05-06 11:10:03 +08:00
|
|
|
#include <types.h>
|
2018-03-07 20:57:14 +08:00
|
|
|
|
|
|
|
#define offsetof(st, m) __builtin_offsetof(st, m)
|
2018-09-13 09:25:27 +08:00
|
|
|
#define va_start __builtin_va_start
|
|
|
|
#define va_end __builtin_va_end
|
2018-03-07 20:57:14 +08:00
|
|
|
|
|
|
|
/** Roundup (x/y) to ( x/y + (x%y) ? 1 : 0) **/
|
2018-07-19 09:16:56 +08:00
|
|
|
#define INT_DIV_ROUNDUP(x, y) ((((x)+(y))-1)/(y))
|
2018-03-07 20:57:14 +08:00
|
|
|
|
2020-10-19 11:17:44 +08:00
|
|
|
/** Roundup (x) to (y) aligned **/
|
|
|
|
#define roundup(x, y) (((x) + ((y) - 1UL)) & (~((y) - 1UL)))
|
|
|
|
|
2021-04-22 14:47:54 +08:00
|
|
|
#define min(x, y) (((x) < (y)) ? (x) : (y))
|
2018-04-19 16:16:42 +08:00
|
|
|
|
2021-04-22 14:47:54 +08:00
|
|
|
#define max(x, y) (((x) < (y)) ? (y) : (x))
|
2018-04-19 16:16:42 +08:00
|
|
|
|
2023-08-31 13:58:37 +08:00
|
|
|
/** return a value of v clamped to the range [l, h] */
|
|
|
|
#define clamp(v, l, h) (max(min((v), (h)), (l)))
|
|
|
|
|
2018-09-13 09:25:27 +08:00
|
|
|
/** Replaces 'x' by the string "x". */
|
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
|
|
|
|
/* Macro used to check if a value is aligned to the required boundary.
|
|
|
|
* Returns TRUE if aligned; FALSE if not aligned
|
|
|
|
* NOTE: The required alignment must be a power of 2 (2, 4, 8, 16, 32, etc)
|
|
|
|
*/
|
|
|
|
static inline bool mem_aligned_check(uint64_t value, uint64_t req_align)
|
|
|
|
{
|
|
|
|
return ((value & (req_align - 1UL)) == 0UL);
|
|
|
|
}
|
|
|
|
|
2019-08-24 03:43:08 +08:00
|
|
|
/**
|
|
|
|
* @pre buf != NULL
|
|
|
|
*/
|
|
|
|
static inline uint8_t calculate_sum8(const void *buf, uint32_t length)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint8_t sum = 0U;
|
|
|
|
|
|
|
|
for (i = 0U; i < length; i++) {
|
|
|
|
sum += *((const uint8_t *)buf + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:37:33 +08:00
|
|
|
/**
|
|
|
|
* @pre buf != NULL
|
|
|
|
*/
|
|
|
|
static inline uint8_t calculate_checksum8(const void *buf, uint32_t len)
|
|
|
|
{
|
|
|
|
return (uint8_t)(0x100U - calculate_sum8(buf, len));
|
|
|
|
}
|
|
|
|
|
2021-08-20 14:24:20 +08:00
|
|
|
/**
|
|
|
|
* @pre (uuid1 != NULL) && (uuid2 != NULL)
|
|
|
|
*/
|
|
|
|
static inline bool uuid_is_equal(const uint8_t *uuid1, const uint8_t *uuid2)
|
|
|
|
{
|
|
|
|
uint64_t uuid1_h = *(const uint64_t *)uuid1;
|
|
|
|
uint64_t uuid1_l = *(const uint64_t *)(uuid1 + 8);
|
|
|
|
uint64_t uuid2_h = *(const uint64_t *)uuid2;
|
|
|
|
uint64_t uuid2_l = *(const uint64_t *)(uuid2 + 8);
|
|
|
|
|
|
|
|
return ((uuid1_h == uuid2_h) && (uuid1_l == uuid2_l));
|
|
|
|
}
|
|
|
|
|
2018-03-07 20:57:14 +08:00
|
|
|
#endif /* UTIL_H */
|