Add big integer implementation
This commit is contained in:
143
include/bh/bigint.h
Normal file
143
include/bh/bigint.h
Normal file
@@ -0,0 +1,143 @@
|
||||
#ifndef BH_BIGINT_H
|
||||
#define BH_BIGINT_H
|
||||
|
||||
#include "bh.h"
|
||||
|
||||
typedef struct bh_bigint_s bh_bigint_t;
|
||||
|
||||
bh_bigint_t *bh_bigint_new(void);
|
||||
void bh_bigint_free(bh_bigint_t *bigint);
|
||||
|
||||
int bh_bigint_block(void);
|
||||
|
||||
int bh_bigint_clear(bh_bigint_t *bigint);
|
||||
|
||||
int bh_bigint_set(bh_bigint_t *to,
|
||||
bh_bigint_t *from);
|
||||
|
||||
int bh_bigint_set_int(bh_bigint_t *to,
|
||||
int from);
|
||||
|
||||
int bh_bigint_set_uint(bh_bigint_t *to,
|
||||
unsigned int from);
|
||||
|
||||
int bh_bigint_get_int(bh_bigint_t *bigint);
|
||||
|
||||
unsigned int bh_bigint_get_uint(bh_bigint_t *bigint);
|
||||
|
||||
int bh_bigint_set_int8(bh_bigint_t *to,
|
||||
bh_int8_t from);
|
||||
|
||||
int bh_bigint_set_uint8(bh_bigint_t *to,
|
||||
bh_uint8_t from);
|
||||
|
||||
bh_int8_t bh_bigint_get_int8(bh_bigint_t *bigint);
|
||||
|
||||
bh_uint8_t bh_bigint_get_uint8(bh_bigint_t *bigint);
|
||||
|
||||
int bh_bigint_set_int16(bh_bigint_t *to,
|
||||
bh_int16_t from);
|
||||
|
||||
int bh_bigint_set_uint16(bh_bigint_t *to,
|
||||
bh_uint16_t from);
|
||||
|
||||
bh_int16_t bh_bigint_get_int16(bh_bigint_t *bigint);
|
||||
|
||||
bh_uint16_t bh_bigint_get_uint16(bh_bigint_t *bigint);
|
||||
|
||||
int bh_bigint_set_int32(bh_bigint_t *to,
|
||||
bh_int32_t from);
|
||||
|
||||
int bh_bigint_set_uint32(bh_bigint_t *to,
|
||||
bh_uint32_t from);
|
||||
|
||||
bh_int32_t bh_bigint_get_int32(bh_bigint_t *bigint);
|
||||
|
||||
bh_uint32_t bh_bigint_get_uint32(bh_bigint_t *bigint);
|
||||
|
||||
int bh_bigint_set_int64(bh_bigint_t *to,
|
||||
bh_int64_t from);
|
||||
|
||||
int bh_bigint_set_uint64(bh_bigint_t *to,
|
||||
bh_uint64_t from);
|
||||
|
||||
bh_int64_t bh_bigint_get_int64(bh_bigint_t *bigint);
|
||||
|
||||
bh_uint64_t bh_bigint_get_uint64(bh_bigint_t *bigint);
|
||||
|
||||
size_t bh_bigint_capacity(bh_bigint_t *bigint);
|
||||
|
||||
size_t bh_bigint_length(bh_bigint_t *bigint);
|
||||
|
||||
int bh_bigint_reserve(bh_bigint_t *bigint,
|
||||
size_t size);
|
||||
|
||||
int bh_bigint_add(bh_bigint_t *to,
|
||||
bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_sub(bh_bigint_t *to,
|
||||
bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_mul(bh_bigint_t *to,
|
||||
bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_pow(bh_bigint_t *to,
|
||||
bh_bigint_t *from,
|
||||
bh_uint32_t power);
|
||||
|
||||
int bh_bigint_powm(bh_bigint_t *to,
|
||||
bh_bigint_t *left,
|
||||
bh_bigint_t *right,
|
||||
bh_bigint_t *mod);
|
||||
|
||||
int bh_bigint_divmod(bh_bigint_t *quotient,
|
||||
bh_bigint_t *remainder,
|
||||
bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_div(bh_bigint_t *to,
|
||||
bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_mod(bh_bigint_t *to,
|
||||
bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_lsh(bh_bigint_t *to,
|
||||
bh_bigint_t *from,
|
||||
bh_uint32_t shift);
|
||||
|
||||
int bh_bigint_rsh(bh_bigint_t *to,
|
||||
bh_bigint_t *from,
|
||||
bh_uint32_t shift);
|
||||
|
||||
int bh_bigint_or(bh_bigint_t *to,
|
||||
bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_and(bh_bigint_t *to,
|
||||
bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_xor(bh_bigint_t *to,
|
||||
bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_negate(bh_bigint_t *to,
|
||||
bh_bigint_t *from);
|
||||
|
||||
int bh_bigint_equal(bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_equal_mod(bh_bigint_t *left,
|
||||
bh_bigint_t *right);
|
||||
|
||||
int bh_bigint_is_negative(bh_bigint_t *bigint);
|
||||
int bh_bigint_is_zero(bh_bigint_t *bigint);
|
||||
int bh_bigint_is_error(bh_bigint_t *bigint);
|
||||
size_t bh_bigint_log2(bh_bigint_t *bigint);
|
||||
|
||||
#endif /* BH_BIGINT_H */
|
||||
31
include/bh/internal/bigint.h
Normal file
31
include/bh/internal/bigint.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef BH_INTERNAL_BIGINT_H
|
||||
#define BH_INTERNAL_BIGINT_H
|
||||
|
||||
#include "bh.h"
|
||||
#include <bh/bigint.h>
|
||||
|
||||
#if defined(BH_BIGINT_LONG)
|
||||
#define BH_BIGINT_BITS 31
|
||||
#define BH_BIGINT_MASK 0x7FFFFFFF
|
||||
#define BH_BIGINT_TYPE bh_uint32_t
|
||||
#define BH_BIGINT_TMP bh_uint64_t
|
||||
#else
|
||||
#define BH_BIGINT_BITS 15
|
||||
#define BH_BIGINT_MASK 0x7FFF
|
||||
#define BH_BIGINT_TYPE bh_uint16_t
|
||||
#define BH_BIGINT_TMP bh_uint32_t
|
||||
#endif
|
||||
|
||||
struct bh_bigint_s
|
||||
{
|
||||
BH_BIGINT_TYPE *data;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
int type;
|
||||
int error;
|
||||
};
|
||||
|
||||
void bh_bigint_init(bh_bigint_t *bigint);
|
||||
void bh_bigint_destroy(bh_bigint_t *bigint);
|
||||
|
||||
#endif /* BH_INTERNAL_BIGINT_H */
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef BH_INTERNAL_CONFIG_H
|
||||
#define BH_INTERNAL_CONFIG_H
|
||||
|
||||
#cmakedefine BH_BIGINT_LONG
|
||||
#cmakedefine BH_THREADS_WINXP
|
||||
|
||||
#endif /* BH_INTERNAL_CONFIG_H */
|
||||
Reference in New Issue
Block a user