Word and Double Word integers in C


I am trying to implement a simple, moderately efficient bignum library in C. I would like to store digits using the full register size of the system it's compiled on (presumably 32 or 64-bit ints). My understanding is that I can accomplish this using intptr_t. Is this correct? Is there a more semantically appropriate type, i.e. something like intword_t?

I also know that with GCC I can easily do overflow detection on a 32-bit machine by upcasting both arguments to 64-bit ints, which will occupy two registers and take advantage of instructions like IA31 ADC (add with carry). Can I do something similar on a 64-bit machine? Is there a 128-bit type I can upcast to which will compile to use these instructions if they're available? Better yet, is there a standard type that represents twice the register size (like intdoubleptr_t) so this could be done in a machine independent fashion?



Any reason not to use size_t? size_t is 4 bytes on a 32-bit system and 8 bytes on a 64-bit system, and is probably more portable than using WORD_SIZE (I think WORD_SIZE is gcc-specific, no?)

I am not aware of any 128-bit value on 64-bit systems, could be wrong here but haven't come across that type in the kernel or regular user apps.


I'd strongly recommend using the C99 <stdint.h> header. It declares int32_t, int64_t, uint32_t, and uint64_t, which look like what you really want to use.

EDIT: As Alok points out, int_fast32_t, int_fast64_t, etc. are probably what you want to use. The number of bits you specify should be the minimum you need for the math to work, i.e. for the calculation to not "roll over".

The optimization comes from the fact that the CPU doesn't have to waste cycles realigning data, padding the leading bits on a read, and doing a read-modify-write on a write. Truth is, a lot of processors (such as recent x86s) have hardware in the CPU that optimizes these access pretty well (at least the padding and read-modify-write parts), since they're so common and usually only involve transfers between the processor and cache.

So the only thing left for you to do is make sure the accesses are aligned: take sizeof(int_fast32_t) or whatever and use it to make sure your buffer pointers are aligned to that.

Truth is, this may not amount to that much improvement (due to the hardware optimizing transfers at runtime anyway), so writing something and timing it may be the only way to be sure. Also, if you're really crazy about performance, you may need to look at SSE or AltiVec or whatever vectorization tech your processor has, since that will outperform anything you can write that is portable when doing vectored math.


  • How can I substitute my own custom dynamic scaffolding methods
  • Equivalent for np.add.at in tensorflow
  • Is it good practice to put Edge Side Includes into my templates?
  • Imagemagick set interline spacing?
  • parallelize process in missForest package
  • Moving data between processes in Spartan 3
  • Lua string.gsub() by '%s' or '\\n' pattern
  • Query pkg-config variable through autotools
  • Generic classes with Collection getter of other types
  • NSMutableArray instance used in a block
  • MySQL performance when updating row with FK
  • Using Sax parsing to edit and write XML in VB6
  • Security issues with PHP's Readfile method
  • Create Instant using a negative year
  • Debug.DrawLine not showing in the GameView
  • How do I open a C file with a relative path?
  • copying resource to sdcard gives a damaged file in android
  • Yii2: Config params vs. const/define
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Spark fat jar to run multiple versions on YARN
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Using variable in a value field in jMeter
  • Adding a button at the bottom of a table view
  • Deleting and Updating values from a cusrsor adapter
  • Sony Xperia Z Tablet not found by adb
  • Is possible to count alias result on mysql
  • Importing jscolor library in angular 2
  • To display the title for the current loaction in map in iphone
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • php design question - will a Helper help here?
  • KeystoneJS: Relationships in Admin UI not updating
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • LevelDB C iterator
  • Is it possible to post an object from jquery to bottle.py?
  • UserPrincipal.Current returns apppool on IIS