Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Preprocessor-Based Arithmetic and Condition Evaluation in Zephyr #83668

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

soburi
Copy link
Member

@soburi soburi commented Jan 7, 2025

It's still in the early stages of implementation and not in perfect condition, but I think it might interest everyone, so I'll put it out as a draft first.

Problem

Currently, DeviceTree macros face limitations in setting flexible compile-time conditions. Specifically, COND_CODE_1 evaluates whether a given argument is 1 or not, but deriving this 1 using only macros can be challenging in certain cases.

Proposed Solution (Overview)

Introduce the capability to perform "calculations" using the preprocessor, allowing for flexible condition evaluation. The following example demonstrates the proposed functionality:

/* (1 + 2 == 9) is not true, so the 3rd argument will remains. */
COND_CODE_1(
    EXPR_IS_TRUE(EXPR_EQ(EXPR_ADD(EXPR_HEX(0x1), EXPR_HEX(0x2)), EXPR_HEX(0x9))),
    (zassert_unreachable("unreachable")),
    (zassert_true(1))
);

Proposed Solution (Details)

To address the problem, implement 32-bit arithmetic calculations with macros. Numbers will be represented as lists of 0 and 1 arguments (referred to as "bit args"). Using these bit args, the following operations will be provided:

  • Comparison operations:
    • EQ(= ,equal)
    • GT(>, greater than)
    • LT(<, less than)
    • GTE(>=, greater than equal)
    • LTE(<=, less than equal)
  • Bitwise operations:
    • OR(|)
    • AND(&)
    • XOR(^)
    • NOT(~)
  • Arithmetic operations:
    • ADD(+, addition)
    • SUB(-, subtraction)
    • MUL(*, multiplication)
    • DIV(/, division)
    • MOD(%, modulus)

This enables complex conditional expressions to be evaluated entirely within the preprocessor.
In other words, it is a proposal to implement a simple computer using only macros.

Considerations

  1. Performance

    Multiplication and division are inherently slower operations. They may need to be avoided in CI environments for performance reasons.
    Introducing a mechanism to limit the bit width of calculations could improve usability and efficiency.

  2. Limitations of Number Representations

    Due to the constraints of the preprocessor, decimal and hexadecimal literals cannot be directly handled. These must first be converted into the internal bit args representation.
    To facilitate this, lookup tables containing up to 4096 entries for both decimal and hexadecimal numbers have been defined. However, this approach has limitations.

Internally, it is possible to generate hexadecimal literals from bit args with ease. As such, outputting results in hexadecimal is unrestricted.

The limitations can be summarized as follows:

Format Input Support Output Support
bit args
Hexadecimal Table-dependent
Decimal Table-dependent Table-dependent
  1. Integration with DeviceTree

    This functionality is designed to work alongside DeviceTree macros.
    Most use cases can be supported by modifying devicetree_generated.h to include bit args values.
    Exceptions may arise when dealing with literals defined in existing headers, which would require manual handling.

Benefits

This feature enables flexible conditional branching and complex calculations at compile time, enhancing the power and versatility of DeviceTree macros.

soburi added 6 commits January 8, 2025 07:30
Adds the XOR operation to the set of logical operation macros
for preprocessors.

Signed-off-by: TOKITA Hiroshi <[email protected]>
Gets the specified number of elements from the beginning of the list.
This is the symmetircal operation as `GET_ARGS_LESS_N`.

Signed-off-by: TOKITA Hiroshi <[email protected]>
add recursive helper

Signed-off-by: TOKITA Hiroshi <[email protected]>
Adds macros `UTIL_ADD` and `UTIL_SUB` that allow addition and
subtraction of decimal literals within the range of 0 to 4095.

Signed-off-by: TOKITA Hiroshi <[email protected]>
expression utils

Signed-off-by: TOKITA Hiroshi <[email protected]>
Signed-off-by: TOKITA Hiroshi <[email protected]>
@soburi soburi added the RFC Request For Comments: want input from the community label Jan 7, 2025
@soburi soburi changed the title Preprocessor-Based Arithmetic and Condition Evaluation in Zephyr [RFC] Preprocessor-Based Arithmetic and Condition Evaluation in Zephyr Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request For Comments: want input from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant