Daniel Lemire's blog

, 1 min read

Rounding integers to even, efficiently

2 thoughts on “Rounding integers to even, efficiently”

  1. Falk Hüffner says:

    Depending on which instructions are available, you might get slightly better code by replacing the last two lines with

    return (d | (roundup ^ ismultiple)) & roundup;

    1. Wow. I doubt it will be faster, at least on recent x64 processors, but your approach is nicer in that it is explicitly branchless. I like it a lot!!!