Skip to content

Commit

Permalink
Update documentation: separate out development instructions from usag…
Browse files Browse the repository at this point in the history
…e and use relative links everywhere.
  • Loading branch information
adbancroft committed Oct 27, 2024
1 parent 7f5f809 commit 9597de0
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 119 deletions.
131 changes: 19 additions & 112 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
# libdivide

[![Build Status](https://github.com/ridiculousfish/libdivide/actions/workflows/canary_build.yml/badge.svg)](https://github.com/ridiculousfish/libdivide/actions/workflows/canary_build.yml)
[![Github Releases](https://img.shields.io/github/release/ridiculousfish/libdivide.svg)](https://github.com/ridiculousfish/libdivide/releases)

```libdivide.h``` is a header-only C/C++ library for optimizing integer division.
Integer division is one of the slowest instructions on most CPUs e.g. on
current x64 CPUs a 64-bit integer division has a latency of up to 90 clock
cycles whereas a multiplication has a latency of only 3 clock cycles.
libdivide allows you to replace expensive integer division instructions by
a sequence of shift, add and multiply instructions that will calculate
the integer division much faster.

On current CPUs you can get a **speedup of up to 10x** for 64-bit integer division
and a speedup of up to to 5x for 32-bit integer division when using libdivide.
libdivide also supports [SSE2](https://en.wikipedia.org/wiki/SSE2),
[AVX2](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions) and
[AVX512](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions)
vector division which provides an even larger speedup. You can test how much
speedup you can achieve on your CPU using the [benchmark](#benchmark-program)
program.
```libdivide.h``` is a header-only C/C++ library for optimizing integer division. Integer division is one of the slowest instructions on most CPUs e.g. on current x64 CPUs a 64-bit integer division has a latency of up to 90 clock cycles whereas a multiplication has a latency of only 3 clock cycles. libdivide allows you to replace expensive integer division instructions by a sequence of shift, add and multiply instructions that will calculate the integer division much faster.

libdivide is compatible with 8-bit microcontrollers, such as the AVR series: [the CI build includes a AtMega2560 target](test/avr/readme.md). Since low end hardware such as this often do not include a hardware divider, libdivide is particularly useful. In addition to the runtime [C](https://github.com/ridiculousfish/libdivide/blob/master/doc/C-API.md) & [C++](https://github.com/ridiculousfish/libdivide/blob/master/doc/CPP-API.md) APIs, a set of [predefined macros](constant_fast_div.h) and [templates](constant_fast_div.hpp) is included to speed up division by 16-bit constants: division by a 16-bit constant is [not optimized by avr-gcc on 8-bit systems](https://stackoverflow.com/questions/47994933/why-doesnt-gcc-or-clang-on-arm-use-division-by-invariant-integers-using-multip).
On current CPUs you can get a **speedup of up to 10x** for 64-bit integer division and a speedup of up to to 5x for 32-bit integer division when using libdivide. libdivide also supports [SSE2](https://en.wikipedia.org/wiki/SSE2), [AVX2](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions) and [AVX512](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions) vector division which provides an even larger speedup. You can test how much speedup you can achieve on your CPU using the [benchmark](#benchmark-program) program.

libdivide is compatible with 8-bit microcontrollers, such as the AVR series: [the CI build includes a AtMega2560 target](test/avr/readme.md). Since low end hardware such as this often do not include a hardware divider, libdivide is particularly useful. In addition to the runtime [C](doc/C-API.md) & [C++](doc/CPP-API.md) APIs, a set of [predefined macros](constant_fast_div.h) and [templates](constant_fast_div.hpp) is included to speed up division by 16-bit constants: division by a 16-bit constant is [not optimized by avr-gcc on 8-bit systems](https://stackoverflow.com/questions/47994933/why-doesnt-gcc-or-clang-on-arm-use-division-by-invariant-integers-using-multip).

See https://libdivide.com for more information on libdivide.

# C++ example
## C++ example

The first code snippet divides all integers in a vector using integer division.
This is slow as integer division is at least one order of magnitude slower than
Expand Down Expand Up @@ -60,7 +48,7 @@ Generally libdivide will give a significant speedup if:
* The divisor is only known at runtime
* The divisor is reused multiple times e.g. in a loop

# C example
## C example

You first need to generate a libdivide divider using one of the ```libdivide_*_gen``` functions (```*```: ```s32```, ```u32```, ```s64```, ```u64```)
which can then be used to compute the actual integer division using the
Expand All @@ -79,28 +67,19 @@ void divide(int64_t *array, size_t size, int64_t divisor)
}
```
# API reference
## API reference
* [C API](https://github.com/ridiculousfish/libdivide/blob/master/doc/C-API.md)
* [C++ API](https://github.com/ridiculousfish/libdivide/blob/master/doc/CPP-API.md)
* [C API](doc/C-API.md)
* [C++ API](doc/CPP-API.md)
* [Macro Invariant Division](constant_fast_div.h)
* [Template Based Invariant Division](constant_fast_div.hpp)
# Branchfull vs branchfree
## Branchfull vs branchfree
The default libdivide divider makes use of
[branches](https://en.wikipedia.org/wiki/Branch_(computer_science)) to compute the integer
division. When the same divider is used inside a hot loop as in the C++ example section the
CPU will accurately predict the branches and there will be no performance slowdown. Often
the compiler is even able to move the branches outside the body of the loop hence
completely eliminating the branches, this is called loop-invariant code motion.
libdivide also has a branchfree divider type which computes the integer division without
using any branch instructions. The branchfree divider generally uses a few more instructions
than the default branchfull divider. The main use case for the branchfree divider is when
you have an array of different divisors and you need to iterate over the divisors. In this
case the default branchfull divider would exhibit poor performance as the CPU won't be
able to correctly predict the branches.
[branches](https://en.wikipedia.org/wiki/Branch_(computer_science)) to compute the integer division. When the same divider is used inside a hot loop as in the C++ example section the CPU will accurately predict the branches and there will be no performance slowdown. Often the compiler is even able to move the branches outside the body of the loop hence completely eliminating the branches, this is called loop-invariant code motion.
libdivide also has a branchfree divider type which computes the integer division without using any branch instructions. The branchfree divider generally uses a few more instructions than the default branchfull divider. The main use case for the branchfree divider is when you have an array of different divisors and you need to iterate over the divisors. In this case the default branchfull divider would exhibit poor performance as the CPU won't be able to correctly predict the branches.
```C++
#include "libdivide.h"
Expand All @@ -124,14 +103,12 @@ Caveats of branchfree divider:
* Unsigned branchfree divider cannot be ```1```
* Faster for unsigned types than for signed types

# Vector division
## Vector division

libdivide supports [SSE2](https://en.wikipedia.org/wiki/SSE2),
[AVX2](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions) and
[AVX512](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions)
vector division on x86 and x64 CPUs. In the example below we divide the packed 32-bit
integers inside an AVX512 vector using libdivide. libdivide supports 32-bit and 64-bit
vector division for both signed and unsigned integers.
vector division on x86 and x64 CPUs. In the example below we divide the packed 32-bit integers inside an AVX512 vector using libdivide. libdivide supports 32-bit and 64-bit vector division for both signed and unsigned integers.

```C++
#include "libdivide.h"
Expand All @@ -153,7 +130,7 @@ Note that you need to define one of macros below to enable vector division:
* ```LIBDIVIDE_AVX512```
* ```LIBDIVIDE_NEON```
# Performance tips
## Performance Tips
* If possible use unsigned integer types because libdivide's unsigned division is measurably
faster than its signed division. This is especially true for the branchfree divider.
Expand All @@ -165,76 +142,6 @@ Note that you need to define one of macros below to enable vector division:
currently no vector multiplication instructions on x86 to efficiently calculate
64-bit * 64-bit to 128-bit.
# Build instructions
libdivide has one test program and two benchmark programs which can be built using cmake and
a recent C++ compiler that supports C++11 or later. Optionally ```libdivide.h``` can also be
installed to ```/usr/local/include```.
```bash
cmake .
make -j
sudo make install
```

# Tester program

You can pass the **tester** program one or more of the following arguments: ```u32```,
```s32```, ```u64```, ```s64``` to test the four cases (signed, unsigned, 32-bit, or 64-bit), or
run it with no arguments to test all four. The tester will verify the correctness of libdivide
via a set of randomly chosen numerators and denominators, by comparing the result of libdivide's
division to hardware division. It will stop with an error message as soon as it finds a
discrepancy.

# Benchmark program

You can pass the **benchmark** program one or more of the following arguments: ```u16```, ```s16```, ```u32```,
```s32```, ```u64```, ```s64``` to compare libdivide's speed against hardware division.
**benchmark** tests a simple function that inputs an array of random numerators and a single
divisor, and returns the sum of their quotients. It tests this using both hardware division, and
the various division approaches supported by libdivide, including vector division.

It will output data like this:

```bash
# system scalar scl_bf vector vec_bf gener algo
1 9.684 0.792 0.783 0.426 0.426 1.346 0
2 9.078 0.781 1.609 0.426 1.529 1.346 0
3 9.078 1.355 1.609 1.334 1.531 29.045 1
4 9.076 0.787 1.609 0.426 1.529 1.346 0
5 9.074 1.349 1.609 1.334 1.531 29.045 1
6 9.078 1.349 1.609 1.334 1.531 29.045 1
...
```

It will keep going as long as you let it, so it's best to stop it when you are happy with the
denominators tested. These columns have the following significance. All times are in
nanoseconds, lower is better.

```
#: The divisor that is tested
system: Hardware divide time
scalar: libdivide time, using scalar division
scl_bf: libdivide time, using scalar branchfree division
vector: libdivide time, using vector division
vec_bf: libdivide time, using vector branchfree division
gener: Time taken to generate the divider struct
algo: The algorithm used.
```

The **benchmark** program will also verify that each function returns the same value,
so benchmark is valuable for its verification as well.

# Contributing

Although there are no individual unit tests, the supplied ```cmake``` builds do include several safety nets:

* They compile with:
* All warnings on and;
* Warnings as errors
* The CI build will build and run with sanitizers on ; these are available as part of the cmake build: ```-DCMAKE_BUILD_TYPE=Sanitize```
* The ```cmake``` and CI builds will compile and run both ```C``` and ```C++``` test programs.

Before sending in patches, build and run at least the ```tester``` and ```benchmark``` using the supplied ```cmake``` scripts on at least ```MSVC``` and ```GCC``` (or ```Clang```).
## Contributing
### Happy hacking!
See the [Development Guide](doc/Development%20Guide.md)
133 changes: 133 additions & 0 deletions doc/Development Guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# libdivide Development Guide

Before sending in patches, build and run at least the ```tester``` and ```benchmark``` using the supplied ```cmake``` scripts on at least ```MSVC``` and ```GCC``` (or ```Clang```).

See [here](../README.md) for an overview of libdivide.

See [here](../test/avr/readme.md) for building & testing on a microcontroller.

## Building

### Summary

libdivide uses the same [```CMake```](https://cmake.org/) [script](./CMakeLists.txt) for local & [CI](#continuous-integration) builds.

Sample build commands from [CI runs](https://github.com/ridiculousfish/libdivide/actions/workflows/canary_build.yml):
* Clang
```pwsh
cmake -B ./build -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLIBDIVIDE_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Release
cmake --build ./build --config Release
```
* MSVC (v2022)
```pwsh
cmake.exe -B ./build -G "Visual Studio 17 2022" -DCMAKE_C_COMPILER=cl.exe -DCMAKE_CXX_COMPILER=cl.exe -DLIBDIVIDE_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Release
cmake --build ./build --config Sanitize
```

### Detail

The script supports 5 target binaries (```--target``` CMake parameter):

| Target | Purpose |
|----------------------|-------------------------------|
| benchmark | [Benchmarking](#benchmarking) |
| benchmark_branchfree | [Benchmarking](#benchmarking) |
| tester | [Testing](#testing) |
| tester_c99 | [Testing](#testing) |
| fast_div_generator | Code Gen |

The CMake scripts support 4 build types (```-DCMAKE_BUILD_TYPE``` and ```--config``` CMake parameters):

* Debug
* Release
* RelWithDebInfo (Release + Debug Info, useful for debugging)
* Sanitizers (Release + Debug Info + [Sanitizers](https://github.com/google/sanitizers))

Build are configured tp compile with:
* All warnings on; and
* Warnings as errors

Optionally ```libdivide.h``` can also be installed to ```/usr/local/include```:

```bash
cmake .
make -j
sudo make install
```

## Testing

### Summary

The CMake script supports CTest, so running tests is as simple as:

```pwsh
cd ./build
ctest --build-config Sanitizers --verbose
```

### Detail

* Tester.exe: pass one or more of the following arguments: ```u16```, ```s16```, ```u32```, ```s32```, ```u64```, ```s64``` to test the six cases (signed, unsigned, 16-bit, 32-bit, or 64-bit), or run it with no arguments to test all six. The tester will verify the correctness of libdivide via a set of randomly chosen numerators and denominators, by comparing the result of libdivide's division to hardware division. It will stop with an error message as soon as it finds a discrepancy.
* Tester_c99: this is a parameterless safety net program to endure libdivide remains C99 compatible. I.e. any use of C++ language features is limited to C++ hosts.

## Benchmarking

### benchmark.exe

Pass one or more of the following arguments: ```u16```, ```s16```, ```u32```, ```s32```, ```u64```, ```s64``` to compare libdivide's speed against hardware division.

**benchmark** tests a simple function that inputs an array of random numerators and a single divisor, and returns the sum of their quotients. It tests this using both hardware division, and the various division approaches supported by libdivide, including vector division.

It will output data like this:

```bash
# system scalar scl_bf vector vec_bf gener algo
1 9.684 0.792 0.783 0.426 0.426 1.346 0
2 9.078 0.781 1.609 0.426 1.529 1.346 0
3 9.078 1.355 1.609 1.334 1.531 29.045 1
4 9.076 0.787 1.609 0.426 1.529 1.346 0
5 9.074 1.349 1.609 1.334 1.531 29.045 1
6 9.078 1.349 1.609 1.334 1.531 29.045 1
...
```

It will keep going as long as you let it, so it's best to stop it when you are happy with the denominators tested. These columns have the following significance. All times are in nanoseconds, lower is better.

```
#: The divisor that is tested
system: Hardware divide time
scalar: libdivide time, using scalar division
scl_bf: libdivide time, using scalar branchfree division
vector: libdivide time, using vector division
vec_bf: libdivide time, using vector branchfree division
gener: Time taken to generate the divider struct
algo: The algorithm used.
```

The **benchmark** program will also verify that each function returns the same value, so benchmark is valuable for verification as well.

### benchmark_branchfree

The branchfree benchmark iterates over an array of dividers and computes divisions. This is the use case where the branchfree divider generally shines and where the default branchfull divider performs poorly because the CPU is not able to correctly predict the branches of the many different dividers.

## Continuous Integration

CI is implemented using [GitHub actions](https://github.com/ridiculousfish/libdivide/actions):

* Uses the ```cmake``` script described above.
* Builds all targets using various combinations of OS (Windows, Linux), compilers (GCC, Clang, MSVC), build types (Release, Sanitize).
* Runs ```tester```, ```test_c99``` and ```benchmark_branchfree``` in all build configurations.

## Releasing

Releases are semi-automated using GitHub actions:

1. Manually run the [Create draft release](https://github.com/ridiculousfish/libdivide/actions/workflows/prepare_release.yml) workflow/action: this does some codebase housekeeping (generating a new commit) and creates a draft release.
2. Follow the output link in the action summary to the generated draft release. E.g.
![image](https://github.com/user-attachments/assets/7e8393f7-f204-4b3a-af37-de5e187479dc)
3. Edit the generated release notes as needed & publish

Note that PRs with the ```ignore-for-release``` label are excluded from the generated release notes.

### Happy hacking!
14 changes: 7 additions & 7 deletions test/avr/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

## Running the Test program

The test program is in the 'megaatmega2560_Test' environment.
The test program is the 'megaatmega2560_sim_unittest' environment.

To run the test program in a simulator:
1. On the activity bar, select PlatformIO
2. Run Project Tasks -> megaatmega2560_Test -> Custom -> Simulate
a. This will build the test program & launch it in the simulator (this might download )supporting packages)
b. **NOTE** Once running it can take a **long** time for ouput to appear in the terminal. **Be patient**
* Or copy the simavr command line from the terminal to a command prompt (or another vscode terminal)
To run the test program in a simulator (no hardware required!):

1. On the activity bar, select PlatformIO
2. Run Project Tasks -> megaatmega2560_sim_unittest -> Advanced -> Test
1. This will build the test program & launch it in the simulator (this might download supporting packages)
2. **NOTE** Once running it can take a **long** time for ouput to appear in the terminal. **Be patient**

0 comments on commit 9597de0

Please sign in to comment.