16th April 2020, 1 min read Rounding integers to even, efficiently 2 thoughts on “Rounding integers to even, efficiently” Falk Hüffner says: April 17, 2020 at 8:08 pm Depending on which instructions are available, you might get slightly better code by replacing the last two lines with return (d | (roundup ^ ismultiple)) & roundup; Daniel Lemire says: April 17, 2020 at 9:56 pm 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!!!
Depending on which instructions are available, you might get slightly better code by replacing the last two lines with
return (d | (roundup ^ ismultiple)) & roundup;
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!!!