qmk_firmware/quantum/util.h
Stefan Kerkmann 5478051d74
[Core] quantum: util: add bit and bitmask helpers (#24229)
quantum: util: add bit and bitmask helpers

These helpers are handy and can prevent off-by-one errors when working
with registers and general low level bit manipulation tasks. The macros
themself are inspired by the bits.h macros from the linux kernel source
code.

Signed-off-by: Stefan Kerkmann <karlk90@pm.me>
Co-authored-by: Pascal Getreuer <50221757+getreuer@users.noreply.github.com>
2024-10-12 18:48:00 +02:00

58 lines
1.5 KiB
C

// Copyright 2022 Stefan Kerkmann (KarlK90)
// Copyright 2011 Jun Wako <wakojun@gmail.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "bits.h"
#include "bitwise.h"
// convert to string
#define STR(s) XSTR(s)
#define XSTR(s) #s
#if !defined(MIN)
# define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
#if !defined(MAX)
# define MAX(x, y) (((x) > (y)) ? (x) : (y))
#endif
#if !defined(CEILING)
/**
* @brief Computes the rounded up result of a division of two integers at
* compile time.
*/
# define CEILING(dividend, divisor) (((dividend) + (divisor)-1) / (divisor))
#endif
#if !defined(IS_ARRAY)
/**
* @brief Returns true if the value is an array, false if it's a pointer.
*
* This macro is ill-formed for scalars, which is OK for its intended use in
* ARRAY_SIZE.
*/
# define IS_ARRAY(value) (!__builtin_types_compatible_p(typeof((value)), typeof(&(value)[0])))
#endif
#if !defined(ARRAY_SIZE)
/**
* @brief Computes the number of elements of the given array at compile time.
*
* This Macro can only be used for statically allocated arrays that have not
* been decayed into a pointer. This is detected at compile time, though the
* error message for scalar values is poor.
*/
# define ARRAY_SIZE(array) (__builtin_choose_expr(IS_ARRAY((array)), sizeof((array)) / sizeof((array)[0]), (void)0))
#endif
#if !defined(PACKED)
# define PACKED __attribute__((__packed__))
#endif
#if __has_include("_util.h")
# include "_util.h" /* Include the platform's _util.h */
#endif