Skip to content

saturating_add doc is not clear enough #64940

Closed

Description

Godbolt link: https://godbolt.org/z/9rm4jF

So I expect these two functions has the same assembly:

pub fn foo(a: i32, b: i32) -> i32 {
    a.saturating_add(b)
}

pub fn bar(a: i32, b: i32) -> i32 {
    match a.checked_add(b) {
        Some(v) => v,
        None => i32::max_value(),
    }
}

But the result is not:

example::foo:
        xor     eax, eax
        mov     ecx, edi
        add     ecx, esi
        setns   al
        add     eax, 2147483647
        add     edi, esi
        cmovno  eax, edi
        ret

example::bar:
        add     edi, esi
        mov     eax, 2147483647
        cmovno  eax, edi
        ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions