big_int overload

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

big_int overload

Post by srvaldez »

FB includes headers and a small example using big_int but it's use without overloaded operators is painful, so here's an attempt at overloading big_int, is not complete but anyone can expand on this.
the git repo is here big_int
compile instructions for msys2
unzip the archive
launch msys2 32-bit shell then from shell prompt
cd big_int-master/libbig_int/src/low_level_funcs
gcc -c -O2 *.c -I../../include
cd ..
gcc -c -O2 *.c -I../include
ar rcs libbig_int.a low_level_funcs/*.o *.o

to built the 64-bit lib launch the msys2 64-bit shell and repeat the steps above.

here are pre compiled libs for windows for those that don't want to build them.
this is my first try at using Google drive so let me know if you have problems downloading.
Big-Int overload.bi

Code: Select all


'#include once "big_int/big_int_full.bi"
'headers translated using fbfrog
#pragma once
#inclib "big_int"

#include once "crt/stddef.bi"
#include once "crt/stdint.bi"

extern "C"

#define BIG_INT_FULL_H
#define BIG_INT_STR_FUNCS_H
#define BIG_INT_H
#define BIG_INT_BUILD_DATE_ __DATE__ " " __TIME__
#define BIG_INT_VERSION_ "0.0.50"
#define MEMORY_MANAGER_H

declare function bi_malloc(byval n as uinteger) as any ptr
declare function bi_realloc(byval p as any ptr, byval n as uinteger) as any ptr
declare sub bi_free(byval p as any ptr)
'' TODO: #define BIG_INT_API extern
const BIG_INT_DIGIT_SIZE = 32
type big_int_word as ulong
type big_int_dword as ulongint

#define BIG_INT_WORD_BYTES_CNT sizeof(big_int_word)
#define BIG_INT_WORD_BITS_CNT (sizeof(big_int_word) * 8)
#define BIG_INT_LO_WORD(n) cast(big_int_word, (n))
#define BIG_INT_HI_WORD(n) cast(big_int_word, cast(big_int_dword, (n)) shr BIG_INT_WORD_BITS_CNT)
#define BIG_INT_MAX_WORD_NUM BIG_INT_LO_WORD(not cast(big_int_word, 0))

type sign_type as long
enum
	PLUS
	MINUS
end enum

type bin_op_type as long
enum
	OP_ADD
	OP_SUB
	OP_MUL
	OP_DIV
	OP_MOD
	OP_CMP
	OP_POW
	OP_GCD
	OP_OR
	OP_XOR
	OP_AND
	OP_ANDNOT
end enum

type big_int
	num as big_int_word ptr
	sign as sign_type
	len as uinteger
	len_allocated as uinteger
end type

#define BIG_INT_STR_TYPES_H

type big_int_str
	str as zstring ptr
	len as uinteger
	len_allocated as uinteger
end type

declare function big_int_str_create(byval len as uinteger) as big_int_str ptr
declare function big_int_str_dup(byval s as const big_int_str ptr) as big_int_str ptr
declare sub big_int_str_destroy(byval s as big_int_str ptr)
declare function big_int_str_copy(byval src as const big_int_str ptr, byval dst as big_int_str ptr) as long
declare function big_int_str_copy_s(byval str as const zstring ptr, byval str_len as uinteger, byval dst as big_int_str ptr) as long
declare function big_int_str_realloc(byval s as big_int_str ptr, byval len as uinteger) as long
#define BIG_INT_SERVICE_FUNCS_H
declare function big_int_version() as const zstring ptr
declare function big_int_build_date() as const zstring ptr
declare function big_int_create(byval len as uinteger) as big_int ptr
declare function big_int_dup(byval a as const big_int ptr) as big_int ptr
declare sub big_int_destroy(byval a as big_int ptr)
declare sub big_int_clear_zeros(byval a as big_int ptr)
declare function big_int_copy(byval src as const big_int ptr, byval dst as big_int ptr) as long
declare function big_int_realloc(byval a as big_int ptr, byval len as uinteger) as long
declare function big_int_from_str(byval s as const big_int_str ptr, byval base as ulong, byval answer as big_int ptr) as long
declare function big_int_to_str(byval a as const big_int ptr, byval base as ulong, byval s as big_int_str ptr) as long
declare function big_int_from_int(byval value as long, byval a as big_int ptr) as long
declare function big_int_to_int(byval a as const big_int ptr, byval value as long ptr) as long
declare function big_int_base_convert(byval src as const big_int_str ptr, byval dst as big_int_str ptr, byval base_from as ulong, byval base_to as ulong) as long
declare function big_int_serialize(byval a as const big_int ptr, byval is_sign as long, byval s as big_int_str ptr) as long
declare function big_int_unserialize(byval s as const big_int_str ptr, byval is_sign as long, byval a as big_int ptr) as long
#define BIG_INT_BASIC_FUNCS_H
declare sub big_int_cmp_abs(byval a as const big_int ptr, byval b as const big_int ptr, byval cmp_flag as long ptr)
declare sub big_int_cmp(byval a as const big_int ptr, byval b as const big_int ptr, byval cmp_flag as long ptr)
declare sub big_int_sign(byval a as const big_int ptr, byval sign as sign_type ptr)
declare sub big_int_is_zero(byval a as const big_int ptr, byval is_zero as long ptr)
declare sub big_int_is_one(byval a as const big_int ptr, byval is_one as long ptr)
declare function big_int_abs(byval a as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_neg(byval a as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_inc(byval a as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_dec(byval a as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_sqr(byval a as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_mul(byval a as const big_int ptr, byval b as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_div(byval a as const big_int ptr, byval b as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_mod(byval a as const big_int ptr, byval b as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_add(byval a as const big_int ptr, byval b as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_sub(byval a as const big_int ptr, byval b as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_muladd(byval a as const big_int ptr, byval b as const big_int ptr, byval c as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_div_extended(byval a as const big_int ptr, byval b as const big_int ptr, byval q as big_int ptr, byval r as big_int ptr) as long
#define BIG_INT_NUMBER_THEORY_H
#define BIG_INT_MODULAR_ARITHMETIC_H
declare function big_int_addmod(byval a as const big_int ptr, byval b as const big_int ptr, byval modulus as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_submod(byval a as const big_int ptr, byval b as const big_int ptr, byval modulus as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_mulmod(byval a as const big_int ptr, byval b as const big_int ptr, byval modulus as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_divmod(byval a as const big_int ptr, byval b as const big_int ptr, byval modulus as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_powmod(byval a as const big_int ptr, byval b as const big_int ptr, byval modulus as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_factmod(byval a as const big_int ptr, byval modulus as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_absmod(byval a as const big_int ptr, byval modulus as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_invmod(byval a as const big_int ptr, byval modulus as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_sqrmod(byval a as const big_int ptr, byval modulus as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_cmpmod(byval a as const big_int ptr, byval b as const big_int ptr, byval modulus as const big_int ptr, byval cmp_flag as long ptr) as long
declare function big_int_gcd(byval a as const big_int ptr, byval b as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_gcd_extended(byval a as const big_int ptr, byval b as const big_int ptr, byval gcd as big_int ptr, byval x as big_int ptr, byval y as big_int ptr) as long
declare function big_int_pow(byval a as const big_int ptr, byval power as long, byval answer as big_int ptr) as long
declare function big_int_sqrt(byval a as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_sqrt_rem(byval a as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_fact(byval n as long, byval answer as big_int ptr) as long
declare function big_int_miller_test(byval a as const big_int ptr, byval base as const big_int ptr, byval is_prime as long ptr) as long
declare function big_int_is_prime(byval a as const big_int ptr, byval primes_to as ulong, byval level as long, byval is_prime as long ptr) as long
declare function big_int_next_prime(byval a as const big_int ptr, byval answer as big_int ptr) as long
declare function big_int_jacobi(byval a as const big_int ptr, byval b as const big_int ptr, byval jacobi as long ptr) as long
#define BIG_INT_BITSET_FUNCS_H
type big_int_rnd_fp as function() as long
declare sub big_int_bit_length(byval a as const big_int ptr, byval len as ulong ptr)
declare sub big_int_bit1_cnt(byval a as const big_int ptr, byval cnt as ulong ptr)
declare function big_int_subint(byval a as const big_int ptr, byval start_bit as uinteger, byval bit_len as uinteger, byval is_invert as long, byval answer as big_int ptr) as long
declare function big_int_or(byval a as const big_int ptr, byval b as const big_int ptr, byval start_bit as uinteger, byval answer as big_int ptr) as long
declare function big_int_and(byval a as const big_int ptr, byval b as const big_int ptr, byval start_bit as uinteger, byval answer as big_int ptr) as long
declare function big_int_andnot(byval a as const big_int ptr, byval b as const big_int ptr, byval start_bit as uinteger, byval answer as big_int ptr) as long
declare function big_int_xor(byval a as const big_int ptr, byval b as const big_int ptr, byval start_bit as uinteger, byval answer as big_int ptr) as long
declare function big_int_set_bit(byval a as const big_int ptr, byval n_bit as uinteger, byval answer as big_int ptr) as long
declare function big_int_clr_bit(byval a as const big_int ptr, byval n_bit as uinteger, byval answer as big_int ptr) as long
declare function big_int_inv_bit(byval a as const big_int ptr, byval n_bit as uinteger, byval answer as big_int ptr) as long
declare function big_int_test_bit(byval a as const big_int ptr, byval n_bit as uinteger, byval bit_value as long ptr) as long
declare function big_int_scan1_bit(byval a as const big_int ptr, byval pos_start as uinteger, byval pos_found as uinteger ptr) as long
declare function big_int_scan0_bit(byval a as const big_int ptr, byval pos_start as uinteger, byval pos_found as uinteger ptr) as long
declare function big_int_hamming_distance(byval a as const big_int ptr, byval b as const big_int ptr, byval distance as ulong ptr) as long
declare function big_int_rshift(byval a as const big_int ptr, byval n_bits as long, byval answer as big_int ptr) as long
declare function big_int_lshift(byval a as const big_int ptr, byval n_bits as long, byval answer as big_int ptr) as long
declare function big_int_rand(byval rand_func as big_int_rnd_fp, byval n_bits as uinteger, byval answer as big_int ptr) as long

end extern

#ifndef NULL
#define NULL 0
#endif

Type bigint
    Declare Constructor ( )
    Declare Constructor ( Byval rhs As Long )
    Declare Constructor ( Byref rhs As String )
    Declare Constructor ( Byref rhs As bigint )
    Declare Destructor ( )
    Declare Operator Let ( Byref rhs As bigint )
    Declare Operator Let ( Byval rhs As Long )
    Declare Operator Let ( Byref rhs As String )
    Declare Operator Cast ( ) As String
    Declare Operator Cast ( ) As Long
    Declare Operator Cast ( ) As Double
    Declare Operator Cast ( ) As Single
    '----------------------------------------------
    Declare Operator += (Byref rhs As bigint)
    Declare Operator += (Byval rhs as long)
    Declare Operator += (byref rhs as String)
    Declare Operator -= (Byref rhs As bigint)
    Declare Operator -= (byval rhs as long)
    Declare Operator -= (byref rhs as String)
    Declare Operator *= (Byref rhs As bigint)
    Declare Operator *= (byval rhs as long)
    Declare Operator *= (byref rhs as String)
    Declare Operator /= (Byref rhs As bigint)
    Declare Operator /= (byval rhs as long)
    Declare Operator /= (byref rhs as String)
    Declare Operator ^= (byval rhs as long)

    ' For Next Implicit step = +1
    Declare Operator For ( )
    Declare Operator Step( )
    Declare Operator Next( Byref end_cond As bigint ) As Integer
    ' For Next Exlicit step
    Declare Operator For ( Byref stp As bigint )
    Declare Operator Step( Byref stp As bigint )
    Declare Operator Next( Byref end_cond As bigint, Byref step_var As bigint ) As Integer
    '----------------------------------------------
    declare function toString( ) as string
    declare Function toLong ( ) As Long
    
    numptr As big_int ptr
End Type

function bigint.toString() as string
	dim as string retstr
    dim as big_int_str ptr s

    s = big_int_str_create( 1 )
    if( s = NULL ) then
        print "error when creating [s]"
        exit function
    end if
    
    if( big_int_to_str( this.numptr, 10, s ) <> 0 ) then
        print "error when converting number [num] to string [s]"
        exit function
    end if
    
    retstr=*s->str
    
    big_int_str_destroy( s )
    return retstr
end function

Function bigint.toLong ( ) As Long
	dim as Long ret
	if (big_int_to_int(this.numptr, @ret)<>0) then
		print "error in function bigint.toLong"
	end if
	return ret
End Function

Constructor bigint ( )
	this.numptr=big_int_create(1)
    if( this.numptr = NULL ) then
        print "error when creating big_int"
    else
		big_int_from_int(0, this.numptr)
    end if
End Constructor

Constructor bigint ( Byval rhs As Long )
	this.numptr=big_int_create(1)
    if( this.numptr = NULL ) then
        print "error when creating big_int"
    else
		big_int_from_int(rhs, this.numptr)
	end if
End Constructor

Constructor bigint ( Byref rhs As String )
	dim as long numbase=10
    dim as big_int_str ptr s

    s = big_int_str_create( len(rhs) )
    if( s = NULL ) then
        print "error when creating string"
    else
		s->len=len(rhs)
		*s->str=rhs
		this.numptr=big_int_create(1)
		if( this.numptr = NULL ) then
			print "error when creating big_int"
		else
			big_int_from_str( s , numbase, this.numptr)
		end if
	end if
	if( s <> NULL ) then big_int_str_destroy( s )
End Constructor

Constructor bigint ( Byref rhs As bigint )
	this.numptr=big_int_create(1)
    if( this.numptr = NULL ) then
        print "error when creating big_int"
    end if
    big_int_copy(rhs.numptr, this.numptr)
End Constructor

Destructor bigint ( )
	big_int_destroy(this.numptr)
End Destructor

Operator bigint.let ( Byref rhs As bigint )
    if (big_int_copy(rhs.numptr, this.numptr)<>0) then
		print "error when copying big_int"
	end if
End Operator

Operator bigint.let ( Byref rhs As String )
	dim as long numbase=10
    dim as big_int_str ptr s

    s = big_int_str_create( len(rhs) )
    if( s = NULL ) then
        print "error when creating string"
    else
		s->len=len(rhs)
		*s->str=rhs
		if (big_int_from_str( s , numbase, this.numptr)<>0) then
			print "error from big_int_from_str"
		end if
	end if
	if( s <> NULL ) then big_int_str_destroy( s )
End Operator

Operator bigint.Let ( Byval rhs As Long )
	if (big_int_from_int(rhs, this.numptr)<>0) then
		print "error from big_int_from_int"
	end if
End Operator



Operator bigint.cast ( ) As String
	dim as string retstr
    dim as big_int_str ptr s

    s = big_int_str_create( 1 )
    if( s = NULL ) then
        print "error when creating [s]"
        exit Operator
    end if
    
    if( big_int_to_str( this.numptr, 10, s ) <> 0 ) then
        print "error when converting number [num] to string [s]"
        exit Operator
    end if
    
    retstr=*s->str
    
    big_int_str_destroy( s )
    return retstr
End Operator

Operator bigint.cast ( ) As Long
	dim as Long ret
	big_int_to_int(this.numptr, @ret)
	return ret
End Operator

Operator bigint.cast ( ) As Double
	dim as string retstr
    dim as big_int_str ptr s

    s = big_int_str_create( 1 )
    if( s = NULL ) then
        print "error when creating [s]"
        exit Operator
    end if
    
    if( big_int_to_str( this.numptr, 10, s ) <> 0 ) then
        print "error when converting number [num] to string [s]"
        exit Operator
    end if
    
    retstr=*s->str
    
    big_int_str_destroy( s )
    return val(retstr)
End Operator

Operator bigint.cast ( ) As Single
	dim as string retstr
    dim as big_int_str ptr s

    s = big_int_str_create( 1 )
    if( s = NULL ) then
        print "error when creating [s]"
        exit Operator
    end if
    
    if( big_int_to_str( this.numptr, 10, s ) <> 0 ) then
        print "error when converting number [num] to string [s]"
        exit Operator
    end if
    
    retstr=*s->str
    
    big_int_str_destroy( s )
    return val(retstr)
End Operator


'============================================================================
'' For Next for bigint type
''
'' implicit step versions
'' 
'' In this example, we interpret implicit step
'' to mean 1
Operator bigint.for ( )
End Operator

Operator bigint.step ( )
	big_int_inc(this.numptr, this.numptr)
    'this = this+1 '
End Operator 
 
Operator bigint.next ( Byref end_cond As bigint ) As Integer
	dim as long flag
	big_int_cmp(this.numptr, end_cond.numptr, @flag)
    Return flag<=0 'this <= end_cond
End Operator
 
 
'' explicit step versions
'' 
Operator bigint.for ( Byref step_var As bigint )
End Operator
 
Operator bigint.step ( Byref step_var As bigint )
	big_int_add(this.numptr, step_var.numptr, this.numptr)
    'this = this + step_var '    
End Operator 

Operator bigint.next ( Byref end_cond As bigint, Byref step_var As bigint ) As Integer
	dim as long flag
	dim as sign_type flag2
	
	big_int_sign(step_var.numptr, @flag2)

    If flag2=MINUS Then
		big_int_cmp(this.numptr, end_cond.numptr, @flag)
		return flag>=0
		'Return this >= end_cond
    Else
		big_int_cmp(this.numptr, end_cond.numptr, @flag)
		return flag<=0
		'Return this <= end_cond
    End If
End Operator

'============================================================================

Operator + ( Byref lhs As bigint, Byref rhs As bigint ) As bigint
	Dim As bigint result
	big_int_add(lhs.numptr, rhs.numptr, result.numptr)
	return result
End Operator

Operator + ( Byref lhs As bigint, Byval rhs As long ) As bigint
	Dim As bigint result
	big_int_from_int(rhs, result.numptr)
	big_int_add(lhs.numptr, result.numptr, result.numptr)
	return result
End Operator

Operator + ( Byval lhs As long, Byref rhs As bigint ) As bigint
	Dim As bigint result
	big_int_from_int(lhs, result.numptr)
	big_int_add(rhs.numptr, result.numptr, result.numptr)
	return result
End Operator

'sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
Operator + ( Byref lhs As bigint, Byval rhs As string ) As bigint
	Dim As bigint result=rhs
	big_int_add(lhs.numptr, result.numptr, result.numptr)
	return result
End Operator

Operator + ( Byref lhs As string , Byval rhs As bigint) As bigint
	Dim As bigint result=lhs
	big_int_add(result.numptr, rhs.numptr, result.numptr)
	return result
End Operator
'---------------------------------------------------------------------------

Operator - ( Byref lhs As bigint, Byref rhs As bigint ) As bigint
	Dim As bigint result
	big_int_sub(lhs.numptr, rhs.numptr, result.numptr)
	return result
End Operator

Operator - ( Byref lhs As bigint, Byval rhs As long ) As bigint
	Dim As bigint result
	big_int_from_int(rhs, result.numptr)
	big_int_sub(lhs.numptr, result.numptr, result.numptr)
	return result
End Operator

Operator - ( Byval lhs As long, Byref rhs As bigint ) As bigint
	Dim As bigint result
	big_int_from_int(lhs, result.numptr)
	big_int_sub( result.numptr, rhs.numptr, result.numptr)
	return result
End Operator

'sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
Operator - ( Byref lhs As bigint, Byval rhs As string ) As bigint
	Dim As bigint result=rhs
	big_int_sub(lhs.numptr, result.numptr, result.numptr)
	return result
End Operator

Operator - ( Byref lhs As string , Byval rhs As bigint) As bigint
	Dim As bigint result=lhs
	big_int_sub(result.numptr, rhs.numptr, result.numptr)
	return result
End Operator
'---------------------------------------------------------------------------
Operator * ( Byref lhs As bigint, Byref rhs As bigint ) As bigint
	Dim As bigint result
	big_int_mul(lhs.numptr, rhs.numptr, result.numptr)
	return result
End Operator

Operator * ( Byref lhs As bigint, Byval rhs As long ) As bigint
	Dim As bigint result
	big_int_from_int(rhs, result.numptr)
	big_int_mul(lhs.numptr, result.numptr, result.numptr)
	return result
End Operator

Operator * ( Byval lhs As long, Byref rhs As bigint ) As bigint
	Dim As bigint result
	big_int_from_int(lhs, result.numptr)
	big_int_mul( result.numptr, rhs.numptr, result.numptr)
	return result
End Operator

'sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
Operator * ( Byref lhs As bigint, Byval rhs As string ) As bigint
	Dim As bigint result=rhs
	big_int_mul(lhs.numptr, result.numptr, result.numptr)
	return result
End Operator

Operator * ( Byref lhs As string , Byval rhs As bigint) As bigint
	Dim As bigint result=lhs
	big_int_mul(result.numptr, rhs.numptr, result.numptr)
	return result
End Operator
'---------------------------------------------------------------------------

Operator / ( Byref lhs As bigint, Byref rhs As bigint ) As bigint
	Dim As bigint result
	big_int_div(lhs.numptr, rhs.numptr, result.numptr)
	return result
End Operator

Operator / ( Byref lhs As bigint, Byval rhs As long ) As bigint
	Dim As bigint result
	big_int_from_int(rhs, result.numptr)
	big_int_div(lhs.numptr, result.numptr, result.numptr)
	return result
End Operator

Operator / ( Byval lhs As long, Byref rhs As bigint ) As bigint
	Dim As bigint result
	big_int_from_int(lhs, result.numptr)
	big_int_div( result.numptr, rhs.numptr, result.numptr)
	return result
End Operator

'sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
Operator / ( Byref lhs As bigint, Byval rhs As string ) As bigint
	Dim As bigint result=rhs
	big_int_div(lhs.numptr, result.numptr, result.numptr)
	return result
End Operator

Operator / ( Byref lhs As string , Byval rhs As bigint) As bigint
	Dim As bigint result=lhs
	big_int_div(result.numptr, rhs.numptr, result.numptr)
	return result
End Operator
'---------------------------------------------------------------------------

Operator ^ ( Byref lhs As bigint, Byval rhs As long ) As bigint
	Dim As bigint result
	big_int_pow(lhs.numptr, rhs, result.numptr)
	Return result
End Operator

Operator bigint.+= (byref rhs As bigint)
	big_int_add(this.numptr, rhs.numptr, this.numptr)
End Operator

Operator bigint.+= (Byval rhs as long)
	Dim As bigint result
	big_int_from_int(rhs, result.numptr)
	big_int_add(this.numptr, result.numptr, this.numptr)
End Operator

Operator bigint.+= (byref rhs as String)
	Dim As bigint tmp
	tmp=rhs
	big_int_add(this.numptr, tmp.numptr, this.numptr)
End Operator

Operator bigint.-= (byref rhs As bigint)
	big_int_sub(this.numptr, rhs.numptr, this.numptr)
End Operator

Operator bigint.-= (Byval rhs as long)
	Dim As bigint result
	big_int_from_int(rhs, result.numptr)
	big_int_sub(this.numptr, result.numptr, this.numptr)
End Operator

Operator bigint.-= (byref rhs as String)
	Dim As bigint tmp
	tmp=rhs
	big_int_sub(this.numptr, tmp.numptr, this.numptr)
End Operator

Operator bigint.*= (byref rhs As bigint)
	big_int_mul(this.numptr, rhs.numptr, this.numptr)
End Operator

Operator bigint.*= (Byval rhs as long)
	Dim As bigint result
	big_int_from_int(rhs, result.numptr)
	big_int_mul(this.numptr, result.numptr, this.numptr)
End Operator

Operator bigint.*= (byref rhs as String)
	Dim As bigint tmp
	tmp=rhs
	big_int_mul(this.numptr, tmp.numptr, this.numptr)
End Operator

Operator bigint./= (byref rhs As bigint)
	big_int_div(this.numptr, rhs.numptr, this.numptr)
End Operator

Operator bigint./= (Byval rhs as long)
	Dim As bigint result
	big_int_from_int(rhs, result.numptr)
	big_int_div(this.numptr, result.numptr, this.numptr)
End Operator

Operator bigint./= (byref rhs as String)
	Dim As bigint tmp
	tmp=rhs
	big_int_div(this.numptr, tmp.numptr, this.numptr)
End Operator

Operator bigint.^= ( Byval rhs As long )
	big_int_pow(this.numptr, rhs, this.numptr)
End Operator

Operator = ( Byref lhs As bigint, Byref rhs As bigint ) As Integer
	dim as long flag
	 big_int_cmp(lhs.numptr, rhs.numptr, @flag)
	Return (flag = 0)
End Operator

Operator < ( Byref lhs As bigint, Byref rhs As bigint ) As Integer
	dim as long flag
	 big_int_cmp(lhs.numptr, rhs.numptr, @flag)
	Return (flag < 0)
End Operator

Operator > ( Byref lhs As bigint, Byref rhs As bigint ) As Integer
	dim as long flag
	 big_int_cmp(lhs.numptr, rhs.numptr, @flag)
	Return (flag > 0)
End Operator

Operator <= ( Byref lhs As bigint, Byref rhs As bigint ) As Integer
	dim as long flag
	 big_int_cmp(lhs.numptr, rhs.numptr, @flag)
	Return (flag <= 0)
End Operator

Operator >= ( Byref lhs As bigint, Byref rhs As bigint ) As Integer
	dim as long flag
	 big_int_cmp(lhs.numptr, rhs.numptr, @flag)
	Return (flag >= 0)
End Operator

Operator <> ( Byref lhs As bigint, Byref rhs As bigint ) As Integer
	dim as long flag
	 big_int_cmp(lhs.numptr, rhs.numptr, @flag)
	Return (flag <> 0)
End Operator

function bigint_Abs(byref rhs As bigint) As bigint
	Dim As bigint result
	big_int_abs(rhs.numptr, result.numptr)
	return result
end function

function bigint_Sgn(byref rhs As bigint) As bigint
	dim as sign_type flag
	big_int_sign(rhs.numptr, @flag)
	Return flag=MINUS
end function

function bigint_fact(byval n as integer) As bigint
	Dim As bigint result
	big_int_fact(n, result.numptr)
	return result
end function

function bigint_miller_test(byref a As bigint, byref bas as bigint, byval is_prime as Long) As Integer
	return big_int_miller_test(a.numptr, bas.numptr, @is_prime)
	'return is_prime
end function

function bigint_next_prime(byref a As bigint) As bigint
	dim as bigint prime
	if (big_int_next_prime(a.numptr, prime.numptr)<>0) then
		print "error computing next prime"
		return 0
	else
		return prime
	end if
end function

function bigint_Sqr Overload (byref x As bigint) As bigint
	Dim As bigint result
	if (big_int_sqrt(x.numptr, result.numptr)<>0) then
		print "error computing sqrt"
		return 0
	else 
		return result
	end if
end function

function bigint_Sqr(byval x As ulong) As bigint
	Dim As bigint result
	if (big_int_from_int(x, result.numptr)<>0) then
		print "error converting from int"
		return 0
	elseif (big_int_sqrt(result.numptr, result.numptr)<>0) then
		print "error computing sqrt"
		return 0
	else
		return result
	end if
end function

function bigint_Sqr(byval x As string) As bigint
	Dim As bigint result=x
	if (big_int_sqrt(result.numptr, result.numptr)<>0) then
		print "error computing sqrt"
		return 0
	else
		return result
	end if
end function

function bigint_base_convert(byval src as string, byval base_from as long, byval base_to as long) as string
	dim as big_int_str ptr x, y
	dim as string ret
	x = big_int_str_create( len(src) )
	if( x = NULL ) then
		print "error when creating string"
		exit function
	else
		x->len=len(src)
		*x->str=src
	end if
	y = big_int_str_create( 1 )
	if( y = NULL ) then
		print "error when creating string"
		exit function	
	end if
	if (big_int_base_convert(x, y, base_from, base_to)<>0) then
		print "error, could not make conversion"
		exit function
	else
		ret= *y->str
	end if
	if( x <> NULL ) then big_int_str_destroy( x )
	if( y <> NULL ) then big_int_str_destroy( y )
	return ret
end function

function bigint_to_str(byref a as bigint, byval nbase as ulong) as string
	dim y as big_int_str ptr
	dim as string ret
	y = big_int_str_create( 1 )
	if( y = NULL ) then
		print "error when creating string"
		exit function
	end if
	if (big_int_to_str(a.numptr, nbase, y)<>0) then
		print "error, could not convert"
		exit function
	else
		ret=*y->str
	end if
	if( y <> NULL ) then big_int_str_destroy( y )
	return ret
end function

function bigint_from_str(byval s as string, byval nbase as ulong) as bigint
	dim as big_int_str ptr x
	dim as bigint ret
	x = big_int_str_create( len(s) )
	if( x = NULL ) then
		print "error when creating string"
	else
		x->len=len(s)
		*x->str=s
	end if
	if (big_int_from_str(x, nbase, ret.numptr)<>0) then
		print "error, could not convert"
	end if
	if( x <> NULL ) then big_int_str_destroy( x )
	return ret
end function

print "'big-int test"
dim as bigint n, m :print "dim as bigint n, m"
dim as long base_from, base_to :print "dim as long base_from, base_to"
dim as string strn :print "dim as string strn"

print
n=4 :print "n = ";n
n=bigint_next_prime(n) :print "n=bigint_next_prime(n)"
print "print n '-> ";n
n=100 :print "n = ";n
m=bigint_sqr(n): print "m=bigint_sqr(n)"
print "print m '-> ";m
n=bigint(2)^1000 :print "n=bigint(2)^1000 '-> ";n
base_from = 10 :print "base_from = 10"
base_to = 16 :print "base_to = 16"
strn = bigint_base_convert(n.toString, base_from, base_to) :print "strn = bigint_base_convert(n.toString, base_from, base_to)"
print "print strn '-> ";strn
Last edited by srvaldez on Nov 24, 2019 12:32, edited 10 times in total.
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Re: big_int overload

Post by integer »

Thank you for the compiled lib.
My system chokes when attempting to generate them.

I did several tests on a few of the functions.
Good Work!


Have you compared this to the speed of Richard's BigInteger, or to GMP?
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: big_int overload

Post by srvaldez »

integer wrote: Have you compared this to the speed of Richard's BigInteger, or to GMP?
GMP is many times faster, have not tested Richard's Biginteger, last time I tried there were problems compiling.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: big_int overload

Post by srvaldez »

you may want to add the mod operator to the overloaded operators

Code: Select all

operator mod overload (byref n As bigint, byref m As bigint) As bigint
	Dim As bigint result
	if big_int_mod( n.numptr, m.numptr, result.numptr)=0 then
		operator=result
	else
		operator=-1
		print "mod failed"
	end if
end operator
Post Reply