Skip to content

__udivdi3(), __divdi3(), __umoddi3(), __moddi3(), etc. for embedded GCC

License

Notifications You must be signed in to change notification settings

glitchub/arith64

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

32- and 64-bit arithmetic functions for use with 32-bit GCC.

Some versions of 32-bit GCC may emit calls to external helper functions to
perform certain 32- and 64-bit operations. Normally these functions are
resolved by libgcc.a which is statically linked to the program.

But libgcc may not be usable in some applications, e.g. embedded systems and
linux kernel drivers. In those cases you'll get an linker error such as:

    undefined reference to `__divdi3'

The solution is to link arith64.c to your code, or just copy the required
functions.

Also see https://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html.

Note not all operations have been implemented, just the ones I've required to
date.

====

'make' performs Monte-Carlo validation testing. Two test executables are
created, one linked to arith64.c and one to libgcc. test.py launches these into
the background and then passes identical sets of random numbers to each,
comparing their outputs. On mismatch it reports error and exits, otherwise it
will run forever, printing the total number of test performed every 5 seconds.

'make bench' starts the test executables in benchmark mode, they perform each
operation one million times and print the average elapsed time in nanoseconds.
test.py then collates the returned information and prints a running average
every 5 seconds, e.g.:

1,772 cases
          arith64      gcc
   abs :   3.85nS   6.15nS
  ashl :   4.22nS   5.00nS
  ashr :   4.31nS   4.04nS
  clzd :   6.51nS   3.62nS
  clzs :   5.14nS   2.98nS
  ctsz :   4.63nS   2.66nS
  ctzd :   6.01nS   2.98nS
   div :  15.41nS   8.12nS
   ffs :   6.19nS   2.80nS
   mod :  17.84nS   8.99nS
  popd :   8.72nS   5.55nS
  pops :   3.94nS   4.40nS
   shr :   4.50nS   4.45nS
  udiv :  11.47nS   5.64nS
  umod :  11.56nS   6.74nS

This shows that the gcc native functions, presumably written in assembly
language and leveraging CPU-specific arithmetic operators, are generally twice
as fast as arith64 equivalents.

NOTE: the test code is built in 32-bit mode. If your development system is
64-bit you'll need to install 32-bit compatible headers and libraries. On
debian this can be done with "sudo apt install gcc-multilib".

About

__udivdi3(), __divdi3(), __umoddi3(), __moddi3(), etc. for embedded GCC

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published