Skip to content

Faster division by constants that aren't known at compile-time

License

Notifications You must be signed in to change notification settings

pkhuong/reciprocal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Baseline implementation of division by constants

Build Status Coverage Status

When dividing integers by compile-time constants, compilers (LLVM) can be trusted to convert those to a sequence of multiplication and shift.

That doesn't work so well when the constant isn't known until runtime.

This crate implements a simple strategy with reliable performance. Does reliable imply good? For this application, it does.

The basic PartialReciprocal should be compiled to a constant-time fast path, and can handle every divisor except 0, 1, and u64::MAX.

The slightly more complex Reciprocal can also divide by 1 and u64::MAX, at the expense of one more u64 field, and a slightly more complex (one more load, maybe one more integer arithmetic instruction) fast path.

About

Faster division by constants that aren't known at compile-time

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages