How to abstract from integer type in math software

I am developing math software with algorithms that work for universal integer types such as machine ints or GMP integers. Performance usually requires working with machine ints, but if there is an overflow, then you can try switching to GMP; perfect at runtime. So far, the entire program is written as a template over an integer type. As the library grows, the pain intensifies:

  • Compilation time and memory consumption are out of control.
  • Error messages during compilation are less useful.
  • Debugging is more painful.
  • All code is in header files.

I can think of the following solution. Code refactoring depends on a fixed type that is typedef'ed through a compile-time macro. Then create several copies of the library, one for each integer type, and combine them together in an executable file. The downside seems to be that I need a library interface for myself.

Short question: what are the design patterns for situations in which almost the entire program depends on the type?

+6
source share
2 answers

GNU Multipoint Arithmetic Library was

carefully designed to be as fast as possible, both for small operands and for huge operands.

In other words, if you use the GMP library, this will help you deal with these difficulties and save you a lot of effort!

+4
source

In my own math software, I use GMP by default, but I want to provide a backup if it is not available. I also did not like the huge gmpxx header file, which slows down compilation on my slow machine.

So, I essentially wrote a wrapper class over the integer undefined (using pimpl using std::aligned_storage ). The back can be selected at compile time.

This eliminates the need for templates and gives me enough flexibility.

+2
source

All Articles