Title | kdiv (Constant division routine generator) |
Author | Nikolaos Kavvadias |
Contact | |
Website | http://www.nkavvadias.com |
Release Date | 29 November 2014 |
Version | 0.1.1 |
Rev. history | |
v0.1.1 | 2014-11-29 Added project logo in README. |
v0.1.0 | 2014-10-16 Documentation updates and fixes. |
v0.0.9 | 2014-06-13 Renamed README to README.rst. |
v0.0.8 | 2014-06-12 Updated contact information. Replaced COPYING.BSD by LICENSE. |
v0.0.7 | 2013-04-28 Converted documentation to RestructuredText. |
v0.0.6 | 2012-03-17 Split build-and-test scripts to build and test. |
v0.0.5 | 2011-12-03 Minor README updates regarding multiple releases, tutorial usage. |
v0.0.4 | 2011-11-20 Minor README, Makefile updates. |
v0.0.3 | 2011-11-09 Added omitted constant value for M in C routines. |
v0.0.2 | 2011-09-16 Small fixes, avoids emitting redundant shift. |
v0.0.1 | 2011-05-21 Initial release. |
kdiv is a generator for routines for optimized division by an integer constant. It can be used for calculating an integer division with the routines presented in Henry S. Warren's "Hacker's Delight" book. kdiv can also be used for emitting a NAC (generic assembly language) or ANSI C implementation of the division.
The kdiv distribution includes the following files:
/kdiv | Top-level directory |
LICENSE | Description of the Modified BSD license. |
Makefile | Makefile for generating the kdiv executable. |
README.html | HTML version of README.rst. |
README.pdf | PDF version of README.rst. |
README.rst | This file. |
build.sh | Build script for kdiv. |
kdiv.c | The source code for the application. |
kdiv.png | PNG image for the kdiv project logo. |
rst2docs.sh | Bash script for generating the HTML and PDF versions. |
test.c | Sample test file. |
test.opt.c | Expected optimized version of test.c. |
test.sh | Perform some sample runs. |
There exists a quite portable Makefile (Makefile in the current directory). Running make from the command prompt should compile kdiv.
The kdiv program can be invoked with several options (see complete option listing below). The usual tasks that can be accomplished with kdiv are:
ANSI C routines have been tested only for a width of 32-bits (see option below).
kdiv can be invoked as:
The complete kdiv options listing:
Here follow some simple usage examples of kdiv.
3. Generate the ANSI C implementation of n/23 optimized routine. Also run some tests with an internal generator for the dividend range [0..1024].
kdiv can be used for arithmetic optimizations in user programs. Assume the following user program (test.c):
// test.c #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int a, b; a = atoi(argv[1]); b = a / 23; printf("b = %d\n", b); return b; }
This file is compiled and run as follows with one additional argument:
and the expected result is:
The user can apply kdiv for generating a constant division routine for a/23:
and the corresponding routine is produced. Then, the user should edit a new file, let's say test.opt.c and include the produced routine. The resulting optimized source file should be as follows:
// test.opt.c #include <stdio.h> #include <stdlib.h> inline signed int kdiv_s32_p_23 (signed int n) { signed int q, M=-1307163959, c; signed long long int t, u, v; t = (signed long long int)M * (signed long long int)n; q = t >> 32; q = q + n; q = q >> 4; c = n >> 31; q = q + c; return (q); } int main(int argc, char *argv[]) { int a, b; a = atoi(argv[1]); b = kdiv_s32_p_23(a); printf("b = %d\n", b); return b; }
This file is compiled and run as follows with one additional argument:
The target platform compiler (e.g. gcc or llvm) is expected to inline the kdiv_s32_p_23 function at its call site.
In order to build and run a series of sample tests do the following:
You may contact me for further questions/suggestions/corrections at: