Pequeña aritmética de entero de precisión arbitraria portátil sin firmar en C, para calcular con grandes cantidades.
Utiliza una matriz de uint8_t , uint16_t o uint32_t como tipo de datos subyacente utilizando todos los bits en cada palabra.
El número de base es 0x100, 0x10000 o 0x100000000 dependiendo del tamaño de palabras elegido: consulte el archivo de encabezado Bn.h para aclarar.
No se utiliza una gestión de memoria dinámica, y stdio.h solo se usa para probar funciones de análisis hacia y desde hexagonal.
Aritmética básica (+, -, *, /, %) y operaciones bitwise (&, |, ^. <<, >>) además de incrementos, disminuciones y comparaciones son compatibles.
El objetivo de diseño principal de esta biblioteca es ser pequeño, correcto, autónomo y usar pocos recursos mientras conserva el rendimiento aceptable y la integridad de las funciones. La claridad del código también es muy valorada.
malloc / free ). Esta es la estructura de datos utilizada, donde dtype es #define 'd a uint8_t , uint16_t o uint32_t .
struct bn
{
DTYPE array [ BN_ARRAY_SIZE ];
};Esta es la API pública / exportada:
/* Initialization functions: */
void bignum_init ( struct bn * n ); /* n gets zero-initialized */
void bignum_from_int ( struct bn * n , DTYPE_TMP i );
int bignum_to_int ( struct bn * n );
/* NOTE: The functions below are meant for testing mainly and expects input in hex-format and of a certain length */
/* See the implementation for details or the test-files for examples of how to use them. */
void bignum_from_string ( struct bn * n , char * str , int nbytes );
void bignum_to_string ( struct bn * n , char * str , int maxsize );
/* Basic arithmetic operations: */
void bignum_add ( struct bn * a , struct bn * b , struct bn * c ); /* c = a + b */
void bignum_sub ( struct bn * a , struct bn * b , struct bn * c ); /* c = a - b */
void bignum_mul ( struct bn * a , struct bn * b , struct bn * c ); /* c = a * b */
void bignum_div ( struct bn * a , struct bn * b , struct bn * c ); /* c = a / b */
void bignum_mod ( struct bn * a , struct bn * b , struct bn * c ); /* c = a % b */
void bignum_divmod ( struct bn * a , struct bn * b , struct bn * c , struct bn * d ); /* c = a/b, d = a%b */
/* Bitwise operations: */
void bignum_and ( struct bn * a , struct bn * b , struct bn * c ); /* c = a & b */
void bignum_or ( struct bn * a , struct bn * b , struct bn * c ); /* c = a | b */
void bignum_xor ( struct bn * a , struct bn * b , struct bn * c ); /* c = a ^ b */
void bignum_lshift ( struct bn * a , struct bn * b , int nbits ); /* b = a << nbits */
void bignum_rshift ( struct bn * a , struct bn * b , int nbits ); /* b = a >> nbits */
/* Special operators and comparison */
int bignum_cmp ( struct bn * a , struct bn * b ); /* Compare: returns LARGER, EQUAL or SMALLER */
int bignum_is_zero ( struct bn * n ); /* For comparison with zero */
void bignum_inc ( struct bn * n ); /* Increment: add one to n */
void bignum_dec ( struct bn * n ); /* Decrement: subtract one from n */
void bignum_pow ( struct bn * a , struct bn * b , struct bn * c ); /* Calculate a^b -- e.g. 2^10 => 1024 */
void bignum_isqrt ( struct bn * a , struct bn * b ); /* Integer square root -- e.g. isqrt(5) => 2 */
void bignum_assign ( struct bn * dst , struct bn * src ); /* Copy src into dst -- dst := src */ Establezca BN_ARRAY_SIZE en bn.h para determinar el tamaño de los números que desea usar. La elección predeterminada es de 1024 números de bits. Establezca WORD_SIZE en {1,2,4} para usar uint8_t , uint16_t o uint32_t como estructura de datos subyacente.
Ejecutar make clean all test para ejemplos de uso y para algunas pruebas aleatorias.
¡Vea tests/factorial.c para un ejemplo de cómo calcular factorial (100) o 100! (un número de más de 150 dígitos).
P: ¿Qué diferencia esta biblioteca de otras implementaciones de Integer B Big?
A: tamaño pequeño para uno. ~ 500 líneas de código C que se compilan a ROM de 2-3kb, utilizando solo cantidades modestas de RAM. Utilizando todos los bits utilizando una base numérica 2^{8,16,32} en lugar de 10, que es una opción habitual.
P: ¿Por qué no hay soporte para el tamaño de una palabra de 64 bits?
R: Todos los cálculos se realizan en una variable temporal, que debe ser más grande que el tamaño de la palabra (para detectar el desbordamiento, etc.). Entonces, el tamaño de la palabra de 64 bits necesitaría, por ejemplo, TEMP-VAR de 128 bits. C99 solo admite enteros portátiles de hasta 64 bits.
Todo el material en este repositorio está en el dominio público.