Browse Source

util: increase several macros limit from 255 to 4095

Currently, the following macros will only work for 0 - 255:
- `IS_EQ`
- `UTIL_X2`
- `UTIL_INC`
- `UTIL_DEC`
- `LISTIFY`

This patch increases their limit to 4095.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
pull/63569/head
Yong Cong Sin 2 years ago committed by Chris Friedt
parent
commit
0722c621c8
  1. 1033
      include/zephyr/sys/util_internal.h
  2. 4114
      include/zephyr/sys/util_internal_is_eq.h
  3. 4122
      include/zephyr/sys/util_internal_util_dec.h
  4. 4122
      include/zephyr/sys/util_internal_util_inc.h
  5. 4121
      include/zephyr/sys/util_internal_util_x2.h
  6. 16400
      include/zephyr/sys/util_listify.h
  7. 1025
      include/zephyr/sys/util_loops.h
  8. 16
      include/zephyr/sys/util_macro.h
  9. 163
      scripts/utils/gen_util_macros.py

1033
include/zephyr/sys/util_internal.h

File diff suppressed because it is too large Load Diff

4114
include/zephyr/sys/util_internal_is_eq.h

File diff suppressed because it is too large Load Diff

4122
include/zephyr/sys/util_internal_util_dec.h

File diff suppressed because it is too large Load Diff

4122
include/zephyr/sys/util_internal_util_inc.h

File diff suppressed because it is too large Load Diff

4121
include/zephyr/sys/util_internal_util_x2.h

File diff suppressed because it is too large Load Diff

16400
include/zephyr/sys/util_listify.h

File diff suppressed because it is too large Load Diff

1025
include/zephyr/sys/util_loops.h

File diff suppressed because it is too large Load Diff

16
include/zephyr/sys/util_macro.h

@ -256,7 +256,7 @@ extern "C" { @@ -256,7 +256,7 @@ extern "C" {
* @brief Like <tt>a == b</tt>, but does evaluation and
* short-circuiting at C preprocessor time.
*
* This however only works for integer literal from 0 to 255.
* This however only works for integer literal from 0 to 4095.
*
*/
#define IS_EQ(a, b) Z_IS_EQ(a, b)
@ -367,26 +367,26 @@ extern "C" { @@ -367,26 +367,26 @@ extern "C" {
#define UTIL_AND(a, b) COND_CODE_1(UTIL_BOOL(a), (b), (0))
/**
* @brief UTIL_INC(x) for an integer literal x from 0 to 255 expands to an
* @brief UTIL_INC(x) for an integer literal x from 0 to 4095 expands to an
* integer literal whose value is x+1.
*
* @see UTIL_DEC(x)
*/
#define UTIL_INC(x) UTIL_PRIMITIVE_CAT(UTIL_INC_, x)
#define UTIL_INC(x) UTIL_PRIMITIVE_CAT(Z_UTIL_INC_, x)
/**
* @brief UTIL_DEC(x) for an integer literal x from 0 to 255 expands to an
* @brief UTIL_DEC(x) for an integer literal x from 0 to 4095 expands to an
* integer literal whose value is x-1.
*
* @see UTIL_INC(x)
*/
#define UTIL_DEC(x) UTIL_PRIMITIVE_CAT(UTIL_DEC_, x)
#define UTIL_DEC(x) UTIL_PRIMITIVE_CAT(Z_UTIL_DEC_, x)
/**
* @brief UTIL_X2(y) for an integer literal y from 0 to 255 expands to an
* @brief UTIL_X2(y) for an integer literal y from 0 to 4095 expands to an
* integer literal whose value is 2y.
*/
#define UTIL_X2(y) UTIL_PRIMITIVE_CAT(UTIL_X2_, y)
#define UTIL_X2(y) UTIL_PRIMITIVE_CAT(Z_UTIL_X2_, y)
/**
@ -402,7 +402,7 @@ extern "C" { @@ -402,7 +402,7 @@ extern "C" {
* { MY_PWM0 , MY_PWM1 }
*
* @param LEN The length of the sequence. Must be an integer literal less
* than 255.
* than 4095.
* @param F A macro function that accepts at least two arguments:
* <tt>F(i, ...)</tt>. @p F is called repeatedly in the expansion.
* Its first argument @p i is the index in the sequence, and

163
scripts/utils/gen_util_macros.py

@ -0,0 +1,163 @@ @@ -0,0 +1,163 @@
"""
Utility script to generate headers for the following macros
- Z_LISTIFY
- Z_UTIL_INC
- Z_UTIL_DEC
- Z_UTIL_X2
- Z_IS_EQ
.. note::
The script will simply create the header files in the current working directory,
they should be copied manually to $ZEPHYR_BASE/include/zephyr/sys/ accordingly.
Usage::
python $ZEPHYR_BASE/scripts/utils/gen_util_macros.py -l 4095
Copyright (c) 2023, Meta
SPDX-License-Identifier: Apache-2.0
"""
import argparse
def write_hidden_start(file):
file.write("/**\n")
file.write(" * @cond INTERNAL_HIDDEN\n")
file.write(" */\n")
def write_hidden_stop(file):
file.write("/**\n")
file.write(" * INTERNAL_HIDDEN @endcond\n")
file.write(" */\n")
def gen_util_listify(limit:int):
with open("util_listify.h", "w") as file:
write_hidden_start(file)
file.write("\n")
file.write("#ifndef ZEPHYR_INCLUDE_SYS_UTIL_LOOPS_H_\n")
file.write("#error \"This header should not be used directly, please include util_loops.h instead\"\n")
file.write("#endif /* ZEPHYR_INCLUDE_SYS_UTIL_LOOPS_H_ */\n")
file.write("\n")
file.write("#ifndef ZEPHYR_INCLUDE_SYS_UTIL_LISTIFY_H_\n")
file.write("#define ZEPHYR_INCLUDE_SYS_UTIL_LISTIFY_H_\n")
file.write("\n")
file.write("/* Set of UTIL_LISTIFY particles */\n")
file.write("#define Z_UTIL_LISTIFY_0(F, sep, ...)\n\n")
file.write("#define Z_UTIL_LISTIFY_1(F, sep, ...) \\\n")
file.write(" F(0, __VA_ARGS__)\n\n")
for i in range(2, limit + 3):
file.write(f"#define Z_UTIL_LISTIFY_{i}(F, sep, ...) \\\n")
file.write(f" Z_UTIL_LISTIFY_{i - 1}(F, sep, __VA_ARGS__) __DEBRACKET sep \\\n")
file.write(f" F({i - 1}, __VA_ARGS__)\n")
file.write("\n")
file.write("#endif /* ZEPHYR_INCLUDE_SYS_UTIL_LISTIFY_H_ */\n")
file.write("\n")
write_hidden_stop(file)
def gen_util_internal_is_eq(limit):
with open("util_internal_is_eq.h", "w") as file:
write_hidden_start(file)
file.write("\n")
file.write("#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_\n")
file.write("#error \"This header should not be used directly, \
please include util_internal.h instead\"\n")
file.write("#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_ */\n")
file.write("\n")
file.write("#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_IS_EQ_H_\n")
file.write("#define ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_IS_EQ_H_\n")
file.write("\n")
for i in range(0, limit + 1):
file.write(f"#define Z_IS_{i}_EQ_{i}(...) \\,\n")
file.write("\n")
file.write("#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_IS_EQ_H_ */\n")
file.write("\n")
write_hidden_stop(file)
def gen_util_internal_util_inc(limit):
with open("util_internal_util_inc.h", "w") as file:
write_hidden_start(file)
file.write("\n")
file.write("#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_\n")
file.write("#error \"This header should not be used directly, \
please include util_internal.h instead\"\n")
file.write("#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_ */\n")
file.write("\n")
file.write("#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_UTIL_INC_H_\n")
file.write("#define ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_UTIL_INC_H_\n")
file.write("\n")
for i in range(0, limit + 2):
file.write(f"#define Z_UTIL_INC_{i} {i + 1}\n")
file.write("\n")
file.write("#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_UTIL_INC_H_ */\n")
file.write("\n")
write_hidden_stop(file)
def gen_util_internal_util_dec(limit):
with open("util_internal_util_dec.h", "w") as file:
write_hidden_start(file)
file.write("\n")
file.write("#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_\n")
file.write("#error \"This header should not be used directly, \
please include util_internal.h instead\"\n")
file.write("#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_ */\n")
file.write("\n")
file.write("#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_UTIL_DEC_H_\n")
file.write("#define ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_UTIL_DEC_H_\n")
file.write("\n")
file.write(f"#define Z_UTIL_DEC_0 0\n")
for i in range(1, limit + 2):
file.write(f"#define Z_UTIL_DEC_{i} {i - 1}\n")
file.write("\n")
file.write("#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_UTIL_DEC_H_ */\n")
file.write("\n")
write_hidden_stop(file)
def gen_util_internal_util_x2(limit):
with open("util_internal_util_x2.h", "w") as file:
write_hidden_start(file)
file.write("\n")
file.write("#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_\n")
file.write("#error \"This header should not be used directly, \
please include util_internal.h instead\"\n")
file.write("#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_ */\n")
file.write("\n")
file.write("#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_UTIL_X2_H_\n")
file.write("#define ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_UTIL_X2_H_\n")
file.write("\n")
for i in range(0, limit + 1):
file.write(f"#define Z_UTIL_X2_{i} {i *2}\n")
file.write("\n")
file.write("#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_UTIL_X2_H_ */\n")
file.write("\n")
write_hidden_stop(file)
if __name__ == "__main__":
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument(
"-l", "--limit", type=int, required=True, help="Limit of macros"
)
args = parser.parse_args()
gen_util_listify(args.limit)
gen_util_internal_is_eq(args.limit)
gen_util_internal_util_inc(args.limit)
gen_util_internal_util_dec(args.limit)
gen_util_internal_util_x2(args.limit)
Loading…
Cancel
Save