Post Scarcity
A prototype for a post scarcity programming environment
Loading...
Searching...
No Matches
integer.h File Reference
#include <stdbool.h>
#include <stdint.h>
Include dependency graph for integer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define replace_integer_i(p, i)   {struct cons_pointer __p = acquire_integer(i,NIL); release_integer(p); p = __p;}
 
#define replace_integer_p(p, q)   {struct cons_pointer __p = p; release_integer( p); p = q;}
 

Functions

struct cons_pointer acquire_integer (int64_t value, struct cons_pointer more)
 Supply small valued integers from the small integer cache, if available.
 
struct cons_pointer add_integers (struct cons_pointer a, struct cons_pointer b)
 Return a pointer to an integer representing the sum of the integers pointed to by a and b.
 
bool equal_integer_integer (struct cons_pointer a, struct cons_pointer b)
 true if a and be are both integers whose value is the same value.
 
bool equal_integer_real (struct cons_pointer a, struct cons_pointer b)
 Private function, don't use.
 
struct cons_pointer integer_to_string (struct cons_pointer int_pointer, int base)
 return a string representation of this integer, which may be a bignum.
 
struct cons_pointer make_integer (int64_t value, struct cons_pointer more)
 Allocate an integer cell representing this value and return a cons_pointer to it.
 
struct cons_pointer multiply_integers (struct cons_pointer a, struct cons_pointer b)
 Return a pointer to an integer representing the product of the integers pointed to by a and b.
 
void release_integer (struct cons_pointer p)
 if the value of p is less than the size of the small integer cache (and thus it was presumably supplied from there), suppress dec_ref.
 

Macro Definition Documentation

◆ replace_integer_i

#define replace_integer_i (   p,
 
)    {struct cons_pointer __p = acquire_integer(i,NIL); release_integer(p); p = __p;}

Definition at line 17 of file integer.h.

◆ replace_integer_p

#define replace_integer_p (   p,
 
)    {struct cons_pointer __p = p; release_integer( p); p = q;}

Definition at line 18 of file integer.h.

Function Documentation

◆ acquire_integer()

struct cons_pointer acquire_integer ( int64_t  value,
struct cons_pointer  more 
)

Supply small valued integers from the small integer cache, if available.

The pattern here is intended to be that, at least within this file, instead of calling make_integer when an integer is required and dec_ref when it's no longer required, we call acquire_integer and release_integer respectively, in order to reduce allocation churn.

In the initial implementation, acquire_integer supplies the integer from the small integer cache if available, else calls make_integer. Later, more sophisticated caching of integers which are currently in play may be enabled.

Parameters
valuethe value of the integer desired.
moreif this value is a bignum, the rest (less significant bits) of the value.
Returns
struct cons_pointer a pointer to the integer acquired.

Definition at line 129 of file integer.c.

References DEBUG_ALLOC, debug_print(), debug_printf(), make_integer(), NIL, nilp, pointer2cell, small_int_cache, small_int_cache_initialised, and SMALL_INT_LIMIT.

Referenced by add_integer_ratio(), add_integers(), base_partial(), multiply_integer_ratio(), multiply_integers(), multiply_ratio_ratio(), read_number(), and simplify_ratio().

◆ add_integers()

struct cons_pointer add_integers ( struct cons_pointer  a,
struct cons_pointer  b 
)

Return a pointer to an integer representing the sum of the integers pointed to by a and b.

If either isn't an integer, will return nil.

Definition at line 224 of file integer.c.

References acquire_integer(), cell_value(), DEBUG_ARITH, debug_dump_object(), debug_print(), debug_print_128bit(), debug_print_object(), debug_println(), int128_to_integer(), integerp, make_integer(), NIL, nilp, pointer2cell, and SMALL_INT_LIMIT.

Referenced by add_2(), add_ratio_ratio(), multiply_integers(), read_number(), and subtract_2().

◆ equal_integer_integer()

bool equal_integer_integer ( struct cons_pointer  a,
struct cons_pointer  b 
)

true if a and be are both integers whose value is the same value.

Definition at line 511 of file integer.c.

References integerp, cons_space_object::payload, and pointer2cell.

Referenced by equal_integer_number(), and equal_ratio_ratio().

◆ equal_integer_real()

bool equal_integer_real ( struct cons_pointer  a,
struct cons_pointer  b 
)

Private function, don't use.

It depends on its arguments being numbers and doesn't sanity check them.

Parameters
aa lisp integer – if it isn't an integer, things will break.
ba lisp real – if it isn't a real, things will break.
Returns
true if the two numbers have equal value.
false if they don't.

Definition at line 87 of file equal.c.

References DEBUG_ARITH, debug_print(), debug_print_object(), debug_printf(), equal_ld_ld(), nilp, cons_space_object::payload, and pointer2cell.

Referenced by equal_integer_number(), and equal_real_number().

◆ integer_to_string()

struct cons_pointer integer_to_string ( struct cons_pointer  int_pointer,
int  base 
)

return a string representation of this integer, which may be a bignum.

The general principle of printing a bignum is that you print the least significant digit in whatever base you're dealing with, divide through by the base, print the next, and carry on until you've none left. Obviously, that means you print from right to left. Given that we build strings from right to left, 'printing' an integer to a lisp string would seem reasonably easy. The problem is when you jump from one integer object to the next. 64 bit integers don't align with decimal numbers, so when we get to the last digit from one integer cell, we have potentially to be looking to the next. H'mmmm.

Parameters
int_pointercons_pointer to the integer to print,
basethe base to print it in.

Definition at line 454 of file integer.c.

References c_string_to_lisp_string(), DEBUG_IO, debug_print(), debug_print_128bit(), debug_print_object(), debug_printf(), debug_println(), hex_digits, INT_CELL_BASE, integer_to_string_add_digit(), integerp, is_negative(), make_string(), MAX_INTEGER, NIL, nilp, cons_pointer::offset, pointer2cell, and stringp.

Referenced by print().

◆ make_integer()

struct cons_pointer make_integer ( int64_t  value,
struct cons_pointer  more 
)

Allocate an integer cell representing this value and return a cons_pointer to it.

Parameters
valuean integer value;
moreNIL, or a pointer to the more significant cell(s) of this number. NOTE that if more is not NIL, value must not exceed MAX_INTEGER.

Definition at line 89 of file integer.c.

References allocate_cell(), DEBUG_ALLOC, debug_dump_object(), debug_print(), integerp, INTEGERTV, NIL, nilp, cons_space_object::payload, and pointer2cell.

Referenced by absolute(), acquire_integer(), add_integers(), add_meta_integer(), append_cell(), lisp_add(), lisp_divide(), lisp_get_hash(), lisp_length(), lisp_multiply(), negative(), and subtract_2().

◆ multiply_integers()

struct cons_pointer multiply_integers ( struct cons_pointer  a,
struct cons_pointer  b 
)

Return a pointer to an integer representing the product of the integers pointed to by a and b.

If either isn't an integer, will return nil.

Yes, this is one of Muhammad ibn Musa al-Khwarizmi's original recipes, so you'd think it would be easy; the reason that each step is documented is because I did not find it so.

Parameters
aan integer;
ban integer.

Definition at line 343 of file integer.c.

References acquire_integer(), add_integers(), append_cell(), base_partial(), DEBUG_ARITH, debug_print(), debug_print_object(), debug_printf(), debug_println(), INTEGER_BIT_SHIFT, integerp, is_negative(), MAX_INTEGER, NIL, nilp, pointer2cell, replace_integer_i, and replace_integer_p.

Referenced by add_ratio_ratio(), multiply_2(), and read_number().

◆ release_integer()

void release_integer ( struct cons_pointer  p)

if the value of p is less than the size of the small integer cache (and thus it was presumably supplied from there), suppress dec_ref.

NOTE THAT at this stage it's still safe to dec_ref an arbitrary integer, because those in the cache are locked and can't be dec_refed.

Parameters
pa pointer, expected to be to an integer.

Definition at line 163 of file integer.c.

References DEBUG_ALLOC, debug_printf(), dec_ref(), eq(), integerp, nilp, cons_space_object::payload, pointer2cell, small_int_cache, and SMALL_INT_LIMIT.

Referenced by add_integer_ratio(), multiply_integer_ratio(), and multiply_ratio_ratio().