Numbers larger than 8 bytes in C
I am writing code to handle numbers in C larger than 8 bytes (don't fit in unsigned long
). For this example, I will use 16 bytes (128 bits) as the width. The numbers are unsigned and whole numbers (no decimal places). They are stored as an unsigned character array, for example:
unsigned char n[16];
I was able to get the complement to work (it works like an unsigned number in C, so if you have a number that was 0xffffffffffffffffffffffffffffffff
(2 ** 128) and you have to add 1
, you get 0
I was able to get the complement to work, but I can't get the subtraction to work I would assume it would be similar code to add, but I can't seem to get it to work.
Add code:
//a and b are numbers
unsigned char *add(unsigned char *a, unsigned char *b){
unsigned char *c = malloc(NUM_SIZE);
//d is the carry and c is the output number
unsigned short d = 0;
if(!c){
return NULL;
}
for(int i = 0; i < NUM_SIZE; i++){
c[i] = 0;
}
for(int i = NUM_SIZE * 2 - 1; i >= 0; i--){
d += a[i % NUM_SIZE] + b[i % NUM_SIZE];
c[i % NUM_SIZE] = d % 256;
d >>= 8;
}
return c;
}
NUM_SIZE
defined as 16 (number width in bytes)
What I have tried:
//changing the signs to minuses
d -= a[i % NUM_SIZE] - b[i % NUM_SIZE];
//changing the some signs to minuses
d -= a[i % NUM_SIZE] + b[i % NUM_SIZE];
//or
d += a[i % NUM_SIZE] - b[i % NUM_SIZE];
//looping through the number backwards
for(int i = 0; i < NUM_SIZE * 2; i++)
source to share
Just an idea (not compiled):
void not( unsigned char* a, unsigned int n )
{
for ( unsigned int i = 0; i < n; ++i )
a[i] = ~a[i];
}
void inc( unsigned char* a, unsigned int n )
{
for ( unsigned int i = 0; i < n; ++i )
if ( ++a[i] )
return;
}
void add( unsigned char* c, unsigned char* a, unsigned char* b, unsigned int n )
{
for ( unsigned int i = 0, r = 0; i < n; ++i )
c[i] = r = a[i] + b[i] + ( r >> 8 );
}
void sub( unsigned char* c, unsigned char* a, unsigned char* b, unsigned int n )
{
not( b, n );
add( c, a, b, n );
not( b, n ); // revert
inc( c, n );
}
source to share
You can use arbitrary precision arithmetic aka like bigint or bignum. You should use a library for this (as bignum algorithms are very smart and use some assembly code). I recommend GMPlib . See also .
source to share
NUM_SIZE * 2
doesn't make sense with malloc(NUM_SIZE); ... for(int i = NUM_SIZE * 2 - 1
. It only needs a loop of iterations NUM_SIZE
.
Recovered code
#define NUM_SIZE 8
//a - b
unsigned char *sub(const unsigned char *a, const unsigned char *b) {
unsigned char *c = malloc(NUM_SIZE);
if (!c) {
return NULL;
}
// zeroing `c[]` not needed. Retain that code if desired
int d = 0; // Use signed accumulator to save the "borrow"
// drop *2
for (int i = NUM_SIZE - 1; i >= 0; i--) {
d += a[i] - b[i]; // Perform the subtraction
c[i] = d; // Save the 8 least significant bits in c[]
d = (d - c[i]) / (UCHAR_MAX+1); // Form the "borrow" for the next loop
}
// If d<0 at this point, b was greater than a
return c;
}
Several performance improvements can be made, but it's important to improve functionality first.
source to share
There may be multiple __int128_t. But if your compiler doesn't support it, you define a structure with hi and lo with the largest type you have. In C ++, you can also add operators similar to the operators you know from other int_t-s.
typedef struct uint128 {
uint64_t lo, hi; // lo comes first if you want to use little-endian else hi comes first
} uint128_t;
If you want to double the size, you use uint128_t in a similar structure.
Edit: Simple function to increase int128:
int128_t& int128_increase(int128_t& value) {
// increase the low part, it is 0 if it was overflown
// so increase hi
if (!(++value.lo)) {
++value.hi;
};
return value;
};
Edit: Temporary scaled version of ints, I use words because it is faster in memory access:
typedef struct uint_dynamic {
// the length as a multiple of the wordsize
size_t length;
size_t* words;
} uint_dynamic_t;
uint_dynamic_t& uint_dynamic_increase(uint_dynamic_t& value) {
size_t* ptr = value.words; size_t i = value.length;
while(i && !(++*ptr)) { ++ptr; --i; };
return value;
};
Or, if you want some kind of constant size, stick it clearly into the structure.
#define uint_fixed_SIZE (16 / sizeof(size_t))
typedef struct uint_fixed {
size_t words[uint_fixed_SIZE];
} uint_fixed_t;
uint_fixed_t& uint_fixed_increase(uint_fixed_t& value) {
size_t* ptr = value.words; size_t i = uint_fixed_SIZE;
while(i && !(++*ptr)) { ++ptr; --i; };
return value;
};
This can be rewritten as # define-macro, where you replace specific values ββwith a parameter. Which has similar functionality, defining specific values ββand including the file:
Fixed_int.h file
// note that here is no #ifndef FILE_H or #pragma once
// to reuse the file
#define _concat1(a, b) a ## b
#define _concat(a, b) _concat1(a, b)
#define _size (-((-fixed_int_size) / sizeof(size_t) / 8))
#ifndef fixed_int_name
#define _name concat(uint_, fixed_int_size)
#else
#define _name fixed_int_name
#endif
#define _name_(member) _concat(_concat(_name, _), member)
typedef struct _name {
size_t words[_size];
} _name_(t);
_name_(t)& _name_(increase)(_name_(t)& value) {
size_t* ptr = value.words; size_t i = _size;
while(i && !(++*ptr)) { ++ptr; --i; };
return value;
};
// undef all defines!
#undef _concat1
#undef _concat
#undef _size
#undef _name
#undef _name_
My_ints.h file
//...
// the following lines define the type uint128_t and the function uint_128_t& uint128_increase(uint128_t&)
#define fixed_int_name uint128 // is optional
#define fixed_int_size 128
#include"fixed_int.h"
#undef fixed_int_size
#undef fixed_int_name
//...
source to share
Numbers have a "base" that defines the range of each digit (for example, "base 10" is decimal).
One uint8_t
represents one digit in "base 256". One uint16_t
represents one digit in "base 65536". One uint32_t
represents one digit in "base 4294967296".
For math operations, performance is highly dependent on the number of digits. By using a large base, you need fewer digits for the same number, which improves performance (as long as you don't exceed the processor's native word size).
To subtract unsigned numbers:
#define DIGITS 4
int subtract(uint32_t *result, uint32_t *src1, uint32_t *src2) {
int carry = 0;
int oldCarry;
int i;
for(i = 0; i < DIGITS; i++) {
oldCarry = carry;
if(src2[i] < src1[i]) {
carry = 1;
} else if( (src2[i] == src1[i]) && (oldCarry != 0) ) {
carry = 1;
} else {
carry = 0;
}
result[i] = src1[i] - src2[i] - oldCarry;
}
return carry;
}
source to share