Skip to content

Make saturating_add and saturating_sub const functions #58030

Closed
@oli-obk

Description

@oli-obk

These used to require if to work, but are forwarded to llvm intrinsics nowadays (#58003). We can now implement them as const eval intrinsics, too.

Impl instructions:

  1. add to
    | "add_with_overflow" // ~> .overflowing_add
  2. remove unsafe for calls to the intrinsic
  3. add to
  4. make functions calling the intrinsic const fn
  5. Implement in
    | "overflowing_add"
    (implementation is basically the two read_immediate calls, a call to binary_op_imm with arguments similar to the other add/sub intrinsics, in case the overflow bool in the return value is false, write the actual value to the destination like in
    self.write_scalar(val, dest)
    . In case the overflow bool is true, compute the min or max value of the type like in
    let max = 1 << (size.bits() - 1);
    and write that value to dest.
  6. Write some tests that use the saturating methods in constants

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions