--- /dev/null
+/*
+ N16 - a processor native type
+
+ For binary operations: a op b -> c
+
+ See the document on the proper use of the Natural types.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+*/
+
+#define N16路DEBUG
+
+#ifndef FACE
+#define N16路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N16路FACE
+#define N16路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint16_t Extent;
+ typedef uint16_t Digit;
+
+ typedef struct N16路T N16路T;
+
+ extern N16路T *N16路zero;
+ extern N16路T *N16路one;
+ extern N16路T *N16路all_one_bit;
+ extern N16路T *N16路lsb;
+ extern N16路T *N16路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum{
+ N16路Status路ok = 0
+ ,N16路Status路overflow = 1
+ ,N16路Status路accumulator1_overflow = 2
+ ,N16路Status路carry = 3
+ ,N16路Status路borrow = 4
+ ,N16路Status路undefined_divide_by_zero = 5
+ ,N16路Status路undefined_modulus_zero = 6
+ ,N16路Status路gt_max_shift_count = 7
+ ,N16路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
+ ,N16路Status路one_word_product = 9
+ ,N16路Status路two_word_product = 10
+ } N16路Status;
+
+ typedef enum{
+ N16路Order_lt = -1
+ ,N16路Order_eq = 0
+ ,N16路Order_gt = 1
+ } N16路Order;
+
+ typedef N16路T *( *N16路Allocate_MemoryFault )(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct{
+
+ N16路T *(*allocate_array_zero)(Extent, N16路Allocate_MemoryFault);
+ N16路T *(*allocate_array)(Extent, N16路Allocate_MemoryFault);
+ void (*deallocate)(N16路T*);
+
+ void (*copy)(N16路T*, N16路T*);
+ void (*bit_and)(N16路T*, N16路T*, N16路T*);
+ void (*bit_or)(N16路T*, N16路T*, N16路T*);
+ void (*bit_complement)(N16路T*, N16路T*);
+ void (*bit_twos_complement)(N16路T*, N16路T*);
+ N16路Order (*compare)(N16路T*, N16路T*);
+ bool (*lt)(N16路T*, N16路T*);
+ bool (*gt)(N16路T*, N16路T*);
+ bool (*eq)(N16路T*, N16路T*);
+ bool (*eq_zero)(N16路T*);
+ N16路Status (*accumulate)(N16路T *accumulator1 ,N16路T *accumulator0 ,...);
+ N16路Status (*add)(N16路T*, N16路T*, N16路T*);
+ bool (*increment)(N16路T *a);
+ N16路Status (*subtract)(N16路T*, N16路T*, N16路T*);
+ N16路Status (*multiply)(N16路T*, N16路T*, N16路T*, N16路T*);
+ N16路Status (*divide)(N16路T*, N16路T*, N16路T*, N16路T*);
+ N16路Status (*modulus)(N16路T*, N16路T*, N16路T*);
+ N16路Status (*shift_left)(Extent, N16路T*, N16路T*, N16路T*);
+ N16路Status (*shift_right)(Extent, N16路T*, N16路T*, N16路T*);
+ N16路Status (*arithmetic_shift_right)(Extent, N16路T*, N16路T*);
+
+ N16路T* (*access)(N16路T*, Extent);
+ void (*from_uint32)(N16路T *destination ,uint32_t value);
+ } N16路螞;
+
+ Local const N16路螞 N16路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N16路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N16路T{
+ Digit d0;
+ };
+
+ N16路T N16路constant[4] = {
+ {.d0 = 0},
+ {.d0 = 1},
+ {.d0 = ~(uint16_t)0},
+ {.d0 = 1 << 15}
+ };
+
+ N16路T *N16路zero = &N16路constant[0];
+ N16路T *N16路one = &N16路constant[1];
+ N16路T *N16路all_one_bit = &N16路constant[2];
+ N16路T *N16路msb = &N16路constant[3];
+ N16路T *N16路lsb = &N16路constant[1];
+
+ // the allocate an array of N16
+ N16路T *N16路allocate_array(Extent extent ,N16路Allocate_MemoryFault memory_fault){
+ N16路T *instance = malloc((extent + 1) * sizeof(N16路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N16路T *N16路allocate_array_zero(Extent extent ,N16路Allocate_MemoryFault memory_fault){
+ N16路T *instance = calloc(extent + 1, sizeof(N16路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N16路deallocate(N16路T *unencumbered){
+ free(unencumbered);
+ }
+
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N16路T{
+ Digit d0;
+ };
+
+ // temporary variables
+ Local N16路T N16路t[4];
+
+ // allocation
+
+ extern N16路T *N16路allocate_array(Extent, N16路Allocate_MemoryFault);
+ extern N16路T *N16路allocate_array_zero(Extent, N16路Allocate_MemoryFault);
+ extern void N16路deallocate(N16路T *);
+
+ // so the user can access numbers in an array allocation
+ Local N16路T* N16路access(N16路T *array ,Extent index){
+ return &array[index];
+ }
+
+ Local void N16路from_uint32(N16路T *destination ,uint32_t value){
+ if(destination == NULL) return;
+ destination->d0 = (uint16_t)(value & 0xFFFF);
+ }
+
+ // copy, convenience copy
+
+ Local void N16路copy(N16路T *destination ,N16路T *source){
+ if(source == destination) return;
+ *destination = *source;
+ }
+
+ Local void N16路set_to_zero(N16路T *instance){
+ instance->d0 = 0;
+ }
+
+ Local void N16路set_to_one(N16路T *instance){
+ instance->d0 = 1;
+ }
+
+ // bit operations
+
+ Local void N16路bit_and(N16路T *result, N16路T *a, N16路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ Local void N16路bit_or(N16路T *result, N16路T *a, N16路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ Local void N16路bit_complement(N16路T *result, N16路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ Local void N16路bit_twos_complement(N16路T *result ,N16路T *a){
+ result->d0 = (uint16_t)(~a->d0 + 1);
+ }
+
+ // test functions
+
+ Local N16路Order N16路compare(N16路T *a, N16路T *b){
+ if(a->d0 < b->d0) return N16路Order_lt;
+ if(a->d0 > b->d0) return N16路Order_gt;
+ return N16路Order_eq;
+ }
+
+ Local bool N16路lt(N16路T *a ,N16路T *b){
+ return a->d0 < b->d0;
+ }
+
+ Local bool N16路gt(N16路T *a ,N16路T *b){
+ return a->d0 > b->d0;
+ }
+
+ Local bool N16路eq(N16路T *a ,N16路T *b){
+ return a->d0 == b->d0;
+ }
+
+ Local bool N16路eq_zero(N16路T *a){
+ return a->d0 == 0;
+ }
+
+ // arithmetic operations
+
+ Local N16路Status N16路accumulate(N16路T *accumulator1 ,N16路T *accumulator0 ,...){
+
+ va_list args;
+ va_start(args ,accumulator0);
+ uint32_t sum = accumulator0->d0;
+ uint32_t carry = 0;
+ N16路T *current;
+
+ while( (current = va_arg(args ,N16路T*)) ){
+ sum += current->d0;
+ if(sum < current->d0){
+ (carry)++;
+ if(carry == 0){
+ va_end(args);
+ return N16路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ accumulator1->d0 = (uint16_t)carry;
+ return N16路Status路ok;
+ }
+
+ Local N16路Status N16路add(N16路T *sum ,N16路T *a ,N16路T *b){
+ uint32_t result = (uint32_t)a->d0 + (uint32_t)b->d0;
+ sum->d0 = (uint16_t)(result & 0xFFFF);
+ return (result >> 16) ? N16路Status路carry : N16路Status路ok;
+ }
+
+ Local bool N16路increment(N16路T *a){
+ a->d0++;
+ return (a->d0 == 0);
+ }
+
+ Local N16路Status N16路subtract(N16路T *difference ,N16路T *a ,N16路T *b){
+ uint32_t diff = (uint32_t)a->d0 - (uint32_t)b->d0;
+ difference->d0 = (uint16_t)(diff & 0xFFFF);
+ return (diff > a->d0) ? N16路Status路borrow : N16路Status路ok;
+ }
+
+ Local N16路Status N16路multiply(N16路T *product1 ,N16路T *product0 ,N16路T *a ,N16路T *b){
+ uint32_t product = (uint32_t)a->d0 * (uint32_t)b->d0;
+ product0->d0 = (uint16_t)(product & 0xFFFF);
+ product1->d0 = (uint16_t)((product >> 16) & 0xFFFF);
+
+ if(product1->d0 == 0) return N16路Status路one_word_product;
+ return N16路Status路two_word_product;
+ }
+
+ Local N16路Status N16路divide(N16路T *remainder ,N16路T *quotient ,N16路T *a ,N16路T *b){
+ if(b->d0 == 0) return N16路Status路undefined_divide_by_zero;
+
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ quotient->d0 = (uint16_t)(dividend / divisor);
+ remainder->d0 = (uint16_t)(dividend - (quotient->d0 * divisor));
+
+ return N16路Status路ok;
+ }
+
+ Local N16路Status N16路modulus(N16路T *remainder ,N16路T *a ,N16路T *b){
+ if(b->d0 == 0) return N16路Status路undefined_modulus_zero;
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ uint32_t q = dividend / divisor;
+ remainder->d0 = (uint16_t)(dividend - (q * divisor));
+ return N16路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint16_t (*ShiftOp)(uint16_t, uint16_t);
+
+ Local uint16_t shift_left_op(uint16_t value, uint16_t amount){
+ return value << amount;
+ }
+
+ Local uint16_t shift_right_op(uint16_t value, uint16_t amount){
+ return (uint16_t)(value >> amount);
+ }
+
+ Local N16路Status N16路shift
+ (
+ uint16_t shift_count
+ ,N16路T *spill
+ ,N16路T *operand
+ ,N16路T *fill
+ ,ShiftOp shift_op
+ ,ShiftOp complement_shift_op
+ ){
+
+ if(operand == NULL && spill == NULL) return N16路Status路ok;
+
+ if(operand == NULL){
+ operand = &N16路t[0];
+ N16路copy(operand, N16路zero);
+ }
+
+ if(shift_count > 15) return N16路Status路gt_max_shift_count;
+
+ N16路T *given_operand = &N16路t[1];
+ N16路copy(given_operand, operand);
+
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (16 - shift_count));
+ N16路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (16 - shift_count));
+ }
+
+ return N16路Status路ok;
+ }
+
+ Local N16路Status
+ N16路shift_left(uint16_t shift_count, N16路T *spill, N16路T *operand, N16路T *fill){
+ return N16路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N16路Status
+ N16路shift_right(uint16_t shift_count, N16路T *spill, N16路T *operand, N16路T *fill){
+ return N16路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N16路Status
+ N16路arithmetic_shift_right(uint16_t shift_count, N16路T *operand, N16路T *spill){
+
+ if(shift_count > 15) return N16路Status路gt_max_shift_count;
+
+ if(operand == NULL){
+ operand = &N16路t[0];
+ N16路copy(operand, N16路zero);
+ }
+
+ N16路T *fill = (operand->d0 & 0x8000) ? N16路all_one_bit : N16路zero;
+ return N16路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N16路螞 N16路位 = {
+
+ .allocate_array = N16路allocate_array
+ ,.allocate_array_zero = N16路allocate_array_zero
+ ,.deallocate = N16路deallocate
+
+ ,.copy = N16路copy
+ ,.bit_and = N16路bit_and
+ ,.bit_or = N16路bit_or
+ ,.bit_complement = N16路bit_complement
+ ,.bit_twos_complement = N16路bit_twos_complement
+ ,.compare = N16路compare
+ ,.lt = N16路lt
+ ,.gt = N16路gt
+ ,.eq = N16路eq
+ ,.eq_zero = N16路eq_zero
+ ,.accumulate = N16路accumulate
+ ,.add = N16路add
+ ,.increment = N16路increment
+ ,.subtract = N16路subtract
+ ,.multiply = N16路multiply
+ ,.divide = N16路divide
+ ,.modulus = N16路modulus
+ ,.shift_left = N16路shift_left
+ ,.shift_right = N16路shift_right
+ ,.arithmetic_shift_right = N16路arithmetic_shift_right
+
+ ,.access = N16路access
+ ,.from_uint32 = N16路from_uint32
+ };
+
+ #endif
+
+#endif
+++ /dev/null
-/*
- N16 - a processor native type
-
- For binary operations: a op b -> c
-
- See the document on the proper use of the Natural types.
-
- On the subject of multiple pointers indicating the same location in memory:
-
- When a routine has multiple results, and one or more of the result location
- pointers point to the same storage, the routine will either return an error
- status, or have defined behavior.
-
- When a routine has multiple operands, in any combination, those
- pointers can point to the same location, and the routine will
- function as advertised.
-
- When an operand functions as both an input and a result, perhaps due
- to a result pointer pointing to the same place as an operand
- pointer, the routine will function as advertised. (Internally the
- routine might make a temporary copy of the operand to accomplish
- this.)
-*/
-
-#define N16PN路DEBUG
-
-#ifndef FACE
-#define N16PN路IMPLEMENTATION
-#define FACE
-#endif
-
-//--------------------------------------------------------------------------------
-// Interface
-
-#ifndef N16PN路FACE
-#define N16PN路FACE
-
- #include <stdint.h>
- #include <stdbool.h>
- #include <stdarg.h>
- #include <stdlib.h>
-
- //----------------------------------------
- // Instance Data (Declaration Only)
-
- typedef uint16_t Extent;
- typedef uint16_t Digit;
-
- typedef struct N16PN路T N16PN路T;
-
- extern N16PN路T *N16PN路zero;
- extern N16PN路T *N16PN路one;
- extern N16PN路T *N16PN路all_one_bit;
- extern N16PN路T *N16PN路lsb;
- extern N16PN路T *N16PN路msb;
-
- //----------------------------------------
- // Return/Error Status and handlers
-
- typedef enum{
- N16PN路Status路ok = 0
- ,N16PN路Status路overflow = 1
- ,N16PN路Status路accumulator1_overflow = 2
- ,N16PN路Status路carry = 3
- ,N16PN路Status路borrow = 4
- ,N16PN路Status路undefined_divide_by_zero = 5
- ,N16PN路Status路undefined_modulus_zero = 6
- ,N16PN路Status路gt_max_shift_count = 7
- ,N16PN路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
- ,N16PN路Status路one_word_product = 9
- ,N16PN路Status路two_word_product = 10
- } N16PN路Status;
-
- typedef enum{
- N16PN路Order_lt = -1
- ,N16PN路Order_eq = 0
- ,N16PN路Order_gt = 1
- } N16PN路Order;
-
- typedef N16PN路T *( *N16PN路Allocate_MemoryFault )(Extent);
-
- //----------------------------------------
- // Interface
-
- typedef struct{
-
- N16PN路T *(*allocate_array_zero)(Extent, N16PN路Allocate_MemoryFault);
- N16PN路T *(*allocate_array)(Extent, N16PN路Allocate_MemoryFault);
- void (*deallocate)(N16PN路T*);
-
- void (*copy)(N16PN路T*, N16PN路T*);
- void (*bit_and)(N16PN路T*, N16PN路T*, N16PN路T*);
- void (*bit_or)(N16PN路T*, N16PN路T*, N16PN路T*);
- void (*bit_complement)(N16PN路T*, N16PN路T*);
- void (*bit_twos_complement)(N16PN路T*, N16PN路T*);
- N16PN路Order (*compare)(N16PN路T*, N16PN路T*);
- bool (*lt)(N16PN路T*, N16PN路T*);
- bool (*gt)(N16PN路T*, N16PN路T*);
- bool (*eq)(N16PN路T*, N16PN路T*);
- bool (*eq_zero)(N16PN路T*);
- N16PN路Status (*accumulate)(N16PN路T *accumulator1 ,N16PN路T *accumulator0 ,...);
- N16PN路Status (*add)(N16PN路T*, N16PN路T*, N16PN路T*);
- bool (*increment)(N16PN路T *a);
- N16PN路Status (*subtract)(N16PN路T*, N16PN路T*, N16PN路T*);
- N16PN路Status (*multiply)(N16PN路T*, N16PN路T*, N16PN路T*, N16PN路T*);
- N16PN路Status (*divide)(N16PN路T*, N16PN路T*, N16PN路T*, N16PN路T*);
- N16PN路Status (*modulus)(N16PN路T*, N16PN路T*, N16PN路T*);
- N16PN路Status (*shift_left)(Extent, N16PN路T*, N16PN路T*, N16PN路T*);
- N16PN路Status (*shift_right)(Extent, N16PN路T*, N16PN路T*, N16PN路T*);
- N16PN路Status (*arithmetic_shift_right)(Extent, N16PN路T*, N16PN路T*);
-
- N16PN路T* (*access)(N16PN路T*, Extent);
- void (*from_uint32)(N16PN路T *destination ,uint32_t value);
- } N16PN路螞;
-
- Local const N16PN路螞 N16PN路位; // initialized in the LOCAL section
-
-#endif
-
-//--------------------------------------------------------------------------------
-// Implementation
-
-#ifdef N16PN路IMPLEMENTATION
-
- // this part goes into the library
- #ifndef LOCAL
-
- #include <stdarg.h>
- #include <stdlib.h>
-
- struct N16PN路T{
- Digit d0;
- };
-
- N16PN路T N16PN路constant[4] = {
- {.d0 = 0},
- {.d0 = 1},
- {.d0 = ~(uint16_t)0},
- {.d0 = 1 << 15}
- };
-
- N16PN路T *N16PN路zero = &N16PN路constant[0];
- N16PN路T *N16PN路one = &N16PN路constant[1];
- N16PN路T *N16PN路all_one_bit = &N16PN路constant[2];
- N16PN路T *N16PN路msb = &N16PN路constant[3];
- N16PN路T *N16PN路lsb = &N16PN路constant[1];
-
- // the allocate an array of N16
- N16PN路T *N16PN路allocate_array(Extent extent ,N16PN路Allocate_MemoryFault memory_fault){
- N16PN路T *instance = malloc((extent + 1) * sizeof(N16PN路T));
- if(!instance){
- return memory_fault ? memory_fault(extent) : NULL;
- }
- return instance;
- }
-
- N16PN路T *N16PN路allocate_array_zero(Extent extent ,N16PN路Allocate_MemoryFault memory_fault){
- N16PN路T *instance = calloc(extent + 1, sizeof(N16PN路T));
- if(!instance){
- return memory_fault ? memory_fault(extent) : NULL;
- }
- return instance;
- }
-
- void N16PN路deallocate(N16PN路T *unencumbered){
- free(unencumbered);
- }
-
-
- #endif
-
- // This part is included after the library user's code
- #ifdef LOCAL
-
- // instance
-
- struct N16PN路T{
- Digit d0;
- };
-
- // temporary variables
- Local N16PN路T N16PN路t[4];
-
- // allocation
-
- extern N16PN路T *N16PN路allocate_array(Extent, N16PN路Allocate_MemoryFault);
- extern N16PN路T *N16PN路allocate_array_zero(Extent, N16PN路Allocate_MemoryFault);
- extern void N16PN路deallocate(N16PN路T *);
-
- // so the user can access numbers in an array allocation
- Local N16PN路T* N16PN路access(N16PN路T *array ,Extent index){
- return &array[index];
- }
-
- Local void N16PN路from_uint32(N16PN路T *destination ,uint32_t value){
- if(destination == NULL) return;
- destination->d0 = (uint16_t)(value & 0xFFFF);
- }
-
- // copy, convenience copy
-
- Local void N16PN路copy(N16PN路T *destination ,N16PN路T *source){
- if(source == destination) return;
- *destination = *source;
- }
-
- Local void N16PN路set_to_zero(N16PN路T *instance){
- instance->d0 = 0;
- }
-
- Local void N16PN路set_to_one(N16PN路T *instance){
- instance->d0 = 1;
- }
-
- // bit operations
-
- Local void N16PN路bit_and(N16PN路T *result, N16PN路T *a, N16PN路T *b){
- result->d0 = a->d0 & b->d0;
- }
-
- Local void N16PN路bit_or(N16PN路T *result, N16PN路T *a, N16PN路T *b){
- result->d0 = a->d0 | b->d0;
- }
-
- Local void N16PN路bit_complement(N16PN路T *result, N16PN路T *a){
- result->d0 = ~a->d0;
- }
-
- Local void N16PN路bit_twos_complement(N16PN路T *result ,N16PN路T *a){
- result->d0 = (uint16_t)(~a->d0 + 1);
- }
-
- // test functions
-
- Local N16PN路Order N16PN路compare(N16PN路T *a, N16PN路T *b){
- if(a->d0 < b->d0) return N16PN路Order_lt;
- if(a->d0 > b->d0) return N16PN路Order_gt;
- return N16PN路Order_eq;
- }
-
- Local bool N16PN路lt(N16PN路T *a ,N16PN路T *b){
- return a->d0 < b->d0;
- }
-
- Local bool N16PN路gt(N16PN路T *a ,N16PN路T *b){
- return a->d0 > b->d0;
- }
-
- Local bool N16PN路eq(N16PN路T *a ,N16PN路T *b){
- return a->d0 == b->d0;
- }
-
- Local bool N16PN路eq_zero(N16PN路T *a){
- return a->d0 == 0;
- }
-
- // arithmetic operations
-
- Local N16PN路Status N16PN路accumulate(N16PN路T *accumulator1 ,N16PN路T *accumulator0 ,...){
-
- va_list args;
- va_start(args ,accumulator0);
- uint32_t sum = accumulator0->d0;
- uint32_t carry = 0;
- N16PN路T *current;
-
- while( (current = va_arg(args ,N16PN路T*)) ){
- sum += current->d0;
- if(sum < current->d0){
- (carry)++;
- if(carry == 0){
- va_end(args);
- return N16PN路Status路accumulator1_overflow;
- }
- }
- }
- va_end(args);
-
- accumulator1->d0 = (uint16_t)carry;
- return N16PN路Status路ok;
- }
-
- Local N16PN路Status N16PN路add(N16PN路T *sum ,N16PN路T *a ,N16PN路T *b){
- uint32_t result = (uint32_t)a->d0 + (uint32_t)b->d0;
- sum->d0 = (uint16_t)(result & 0xFFFF);
- return (result >> 16) ? N16PN路Status路carry : N16PN路Status路ok;
- }
-
- Local bool N16PN路increment(N16PN路T *a){
- a->d0++;
- return (a->d0 == 0);
- }
-
- Local N16PN路Status N16PN路subtract(N16PN路T *difference ,N16PN路T *a ,N16PN路T *b){
- uint32_t diff = (uint32_t)a->d0 - (uint32_t)b->d0;
- difference->d0 = (uint16_t)(diff & 0xFFFF);
- return (diff > a->d0) ? N16PN路Status路borrow : N16PN路Status路ok;
- }
-
- Local N16PN路Status N16PN路multiply(N16PN路T *product1 ,N16PN路T *product0 ,N16PN路T *a ,N16PN路T *b){
- uint32_t product = (uint32_t)a->d0 * (uint32_t)b->d0;
- product0->d0 = (uint16_t)(product & 0xFFFF);
- product1->d0 = (uint16_t)((product >> 16) & 0xFFFF);
-
- if(product1->d0 == 0) return N16PN路Status路one_word_product;
- return N16PN路Status路two_word_product;
- }
-
- Local N16PN路Status N16PN路divide(N16PN路T *remainder ,N16PN路T *quotient ,N16PN路T *a ,N16PN路T *b){
- if(b->d0 == 0) return N16PN路Status路undefined_divide_by_zero;
-
- uint32_t dividend = a->d0;
- uint32_t divisor = b->d0;
- quotient->d0 = (uint16_t)(dividend / divisor);
- remainder->d0 = (uint16_t)(dividend - (quotient->d0 * divisor));
-
- return N16PN路Status路ok;
- }
-
- Local N16PN路Status N16PN路modulus(N16PN路T *remainder ,N16PN路T *a ,N16PN路T *b){
- if(b->d0 == 0) return N16PN路Status路undefined_modulus_zero;
- uint32_t dividend = a->d0;
- uint32_t divisor = b->d0;
- uint32_t q = dividend / divisor;
- remainder->d0 = (uint16_t)(dividend - (q * divisor));
- return N16PN路Status路ok;
- }
-
- // bit motion
-
- typedef uint16_t (*ShiftOp)(uint16_t, uint16_t);
-
- Local uint16_t shift_left_op(uint16_t value, uint16_t amount){
- return value << amount;
- }
-
- Local uint16_t shift_right_op(uint16_t value, uint16_t amount){
- return (uint16_t)(value >> amount);
- }
-
- Local N16PN路Status N16PN路shift
- (
- uint16_t shift_count
- ,N16PN路T *spill
- ,N16PN路T *operand
- ,N16PN路T *fill
- ,ShiftOp shift_op
- ,ShiftOp complement_shift_op
- ){
-
- if(operand == NULL && spill == NULL) return N16PN路Status路ok;
-
- if(operand == NULL){
- operand = &N16PN路t[0];
- N16PN路copy(operand, N16PN路zero);
- }
-
- if(shift_count > 15) return N16PN路Status路gt_max_shift_count;
-
- N16PN路T *given_operand = &N16PN路t[1];
- N16PN路copy(given_operand, operand);
-
- operand->d0 = shift_op(given_operand->d0, shift_count);
- if(fill != NULL){
- fill->d0 = complement_shift_op(fill->d0, (16 - shift_count));
- N16PN路bit_or(operand, operand, fill);
- }
- if(spill != NULL){
- spill->d0 = shift_op(spill->d0, shift_count);
- spill->d0 += complement_shift_op(given_operand->d0, (16 - shift_count));
- }
-
- return N16PN路Status路ok;
- }
-
- Local N16PN路Status
- N16PN路shift_left(uint16_t shift_count, N16PN路T *spill, N16PN路T *operand, N16PN路T *fill){
- return N16PN路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
- }
-
- Local N16PN路Status
- N16PN路shift_right(uint16_t shift_count, N16PN路T *spill, N16PN路T *operand, N16PN路T *fill){
- return N16PN路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
- }
-
- Local N16PN路Status
- N16PN路arithmetic_shift_right(uint16_t shift_count, N16PN路T *operand, N16PN路T *spill){
-
- if(shift_count > 15) return N16PN路Status路gt_max_shift_count;
-
- if(operand == NULL){
- operand = &N16PN路t[0];
- N16PN路copy(operand, N16PN路zero);
- }
-
- N16PN路T *fill = (operand->d0 & 0x8000) ? N16PN路all_one_bit : N16PN路zero;
- return N16PN路shift_right(shift_count, spill, operand, fill);
- }
-
- Local const N16PN路螞 N16PN路位 = {
-
- .allocate_array = N16PN路allocate_array
- ,.allocate_array_zero = N16PN路allocate_array_zero
- ,.deallocate = N16PN路deallocate
-
- ,.copy = N16PN路copy
- ,.bit_and = N16PN路bit_and
- ,.bit_or = N16PN路bit_or
- ,.bit_complement = N16PN路bit_complement
- ,.bit_twos_complement = N16PN路bit_twos_complement
- ,.compare = N16PN路compare
- ,.lt = N16PN路lt
- ,.gt = N16PN路gt
- ,.eq = N16PN路eq
- ,.eq_zero = N16PN路eq_zero
- ,.accumulate = N16PN路accumulate
- ,.add = N16PN路add
- ,.increment = N16PN路increment
- ,.subtract = N16PN路subtract
- ,.multiply = N16PN路multiply
- ,.divide = N16PN路divide
- ,.modulus = N16PN路modulus
- ,.shift_left = N16PN路shift_left
- ,.shift_right = N16PN路shift_right
- ,.arithmetic_shift_right = N16PN路arithmetic_shift_right
-
- ,.access = N16PN路access
- ,.from_uint32 = N16PN路from_uint32
- };
-
- #endif
-
-#endif
--- /dev/null
+/*
+ N32 - a processor native type
+
+ For binary operations: a op b -> c
+
+ See the document on the proper use of the Natural types.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+
+*/
+
+#define N32路DEBUG
+
+#ifndef FACE
+#define N32路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N32路FACE
+#define N32路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint32_t Extent;
+ typedef uint32_t Digit;
+
+ typedef struct N32路T N32路T;
+
+ extern N32路T *N32路zero;
+ extern N32路T *N32路one;
+ extern N32路T *N32路all_one_bit;
+ extern N32路T *N32路lsb;
+ extern N32路T *N32路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum{
+ N32路Status路ok = 0
+ ,N32路Status路overflow = 1
+ ,N32路Status路accumulator1_overflow = 2
+ ,N32路Status路carry = 3
+ ,N32路Status路borrow = 4
+ ,N32路Status路undefined_divide_by_zero = 5
+ ,N32路Status路undefined_modulus_zero = 6
+ ,N32路Status路gt_max_shift_count = 7
+ ,N32路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
+ ,N32路Status路one_word_product = 9
+ ,N32路Status路two_word_product = 10
+ } N32路Status;
+
+ typedef enum{
+ N32路Order_lt = -1
+ ,N32路Order_eq = 0
+ ,N32路Order_gt = 1
+ } N32路Order;
+
+ typedef N32路T *( *N32路Allocate_MemoryFault )(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct{
+
+ N32路T *(*allocate_array_zero)(Extent, N32路Allocate_MemoryFault);
+ N32路T *(*allocate_array)(Extent, N32路Allocate_MemoryFault);
+ void (*deallocate)(N32路T*);
+
+ void (*copy)(N32路T*, N32路T*);
+ void (*bit_and)(N32路T*, N32路T*, N32路T*);
+ void (*bit_or)(N32路T*, N32路T*, N32路T*);
+ void (*bit_complement)(N32路T*, N32路T*);
+ void (*bit_twos_complement)(N32路T*, N32路T*);
+ N32路Order (*compare)(N32路T*, N32路T*);
+ bool (*lt)(N32路T*, N32路T*);
+ bool (*gt)(N32路T*, N32路T*);
+ bool (*eq)(N32路T*, N32路T*);
+ bool (*eq_zero)(N32路T*);
+ N32路Status (*accumulate)(N32路T *accumulator1 ,N32路T *accumulator0 ,...);
+ N32路Status (*add)(N32路T*, N32路T*, N32路T*);
+ bool (*increment)(N32路T *a);
+ N32路Status (*subtract)(N32路T*, N32路T*, N32路T*);
+ N32路Status (*multiply)(N32路T*, N32路T*, N32路T*, N32路T*);
+ N32路Status (*divide)(N32路T*, N32路T*, N32路T*, N32路T*);
+ N32路Status (*modulus)(N32路T*, N32路T*, N32路T*);
+ N32路Status (*shift_left)(Extent, N32路T*, N32路T*, N32路T*);
+ N32路Status (*shift_right)(Extent, N32路T*, N32路T*, N32路T*);
+ N32路Status (*arithmetic_shift_right)(Extent, N32路T*, N32路T*);
+
+ N32路T* (*access)(N32路T*, Extent);
+ void (*from_uint32)(N32路T *destination ,uint32_t value);
+ } N32路螞;
+
+ Local const N32路螞 N32路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N32路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N32路T{
+ Digit d0;
+ };
+
+ N32路T N32路constant[4] = {
+ {.d0 = 0},
+ {.d0 = 1},
+ {.d0 = ~(uint32_t)0},
+ {.d0 = 1 << 31}
+ };
+
+ N32路T *N32路zero = &N32路constant[0];
+ N32路T *N32路one = &N32路constant[1];
+ N32路T *N32路all_one_bit = &N32路constant[2];
+ N32路T *N32路msb = &N32路constant[3];
+ N32路T *N32路lsb = &N32路constant[1];
+
+ // the allocate an array of N32
+ N32路T *N32路allocate_array(Extent extent ,N32路Allocate_MemoryFault memory_fault){
+ N32路T *instance = malloc((extent + 1) * sizeof(N32路T) );
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N32路T *N32路allocate_array_zero(Extent extent ,N32路Allocate_MemoryFault memory_fault){
+ N32路T *instance = calloc( extent + 1 ,sizeof(N32路T) );
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N32路deallocate(N32路T *unencumbered){
+ free(unencumbered);
+ }
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N32路T{
+ Digit d0;
+ };
+
+ // temporary variables
+ // making these LOCAL rather than reserving one block in the library is thread safe
+ // allocating a block once is more efficient
+ // library code writes these, they are not on the interface
+
+ Local N32路T N32路t[4];
+
+
+ // allocation
+
+ extern N32路T *N32路allocate_array(Extent, N32路Allocate_MemoryFault);
+ extern N32路T *N32路allocate_array_zero(Extent, N32路Allocate_MemoryFault);
+ extern void N32路deallocate(N32路T *);
+
+ // so the user can access numbers in an array allocation
+ Local N32路T* N32路access(N32路T *array ,Extent index){
+ return &array[index];
+ }
+
+ Local void N32路from_uint32(N32路T *destination ,uint32_t value){
+ if(destination == NULL) return;
+ destination->d0 = value;
+ }
+
+ // copy, convenience copy
+
+ Local void N32路copy(N32路T *destination ,N32路T *source){
+ if(source == destination) return; // that was easy!
+ *destination = *source;
+ }
+
+ Local void N32路set_to_zero(N32路T *instance){
+ instance->d0 = 0;
+ }
+
+ Local void N32路set_to_one(N32路T *instance){
+ instance->d0 = 1;
+ }
+
+ // bit operations
+
+ Local void N32路bit_and(N32路T *result, N32路T *a, N32路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ // result can be one of the operands
+ Local void N32路bit_or(N32路T *result, N32路T *a, N32路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ // result can the same as the operand
+ Local void N32路bit_complement(N32路T *result, N32路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ // result can the same as the operand
+ Local void N32路bit_twos_complement(N32路T *result ,N32路T *a){
+ result->d0 = ~a->d0 + 1;
+ }
+
+ // test functions
+
+ Local N32路Order N32路compare(N32路T *a, N32路T *b){
+ if(a->d0 < b->d0) return N32路Order_lt;
+ if(a->d0 > b->d0) return N32路Order_gt;
+ return N32路Order_eq;
+ }
+
+ Local bool N32路lt(N32路T *a ,N32路T *b){
+ return a->d0 < b->d0;
+ }
+
+ Local bool N32路gt(N32路T *a ,N32路T *b){
+ return a->d0 > b->d0;
+ }
+
+ Local bool N32路eq(N32路T *a ,N32路T *b){
+ return a->d0 == b->d0;
+ }
+
+ Local bool N32路eq_zero(N32路T *a){
+ return a->d0 == 0;
+ }
+
+
+ // arithmetic operations
+
+ // For a large number of summands for the lower precision Natural implementations, for accumulate/add/sub, the 'overflow' operand could overflow and thus this routine will halt and return N32路Status路accumulator1_overflow
+ //
+ // When accumulator1 and accumulator0 point to the same location, the result is the accumulator1 value.
+ Local N32路Status N32路accumulate(N32路T *accumulator1 ,N32路T *accumulator0 ,...){
+
+ va_list args;
+ va_start(args ,accumulator0);
+ uint32_t sum = accumulator0->d0;
+ uint32_t carry = 0;
+ N32路T *current;
+
+ while( (current = va_arg(args ,N32路T *)) ){
+ sum += current->d0;
+ if(sum < current->d0){ // Accumulator1 into carry
+ (carry)++;
+ if(carry == 0){
+ va_end(args);
+ return N32路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ // wipes out prior value of accumulator1
+ accumulator1->d0 = carry;
+
+ return N32路Status路ok;
+ }
+
+ Local N32路Status N32路add(N32路T *sum ,N32路T *a ,N32路T *b){
+ uint64_t result = (uint64_t)a->d0 + (uint64_t)b->d0;
+ sum->d0 = (uint32_t)result;
+ return (result >> 32) ? N32路Status路carry : N32路Status路ok;
+ }
+
+ Local bool N32路increment(N32路T *a){
+ a->d0++;
+ return a->d0 == 0;
+ }
+
+ Local N32路Status N32路subtract(N32路T *difference ,N32路T *a ,N32路T *b){
+ uint64_t diff = (uint64_t) a->d0 - (uint64_t) b->d0;
+ difference->d0 = (uint32_t)diff;
+ return (diff > a->d0) ? N32路Status路borrow : N32路Status路ok;
+ }
+
+
+ Local N32路Status N32路multiply(N32路T *product1 ,N32路T *product0 ,N32路T *a ,N32路T *b){
+ uint64_t product = (uint64_t)a->d0 * (uint64_t)b->d0;
+ product0->d0 = (uint32_t)product;
+ product1->d0 = (uint32_t)(product >> 32);
+
+ if(product1->d0 == 0) return N32路Status路one_word_product;
+ return N32路Status路two_word_product;
+ }
+
+ Local N32路Status N32路divide(N32路T *remainder ,N32路T *quotient ,N32路T *a ,N32路T *b){
+ if(b->d0 == 0) return N32路Status路undefined_divide_by_zero;
+
+ quotient->d0 = a->d0 / b->d0;
+ remainder->d0 = a->d0 - (quotient->d0 * b->d0);
+
+ return N32路Status路ok;
+ }
+
+ Local N32路Status N32路modulus(N32路T *remainder ,N32路T *a ,N32路T *b){
+ if(b->d0 == 0) return N32路Status路undefined_modulus_zero;
+ uint32_t quotient = a->d0 / b->d0;
+ remainder->d0 = a->d0 - (quotient * b->d0);
+ return N32路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint32_t (*ShiftOp)(uint32_t, uint32_t);
+
+ Local uint32_t shift_left_op(uint32_t value, uint32_t amount){
+ return value << amount;
+ }
+
+ Local uint32_t shift_right_op(uint32_t value, uint32_t amount){
+ return value >> amount;
+ }
+
+ // modifies all three of its operands
+ // in the case of duplicate operands this is the order: first modifies operand, then fill, then spill,
+ Local N32路Status N32路shift
+ (
+ uint32_t shift_count
+ ,N32路T *spill
+ ,N32路T *operand
+ ,N32路T *fill
+ ,ShiftOp shift_op
+ ,ShiftOp complement_shift_op
+ ){
+
+ // If no result is needed, return immediately.
+ if(operand == NULL && spill == NULL) return N32路Status路ok;
+
+ // Treat NULL operand as zero.
+ if(operand == NULL){
+ operand = &N32路t[0];
+ N32路copy(operand, N32路zero);
+ }
+
+ // Shifting more than one word breaks our fill/spill model.
+ if(shift_count > 31) return N32路Status路gt_max_shift_count;
+
+ // The given operand is still required after it is modified, so we copy it.
+ N32路T *given_operand = &N32路t[1];
+ N32路copy(given_operand, operand);
+
+ // Perform the shift
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (32 - shift_count));
+ N32路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (32 - shift_count));
+ }
+
+ return N32路Status路ok;
+ }
+
+ // Define concrete shift functions using valid C function pointers
+ Local N32路Status
+ N32路shift_left(uint32_t shift_count, N32路T *spill, N32路T *operand, N32路T *fill){
+ return N32路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N32路Status
+ N32路shift_right(uint32_t shift_count, N32路T *spill, N32路T *operand, N32路T *fill){
+ return N32路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N32路Status
+ N32路arithmetic_shift_right(uint32_t shift_count, N32路T *operand, N32路T *spill){
+
+ // Guard against excessive shift counts
+ if(shift_count > 31) return N32路Status路gt_max_shift_count;
+
+ // A NULL operand is treated as zero
+ if(operand == NULL){
+ operand = &N32路t[0];
+ N32路copy(operand, N32路zero);
+ }
+
+ // Pick the fill value based on the sign bit
+ N32路T *fill = (operand->d0 & 0x80000000) ? N32路all_one_bit : N32路zero;
+
+ // Call shift_right with the appropriate fill
+ return N32路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N32路螞 N32路位 = {
+
+ .allocate_array = N32路allocate_array
+ ,.allocate_array_zero = N32路allocate_array_zero
+ ,.deallocate = N32路deallocate
+
+ ,.copy = N32路copy
+ ,.bit_and = N32路bit_and
+ ,.bit_or = N32路bit_or
+ ,.bit_complement = N32路bit_complement
+ ,.bit_twos_complement = N32路bit_twos_complement
+ ,.compare = N32路compare
+ ,.lt = N32路lt
+ ,.gt = N32路gt
+ ,.eq = N32路eq
+ ,.eq_zero = N32路eq_zero
+ ,.accumulate = N32路accumulate
+ ,.add = N32路add
+ ,.increment = N32路increment
+ ,.subtract = N32路subtract
+ ,.multiply = N32路multiply
+ ,.divide = N32路divide
+ ,.modulus = N32路modulus
+ ,.shift_left = N32路shift_left
+ ,.shift_right = N32路shift_right
+ ,.arithmetic_shift_right = N32路arithmetic_shift_right
+
+ ,.access = N32路access
+ ,.from_uint32 = N32路from_uint32
+ };
+
+ #endif
+
+#endif
+++ /dev/null
-/*
- N32 - a processor native type
-
- For binary operations: a op b -> c
-
- See the document on the proper use of the Natural types.
-
- On the subject of multiple pointers indicating the same location in memory:
-
- When a routine has multiple results, and one or more of the result location
- pointers point to the same storage, the routine will either return an error
- status, or have defined behavior.
-
- When a routine has multiple operands, in any combination, those
- pointers can point to the same location, and the routine will
- function as advertised.
-
- When an operand functions as both an input and a result, perhaps due
- to a result pointer pointing to the same place as an operand
- pointer, the routine will function as advertised. (Internally the
- routine might make a temporary copy of the operand to accomplish
- this.)
-
-*/
-
-#define N32PN路DEBUG
-
-#ifndef FACE
-#define N32PN路IMPLEMENTATION
-#define FACE
-#endif
-
-//--------------------------------------------------------------------------------
-// Interface
-
-#ifndef N32PN路FACE
-#define N32PN路FACE
-
- #include <stdint.h>
- #include <stdbool.h>
- #include <stdarg.h>
- #include <stdlib.h>
-
- //----------------------------------------
- // Instance Data (Declaration Only)
-
- typedef uint32_t Extent;
- typedef uint32_t Digit;
-
- typedef struct N32PN路T N32PN路T;
-
- extern N32PN路T *N32PN路zero;
- extern N32PN路T *N32PN路one;
- extern N32PN路T *N32PN路all_one_bit;
- extern N32PN路T *N32PN路lsb;
- extern N32PN路T *N32PN路msb;
-
- //----------------------------------------
- // Return/Error Status and handlers
-
- typedef enum{
- N32PN路Status路ok = 0
- ,N32PN路Status路overflow = 1
- ,N32PN路Status路accumulator1_overflow = 2
- ,N32PN路Status路carry = 3
- ,N32PN路Status路borrow = 4
- ,N32PN路Status路undefined_divide_by_zero = 5
- ,N32PN路Status路undefined_modulus_zero = 6
- ,N32PN路Status路gt_max_shift_count = 7
- ,N32PN路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
- ,N32PN路Status路one_word_product = 9
- ,N32PN路Status路two_word_product = 10
- } N32PN路Status;
-
- typedef enum{
- N32PN路Order_lt = -1
- ,N32PN路Order_eq = 0
- ,N32PN路Order_gt = 1
- } N32PN路Order;
-
- typedef N32PN路T *( *N32PN路Allocate_MemoryFault )(Extent);
-
- //----------------------------------------
- // Interface
-
- typedef struct{
-
- N32PN路T *(*allocate_array_zero)(Extent, N32PN路Allocate_MemoryFault);
- N32PN路T *(*allocate_array)(Extent, N32PN路Allocate_MemoryFault);
- void (*deallocate)(N32PN路T*);
-
- void (*copy)(N32PN路T*, N32PN路T*);
- void (*bit_and)(N32PN路T*, N32PN路T*, N32PN路T*);
- void (*bit_or)(N32PN路T*, N32PN路T*, N32PN路T*);
- void (*bit_complement)(N32PN路T*, N32PN路T*);
- void (*bit_twos_complement)(N32PN路T*, N32PN路T*);
- N32PN路Order (*compare)(N32PN路T*, N32PN路T*);
- bool (*lt)(N32PN路T*, N32PN路T*);
- bool (*gt)(N32PN路T*, N32PN路T*);
- bool (*eq)(N32PN路T*, N32PN路T*);
- bool (*eq_zero)(N32PN路T*);
- N32PN路Status (*accumulate)(N32PN路T *accumulator1 ,N32PN路T *accumulator0 ,...);
- N32PN路Status (*add)(N32PN路T*, N32PN路T*, N32PN路T*);
- bool (*increment)(N32PN路T *a);
- N32PN路Status (*subtract)(N32PN路T*, N32PN路T*, N32PN路T*);
- N32PN路Status (*multiply)(N32PN路T*, N32PN路T*, N32PN路T*, N32PN路T*);
- N32PN路Status (*divide)(N32PN路T*, N32PN路T*, N32PN路T*, N32PN路T*);
- N32PN路Status (*modulus)(N32PN路T*, N32PN路T*, N32PN路T*);
- N32PN路Status (*shift_left)(Extent, N32PN路T*, N32PN路T*, N32PN路T*);
- N32PN路Status (*shift_right)(Extent, N32PN路T*, N32PN路T*, N32PN路T*);
- N32PN路Status (*arithmetic_shift_right)(Extent, N32PN路T*, N32PN路T*);
-
- N32PN路T* (*access)(N32PN路T*, Extent);
- void (*from_uint32)(N32PN路T *destination ,uint32_t value);
- } N32PN路螞;
-
- Local const N32PN路螞 N32PN路位; // initialized in the LOCAL section
-
-#endif
-
-//--------------------------------------------------------------------------------
-// Implementation
-
-#ifdef N32PN路IMPLEMENTATION
-
- // this part goes into the library
- #ifndef LOCAL
-
- #include <stdarg.h>
- #include <stdlib.h>
-
- struct N32PN路T{
- Digit d0;
- };
-
- N32PN路T N32PN路constant[4] = {
- {.d0 = 0},
- {.d0 = 1},
- {.d0 = ~(uint32_t)0},
- {.d0 = 1 << 31}
- };
-
- N32PN路T *N32PN路zero = &N32PN路constant[0];
- N32PN路T *N32PN路one = &N32PN路constant[1];
- N32PN路T *N32PN路all_one_bit = &N32PN路constant[2];
- N32PN路T *N32PN路msb = &N32PN路constant[3];
- N32PN路T *N32PN路lsb = &N32PN路constant[1];
-
- // the allocate an array of N32
- N32PN路T *N32PN路allocate_array(Extent extent ,N32PN路Allocate_MemoryFault memory_fault){
- N32PN路T *instance = malloc((extent + 1) * sizeof(N32PN路T) );
- if(!instance){
- return memory_fault ? memory_fault(extent) : NULL;
- }
- return instance;
- }
-
- N32PN路T *N32PN路allocate_array_zero(Extent extent ,N32PN路Allocate_MemoryFault memory_fault){
- N32PN路T *instance = calloc( extent + 1 ,sizeof(N32PN路T) );
- if(!instance){
- return memory_fault ? memory_fault(extent) : NULL;
- }
- return instance;
- }
-
- void N32PN路deallocate(N32PN路T *unencumbered){
- free(unencumbered);
- }
-
- #endif
-
- // This part is included after the library user's code
- #ifdef LOCAL
-
- // instance
-
- struct N32PN路T{
- Digit d0;
- };
-
- // temporary variables
- // making these LOCAL rather than reserving one block in the library is thread safe
- // allocating a block once is more efficient
- // library code writes these, they are not on the interface
-
- Local N32PN路T N32PN路t[4];
-
-
- // allocation
-
- extern N32PN路T *N32PN路allocate_array(Extent, N32PN路Allocate_MemoryFault);
- extern N32PN路T *N32PN路allocate_array_zero(Extent, N32PN路Allocate_MemoryFault);
- extern void N32PN路deallocate(N32PN路T *);
-
- // so the user can access numbers in an array allocation
- Local N32PN路T* N32PN路access(N32PN路T *array ,Extent index){
- return &array[index];
- }
-
- Local void N32PN路from_uint32(N32PN路T *destination ,uint32_t value){
- if(destination == NULL) return;
- destination->d0 = value;
- }
-
- // copy, convenience copy
-
- Local void N32PN路copy(N32PN路T *destination ,N32PN路T *source){
- if(source == destination) return; // that was easy!
- *destination = *source;
- }
-
- Local void N32PN路set_to_zero(N32PN路T *instance){
- instance->d0 = 0;
- }
-
- Local void N32PN路set_to_one(N32PN路T *instance){
- instance->d0 = 1;
- }
-
- // bit operations
-
- Local void N32PN路bit_and(N32PN路T *result, N32PN路T *a, N32PN路T *b){
- result->d0 = a->d0 & b->d0;
- }
-
- // result can be one of the operands
- Local void N32PN路bit_or(N32PN路T *result, N32PN路T *a, N32PN路T *b){
- result->d0 = a->d0 | b->d0;
- }
-
- // result can the same as the operand
- Local void N32PN路bit_complement(N32PN路T *result, N32PN路T *a){
- result->d0 = ~a->d0;
- }
-
- // result can the same as the operand
- Local void N32PN路bit_twos_complement(N32PN路T *result ,N32PN路T *a){
- result->d0 = ~a->d0 + 1;
- }
-
- // test functions
-
- Local N32PN路Order N32PN路compare(N32PN路T *a, N32PN路T *b){
- if(a->d0 < b->d0) return N32PN路Order_lt;
- if(a->d0 > b->d0) return N32PN路Order_gt;
- return N32PN路Order_eq;
- }
-
- Local bool N32PN路lt(N32PN路T *a ,N32PN路T *b){
- return a->d0 < b->d0;
- }
-
- Local bool N32PN路gt(N32PN路T *a ,N32PN路T *b){
- return a->d0 > b->d0;
- }
-
- Local bool N32PN路eq(N32PN路T *a ,N32PN路T *b){
- return a->d0 == b->d0;
- }
-
- Local bool N32PN路eq_zero(N32PN路T *a){
- return a->d0 == 0;
- }
-
-
- // arithmetic operations
-
- // For a large number of summands for the lower precision Natural implementations, for accumulate/add/sub, the 'overflow' operand could overflow and thus this routine will halt and return N32PN路Status路accumulator1_overflow
- //
- // When accumulator1 and accumulator0 point to the same location, the result is the accumulator1 value.
- Local N32PN路Status N32PN路accumulate(N32PN路T *accumulator1 ,N32PN路T *accumulator0 ,...){
-
- va_list args;
- va_start(args ,accumulator0);
- uint32_t sum = accumulator0->d0;
- uint32_t carry = 0;
- N32PN路T *current;
-
- while( (current = va_arg(args ,N32PN路T *)) ){
- sum += current->d0;
- if(sum < current->d0){ // Accumulator1 into carry
- (carry)++;
- if(carry == 0){
- va_end(args);
- return N32PN路Status路accumulator1_overflow;
- }
- }
- }
- va_end(args);
-
- // wipes out prior value of accumulator1
- accumulator1->d0 = carry;
-
- return N32PN路Status路ok;
- }
-
- Local N32PN路Status N32PN路add(N32PN路T *sum ,N32PN路T *a ,N32PN路T *b){
- uint64_t result = (uint64_t)a->d0 + (uint64_t)b->d0;
- sum->d0 = (uint32_t)result;
- return (result >> 32) ? N32PN路Status路carry : N32PN路Status路ok;
- }
-
- Local bool N32PN路increment(N32PN路T *a){
- a->d0++;
- return a->d0 == 0;
- }
-
- Local N32PN路Status N32PN路subtract(N32PN路T *difference ,N32PN路T *a ,N32PN路T *b){
- uint64_t diff = (uint64_t) a->d0 - (uint64_t) b->d0;
- difference->d0 = (uint32_t)diff;
- return (diff > a->d0) ? N32PN路Status路borrow : N32PN路Status路ok;
- }
-
-
- Local N32PN路Status N32PN路multiply(N32PN路T *product1 ,N32PN路T *product0 ,N32PN路T *a ,N32PN路T *b){
- uint64_t product = (uint64_t)a->d0 * (uint64_t)b->d0;
- product0->d0 = (uint32_t)product;
- product1->d0 = (uint32_t)(product >> 32);
-
- if(product1->d0 == 0) return N32PN路Status路one_word_product;
- return N32PN路Status路two_word_product;
- }
-
- Local N32PN路Status N32PN路divide(N32PN路T *remainder ,N32PN路T *quotient ,N32PN路T *a ,N32PN路T *b){
- if(b->d0 == 0) return N32PN路Status路undefined_divide_by_zero;
-
- quotient->d0 = a->d0 / b->d0;
- remainder->d0 = a->d0 - (quotient->d0 * b->d0);
-
- return N32PN路Status路ok;
- }
-
- Local N32PN路Status N32PN路modulus(N32PN路T *remainder ,N32PN路T *a ,N32PN路T *b){
- if(b->d0 == 0) return N32PN路Status路undefined_modulus_zero;
- uint32_t quotient = a->d0 / b->d0;
- remainder->d0 = a->d0 - (quotient * b->d0);
- return N32PN路Status路ok;
- }
-
- // bit motion
-
- typedef uint32_t (*ShiftOp)(uint32_t, uint32_t);
-
- Local uint32_t shift_left_op(uint32_t value, uint32_t amount){
- return value << amount;
- }
-
- Local uint32_t shift_right_op(uint32_t value, uint32_t amount){
- return value >> amount;
- }
-
- // modifies all three of its operands
- // in the case of duplicate operands this is the order: first modifies operand, then fill, then spill,
- Local N32PN路Status N32PN路shift
- (
- uint32_t shift_count
- ,N32PN路T *spill
- ,N32PN路T *operand
- ,N32PN路T *fill
- ,ShiftOp shift_op
- ,ShiftOp complement_shift_op
- ){
-
- // If no result is needed, return immediately.
- if(operand == NULL && spill == NULL) return N32PN路Status路ok;
-
- // Treat NULL operand as zero.
- if(operand == NULL){
- operand = &N32PN路t[0];
- N32PN路copy(operand, N32PN路zero);
- }
-
- // Shifting more than one word breaks our fill/spill model.
- if(shift_count > 31) return N32PN路Status路gt_max_shift_count;
-
- // The given operand is still required after it is modified, so we copy it.
- N32PN路T *given_operand = &N32PN路t[1];
- N32PN路copy(given_operand, operand);
-
- // Perform the shift
- operand->d0 = shift_op(given_operand->d0, shift_count);
- if(fill != NULL){
- fill->d0 = complement_shift_op(fill->d0, (32 - shift_count));
- N32PN路bit_or(operand, operand, fill);
- }
- if(spill != NULL){
- spill->d0 = shift_op(spill->d0, shift_count);
- spill->d0 += complement_shift_op(given_operand->d0, (32 - shift_count));
- }
-
- return N32PN路Status路ok;
- }
-
- // Define concrete shift functions using valid C function pointers
- Local N32PN路Status
- N32PN路shift_left(uint32_t shift_count, N32PN路T *spill, N32PN路T *operand, N32PN路T *fill){
- return N32PN路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
- }
-
- Local N32PN路Status
- N32PN路shift_right(uint32_t shift_count, N32PN路T *spill, N32PN路T *operand, N32PN路T *fill){
- return N32PN路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
- }
-
- Local N32PN路Status
- N32PN路arithmetic_shift_right(uint32_t shift_count, N32PN路T *operand, N32PN路T *spill){
-
- // Guard against excessive shift counts
- if(shift_count > 31) return N32PN路Status路gt_max_shift_count;
-
- // A NULL operand is treated as zero
- if(operand == NULL){
- operand = &N32PN路t[0];
- N32PN路copy(operand, N32PN路zero);
- }
-
- // Pick the fill value based on the sign bit
- N32PN路T *fill = (operand->d0 & 0x80000000) ? N32PN路all_one_bit : N32PN路zero;
-
- // Call shift_right with the appropriate fill
- return N32PN路shift_right(shift_count, spill, operand, fill);
- }
-
- Local const N32PN路螞 N32PN路位 = {
-
- .allocate_array = N32PN路allocate_array
- ,.allocate_array_zero = N32PN路allocate_array_zero
- ,.deallocate = N32PN路deallocate
-
- ,.copy = N32PN路copy
- ,.bit_and = N32PN路bit_and
- ,.bit_or = N32PN路bit_or
- ,.bit_complement = N32PN路bit_complement
- ,.bit_twos_complement = N32PN路bit_twos_complement
- ,.compare = N32PN路compare
- ,.lt = N32PN路lt
- ,.gt = N32PN路gt
- ,.eq = N32PN路eq
- ,.eq_zero = N32PN路eq_zero
- ,.accumulate = N32PN路accumulate
- ,.add = N32PN路add
- ,.increment = N32PN路increment
- ,.subtract = N32PN路subtract
- ,.multiply = N32PN路multiply
- ,.divide = N32PN路divide
- ,.modulus = N32PN路modulus
- ,.shift_left = N32PN路shift_left
- ,.shift_right = N32PN路shift_right
- ,.arithmetic_shift_right = N32PN路arithmetic_shift_right
-
- ,.access = N32PN路access
- ,.from_uint32 = N32PN路from_uint32
- };
-
- #endif
-
-#endif
--- /dev/null
+/*
+ N64 - a 64-bit native type
+
+ For binary operations: a op b -> c
+
+ Similar to N32, but now each Digit is 64 bits. Where a 128-bit
+ intermediate is necessary (e.g. multiplication), we handle it
+ manually using two 64-bit parts.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+*/
+
+#define N64路DEBUG
+
+#ifndef FACE
+#define N64路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N64路FACE
+#define N64路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint64_t Extent;
+ typedef uint64_t Digit;
+
+ typedef struct N64路T N64路T;
+
+ extern N64路T *N64路zero;
+ extern N64路T *N64路one;
+ extern N64路T *N64路all_one_bit;
+ extern N64路T *N64路lsb;
+ extern N64路T *N64路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum {
+ N64路Status路ok = 0
+ ,N64路Status路overflow = 1
+ ,N64路Status路accumulator1_overflow = 2
+ ,N64路Status路carry = 3
+ ,N64路Status路borrow = 4
+ ,N64路Status路undefined_divide_by_zero = 5
+ ,N64路Status路undefined_modulus_zero = 6
+ ,N64路Status路gt_max_shift_count = 7
+ ,N64路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
+ ,N64路Status路one_word_product = 9
+ ,N64路Status路two_word_product = 10
+ } N64路Status;
+
+ typedef enum {
+ N64路Order_lt = -1
+ ,N64路Order_eq = 0
+ ,N64路Order_gt = 1
+ } N64路Order;
+
+ typedef N64路T *(*N64路Allocate_MemoryFault)(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct {
+
+ N64路T *(*allocate_array_zero)(Extent, N64路Allocate_MemoryFault);
+ N64路T *(*allocate_array)(Extent, N64路Allocate_MemoryFault);
+ void (*deallocate)(N64路T*);
+
+ void (*copy)(N64路T*, N64路T*);
+ void (*bit_and)(N64路T*, N64路T*, N64路T*);
+ void (*bit_or)(N64路T*, N64路T*, N64路T*);
+ void (*bit_complement)(N64路T*, N64路T*);
+ void (*bit_twos_complement)(N64路T*, N64路T*);
+ N64路Order (*compare)(N64路T*, N64路T*);
+ bool (*lt)(N64路T*, N64路T*);
+ bool (*gt)(N64路T*, N64路T*);
+ bool (*eq)(N64路T*, N64路T*);
+ bool (*eq_zero)(N64路T*);
+ N64路Status (*accumulate)(N64路T *accumulator1, N64路T *accumulator0, ...);
+ N64路Status (*add)(N64路T*, N64路T*, N64路T*);
+ bool (*increment)(N64路T *a);
+ N64路Status (*subtract)(N64路T*, N64路T*, N64路T*);
+ N64路Status (*multiply)(N64路T*, N64路T*, N64路T*, N64路T*);
+ N64路Status (*divide)(N64路T*, N64路T*, N64路T*, N64路T*);
+ N64路Status (*modulus)(N64路T*, N64路T*, N64路T*);
+ N64路Status (*shift_left)(Extent, N64路T*, N64路T*, N64路T*);
+ N64路Status (*shift_right)(Extent, N64路T*, N64路T*, N64路T*);
+ N64路Status (*arithmetic_shift_right)(Extent, N64路T*, N64路T*);
+
+ N64路T* (*access)(N64路T*, Extent);
+ void (*from_uint64)(N64路T *destination, uint64_t value);
+
+ } N64路螞;
+
+ Local const N64路螞 N64路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N64路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N64路T {
+ Digit d0;
+ };
+
+ // For constants, we store them in an array for convenience
+ // 0, 1, all bits set (~0ULL), and MSB set (1ULL<<63)
+ N64路T N64路constant[4] = {
+ {.d0 = 0ULL},
+ {.d0 = 1ULL},
+ {.d0 = ~(uint64_t)0ULL},
+ {.d0 = 1ULL << 63}
+ };
+
+ N64路T *N64路zero = &N64路constant[0];
+ N64路T *N64路one = &N64路constant[1];
+ N64路T *N64路all_one_bit = &N64路constant[2];
+ N64路T *N64路msb = &N64路constant[3];
+ N64路T *N64路lsb = &N64路constant[1];
+
+ // allocate an array of N64
+ N64路T *N64路allocate_array(Extent extent, N64路Allocate_MemoryFault memory_fault){
+ N64路T *instance = malloc( (extent + 1) * sizeof(N64路T) );
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N64路T *N64路allocate_array_zero(Extent extent, N64路Allocate_MemoryFault memory_fault){
+ N64路T *instance = calloc(extent + 1, sizeof(N64路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N64路deallocate(N64路T *unencumbered){
+ free(unencumbered);
+ }
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N64路T {
+ Digit d0;
+ };
+
+ // local temporary variables
+ Local N64路T N64路t[4];
+
+ // allocation references
+ extern N64路T *N64路allocate_array(Extent, N64路Allocate_MemoryFault);
+ extern N64路T *N64路allocate_array_zero(Extent, N64路Allocate_MemoryFault);
+ extern void N64路deallocate(N64路T *);
+
+ // Access array
+ Local N64路T* N64路access(N64路T *array, Extent index){
+ return &array[index];
+ }
+
+ Local void N64路from_uint64(N64路T *destination, uint64_t value){
+ if(destination == NULL) return;
+ destination->d0 = value;
+ }
+
+ // copy
+ Local void N64路copy(N64路T *destination, N64路T *source){
+ if(source == destination) return;
+ *destination = *source;
+ }
+
+ // bit operations
+
+ Local void N64路bit_and(N64路T *result, N64路T *a, N64路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ Local void N64路bit_or(N64路T *result, N64路T *a, N64路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ Local void N64路bit_complement(N64路T *result, N64路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ Local void N64路bit_twos_complement(N64路T *result, N64路T *a){
+ result->d0 = ~a->d0 + 1ULL;
+ }
+
+ // compare & test functions
+
+ Local N64路Order N64路compare(N64路T *a, N64路T *b){
+ if(a->d0 < b->d0) return N64路Order_lt;
+ if(a->d0 > b->d0) return N64路Order_gt;
+ return N64路Order_eq;
+ }
+
+ Local bool N64路lt(N64路T *a, N64路T *b){
+ return (a->d0 < b->d0);
+ }
+
+ Local bool N64路gt(N64路T *a, N64路T *b){
+ return (a->d0 > b->d0);
+ }
+
+ Local bool N64路eq(N64路T *a, N64路T *b){
+ return (a->d0 == b->d0);
+ }
+
+ Local bool N64路eq_zero(N64路T *a){
+ return (a->d0 == 0ULL);
+ }
+
+ // arithmetic operations
+
+ // accumulate
+ Local N64路Status N64路accumulate(N64路T *accumulator1, N64路T *accumulator0, ...){
+ va_list args;
+ va_start(args, accumulator0);
+
+ uint64_t sum = accumulator0->d0;
+ uint64_t carry = 0;
+ N64路T *current;
+
+ while( (current = va_arg(args, N64路T*)) ){
+ uint64_t prior = sum;
+ sum += current->d0;
+ if(sum < prior){ // indicates carry
+ carry++;
+ // if carry overflowed a 64-bit, that's an accumulator1 overflow
+ if(carry == 0ULL){
+ va_end(args);
+ return N64路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ accumulator1->d0 = carry;
+ return N64路Status路ok;
+ }
+
+ // add
+ Local N64路Status N64路add(N64路T *sum, N64路T *a, N64路T *b){
+ __uint128_t result = ( __uint128_t )a->d0 + ( __uint128_t )b->d0;
+ // But to avoid using a GNU extension, we can do the simpler approach:
+ // Actually let's do it directly with 64-bit since we only need to detect carry out of 64 bits:
+ uint64_t temp = a->d0 + b->d0;
+ sum->d0 = temp;
+ if(temp < a->d0) return N64路Status路carry; // means we overflowed
+ return N64路Status路ok;
+ }
+
+ Local bool N64路increment(N64路T *a){
+ uint64_t old = a->d0;
+ a->d0++;
+ // if it wrapped around to 0, then it was 0xFFFFFFFFFFFFFFFF
+ return (a->d0 < old);
+ }
+
+ // subtract
+ Local N64路Status N64路subtract(N64路T *difference, N64路T *a, N64路T *b){
+ uint64_t tmpA = a->d0;
+ uint64_t tmpB = b->d0;
+ uint64_t diff = tmpA - tmpB;
+ difference->d0 = diff;
+ if(diff > tmpA) return N64路Status路borrow; // indicates we borrowed
+ return N64路Status路ok;
+ }
+
+ // multiply
+ // We'll do a 64x64->128 using two 64-bit accumulators
+ Local N64路Status N64路multiply(N64路T *product1, N64路T *product0, N64路T *a, N64路T *b){
+ uint64_t A = a->d0;
+ uint64_t B = b->d0;
+
+ // Break each operand into high & low 32 bits
+ uint64_t a_lo = (uint32_t)(A & 0xffffffffULL);
+ uint64_t a_hi = A >> 32;
+ uint64_t b_lo = (uint32_t)(B & 0xffffffffULL);
+ uint64_t b_hi = B >> 32;
+
+ // partial products
+ uint64_t low = a_lo * b_lo; // 64-bit
+ uint64_t cross = (a_lo * b_hi) + (a_hi * b_lo); // potentially up to 2 * 32 bits => 64 bits
+ uint64_t high = a_hi * b_hi; // up to 64 bits
+
+ // incorporate cross into low, high
+ // cross is effectively the middle bits, so shift cross by 32 and add to low
+ uint64_t cross_low = (cross & 0xffffffffULL) << 32; // lower part
+ uint64_t cross_high = cross >> 32; // upper part
+
+ // add cross_low to low, capture carry
+ uint64_t old_low = low;
+ low += cross_low;
+ if(low < old_low) cross_high++;
+
+ // final high
+ high += cross_high;
+
+ // store results
+ product0->d0 = low;
+ product1->d0 = high;
+
+ if(high == 0ULL) return N64路Status路one_word_product;
+ return N64路Status路two_word_product;
+ }
+
+ // divide
+ Local N64路Status N64路divide(N64路T *remainder, N64路T *quotient, N64路T *a, N64路T *b){
+ // we do not handle a > 64-bit, just the single 64-bit
+ if(b->d0 == 0ULL) return N64路Status路undefined_divide_by_zero;
+
+ uint64_t divd = a->d0; // dividend
+ uint64_t divs = b->d0; // divisor
+
+ quotient->d0 = divd / divs;
+ remainder->d0 = divd - (quotient->d0 * divs);
+
+ return N64路Status路ok;
+ }
+
+ // modulus
+ Local N64路Status N64路modulus(N64路T *remainder, N64路T *a, N64路T *b){
+ if(b->d0 == 0ULL) return N64路Status路undefined_modulus_zero;
+
+ uint64_t divd = a->d0;
+ uint64_t divs = b->d0;
+ uint64_t q = divd / divs;
+ remainder->d0 = divd - (q * divs);
+
+ return N64路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint64_t (*ShiftOp)(uint64_t, uint64_t);
+
+ Local uint64_t shift_left_op(uint64_t value, uint64_t amount){
+ return (value << amount);
+ }
+
+ Local uint64_t shift_right_op(uint64_t value, uint64_t amount){
+ return (value >> amount);
+ }
+
+ // modifies all three of its operands
+ // in the case of duplicate operands this is the order: first modifies operand, then fill, then spill
+ Local N64路Status N64路shift
+ (
+ uint64_t shift_count,
+ N64路T *spill,
+ N64路T *operand,
+ N64路T *fill,
+ ShiftOp shift_op,
+ ShiftOp complement_shift_op
+ ){
+ if(operand == NULL && spill == NULL) return N64路Status路ok;
+
+ // Treat NULL operand as zero
+ if(operand == NULL){
+ operand = &N64路t[0];
+ N64路copy(operand, N64路zero);
+ }
+
+ // Shifting more than 63 bits breaks fill/spill logic
+ if(shift_count > 63ULL) return N64路Status路gt_max_shift_count;
+
+ N64路T *given_operand = &N64路t[1];
+ N64路copy(given_operand, operand);
+
+ // Perform the shift
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (64ULL - shift_count));
+ N64路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (64ULL - shift_count));
+ }
+
+ return N64路Status路ok;
+ }
+
+ Local N64路Status N64路shift_left(uint64_t shift_count, N64路T *spill, N64路T *operand, N64路T *fill){
+ return N64路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N64路Status N64路shift_right(uint64_t shift_count, N64路T *spill, N64路T *operand, N64路T *fill){
+ return N64路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N64路Status N64路arithmetic_shift_right(uint64_t shift_count, N64路T *operand, N64路T *spill){
+ if(shift_count > 63ULL) return N64路Status路gt_max_shift_count;
+
+ // A NULL operand is treated as zero
+ if(operand == NULL){
+ operand = &N64路t[0];
+ N64路copy(operand, N64路zero);
+ }
+
+ // sign bit check
+ N64路T *fill = (operand->d0 & (1ULL << 63)) ? N64路all_one_bit : N64路zero;
+ return N64路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N64路螞 N64路位 = {
+ .allocate_array = N64路allocate_array
+ ,.allocate_array_zero = N64路allocate_array_zero
+ ,.deallocate = N64路deallocate
+
+ ,.copy = N64路copy
+ ,.bit_and = N64路bit_and
+ ,.bit_or = N64路bit_or
+ ,.bit_complement = N64路bit_complement
+ ,.bit_twos_complement = N64路bit_twos_complement
+ ,.compare = N64路compare
+ ,.lt = N64路lt
+ ,.gt = N64路gt
+ ,.eq = N64路eq
+ ,.eq_zero = N64路eq_zero
+ ,.accumulate = N64路accumulate
+ ,.add = N64路add
+ ,.increment = N64路increment
+ ,.subtract = N64路subtract
+ ,.multiply = N64路multiply
+ ,.divide = N64路divide
+ ,.modulus = N64路modulus
+ ,.shift_left = N64路shift_left
+ ,.shift_right = N64路shift_right
+ ,.arithmetic_shift_right = N64路arithmetic_shift_right
+
+ ,.access = N64路access
+ ,.from_uint64 = N64路from_uint64
+ };
+
+ #endif
+
+#endif
+++ /dev/null
-/*
- N64 - a 64-bit native type
-
- For binary operations: a op b -> c
-
- Similar to N32, but now each Digit is 64 bits. Where a 128-bit
- intermediate is necessary (e.g. multiplication), we handle it
- manually using two 64-bit parts.
-
- On the subject of multiple pointers indicating the same location in memory:
-
- When a routine has multiple results, and one or more of the result location
- pointers point to the same storage, the routine will either return an error
- status, or have defined behavior.
-
- When a routine has multiple operands, in any combination, those
- pointers can point to the same location, and the routine will
- function as advertised.
-
- When an operand functions as both an input and a result, perhaps due
- to a result pointer pointing to the same place as an operand
- pointer, the routine will function as advertised. (Internally the
- routine might make a temporary copy of the operand to accomplish
- this.)
-*/
-
-#define N64PN路DEBUG
-
-#ifndef FACE
-#define N64PN路IMPLEMENTATION
-#define FACE
-#endif
-
-//--------------------------------------------------------------------------------
-// Interface
-
-#ifndef N64PN路FACE
-#define N64PN路FACE
-
- #include <stdint.h>
- #include <stdbool.h>
- #include <stdarg.h>
- #include <stdlib.h>
-
- //----------------------------------------
- // Instance Data (Declaration Only)
-
- typedef uint64_t Extent;
- typedef uint64_t Digit;
-
- typedef struct N64PN路T N64PN路T;
-
- extern N64PN路T *N64PN路zero;
- extern N64PN路T *N64PN路one;
- extern N64PN路T *N64PN路all_one_bit;
- extern N64PN路T *N64PN路lsb;
- extern N64PN路T *N64PN路msb;
-
- //----------------------------------------
- // Return/Error Status and handlers
-
- typedef enum {
- N64PN路Status路ok = 0
- ,N64PN路Status路overflow = 1
- ,N64PN路Status路accumulator1_overflow = 2
- ,N64PN路Status路carry = 3
- ,N64PN路Status路borrow = 4
- ,N64PN路Status路undefined_divide_by_zero = 5
- ,N64PN路Status路undefined_modulus_zero = 6
- ,N64PN路Status路gt_max_shift_count = 7
- ,N64PN路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
- ,N64PN路Status路one_word_product = 9
- ,N64PN路Status路two_word_product = 10
- } N64PN路Status;
-
- typedef enum {
- N64PN路Order_lt = -1
- ,N64PN路Order_eq = 0
- ,N64PN路Order_gt = 1
- } N64PN路Order;
-
- typedef N64PN路T *(*N64PN路Allocate_MemoryFault)(Extent);
-
- //----------------------------------------
- // Interface
-
- typedef struct {
-
- N64PN路T *(*allocate_array_zero)(Extent, N64PN路Allocate_MemoryFault);
- N64PN路T *(*allocate_array)(Extent, N64PN路Allocate_MemoryFault);
- void (*deallocate)(N64PN路T*);
-
- void (*copy)(N64PN路T*, N64PN路T*);
- void (*bit_and)(N64PN路T*, N64PN路T*, N64PN路T*);
- void (*bit_or)(N64PN路T*, N64PN路T*, N64PN路T*);
- void (*bit_complement)(N64PN路T*, N64PN路T*);
- void (*bit_twos_complement)(N64PN路T*, N64PN路T*);
- N64PN路Order (*compare)(N64PN路T*, N64PN路T*);
- bool (*lt)(N64PN路T*, N64PN路T*);
- bool (*gt)(N64PN路T*, N64PN路T*);
- bool (*eq)(N64PN路T*, N64PN路T*);
- bool (*eq_zero)(N64PN路T*);
- N64PN路Status (*accumulate)(N64PN路T *accumulator1, N64PN路T *accumulator0, ...);
- N64PN路Status (*add)(N64PN路T*, N64PN路T*, N64PN路T*);
- bool (*increment)(N64PN路T *a);
- N64PN路Status (*subtract)(N64PN路T*, N64PN路T*, N64PN路T*);
- N64PN路Status (*multiply)(N64PN路T*, N64PN路T*, N64PN路T*, N64PN路T*);
- N64PN路Status (*divide)(N64PN路T*, N64PN路T*, N64PN路T*, N64PN路T*);
- N64PN路Status (*modulus)(N64PN路T*, N64PN路T*, N64PN路T*);
- N64PN路Status (*shift_left)(Extent, N64PN路T*, N64PN路T*, N64PN路T*);
- N64PN路Status (*shift_right)(Extent, N64PN路T*, N64PN路T*, N64PN路T*);
- N64PN路Status (*arithmetic_shift_right)(Extent, N64PN路T*, N64PN路T*);
-
- N64PN路T* (*access)(N64PN路T*, Extent);
- void (*from_uint64)(N64PN路T *destination, uint64_t value);
-
- } N64PN路螞;
-
- Local const N64PN路螞 N64PN路位; // initialized in the LOCAL section
-
-#endif
-
-//--------------------------------------------------------------------------------
-// Implementation
-
-#ifdef N64PN路IMPLEMENTATION
-
- // this part goes into the library
- #ifndef LOCAL
-
- #include <stdarg.h>
- #include <stdlib.h>
-
- struct N64PN路T {
- Digit d0;
- };
-
- // For constants, we store them in an array for convenience
- // 0, 1, all bits set (~0ULL), and MSB set (1ULL<<63)
- N64PN路T N64PN路constant[4] = {
- {.d0 = 0ULL},
- {.d0 = 1ULL},
- {.d0 = ~(uint64_t)0ULL},
- {.d0 = 1ULL << 63}
- };
-
- N64PN路T *N64PN路zero = &N64PN路constant[0];
- N64PN路T *N64PN路one = &N64PN路constant[1];
- N64PN路T *N64PN路all_one_bit = &N64PN路constant[2];
- N64PN路T *N64PN路msb = &N64PN路constant[3];
- N64PN路T *N64PN路lsb = &N64PN路constant[1];
-
- // allocate an array of N64
- N64PN路T *N64PN路allocate_array(Extent extent, N64PN路Allocate_MemoryFault memory_fault){
- N64PN路T *instance = malloc( (extent + 1) * sizeof(N64PN路T) );
- if(!instance){
- return memory_fault ? memory_fault(extent) : NULL;
- }
- return instance;
- }
-
- N64PN路T *N64PN路allocate_array_zero(Extent extent, N64PN路Allocate_MemoryFault memory_fault){
- N64PN路T *instance = calloc(extent + 1, sizeof(N64PN路T));
- if(!instance){
- return memory_fault ? memory_fault(extent) : NULL;
- }
- return instance;
- }
-
- void N64PN路deallocate(N64PN路T *unencumbered){
- free(unencumbered);
- }
-
- #endif
-
- // This part is included after the library user's code
- #ifdef LOCAL
-
- // instance
-
- struct N64PN路T {
- Digit d0;
- };
-
- // local temporary variables
- Local N64PN路T N64PN路t[4];
-
- // allocation references
- extern N64PN路T *N64PN路allocate_array(Extent, N64PN路Allocate_MemoryFault);
- extern N64PN路T *N64PN路allocate_array_zero(Extent, N64PN路Allocate_MemoryFault);
- extern void N64PN路deallocate(N64PN路T *);
-
- // Access array
- Local N64PN路T* N64PN路access(N64PN路T *array, Extent index){
- return &array[index];
- }
-
- Local void N64PN路from_uint64(N64PN路T *destination, uint64_t value){
- if(destination == NULL) return;
- destination->d0 = value;
- }
-
- // copy
- Local void N64PN路copy(N64PN路T *destination, N64PN路T *source){
- if(source == destination) return;
- *destination = *source;
- }
-
- // bit operations
-
- Local void N64PN路bit_and(N64PN路T *result, N64PN路T *a, N64PN路T *b){
- result->d0 = a->d0 & b->d0;
- }
-
- Local void N64PN路bit_or(N64PN路T *result, N64PN路T *a, N64PN路T *b){
- result->d0 = a->d0 | b->d0;
- }
-
- Local void N64PN路bit_complement(N64PN路T *result, N64PN路T *a){
- result->d0 = ~a->d0;
- }
-
- Local void N64PN路bit_twos_complement(N64PN路T *result, N64PN路T *a){
- result->d0 = ~a->d0 + 1ULL;
- }
-
- // compare & test functions
-
- Local N64PN路Order N64PN路compare(N64PN路T *a, N64PN路T *b){
- if(a->d0 < b->d0) return N64PN路Order_lt;
- if(a->d0 > b->d0) return N64PN路Order_gt;
- return N64PN路Order_eq;
- }
-
- Local bool N64PN路lt(N64PN路T *a, N64PN路T *b){
- return (a->d0 < b->d0);
- }
-
- Local bool N64PN路gt(N64PN路T *a, N64PN路T *b){
- return (a->d0 > b->d0);
- }
-
- Local bool N64PN路eq(N64PN路T *a, N64PN路T *b){
- return (a->d0 == b->d0);
- }
-
- Local bool N64PN路eq_zero(N64PN路T *a){
- return (a->d0 == 0ULL);
- }
-
- // arithmetic operations
-
- // accumulate
- Local N64PN路Status N64PN路accumulate(N64PN路T *accumulator1, N64PN路T *accumulator0, ...){
- va_list args;
- va_start(args, accumulator0);
-
- uint64_t sum = accumulator0->d0;
- uint64_t carry = 0;
- N64PN路T *current;
-
- while( (current = va_arg(args, N64PN路T*)) ){
- uint64_t prior = sum;
- sum += current->d0;
- if(sum < prior){ // indicates carry
- carry++;
- // if carry overflowed a 64-bit, that's an accumulator1 overflow
- if(carry == 0ULL){
- va_end(args);
- return N64PN路Status路accumulator1_overflow;
- }
- }
- }
- va_end(args);
-
- accumulator1->d0 = carry;
- return N64PN路Status路ok;
- }
-
- // add
- Local N64PN路Status N64PN路add(N64PN路T *sum, N64PN路T *a, N64PN路T *b){
- __uint128_t result = ( __uint128_t )a->d0 + ( __uint128_t )b->d0;
- // But to avoid using a GNU extension, we can do the simpler approach:
- // Actually let's do it directly with 64-bit since we only need to detect carry out of 64 bits:
- uint64_t temp = a->d0 + b->d0;
- sum->d0 = temp;
- if(temp < a->d0) return N64PN路Status路carry; // means we overflowed
- return N64PN路Status路ok;
- }
-
- Local bool N64PN路increment(N64PN路T *a){
- uint64_t old = a->d0;
- a->d0++;
- // if it wrapped around to 0, then it was 0xFFFFFFFFFFFFFFFF
- return (a->d0 < old);
- }
-
- // subtract
- Local N64PN路Status N64PN路subtract(N64PN路T *difference, N64PN路T *a, N64PN路T *b){
- uint64_t tmpA = a->d0;
- uint64_t tmpB = b->d0;
- uint64_t diff = tmpA - tmpB;
- difference->d0 = diff;
- if(diff > tmpA) return N64PN路Status路borrow; // indicates we borrowed
- return N64PN路Status路ok;
- }
-
- // multiply
- // We'll do a 64x64->128 using two 64-bit accumulators
- Local N64PN路Status N64PN路multiply(N64PN路T *product1, N64PN路T *product0, N64PN路T *a, N64PN路T *b){
- uint64_t A = a->d0;
- uint64_t B = b->d0;
-
- // Break each operand into high & low 32 bits
- uint64_t a_lo = (uint32_t)(A & 0xffffffffULL);
- uint64_t a_hi = A >> 32;
- uint64_t b_lo = (uint32_t)(B & 0xffffffffULL);
- uint64_t b_hi = B >> 32;
-
- // partial products
- uint64_t low = a_lo * b_lo; // 64-bit
- uint64_t cross = (a_lo * b_hi) + (a_hi * b_lo); // potentially up to 2 * 32 bits => 64 bits
- uint64_t high = a_hi * b_hi; // up to 64 bits
-
- // incorporate cross into low, high
- // cross is effectively the middle bits, so shift cross by 32 and add to low
- uint64_t cross_low = (cross & 0xffffffffULL) << 32; // lower part
- uint64_t cross_high = cross >> 32; // upper part
-
- // add cross_low to low, capture carry
- uint64_t old_low = low;
- low += cross_low;
- if(low < old_low) cross_high++;
-
- // final high
- high += cross_high;
-
- // store results
- product0->d0 = low;
- product1->d0 = high;
-
- if(high == 0ULL) return N64PN路Status路one_word_product;
- return N64PN路Status路two_word_product;
- }
-
- // divide
- Local N64PN路Status N64PN路divide(N64PN路T *remainder, N64PN路T *quotient, N64PN路T *a, N64PN路T *b){
- // we do not handle a > 64-bit, just the single 64-bit
- if(b->d0 == 0ULL) return N64PN路Status路undefined_divide_by_zero;
-
- uint64_t divd = a->d0; // dividend
- uint64_t divs = b->d0; // divisor
-
- quotient->d0 = divd / divs;
- remainder->d0 = divd - (quotient->d0 * divs);
-
- return N64PN路Status路ok;
- }
-
- // modulus
- Local N64PN路Status N64PN路modulus(N64PN路T *remainder, N64PN路T *a, N64PN路T *b){
- if(b->d0 == 0ULL) return N64PN路Status路undefined_modulus_zero;
-
- uint64_t divd = a->d0;
- uint64_t divs = b->d0;
- uint64_t q = divd / divs;
- remainder->d0 = divd - (q * divs);
-
- return N64PN路Status路ok;
- }
-
- // bit motion
-
- typedef uint64_t (*ShiftOp)(uint64_t, uint64_t);
-
- Local uint64_t shift_left_op(uint64_t value, uint64_t amount){
- return (value << amount);
- }
-
- Local uint64_t shift_right_op(uint64_t value, uint64_t amount){
- return (value >> amount);
- }
-
- // modifies all three of its operands
- // in the case of duplicate operands this is the order: first modifies operand, then fill, then spill
- Local N64PN路Status N64PN路shift
- (
- uint64_t shift_count,
- N64PN路T *spill,
- N64PN路T *operand,
- N64PN路T *fill,
- ShiftOp shift_op,
- ShiftOp complement_shift_op
- ){
- if(operand == NULL && spill == NULL) return N64PN路Status路ok;
-
- // Treat NULL operand as zero
- if(operand == NULL){
- operand = &N64PN路t[0];
- N64PN路copy(operand, N64PN路zero);
- }
-
- // Shifting more than 63 bits breaks fill/spill logic
- if(shift_count > 63ULL) return N64PN路Status路gt_max_shift_count;
-
- N64PN路T *given_operand = &N64PN路t[1];
- N64PN路copy(given_operand, operand);
-
- // Perform the shift
- operand->d0 = shift_op(given_operand->d0, shift_count);
- if(fill != NULL){
- fill->d0 = complement_shift_op(fill->d0, (64ULL - shift_count));
- N64PN路bit_or(operand, operand, fill);
- }
- if(spill != NULL){
- spill->d0 = shift_op(spill->d0, shift_count);
- spill->d0 += complement_shift_op(given_operand->d0, (64ULL - shift_count));
- }
-
- return N64PN路Status路ok;
- }
-
- Local N64PN路Status N64PN路shift_left(uint64_t shift_count, N64PN路T *spill, N64PN路T *operand, N64PN路T *fill){
- return N64PN路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
- }
-
- Local N64PN路Status N64PN路shift_right(uint64_t shift_count, N64PN路T *spill, N64PN路T *operand, N64PN路T *fill){
- return N64PN路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
- }
-
- Local N64PN路Status N64PN路arithmetic_shift_right(uint64_t shift_count, N64PN路T *operand, N64PN路T *spill){
- if(shift_count > 63ULL) return N64PN路Status路gt_max_shift_count;
-
- // A NULL operand is treated as zero
- if(operand == NULL){
- operand = &N64PN路t[0];
- N64PN路copy(operand, N64PN路zero);
- }
-
- // sign bit check
- N64PN路T *fill = (operand->d0 & (1ULL << 63)) ? N64PN路all_one_bit : N64PN路zero;
- return N64PN路shift_right(shift_count, spill, operand, fill);
- }
-
- Local const N64PN路螞 N64PN路位 = {
- .allocate_array = N64PN路allocate_array
- ,.allocate_array_zero = N64PN路allocate_array_zero
- ,.deallocate = N64PN路deallocate
-
- ,.copy = N64PN路copy
- ,.bit_and = N64PN路bit_and
- ,.bit_or = N64PN路bit_or
- ,.bit_complement = N64PN路bit_complement
- ,.bit_twos_complement = N64PN路bit_twos_complement
- ,.compare = N64PN路compare
- ,.lt = N64PN路lt
- ,.gt = N64PN路gt
- ,.eq = N64PN路eq
- ,.eq_zero = N64PN路eq_zero
- ,.accumulate = N64PN路accumulate
- ,.add = N64PN路add
- ,.increment = N64PN路increment
- ,.subtract = N64PN路subtract
- ,.multiply = N64PN路multiply
- ,.divide = N64PN路divide
- ,.modulus = N64PN路modulus
- ,.shift_left = N64PN路shift_left
- ,.shift_right = N64PN路shift_right
- ,.arithmetic_shift_right = N64PN路arithmetic_shift_right
-
- ,.access = N64PN路access
- ,.from_uint64 = N64PN路from_uint64
- };
-
- #endif
-
-#endif
--- /dev/null
+/*
+ N8 - PN = refers to the use of processor native accumulator
+
+ For binary operations: a op b -> c
+
+ See the document on the proper use of the Natural types.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+*/
+
+#define N8路DEBUG
+
+#ifndef FACE
+#define N8路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N8路FACE
+#define N8路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint8_t Extent;
+ typedef uint8_t Digit;
+
+ typedef struct N8路T N8路T;
+
+ extern N8路T *N8路zero;
+ extern N8路T *N8路one;
+ extern N8路T *N8路all_one_bit;
+ extern N8路T *N8路lsb;
+ extern N8路T *N8路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum{
+ N8路Status路ok = 0
+ ,N8路Status路overflow = 1
+ ,N8路Status路accumulator1_overflow = 2
+ ,N8路Status路carry = 3
+ ,N8路Status路borrow = 4
+ ,N8路Status路undefined_divide_by_zero = 5
+ ,N8路Status路undefined_modulus_zero = 6
+ ,N8路Status路gt_max_shift_count = 7
+ ,N8路Status路spill_eq_operand = 8
+ ,N8路Status路one_word_product = 9
+ ,N8路Status路two_word_product = 10
+ } N8路Status;
+
+ typedef enum{
+ N8路Order_lt = -1
+ ,N8路Order_eq = 0
+ ,N8路Order_gt = 1
+ } N8路Order;
+
+ typedef N8路T *( *N8路Allocate_MemoryFault )(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct{
+
+ N8路T *(*allocate_array_zero)(Extent, N8路Allocate_MemoryFault);
+ N8路T *(*allocate_array)(Extent, N8路Allocate_MemoryFault);
+ void (*deallocate)(N8路T*);
+
+ void (*copy)(N8路T*, N8路T*);
+ void (*bit_and)(N8路T*, N8路T*, N8路T*);
+ void (*bit_or)(N8路T*, N8路T*, N8路T*);
+ void (*bit_complement)(N8路T*, N8路T*);
+ void (*bit_twos_complement)(N8路T*, N8路T*);
+ N8路Order (*compare)(N8路T*, N8路T*);
+ bool (*lt)(N8路T*, N8路T*);
+ bool (*gt)(N8路T*, N8路T*);
+ bool (*eq)(N8路T*, N8路T*);
+ bool (*eq_zero)(N8路T*);
+ N8路Status (*accumulate)(N8路T *accumulator1 ,N8路T *accumulator0 ,...);
+ N8路Status (*add)(N8路T*, N8路T*, N8路T*);
+ bool (*increment)(N8路T *a);
+ N8路Status (*subtract)(N8路T*, N8路T*, N8路T*);
+ N8路Status (*multiply)(N8路T*, N8路T*, N8路T*, N8路T*);
+ N8路Status (*divide)(N8路T*, N8路T*, N8路T*, N8路T*);
+ N8路Status (*modulus)(N8路T*, N8路T*, N8路T*);
+ N8路Status (*shift_left)(Extent, N8路T*, N8路T*, N8路T*);
+ N8路Status (*shift_right)(Extent, N8路T*, N8路T*, N8路T*);
+ N8路Status (*arithmetic_shift_right)(Extent, N8路T*, N8路T*);
+
+ N8路T* (*access)(N8路T*, Extent);
+ void (*from_uint32)(N8路T *destination ,uint32_t value);
+ } N8路螞;
+
+ Local const N8路螞 N8路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N8路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N8路T{
+ Digit d0;
+ };
+
+ N8路T N8路constant[4] = {
+ {.d0 = 0},
+ {.d0 = 1},
+ {.d0 = ~(uint8_t)0},
+ {.d0 = 1 << 7}
+ };
+
+ N8路T *N8路zero = &N8路constant[0];
+ N8路T *N8路one = &N8路constant[1];
+ N8路T *N8路all_one_bit = &N8路constant[2];
+ N8路T *N8路msb = &N8路constant[3];
+ N8路T *N8路lsb = &N8路constant[1];
+
+ // the allocate an array of N8
+ N8路T *N8路allocate_array(Extent extent ,N8路Allocate_MemoryFault memory_fault){
+ N8路T *instance = malloc((extent + 1) * sizeof(N8路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N8路T *N8路allocate_array_zero(Extent extent ,N8路Allocate_MemoryFault memory_fault){
+ N8路T *instance = calloc(extent + 1, sizeof(N8路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N8路deallocate(N8路T *unencumbered){
+ free(unencumbered);
+ }
+
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N8路T{
+ Digit d0;
+ };
+
+ // temporary variables
+ Local N8路T N8路t[4];
+
+ // allocation
+
+ extern N8路T *N8路allocate_array(Extent, N8路Allocate_MemoryFault);
+ extern N8路T *N8路allocate_array_zero(Extent, N8路Allocate_MemoryFault);
+ extern void N8路deallocate(N8路T *);
+
+ // so the user can access numbers in an array allocation
+ Local N8路T* N8路access(N8路T *array ,Extent index){
+ return &array[index];
+ }
+
+ Local void N8路from_uint32(N8路T *destination ,uint32_t value){
+ if(destination == NULL) return;
+ destination->d0 = (uint8_t)(value & 0xFF);
+ }
+
+ // copy, convenience copy
+
+ Local void N8路copy(N8路T *destination ,N8路T *source){
+ if(source == destination) return;
+ *destination = *source;
+ }
+
+ Local void N8路set_to_zero(N8路T *instance){
+ instance->d0 = 0;
+ }
+
+ Local void N8路set_to_one(N8路T *instance){
+ instance->d0 = 1;
+ }
+
+ // bit operations
+
+ Local void N8路bit_and(N8路T *result, N8路T *a, N8路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ Local void N8路bit_or(N8路T *result, N8路T *a, N8路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ Local void N8路bit_complement(N8路T *result, N8路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ Local void N8路bit_twos_complement(N8路T *result ,N8路T *a){
+ result->d0 = (uint8_t)(~a->d0 + 1);
+ }
+
+ // test functions
+
+ Local N8路Order N8路compare(N8路T *a, N8路T *b){
+ if(a->d0 < b->d0) return N8路Order_lt;
+ if(a->d0 > b->d0) return N8路Order_gt;
+ return N8路Order_eq;
+ }
+
+ Local bool N8路lt(N8路T *a ,N8路T *b){
+ return a->d0 < b->d0;
+ }
+
+ Local bool N8路gt(N8路T *a ,N8路T *b){
+ return a->d0 > b->d0;
+ }
+
+ Local bool N8路eq(N8路T *a ,N8路T *b){
+ return a->d0 == b->d0;
+ }
+
+ Local bool N8路eq_zero(N8路T *a){
+ return a->d0 == 0;
+ }
+
+ // arithmetic operations
+
+ Local N8路Status N8路accumulate(N8路T *accumulator1 ,N8路T *accumulator0 ,...){
+
+ va_list args;
+ va_start(args ,accumulator0);
+ uint32_t sum = accumulator0->d0;
+ uint32_t carry = 0;
+ N8路T *current;
+
+ while( (current = va_arg(args ,N8路T*)) ){
+ sum += current->d0;
+ if(sum < current->d0){
+ (carry)++;
+ if(carry == 0){
+ va_end(args);
+ return N8路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ accumulator1->d0 = (uint8_t)carry;
+ return N8路Status路ok;
+ }
+
+ Local N8路Status N8路add(N8路T *sum ,N8路T *a ,N8路T *b){
+ uint32_t result = (uint32_t)a->d0 + (uint32_t)b->d0;
+ sum->d0 = (uint8_t)(result & 0xFF);
+ return (result >> 8) ? N8路Status路carry : N8路Status路ok;
+ }
+
+ Local bool N8路increment(N8路T *a){
+ a->d0++;
+ return (a->d0 == 0);
+ }
+
+ Local N8路Status N8路subtract(N8路T *difference ,N8路T *a ,N8路T *b){
+ uint32_t diff = (uint32_t)a->d0 - (uint32_t)b->d0;
+ difference->d0 = (uint8_t)(diff & 0xFF);
+ return (diff > a->d0) ? N8路Status路borrow : N8路Status路ok;
+ }
+
+ Local N8路Status N8路multiply(N8路T *product1 ,N8路T *product0 ,N8路T *a ,N8路T *b){
+ uint32_t product = (uint32_t)a->d0 * (uint32_t)b->d0;
+ product0->d0 = (uint8_t)(product & 0xFF);
+ product1->d0 = (uint8_t)((product >> 8) & 0xFF);
+
+ if(product1->d0 == 0) return N8路Status路one_word_product;
+ return N8路Status路two_word_product;
+ }
+
+ Local N8路Status N8路divide(N8路T *remainder ,N8路T *quotient ,N8路T *a ,N8路T *b){
+ if(b->d0 == 0) return N8路Status路undefined_divide_by_zero;
+
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ quotient->d0 = (uint8_t)(dividend / divisor);
+ remainder->d0 = (uint8_t)(dividend - (quotient->d0 * divisor));
+
+ return N8路Status路ok;
+ }
+
+ Local N8路Status N8路modulus(N8路T *remainder ,N8路T *a ,N8路T *b){
+ if(b->d0 == 0) return N8路Status路undefined_modulus_zero;
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ uint32_t q = dividend / divisor;
+ remainder->d0 = (uint8_t)(dividend - (q * divisor));
+ return N8路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint8_t (*ShiftOp)(uint8_t, uint8_t);
+
+ Local uint8_t shift_left_op(uint8_t value, uint8_t amount){
+ return (uint8_t)(value << amount);
+ }
+
+ Local uint8_t shift_right_op(uint8_t value, uint8_t amount){
+ return (uint8_t)(value >> amount);
+ }
+
+ Local N8路Status N8路shift
+ (
+ uint8_t shift_count
+ ,N8路T *spill
+ ,N8路T *operand
+ ,N8路T *fill
+ ,ShiftOp shift_op
+ ,ShiftOp complement_shift_op
+ ){
+
+ if(operand == NULL && spill == NULL) return N8路Status路ok;
+
+ if(operand == NULL){
+ operand = &N8路t[0];
+ N8路copy(operand, N8路zero);
+ }
+
+ if(shift_count > 7) return N8路Status路gt_max_shift_count;
+
+ N8路T *given_operand = &N8路t[1];
+ N8路copy(given_operand, operand);
+
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (8 - shift_count));
+ N8路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (8 - shift_count));
+ }
+
+ return N8路Status路ok;
+ }
+
+ Local N8路Status
+ N8路shift_left(uint8_t shift_count, N8路T *spill, N8路T *operand, N8路T *fill){
+ return N8路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N8路Status
+ N8路shift_right(uint8_t shift_count, N8路T *spill, N8路T *operand, N8路T *fill){
+ return N8路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N8路Status
+ N8路arithmetic_shift_right(uint8_t shift_count, N8路T *operand, N8路T *spill){
+
+ if(shift_count > 7) return N8路Status路gt_max_shift_count;
+
+ if(operand == NULL){
+ operand = &N8路t[0];
+ N8路copy(operand, N8路zero);
+ }
+
+ N8路T *fill = (operand->d0 & 0x80) ? N8路all_one_bit : N8路zero;
+ return N8路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N8路螞 N8路位 = {
+
+ .allocate_array = N8路allocate_array
+ ,.allocate_array_zero = N8路allocate_array_zero
+ ,.deallocate = N8路deallocate
+
+ ,.copy = N8路copy
+ ,.bit_and = N8路bit_and
+ ,.bit_or = N8路bit_or
+ ,.bit_complement = N8路bit_complement
+ ,.bit_twos_complement = N8路bit_twos_complement
+ ,.compare = N8路compare
+ ,.lt = N8路lt
+ ,.gt = N8路gt
+ ,.eq = N8路eq
+ ,.eq_zero = N8路eq_zero
+ ,.accumulate = N8路accumulate
+ ,.add = N8路add
+ ,.increment = N8路increment
+ ,.subtract = N8路subtract
+ ,.multiply = N8路multiply
+ ,.divide = N8路divide
+ ,.modulus = N8路modulus
+ ,.shift_left = N8路shift_left
+ ,.shift_right = N8路shift_right
+ ,.arithmetic_shift_right = N8路arithmetic_shift_right
+
+ ,.access = N8路access
+ ,.from_uint32 = N8路from_uint32
+ };
+
+ #endif
+
+#endif
+++ /dev/null
-/*
- N8PN - PN = refers to the use of processor native accumulator
-
- For binary operations: a op b -> c
-
- See the document on the proper use of the Natural types.
-
- On the subject of multiple pointers indicating the same location in memory:
-
- When a routine has multiple results, and one or more of the result location
- pointers point to the same storage, the routine will either return an error
- status, or have defined behavior.
-
- When a routine has multiple operands, in any combination, those
- pointers can point to the same location, and the routine will
- function as advertised.
-
- When an operand functions as both an input and a result, perhaps due
- to a result pointer pointing to the same place as an operand
- pointer, the routine will function as advertised. (Internally the
- routine might make a temporary copy of the operand to accomplish
- this.)
-*/
-
-#define N8PN路DEBUG
-
-#ifndef FACE
-#define N8PN路IMPLEMENTATION
-#define FACE
-#endif
-
-//--------------------------------------------------------------------------------
-// Interface
-
-#ifndef N8PN路FACE
-#define N8PN路FACE
-
- #include <stdint.h>
- #include <stdbool.h>
- #include <stdarg.h>
- #include <stdlib.h>
-
- //----------------------------------------
- // Instance Data (Declaration Only)
-
- typedef uint8_t Extent;
- typedef uint8_t Digit;
-
- typedef struct N8PN路T N8PN路T;
-
- extern N8PN路T *N8PN路zero;
- extern N8PN路T *N8PN路one;
- extern N8PN路T *N8PN路all_one_bit;
- extern N8PN路T *N8PN路lsb;
- extern N8PN路T *N8PN路msb;
-
- //----------------------------------------
- // Return/Error Status and handlers
-
- typedef enum{
- N8PN路Status路ok = 0
- ,N8PN路Status路overflow = 1
- ,N8PN路Status路accumulator1_overflow = 2
- ,N8PN路Status路carry = 3
- ,N8PN路Status路borrow = 4
- ,N8PN路Status路undefined_divide_by_zero = 5
- ,N8PN路Status路undefined_modulus_zero = 6
- ,N8PN路Status路gt_max_shift_count = 7
- ,N8PN路Status路spill_eq_operand = 8
- ,N8PN路Status路one_word_product = 9
- ,N8PN路Status路two_word_product = 10
- } N8PN路Status;
-
- typedef enum{
- N8PN路Order_lt = -1
- ,N8PN路Order_eq = 0
- ,N8PN路Order_gt = 1
- } N8PN路Order;
-
- typedef N8PN路T *( *N8PN路Allocate_MemoryFault )(Extent);
-
- //----------------------------------------
- // Interface
-
- typedef struct{
-
- N8PN路T *(*allocate_array_zero)(Extent, N8PN路Allocate_MemoryFault);
- N8PN路T *(*allocate_array)(Extent, N8PN路Allocate_MemoryFault);
- void (*deallocate)(N8PN路T*);
-
- void (*copy)(N8PN路T*, N8PN路T*);
- void (*bit_and)(N8PN路T*, N8PN路T*, N8PN路T*);
- void (*bit_or)(N8PN路T*, N8PN路T*, N8PN路T*);
- void (*bit_complement)(N8PN路T*, N8PN路T*);
- void (*bit_twos_complement)(N8PN路T*, N8PN路T*);
- N8PN路Order (*compare)(N8PN路T*, N8PN路T*);
- bool (*lt)(N8PN路T*, N8PN路T*);
- bool (*gt)(N8PN路T*, N8PN路T*);
- bool (*eq)(N8PN路T*, N8PN路T*);
- bool (*eq_zero)(N8PN路T*);
- N8PN路Status (*accumulate)(N8PN路T *accumulator1 ,N8PN路T *accumulator0 ,...);
- N8PN路Status (*add)(N8PN路T*, N8PN路T*, N8PN路T*);
- bool (*increment)(N8PN路T *a);
- N8PN路Status (*subtract)(N8PN路T*, N8PN路T*, N8PN路T*);
- N8PN路Status (*multiply)(N8PN路T*, N8PN路T*, N8PN路T*, N8PN路T*);
- N8PN路Status (*divide)(N8PN路T*, N8PN路T*, N8PN路T*, N8PN路T*);
- N8PN路Status (*modulus)(N8PN路T*, N8PN路T*, N8PN路T*);
- N8PN路Status (*shift_left)(Extent, N8PN路T*, N8PN路T*, N8PN路T*);
- N8PN路Status (*shift_right)(Extent, N8PN路T*, N8PN路T*, N8PN路T*);
- N8PN路Status (*arithmetic_shift_right)(Extent, N8PN路T*, N8PN路T*);
-
- N8PN路T* (*access)(N8PN路T*, Extent);
- void (*from_uint32)(N8PN路T *destination ,uint32_t value);
- } N8PN路螞;
-
- Local const N8PN路螞 N8PN路位; // initialized in the LOCAL section
-
-#endif
-
-//--------------------------------------------------------------------------------
-// Implementation
-
-#ifdef N8PN路IMPLEMENTATION
-
- // this part goes into the library
- #ifndef LOCAL
-
- #include <stdarg.h>
- #include <stdlib.h>
-
- struct N8PN路T{
- Digit d0;
- };
-
- N8PN路T N8PN路constant[4] = {
- {.d0 = 0},
- {.d0 = 1},
- {.d0 = ~(uint8_t)0},
- {.d0 = 1 << 7}
- };
-
- N8PN路T *N8PN路zero = &N8PN路constant[0];
- N8PN路T *N8PN路one = &N8PN路constant[1];
- N8PN路T *N8PN路all_one_bit = &N8PN路constant[2];
- N8PN路T *N8PN路msb = &N8PN路constant[3];
- N8PN路T *N8PN路lsb = &N8PN路constant[1];
-
- // the allocate an array of N8
- N8PN路T *N8PN路allocate_array(Extent extent ,N8PN路Allocate_MemoryFault memory_fault){
- N8PN路T *instance = malloc((extent + 1) * sizeof(N8PN路T));
- if(!instance){
- return memory_fault ? memory_fault(extent) : NULL;
- }
- return instance;
- }
-
- N8PN路T *N8PN路allocate_array_zero(Extent extent ,N8PN路Allocate_MemoryFault memory_fault){
- N8PN路T *instance = calloc(extent + 1, sizeof(N8PN路T));
- if(!instance){
- return memory_fault ? memory_fault(extent) : NULL;
- }
- return instance;
- }
-
- void N8PN路deallocate(N8PN路T *unencumbered){
- free(unencumbered);
- }
-
-
- #endif
-
- // This part is included after the library user's code
- #ifdef LOCAL
-
- // instance
-
- struct N8PN路T{
- Digit d0;
- };
-
- // temporary variables
- Local N8PN路T N8PN路t[4];
-
- // allocation
-
- extern N8PN路T *N8PN路allocate_array(Extent, N8PN路Allocate_MemoryFault);
- extern N8PN路T *N8PN路allocate_array_zero(Extent, N8PN路Allocate_MemoryFault);
- extern void N8PN路deallocate(N8PN路T *);
-
- // so the user can access numbers in an array allocation
- Local N8PN路T* N8PN路access(N8PN路T *array ,Extent index){
- return &array[index];
- }
-
- Local void N8PN路from_uint32(N8PN路T *destination ,uint32_t value){
- if(destination == NULL) return;
- destination->d0 = (uint8_t)(value & 0xFF);
- }
-
- // copy, convenience copy
-
- Local void N8PN路copy(N8PN路T *destination ,N8PN路T *source){
- if(source == destination) return;
- *destination = *source;
- }
-
- Local void N8PN路set_to_zero(N8PN路T *instance){
- instance->d0 = 0;
- }
-
- Local void N8PN路set_to_one(N8PN路T *instance){
- instance->d0 = 1;
- }
-
- // bit operations
-
- Local void N8PN路bit_and(N8PN路T *result, N8PN路T *a, N8PN路T *b){
- result->d0 = a->d0 & b->d0;
- }
-
- Local void N8PN路bit_or(N8PN路T *result, N8PN路T *a, N8PN路T *b){
- result->d0 = a->d0 | b->d0;
- }
-
- Local void N8PN路bit_complement(N8PN路T *result, N8PN路T *a){
- result->d0 = ~a->d0;
- }
-
- Local void N8PN路bit_twos_complement(N8PN路T *result ,N8PN路T *a){
- result->d0 = (uint8_t)(~a->d0 + 1);
- }
-
- // test functions
-
- Local N8PN路Order N8PN路compare(N8PN路T *a, N8PN路T *b){
- if(a->d0 < b->d0) return N8PN路Order_lt;
- if(a->d0 > b->d0) return N8PN路Order_gt;
- return N8PN路Order_eq;
- }
-
- Local bool N8PN路lt(N8PN路T *a ,N8PN路T *b){
- return a->d0 < b->d0;
- }
-
- Local bool N8PN路gt(N8PN路T *a ,N8PN路T *b){
- return a->d0 > b->d0;
- }
-
- Local bool N8PN路eq(N8PN路T *a ,N8PN路T *b){
- return a->d0 == b->d0;
- }
-
- Local bool N8PN路eq_zero(N8PN路T *a){
- return a->d0 == 0;
- }
-
- // arithmetic operations
-
- Local N8PN路Status N8PN路accumulate(N8PN路T *accumulator1 ,N8PN路T *accumulator0 ,...){
-
- va_list args;
- va_start(args ,accumulator0);
- uint32_t sum = accumulator0->d0;
- uint32_t carry = 0;
- N8PN路T *current;
-
- while( (current = va_arg(args ,N8PN路T*)) ){
- sum += current->d0;
- if(sum < current->d0){
- (carry)++;
- if(carry == 0){
- va_end(args);
- return N8PN路Status路accumulator1_overflow;
- }
- }
- }
- va_end(args);
-
- accumulator1->d0 = (uint8_t)carry;
- return N8PN路Status路ok;
- }
-
- Local N8PN路Status N8PN路add(N8PN路T *sum ,N8PN路T *a ,N8PN路T *b){
- uint32_t result = (uint32_t)a->d0 + (uint32_t)b->d0;
- sum->d0 = (uint8_t)(result & 0xFF);
- return (result >> 8) ? N8PN路Status路carry : N8PN路Status路ok;
- }
-
- Local bool N8PN路increment(N8PN路T *a){
- a->d0++;
- return (a->d0 == 0);
- }
-
- Local N8PN路Status N8PN路subtract(N8PN路T *difference ,N8PN路T *a ,N8PN路T *b){
- uint32_t diff = (uint32_t)a->d0 - (uint32_t)b->d0;
- difference->d0 = (uint8_t)(diff & 0xFF);
- return (diff > a->d0) ? N8PN路Status路borrow : N8PN路Status路ok;
- }
-
- Local N8PN路Status N8PN路multiply(N8PN路T *product1 ,N8PN路T *product0 ,N8PN路T *a ,N8PN路T *b){
- uint32_t product = (uint32_t)a->d0 * (uint32_t)b->d0;
- product0->d0 = (uint8_t)(product & 0xFF);
- product1->d0 = (uint8_t)((product >> 8) & 0xFF);
-
- if(product1->d0 == 0) return N8PN路Status路one_word_product;
- return N8PN路Status路two_word_product;
- }
-
- Local N8PN路Status N8PN路divide(N8PN路T *remainder ,N8PN路T *quotient ,N8PN路T *a ,N8PN路T *b){
- if(b->d0 == 0) return N8PN路Status路undefined_divide_by_zero;
-
- uint32_t dividend = a->d0;
- uint32_t divisor = b->d0;
- quotient->d0 = (uint8_t)(dividend / divisor);
- remainder->d0 = (uint8_t)(dividend - (quotient->d0 * divisor));
-
- return N8PN路Status路ok;
- }
-
- Local N8PN路Status N8PN路modulus(N8PN路T *remainder ,N8PN路T *a ,N8PN路T *b){
- if(b->d0 == 0) return N8PN路Status路undefined_modulus_zero;
- uint32_t dividend = a->d0;
- uint32_t divisor = b->d0;
- uint32_t q = dividend / divisor;
- remainder->d0 = (uint8_t)(dividend - (q * divisor));
- return N8PN路Status路ok;
- }
-
- // bit motion
-
- typedef uint8_t (*ShiftOp)(uint8_t, uint8_t);
-
- Local uint8_t shift_left_op(uint8_t value, uint8_t amount){
- return (uint8_t)(value << amount);
- }
-
- Local uint8_t shift_right_op(uint8_t value, uint8_t amount){
- return (uint8_t)(value >> amount);
- }
-
- Local N8PN路Status N8PN路shift
- (
- uint8_t shift_count
- ,N8PN路T *spill
- ,N8PN路T *operand
- ,N8PN路T *fill
- ,ShiftOp shift_op
- ,ShiftOp complement_shift_op
- ){
-
- if(operand == NULL && spill == NULL) return N8PN路Status路ok;
-
- if(operand == NULL){
- operand = &N8PN路t[0];
- N8PN路copy(operand, N8PN路zero);
- }
-
- if(shift_count > 7) return N8PN路Status路gt_max_shift_count;
-
- N8PN路T *given_operand = &N8PN路t[1];
- N8PN路copy(given_operand, operand);
-
- operand->d0 = shift_op(given_operand->d0, shift_count);
- if(fill != NULL){
- fill->d0 = complement_shift_op(fill->d0, (8 - shift_count));
- N8PN路bit_or(operand, operand, fill);
- }
- if(spill != NULL){
- spill->d0 = shift_op(spill->d0, shift_count);
- spill->d0 += complement_shift_op(given_operand->d0, (8 - shift_count));
- }
-
- return N8PN路Status路ok;
- }
-
- Local N8PN路Status
- N8PN路shift_left(uint8_t shift_count, N8PN路T *spill, N8PN路T *operand, N8PN路T *fill){
- return N8PN路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
- }
-
- Local N8PN路Status
- N8PN路shift_right(uint8_t shift_count, N8PN路T *spill, N8PN路T *operand, N8PN路T *fill){
- return N8PN路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
- }
-
- Local N8PN路Status
- N8PN路arithmetic_shift_right(uint8_t shift_count, N8PN路T *operand, N8PN路T *spill){
-
- if(shift_count > 7) return N8PN路Status路gt_max_shift_count;
-
- if(operand == NULL){
- operand = &N8PN路t[0];
- N8PN路copy(operand, N8PN路zero);
- }
-
- N8PN路T *fill = (operand->d0 & 0x80) ? N8PN路all_one_bit : N8PN路zero;
- return N8PN路shift_right(shift_count, spill, operand, fill);
- }
-
- Local const N8PN路螞 N8PN路位 = {
-
- .allocate_array = N8PN路allocate_array
- ,.allocate_array_zero = N8PN路allocate_array_zero
- ,.deallocate = N8PN路deallocate
-
- ,.copy = N8PN路copy
- ,.bit_and = N8PN路bit_and
- ,.bit_or = N8PN路bit_or
- ,.bit_complement = N8PN路bit_complement
- ,.bit_twos_complement = N8PN路bit_twos_complement
- ,.compare = N8PN路compare
- ,.lt = N8PN路lt
- ,.gt = N8PN路gt
- ,.eq = N8PN路eq
- ,.eq_zero = N8PN路eq_zero
- ,.accumulate = N8PN路accumulate
- ,.add = N8PN路add
- ,.increment = N8PN路increment
- ,.subtract = N8PN路subtract
- ,.multiply = N8PN路multiply
- ,.divide = N8PN路divide
- ,.modulus = N8PN路modulus
- ,.shift_left = N8PN路shift_left
- ,.shift_right = N8PN路shift_right
- ,.arithmetic_shift_right = N8PN路arithmetic_shift_right
-
- ,.access = N8PN路access
- ,.from_uint32 = N8PN路from_uint32
- };
-
- #endif
-
-#endif
#----------------------------------------
# N32 made from 1 digit, of 32 bits.
- type_name = "N32"
+ type_name = "N32_1x32"
digit_type = "uint32_t"
digit_extent = 0
--- /dev/null
+/*
+ N16 - a processor native type
+
+ For binary operations: a op b -> c
+
+ See the document on the proper use of the Natural types.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+*/
+
+#define N16路DEBUG
+
+#ifndef FACE
+#define N16路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N16路FACE
+#define N16路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint16_t Extent;
+ typedef uint16_t Digit;
+
+ typedef struct N16路T N16路T;
+
+ extern N16路T *N16路zero;
+ extern N16路T *N16路one;
+ extern N16路T *N16路all_one_bit;
+ extern N16路T *N16路lsb;
+ extern N16路T *N16路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum{
+ N16路Status路ok = 0
+ ,N16路Status路overflow = 1
+ ,N16路Status路accumulator1_overflow = 2
+ ,N16路Status路carry = 3
+ ,N16路Status路borrow = 4
+ ,N16路Status路undefined_divide_by_zero = 5
+ ,N16路Status路undefined_modulus_zero = 6
+ ,N16路Status路gt_max_shift_count = 7
+ ,N16路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
+ ,N16路Status路one_word_product = 9
+ ,N16路Status路two_word_product = 10
+ } N16路Status;
+
+ typedef enum{
+ N16路Order_lt = -1
+ ,N16路Order_eq = 0
+ ,N16路Order_gt = 1
+ } N16路Order;
+
+ typedef N16路T *( *N16路Allocate_MemoryFault )(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct{
+
+ N16路T *(*allocate_array_zero)(Extent, N16路Allocate_MemoryFault);
+ N16路T *(*allocate_array)(Extent, N16路Allocate_MemoryFault);
+ void (*deallocate)(N16路T*);
+
+ void (*copy)(N16路T*, N16路T*);
+ void (*bit_and)(N16路T*, N16路T*, N16路T*);
+ void (*bit_or)(N16路T*, N16路T*, N16路T*);
+ void (*bit_complement)(N16路T*, N16路T*);
+ void (*bit_twos_complement)(N16路T*, N16路T*);
+ N16路Order (*compare)(N16路T*, N16路T*);
+ bool (*lt)(N16路T*, N16路T*);
+ bool (*gt)(N16路T*, N16路T*);
+ bool (*eq)(N16路T*, N16路T*);
+ bool (*eq_zero)(N16路T*);
+ N16路Status (*accumulate)(N16路T *accumulator1 ,N16路T *accumulator0 ,...);
+ N16路Status (*add)(N16路T*, N16路T*, N16路T*);
+ bool (*increment)(N16路T *a);
+ N16路Status (*subtract)(N16路T*, N16路T*, N16路T*);
+ N16路Status (*multiply)(N16路T*, N16路T*, N16路T*, N16路T*);
+ N16路Status (*divide)(N16路T*, N16路T*, N16路T*, N16路T*);
+ N16路Status (*modulus)(N16路T*, N16路T*, N16路T*);
+ N16路Status (*shift_left)(Extent, N16路T*, N16路T*, N16路T*);
+ N16路Status (*shift_right)(Extent, N16路T*, N16路T*, N16路T*);
+ N16路Status (*arithmetic_shift_right)(Extent, N16路T*, N16路T*);
+
+ N16路T* (*access)(N16路T*, Extent);
+ void (*from_uint32)(N16路T *destination ,uint32_t value);
+ } N16路螞;
+
+ Local const N16路螞 N16路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N16路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N16路T{
+ Digit d0;
+ };
+
+ N16路T N16路constant[4] = {
+ {.d0 = 0},
+ {.d0 = 1},
+ {.d0 = ~(uint16_t)0},
+ {.d0 = 1 << 15}
+ };
+
+ N16路T *N16路zero = &N16路constant[0];
+ N16路T *N16路one = &N16路constant[1];
+ N16路T *N16路all_one_bit = &N16路constant[2];
+ N16路T *N16路msb = &N16路constant[3];
+ N16路T *N16路lsb = &N16路constant[1];
+
+ // the allocate an array of N16
+ N16路T *N16路allocate_array(Extent extent ,N16路Allocate_MemoryFault memory_fault){
+ N16路T *instance = malloc((extent + 1) * sizeof(N16路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N16路T *N16路allocate_array_zero(Extent extent ,N16路Allocate_MemoryFault memory_fault){
+ N16路T *instance = calloc(extent + 1, sizeof(N16路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N16路deallocate(N16路T *unencumbered){
+ free(unencumbered);
+ }
+
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N16路T{
+ Digit d0;
+ };
+
+ // temporary variables
+ Local N16路T N16路t[4];
+
+ // allocation
+
+ extern N16路T *N16路allocate_array(Extent, N16路Allocate_MemoryFault);
+ extern N16路T *N16路allocate_array_zero(Extent, N16路Allocate_MemoryFault);
+ extern void N16路deallocate(N16路T *);
+
+ // so the user can access numbers in an array allocation
+ Local N16路T* N16路access(N16路T *array ,Extent index){
+ return &array[index];
+ }
+
+ Local void N16路from_uint32(N16路T *destination ,uint32_t value){
+ if(destination == NULL) return;
+ destination->d0 = (uint16_t)(value & 0xFFFF);
+ }
+
+ // copy, convenience copy
+
+ Local void N16路copy(N16路T *destination ,N16路T *source){
+ if(source == destination) return;
+ *destination = *source;
+ }
+
+ Local void N16路set_to_zero(N16路T *instance){
+ instance->d0 = 0;
+ }
+
+ Local void N16路set_to_one(N16路T *instance){
+ instance->d0 = 1;
+ }
+
+ // bit operations
+
+ Local void N16路bit_and(N16路T *result, N16路T *a, N16路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ Local void N16路bit_or(N16路T *result, N16路T *a, N16路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ Local void N16路bit_complement(N16路T *result, N16路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ Local void N16路bit_twos_complement(N16路T *result ,N16路T *a){
+ result->d0 = (uint16_t)(~a->d0 + 1);
+ }
+
+ // test functions
+
+ Local N16路Order N16路compare(N16路T *a, N16路T *b){
+ if(a->d0 < b->d0) return N16路Order_lt;
+ if(a->d0 > b->d0) return N16路Order_gt;
+ return N16路Order_eq;
+ }
+
+ Local bool N16路lt(N16路T *a ,N16路T *b){
+ return a->d0 < b->d0;
+ }
+
+ Local bool N16路gt(N16路T *a ,N16路T *b){
+ return a->d0 > b->d0;
+ }
+
+ Local bool N16路eq(N16路T *a ,N16路T *b){
+ return a->d0 == b->d0;
+ }
+
+ Local bool N16路eq_zero(N16路T *a){
+ return a->d0 == 0;
+ }
+
+ // arithmetic operations
+
+ Local N16路Status N16路accumulate(N16路T *accumulator1 ,N16路T *accumulator0 ,...){
+
+ va_list args;
+ va_start(args ,accumulator0);
+ uint32_t sum = accumulator0->d0;
+ uint32_t carry = 0;
+ N16路T *current;
+
+ while( (current = va_arg(args ,N16路T*)) ){
+ sum += current->d0;
+ if(sum < current->d0){
+ (carry)++;
+ if(carry == 0){
+ va_end(args);
+ return N16路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ accumulator1->d0 = (uint16_t)carry;
+ return N16路Status路ok;
+ }
+
+ Local N16路Status N16路add(N16路T *sum ,N16路T *a ,N16路T *b){
+ uint32_t result = (uint32_t)a->d0 + (uint32_t)b->d0;
+ sum->d0 = (uint16_t)(result & 0xFFFF);
+ return (result >> 16) ? N16路Status路carry : N16路Status路ok;
+ }
+
+ Local bool N16路increment(N16路T *a){
+ a->d0++;
+ return (a->d0 == 0);
+ }
+
+ Local N16路Status N16路subtract(N16路T *difference ,N16路T *a ,N16路T *b){
+ uint32_t diff = (uint32_t)a->d0 - (uint32_t)b->d0;
+ difference->d0 = (uint16_t)(diff & 0xFFFF);
+ return (diff > a->d0) ? N16路Status路borrow : N16路Status路ok;
+ }
+
+ Local N16路Status N16路multiply(N16路T *product1 ,N16路T *product0 ,N16路T *a ,N16路T *b){
+ uint32_t product = (uint32_t)a->d0 * (uint32_t)b->d0;
+ product0->d0 = (uint16_t)(product & 0xFFFF);
+ product1->d0 = (uint16_t)((product >> 16) & 0xFFFF);
+
+ if(product1->d0 == 0) return N16路Status路one_word_product;
+ return N16路Status路two_word_product;
+ }
+
+ Local N16路Status N16路divide(N16路T *remainder ,N16路T *quotient ,N16路T *a ,N16路T *b){
+ if(b->d0 == 0) return N16路Status路undefined_divide_by_zero;
+
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ quotient->d0 = (uint16_t)(dividend / divisor);
+ remainder->d0 = (uint16_t)(dividend - (quotient->d0 * divisor));
+
+ return N16路Status路ok;
+ }
+
+ Local N16路Status N16路modulus(N16路T *remainder ,N16路T *a ,N16路T *b){
+ if(b->d0 == 0) return N16路Status路undefined_modulus_zero;
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ uint32_t q = dividend / divisor;
+ remainder->d0 = (uint16_t)(dividend - (q * divisor));
+ return N16路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint16_t (*ShiftOp)(uint16_t, uint16_t);
+
+ Local uint16_t shift_left_op(uint16_t value, uint16_t amount){
+ return value << amount;
+ }
+
+ Local uint16_t shift_right_op(uint16_t value, uint16_t amount){
+ return (uint16_t)(value >> amount);
+ }
+
+ Local N16路Status N16路shift
+ (
+ uint16_t shift_count
+ ,N16路T *spill
+ ,N16路T *operand
+ ,N16路T *fill
+ ,ShiftOp shift_op
+ ,ShiftOp complement_shift_op
+ ){
+
+ if(operand == NULL && spill == NULL) return N16路Status路ok;
+
+ if(operand == NULL){
+ operand = &N16路t[0];
+ N16路copy(operand, N16路zero);
+ }
+
+ if(shift_count > 15) return N16路Status路gt_max_shift_count;
+
+ N16路T *given_operand = &N16路t[1];
+ N16路copy(given_operand, operand);
+
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (16 - shift_count));
+ N16路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (16 - shift_count));
+ }
+
+ return N16路Status路ok;
+ }
+
+ Local N16路Status
+ N16路shift_left(uint16_t shift_count, N16路T *spill, N16路T *operand, N16路T *fill){
+ return N16路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N16路Status
+ N16路shift_right(uint16_t shift_count, N16路T *spill, N16路T *operand, N16路T *fill){
+ return N16路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N16路Status
+ N16路arithmetic_shift_right(uint16_t shift_count, N16路T *operand, N16路T *spill){
+
+ if(shift_count > 15) return N16路Status路gt_max_shift_count;
+
+ if(operand == NULL){
+ operand = &N16路t[0];
+ N16路copy(operand, N16路zero);
+ }
+
+ N16路T *fill = (operand->d0 & 0x8000) ? N16路all_one_bit : N16路zero;
+ return N16路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N16路螞 N16路位 = {
+
+ .allocate_array = N16路allocate_array
+ ,.allocate_array_zero = N16路allocate_array_zero
+ ,.deallocate = N16路deallocate
+
+ ,.copy = N16路copy
+ ,.bit_and = N16路bit_and
+ ,.bit_or = N16路bit_or
+ ,.bit_complement = N16路bit_complement
+ ,.bit_twos_complement = N16路bit_twos_complement
+ ,.compare = N16路compare
+ ,.lt = N16路lt
+ ,.gt = N16路gt
+ ,.eq = N16路eq
+ ,.eq_zero = N16路eq_zero
+ ,.accumulate = N16路accumulate
+ ,.add = N16路add
+ ,.increment = N16路increment
+ ,.subtract = N16路subtract
+ ,.multiply = N16路multiply
+ ,.divide = N16路divide
+ ,.modulus = N16路modulus
+ ,.shift_left = N16路shift_left
+ ,.shift_right = N16路shift_right
+ ,.arithmetic_shift_right = N16路arithmetic_shift_right
+
+ ,.access = N16路access
+ ,.from_uint32 = N16路from_uint32
+ };
+
+ #endif
+
+#endif
--- /dev/null
+/*
+ N32 - a processor native type
+
+ For binary operations: a op b -> c
+
+ See the document on the proper use of the Natural types.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+
+*/
+
+#define N32_1x32路DEBUG
+
+#ifndef FACE
+#define N32_1x32路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N32_1x32路FACE
+#define N32_1x32路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint32_t Extent;
+ typedef uint32_t Digit;
+
+ typedef struct N32_1x32路T N32_1x32路T;
+
+ extern N32_1x32路T *N32_1x32路zero;
+ extern N32_1x32路T *N32_1x32路one;
+ extern N32_1x32路T *N32_1x32路all_one_bit;
+ extern N32_1x32路T *N32_1x32路lsb;
+ extern N32_1x32路T *N32_1x32路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum{
+ N32_1x32路Status路ok = 0
+ ,N32_1x32路Status路overflow = 1
+ ,N32_1x32路Status路accumulator1_overflow = 2
+ ,N32_1x32路Status路carry = 3
+ ,N32_1x32路Status路borrow = 4
+ ,N32_1x32路Status路undefined_divide_by_zero = 5
+ ,N32_1x32路Status路undefined_modulus_zero = 6
+ ,N32_1x32路Status路gt_max_shift_count = 7
+ ,N32_1x32路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
+ ,N32_1x32路Status路one_word_product = 9
+ ,N32_1x32路Status路two_word_product = 10
+ } N32_1x32路Status;
+
+ typedef enum{
+ N32_1x32路Order_lt = -1
+ ,N32_1x32路Order_eq = 0
+ ,N32_1x32路Order_gt = 1
+ } N32_1x32路Order;
+
+ typedef N32_1x32路T *( *N32_1x32路Allocate_MemoryFault )(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct{
+
+ N32_1x32路T *(*allocate_array_zero)(Extent, N32_1x32路Allocate_MemoryFault);
+ N32_1x32路T *(*allocate_array)(Extent, N32_1x32路Allocate_MemoryFault);
+ void (*deallocate)(N32_1x32路T*);
+
+ void (*copy)(N32_1x32路T*, N32_1x32路T*);
+ void (*bit_and)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ void (*bit_or)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ void (*bit_complement)(N32_1x32路T*, N32_1x32路T*);
+ void (*bit_twos_complement)(N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Order (*compare)(N32_1x32路T*, N32_1x32路T*);
+ bool (*lt)(N32_1x32路T*, N32_1x32路T*);
+ bool (*gt)(N32_1x32路T*, N32_1x32路T*);
+ bool (*eq)(N32_1x32路T*, N32_1x32路T*);
+ bool (*eq_zero)(N32_1x32路T*);
+ N32_1x32路Status (*accumulate)(N32_1x32路T *accumulator1 ,N32_1x32路T *accumulator0 ,...);
+ N32_1x32路Status (*add)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ bool (*increment)(N32_1x32路T *a);
+ N32_1x32路Status (*subtract)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*multiply)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*divide)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*modulus)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*shift_left)(Extent, N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*shift_right)(Extent, N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*arithmetic_shift_right)(Extent, N32_1x32路T*, N32_1x32路T*);
+
+ N32_1x32路T* (*access)(N32_1x32路T*, Extent);
+ void (*from_uint32)(N32_1x32路T *destination ,uint32_t value);
+ } N32_1x32路螞;
+
+ Local const N32_1x32路螞 N32_1x32路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N32_1x32路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N32_1x32路T{
+ Digit d0;
+ };
+
+ N32_1x32路T N32_1x32路constant[4] = {
+ {.d0 = 0},
+ {.d0 = 1},
+ {.d0 = ~(uint32_t)0},
+ {.d0 = 1 << 31}
+ };
+
+ N32_1x32路T *N32_1x32路zero = &N32_1x32路constant[0];
+ N32_1x32路T *N32_1x32路one = &N32_1x32路constant[1];
+ N32_1x32路T *N32_1x32路all_one_bit = &N32_1x32路constant[2];
+ N32_1x32路T *N32_1x32路msb = &N32_1x32路constant[3];
+ N32_1x32路T *N32_1x32路lsb = &N32_1x32路constant[1];
+
+ // the allocate an array of N32
+ N32_1x32路T *N32_1x32路allocate_array(Extent extent ,N32_1x32路Allocate_MemoryFault memory_fault){
+ N32_1x32路T *instance = malloc((extent + 1) * sizeof(N32_1x32路T) );
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N32_1x32路T *N32_1x32路allocate_array_zero(Extent extent ,N32_1x32路Allocate_MemoryFault memory_fault){
+ N32_1x32路T *instance = calloc( extent + 1 ,sizeof(N32_1x32路T) );
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N32_1x32路deallocate(N32_1x32路T *unencumbered){
+ free(unencumbered);
+ }
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N32_1x32路T{
+ Digit d0;
+ };
+
+ // temporary variables
+ // making these LOCAL rather than reserving one block in the library is thread safe
+ // allocating a block once is more efficient
+ // library code writes these, they are not on the interface
+
+ Local N32_1x32路T N32_1x32路t[4];
+
+
+ // allocation
+
+ extern N32_1x32路T *N32_1x32路allocate_array(Extent, N32_1x32路Allocate_MemoryFault);
+ extern N32_1x32路T *N32_1x32路allocate_array_zero(Extent, N32_1x32路Allocate_MemoryFault);
+ extern void N32_1x32路deallocate(N32_1x32路T *);
+
+ // so the user can access numbers in an array allocation
+ Local N32_1x32路T* N32_1x32路access(N32_1x32路T *array ,Extent index){
+ return &array[index];
+ }
+
+ Local void N32_1x32路from_uint32(N32_1x32路T *destination ,uint32_t value){
+ if(destination == NULL) return;
+ destination->d0 = value;
+ }
+
+ // copy, convenience copy
+
+ Local void N32_1x32路copy(N32_1x32路T *destination ,N32_1x32路T *source){
+ if(source == destination) return; // that was easy!
+ *destination = *source;
+ }
+
+ Local void N32_1x32路set_to_zero(N32_1x32路T *instance){
+ instance->d0 = 0;
+ }
+
+ Local void N32_1x32路set_to_one(N32_1x32路T *instance){
+ instance->d0 = 1;
+ }
+
+ // bit operations
+
+ Local void N32_1x32路bit_and(N32_1x32路T *result, N32_1x32路T *a, N32_1x32路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ // result can be one of the operands
+ Local void N32_1x32路bit_or(N32_1x32路T *result, N32_1x32路T *a, N32_1x32路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ // result can the same as the operand
+ Local void N32_1x32路bit_complement(N32_1x32路T *result, N32_1x32路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ // result can the same as the operand
+ Local void N32_1x32路bit_twos_complement(N32_1x32路T *result ,N32_1x32路T *a){
+ result->d0 = ~a->d0 + 1;
+ }
+
+ // test functions
+
+ Local N32_1x32路Order N32_1x32路compare(N32_1x32路T *a, N32_1x32路T *b){
+ if(a->d0 < b->d0) return N32_1x32路Order_lt;
+ if(a->d0 > b->d0) return N32_1x32路Order_gt;
+ return N32_1x32路Order_eq;
+ }
+
+ Local bool N32_1x32路lt(N32_1x32路T *a ,N32_1x32路T *b){
+ return a->d0 < b->d0;
+ }
+
+ Local bool N32_1x32路gt(N32_1x32路T *a ,N32_1x32路T *b){
+ return a->d0 > b->d0;
+ }
+
+ Local bool N32_1x32路eq(N32_1x32路T *a ,N32_1x32路T *b){
+ return a->d0 == b->d0;
+ }
+
+ Local bool N32_1x32路eq_zero(N32_1x32路T *a){
+ return a->d0 == 0;
+ }
+
+
+ // arithmetic operations
+
+ // For a large number of summands for the lower precision Natural implementations, for accumulate/add/sub, the 'overflow' operand could overflow and thus this routine will halt and return N32_1x32路Status路accumulator1_overflow
+ //
+ // When accumulator1 and accumulator0 point to the same location, the result is the accumulator1 value.
+ Local N32_1x32路Status N32_1x32路accumulate(N32_1x32路T *accumulator1 ,N32_1x32路T *accumulator0 ,...){
+
+ va_list args;
+ va_start(args ,accumulator0);
+ uint32_t sum = accumulator0->d0;
+ uint32_t carry = 0;
+ N32_1x32路T *current;
+
+ while( (current = va_arg(args ,N32_1x32路T *)) ){
+ sum += current->d0;
+ if(sum < current->d0){ // Accumulator1 into carry
+ (carry)++;
+ if(carry == 0){
+ va_end(args);
+ return N32_1x32路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ // wipes out prior value of accumulator1
+ accumulator1->d0 = carry;
+
+ return N32_1x32路Status路ok;
+ }
+
+ Local N32_1x32路Status N32_1x32路add(N32_1x32路T *sum ,N32_1x32路T *a ,N32_1x32路T *b){
+ uint64_t result = (uint64_t)a->d0 + (uint64_t)b->d0;
+ sum->d0 = (uint32_t)result;
+ return (result >> 32) ? N32_1x32路Status路carry : N32_1x32路Status路ok;
+ }
+
+ Local bool N32_1x32路increment(N32_1x32路T *a){
+ a->d0++;
+ return a->d0 == 0;
+ }
+
+ Local N32_1x32路Status N32_1x32路subtract(N32_1x32路T *difference ,N32_1x32路T *a ,N32_1x32路T *b){
+ uint64_t diff = (uint64_t) a->d0 - (uint64_t) b->d0;
+ difference->d0 = (uint32_t)diff;
+ return (diff > a->d0) ? N32_1x32路Status路borrow : N32_1x32路Status路ok;
+ }
+
+
+ Local N32_1x32路Status N32_1x32路multiply(N32_1x32路T *product1 ,N32_1x32路T *product0 ,N32_1x32路T *a ,N32_1x32路T *b){
+ uint64_t product = (uint64_t)a->d0 * (uint64_t)b->d0;
+ product0->d0 = (uint32_t)product;
+ product1->d0 = (uint32_t)(product >> 32);
+
+ if(product1->d0 == 0) return N32_1x32路Status路one_word_product;
+ return N32_1x32路Status路two_word_product;
+ }
+
+ Local N32_1x32路Status N32_1x32路divide(N32_1x32路T *remainder ,N32_1x32路T *quotient ,N32_1x32路T *a ,N32_1x32路T *b){
+ if(b->d0 == 0) return N32_1x32路Status路undefined_divide_by_zero;
+
+ quotient->d0 = a->d0 / b->d0;
+ remainder->d0 = a->d0 - (quotient->d0 * b->d0);
+
+ return N32_1x32路Status路ok;
+ }
+
+ Local N32_1x32路Status N32_1x32路modulus(N32_1x32路T *remainder ,N32_1x32路T *a ,N32_1x32路T *b){
+ if(b->d0 == 0) return N32_1x32路Status路undefined_modulus_zero;
+ uint32_t quotient = a->d0 / b->d0;
+ remainder->d0 = a->d0 - (quotient * b->d0);
+ return N32_1x32路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint32_t (*ShiftOp)(uint32_t, uint32_t);
+
+ Local uint32_t shift_left_op(uint32_t value, uint32_t amount){
+ return value << amount;
+ }
+
+ Local uint32_t shift_right_op(uint32_t value, uint32_t amount){
+ return value >> amount;
+ }
+
+ // modifies all three of its operands
+ // in the case of duplicate operands this is the order: first modifies operand, then fill, then spill,
+ Local N32_1x32路Status N32_1x32路shift
+ (
+ uint32_t shift_count
+ ,N32_1x32路T *spill
+ ,N32_1x32路T *operand
+ ,N32_1x32路T *fill
+ ,ShiftOp shift_op
+ ,ShiftOp complement_shift_op
+ ){
+
+ // If no result is needed, return immediately.
+ if(operand == NULL && spill == NULL) return N32_1x32路Status路ok;
+
+ // Treat NULL operand as zero.
+ if(operand == NULL){
+ operand = &N32_1x32路t[0];
+ N32_1x32路copy(operand, N32_1x32路zero);
+ }
+
+ // Shifting more than one word breaks our fill/spill model.
+ if(shift_count > 31) return N32_1x32路Status路gt_max_shift_count;
+
+ // The given operand is still required after it is modified, so we copy it.
+ N32_1x32路T *given_operand = &N32_1x32路t[1];
+ N32_1x32路copy(given_operand, operand);
+
+ // Perform the shift
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (32 - shift_count));
+ N32_1x32路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (32 - shift_count));
+ }
+
+ return N32_1x32路Status路ok;
+ }
+
+ // Define concrete shift functions using valid C function pointers
+ Local N32_1x32路Status
+ N32_1x32路shift_left(uint32_t shift_count, N32_1x32路T *spill, N32_1x32路T *operand, N32_1x32路T *fill){
+ return N32_1x32路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N32_1x32路Status
+ N32_1x32路shift_right(uint32_t shift_count, N32_1x32路T *spill, N32_1x32路T *operand, N32_1x32路T *fill){
+ return N32_1x32路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N32_1x32路Status
+ N32_1x32路arithmetic_shift_right(uint32_t shift_count, N32_1x32路T *operand, N32_1x32路T *spill){
+
+ // Guard against excessive shift counts
+ if(shift_count > 31) return N32_1x32路Status路gt_max_shift_count;
+
+ // A NULL operand is treated as zero
+ if(operand == NULL){
+ operand = &N32_1x32路t[0];
+ N32_1x32路copy(operand, N32_1x32路zero);
+ }
+
+ // Pick the fill value based on the sign bit
+ N32_1x32路T *fill = (operand->d0 & 0x80000000) ? N32_1x32路all_one_bit : N32_1x32路zero;
+
+ // Call shift_right with the appropriate fill
+ return N32_1x32路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N32_1x32路螞 N32_1x32路位 = {
+
+ .allocate_array = N32_1x32路allocate_array
+ ,.allocate_array_zero = N32_1x32路allocate_array_zero
+ ,.deallocate = N32_1x32路deallocate
+
+ ,.copy = N32_1x32路copy
+ ,.bit_and = N32_1x32路bit_and
+ ,.bit_or = N32_1x32路bit_or
+ ,.bit_complement = N32_1x32路bit_complement
+ ,.bit_twos_complement = N32_1x32路bit_twos_complement
+ ,.compare = N32_1x32路compare
+ ,.lt = N32_1x32路lt
+ ,.gt = N32_1x32路gt
+ ,.eq = N32_1x32路eq
+ ,.eq_zero = N32_1x32路eq_zero
+ ,.accumulate = N32_1x32路accumulate
+ ,.add = N32_1x32路add
+ ,.increment = N32_1x32路increment
+ ,.subtract = N32_1x32路subtract
+ ,.multiply = N32_1x32路multiply
+ ,.divide = N32_1x32路divide
+ ,.modulus = N32_1x32路modulus
+ ,.shift_left = N32_1x32路shift_left
+ ,.shift_right = N32_1x32路shift_right
+ ,.arithmetic_shift_right = N32_1x32路arithmetic_shift_right
+
+ ,.access = N32_1x32路access
+ ,.from_uint32 = N32_1x32路from_uint32
+ };
+
+ #endif
+
+#endif
--- /dev/null
+/*
+ N64 - a 64-bit native type
+
+ For binary operations: a op b -> c
+
+ Similar to N32, but now each Digit is 64 bits. Where a 128-bit
+ intermediate is necessary (e.g. multiplication), we handle it
+ manually using two 64-bit parts.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+*/
+
+#define N64路DEBUG
+
+#ifndef FACE
+#define N64路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N64路FACE
+#define N64路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint64_t Extent;
+ typedef uint64_t Digit;
+
+ typedef struct N64路T N64路T;
+
+ extern N64路T *N64路zero;
+ extern N64路T *N64路one;
+ extern N64路T *N64路all_one_bit;
+ extern N64路T *N64路lsb;
+ extern N64路T *N64路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum {
+ N64路Status路ok = 0
+ ,N64路Status路overflow = 1
+ ,N64路Status路accumulator1_overflow = 2
+ ,N64路Status路carry = 3
+ ,N64路Status路borrow = 4
+ ,N64路Status路undefined_divide_by_zero = 5
+ ,N64路Status路undefined_modulus_zero = 6
+ ,N64路Status路gt_max_shift_count = 7
+ ,N64路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
+ ,N64路Status路one_word_product = 9
+ ,N64路Status路two_word_product = 10
+ } N64路Status;
+
+ typedef enum {
+ N64路Order_lt = -1
+ ,N64路Order_eq = 0
+ ,N64路Order_gt = 1
+ } N64路Order;
+
+ typedef N64路T *(*N64路Allocate_MemoryFault)(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct {
+
+ N64路T *(*allocate_array_zero)(Extent, N64路Allocate_MemoryFault);
+ N64路T *(*allocate_array)(Extent, N64路Allocate_MemoryFault);
+ void (*deallocate)(N64路T*);
+
+ void (*copy)(N64路T*, N64路T*);
+ void (*bit_and)(N64路T*, N64路T*, N64路T*);
+ void (*bit_or)(N64路T*, N64路T*, N64路T*);
+ void (*bit_complement)(N64路T*, N64路T*);
+ void (*bit_twos_complement)(N64路T*, N64路T*);
+ N64路Order (*compare)(N64路T*, N64路T*);
+ bool (*lt)(N64路T*, N64路T*);
+ bool (*gt)(N64路T*, N64路T*);
+ bool (*eq)(N64路T*, N64路T*);
+ bool (*eq_zero)(N64路T*);
+ N64路Status (*accumulate)(N64路T *accumulator1, N64路T *accumulator0, ...);
+ N64路Status (*add)(N64路T*, N64路T*, N64路T*);
+ bool (*increment)(N64路T *a);
+ N64路Status (*subtract)(N64路T*, N64路T*, N64路T*);
+ N64路Status (*multiply)(N64路T*, N64路T*, N64路T*, N64路T*);
+ N64路Status (*divide)(N64路T*, N64路T*, N64路T*, N64路T*);
+ N64路Status (*modulus)(N64路T*, N64路T*, N64路T*);
+ N64路Status (*shift_left)(Extent, N64路T*, N64路T*, N64路T*);
+ N64路Status (*shift_right)(Extent, N64路T*, N64路T*, N64路T*);
+ N64路Status (*arithmetic_shift_right)(Extent, N64路T*, N64路T*);
+
+ N64路T* (*access)(N64路T*, Extent);
+ void (*from_uint64)(N64路T *destination, uint64_t value);
+
+ } N64路螞;
+
+ Local const N64路螞 N64路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N64路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N64路T {
+ Digit d0;
+ };
+
+ // For constants, we store them in an array for convenience
+ // 0, 1, all bits set (~0ULL), and MSB set (1ULL<<63)
+ N64路T N64路constant[4] = {
+ {.d0 = 0ULL},
+ {.d0 = 1ULL},
+ {.d0 = ~(uint64_t)0ULL},
+ {.d0 = 1ULL << 63}
+ };
+
+ N64路T *N64路zero = &N64路constant[0];
+ N64路T *N64路one = &N64路constant[1];
+ N64路T *N64路all_one_bit = &N64路constant[2];
+ N64路T *N64路msb = &N64路constant[3];
+ N64路T *N64路lsb = &N64路constant[1];
+
+ // allocate an array of N64
+ N64路T *N64路allocate_array(Extent extent, N64路Allocate_MemoryFault memory_fault){
+ N64路T *instance = malloc( (extent + 1) * sizeof(N64路T) );
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N64路T *N64路allocate_array_zero(Extent extent, N64路Allocate_MemoryFault memory_fault){
+ N64路T *instance = calloc(extent + 1, sizeof(N64路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N64路deallocate(N64路T *unencumbered){
+ free(unencumbered);
+ }
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N64路T {
+ Digit d0;
+ };
+
+ // local temporary variables
+ Local N64路T N64路t[4];
+
+ // allocation references
+ extern N64路T *N64路allocate_array(Extent, N64路Allocate_MemoryFault);
+ extern N64路T *N64路allocate_array_zero(Extent, N64路Allocate_MemoryFault);
+ extern void N64路deallocate(N64路T *);
+
+ // Access array
+ Local N64路T* N64路access(N64路T *array, Extent index){
+ return &array[index];
+ }
+
+ Local void N64路from_uint64(N64路T *destination, uint64_t value){
+ if(destination == NULL) return;
+ destination->d0 = value;
+ }
+
+ // copy
+ Local void N64路copy(N64路T *destination, N64路T *source){
+ if(source == destination) return;
+ *destination = *source;
+ }
+
+ // bit operations
+
+ Local void N64路bit_and(N64路T *result, N64路T *a, N64路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ Local void N64路bit_or(N64路T *result, N64路T *a, N64路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ Local void N64路bit_complement(N64路T *result, N64路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ Local void N64路bit_twos_complement(N64路T *result, N64路T *a){
+ result->d0 = ~a->d0 + 1ULL;
+ }
+
+ // compare & test functions
+
+ Local N64路Order N64路compare(N64路T *a, N64路T *b){
+ if(a->d0 < b->d0) return N64路Order_lt;
+ if(a->d0 > b->d0) return N64路Order_gt;
+ return N64路Order_eq;
+ }
+
+ Local bool N64路lt(N64路T *a, N64路T *b){
+ return (a->d0 < b->d0);
+ }
+
+ Local bool N64路gt(N64路T *a, N64路T *b){
+ return (a->d0 > b->d0);
+ }
+
+ Local bool N64路eq(N64路T *a, N64路T *b){
+ return (a->d0 == b->d0);
+ }
+
+ Local bool N64路eq_zero(N64路T *a){
+ return (a->d0 == 0ULL);
+ }
+
+ // arithmetic operations
+
+ // accumulate
+ Local N64路Status N64路accumulate(N64路T *accumulator1, N64路T *accumulator0, ...){
+ va_list args;
+ va_start(args, accumulator0);
+
+ uint64_t sum = accumulator0->d0;
+ uint64_t carry = 0;
+ N64路T *current;
+
+ while( (current = va_arg(args, N64路T*)) ){
+ uint64_t prior = sum;
+ sum += current->d0;
+ if(sum < prior){ // indicates carry
+ carry++;
+ // if carry overflowed a 64-bit, that's an accumulator1 overflow
+ if(carry == 0ULL){
+ va_end(args);
+ return N64路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ accumulator1->d0 = carry;
+ return N64路Status路ok;
+ }
+
+ // add
+ Local N64路Status N64路add(N64路T *sum, N64路T *a, N64路T *b){
+ __uint128_t result = ( __uint128_t )a->d0 + ( __uint128_t )b->d0;
+ // But to avoid using a GNU extension, we can do the simpler approach:
+ // Actually let's do it directly with 64-bit since we only need to detect carry out of 64 bits:
+ uint64_t temp = a->d0 + b->d0;
+ sum->d0 = temp;
+ if(temp < a->d0) return N64路Status路carry; // means we overflowed
+ return N64路Status路ok;
+ }
+
+ Local bool N64路increment(N64路T *a){
+ uint64_t old = a->d0;
+ a->d0++;
+ // if it wrapped around to 0, then it was 0xFFFFFFFFFFFFFFFF
+ return (a->d0 < old);
+ }
+
+ // subtract
+ Local N64路Status N64路subtract(N64路T *difference, N64路T *a, N64路T *b){
+ uint64_t tmpA = a->d0;
+ uint64_t tmpB = b->d0;
+ uint64_t diff = tmpA - tmpB;
+ difference->d0 = diff;
+ if(diff > tmpA) return N64路Status路borrow; // indicates we borrowed
+ return N64路Status路ok;
+ }
+
+ // multiply
+ // We'll do a 64x64->128 using two 64-bit accumulators
+ Local N64路Status N64路multiply(N64路T *product1, N64路T *product0, N64路T *a, N64路T *b){
+ uint64_t A = a->d0;
+ uint64_t B = b->d0;
+
+ // Break each operand into high & low 32 bits
+ uint64_t a_lo = (uint32_t)(A & 0xffffffffULL);
+ uint64_t a_hi = A >> 32;
+ uint64_t b_lo = (uint32_t)(B & 0xffffffffULL);
+ uint64_t b_hi = B >> 32;
+
+ // partial products
+ uint64_t low = a_lo * b_lo; // 64-bit
+ uint64_t cross = (a_lo * b_hi) + (a_hi * b_lo); // potentially up to 2 * 32 bits => 64 bits
+ uint64_t high = a_hi * b_hi; // up to 64 bits
+
+ // incorporate cross into low, high
+ // cross is effectively the middle bits, so shift cross by 32 and add to low
+ uint64_t cross_low = (cross & 0xffffffffULL) << 32; // lower part
+ uint64_t cross_high = cross >> 32; // upper part
+
+ // add cross_low to low, capture carry
+ uint64_t old_low = low;
+ low += cross_low;
+ if(low < old_low) cross_high++;
+
+ // final high
+ high += cross_high;
+
+ // store results
+ product0->d0 = low;
+ product1->d0 = high;
+
+ if(high == 0ULL) return N64路Status路one_word_product;
+ return N64路Status路two_word_product;
+ }
+
+ // divide
+ Local N64路Status N64路divide(N64路T *remainder, N64路T *quotient, N64路T *a, N64路T *b){
+ // we do not handle a > 64-bit, just the single 64-bit
+ if(b->d0 == 0ULL) return N64路Status路undefined_divide_by_zero;
+
+ uint64_t divd = a->d0; // dividend
+ uint64_t divs = b->d0; // divisor
+
+ quotient->d0 = divd / divs;
+ remainder->d0 = divd - (quotient->d0 * divs);
+
+ return N64路Status路ok;
+ }
+
+ // modulus
+ Local N64路Status N64路modulus(N64路T *remainder, N64路T *a, N64路T *b){
+ if(b->d0 == 0ULL) return N64路Status路undefined_modulus_zero;
+
+ uint64_t divd = a->d0;
+ uint64_t divs = b->d0;
+ uint64_t q = divd / divs;
+ remainder->d0 = divd - (q * divs);
+
+ return N64路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint64_t (*ShiftOp)(uint64_t, uint64_t);
+
+ Local uint64_t shift_left_op(uint64_t value, uint64_t amount){
+ return (value << amount);
+ }
+
+ Local uint64_t shift_right_op(uint64_t value, uint64_t amount){
+ return (value >> amount);
+ }
+
+ // modifies all three of its operands
+ // in the case of duplicate operands this is the order: first modifies operand, then fill, then spill
+ Local N64路Status N64路shift
+ (
+ uint64_t shift_count,
+ N64路T *spill,
+ N64路T *operand,
+ N64路T *fill,
+ ShiftOp shift_op,
+ ShiftOp complement_shift_op
+ ){
+ if(operand == NULL && spill == NULL) return N64路Status路ok;
+
+ // Treat NULL operand as zero
+ if(operand == NULL){
+ operand = &N64路t[0];
+ N64路copy(operand, N64路zero);
+ }
+
+ // Shifting more than 63 bits breaks fill/spill logic
+ if(shift_count > 63ULL) return N64路Status路gt_max_shift_count;
+
+ N64路T *given_operand = &N64路t[1];
+ N64路copy(given_operand, operand);
+
+ // Perform the shift
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (64ULL - shift_count));
+ N64路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (64ULL - shift_count));
+ }
+
+ return N64路Status路ok;
+ }
+
+ Local N64路Status N64路shift_left(uint64_t shift_count, N64路T *spill, N64路T *operand, N64路T *fill){
+ return N64路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N64路Status N64路shift_right(uint64_t shift_count, N64路T *spill, N64路T *operand, N64路T *fill){
+ return N64路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N64路Status N64路arithmetic_shift_right(uint64_t shift_count, N64路T *operand, N64路T *spill){
+ if(shift_count > 63ULL) return N64路Status路gt_max_shift_count;
+
+ // A NULL operand is treated as zero
+ if(operand == NULL){
+ operand = &N64路t[0];
+ N64路copy(operand, N64路zero);
+ }
+
+ // sign bit check
+ N64路T *fill = (operand->d0 & (1ULL << 63)) ? N64路all_one_bit : N64路zero;
+ return N64路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N64路螞 N64路位 = {
+ .allocate_array = N64路allocate_array
+ ,.allocate_array_zero = N64路allocate_array_zero
+ ,.deallocate = N64路deallocate
+
+ ,.copy = N64路copy
+ ,.bit_and = N64路bit_and
+ ,.bit_or = N64路bit_or
+ ,.bit_complement = N64路bit_complement
+ ,.bit_twos_complement = N64路bit_twos_complement
+ ,.compare = N64路compare
+ ,.lt = N64路lt
+ ,.gt = N64路gt
+ ,.eq = N64路eq
+ ,.eq_zero = N64路eq_zero
+ ,.accumulate = N64路accumulate
+ ,.add = N64路add
+ ,.increment = N64路increment
+ ,.subtract = N64路subtract
+ ,.multiply = N64路multiply
+ ,.divide = N64路divide
+ ,.modulus = N64路modulus
+ ,.shift_left = N64路shift_left
+ ,.shift_right = N64路shift_right
+ ,.arithmetic_shift_right = N64路arithmetic_shift_right
+
+ ,.access = N64路access
+ ,.from_uint64 = N64路from_uint64
+ };
+
+ #endif
+
+#endif
--- /dev/null
+/*
+ N8 - PN = refers to the use of processor native accumulator
+
+ For binary operations: a op b -> c
+
+ See the document on the proper use of the Natural types.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+*/
+
+#define N8路DEBUG
+
+#ifndef FACE
+#define N8路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N8路FACE
+#define N8路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint8_t Extent;
+ typedef uint8_t Digit;
+
+ typedef struct N8路T N8路T;
+
+ extern N8路T *N8路zero;
+ extern N8路T *N8路one;
+ extern N8路T *N8路all_one_bit;
+ extern N8路T *N8路lsb;
+ extern N8路T *N8路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum{
+ N8路Status路ok = 0
+ ,N8路Status路overflow = 1
+ ,N8路Status路accumulator1_overflow = 2
+ ,N8路Status路carry = 3
+ ,N8路Status路borrow = 4
+ ,N8路Status路undefined_divide_by_zero = 5
+ ,N8路Status路undefined_modulus_zero = 6
+ ,N8路Status路gt_max_shift_count = 7
+ ,N8路Status路spill_eq_operand = 8
+ ,N8路Status路one_word_product = 9
+ ,N8路Status路two_word_product = 10
+ } N8路Status;
+
+ typedef enum{
+ N8路Order_lt = -1
+ ,N8路Order_eq = 0
+ ,N8路Order_gt = 1
+ } N8路Order;
+
+ typedef N8路T *( *N8路Allocate_MemoryFault )(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct{
+
+ N8路T *(*allocate_array_zero)(Extent, N8路Allocate_MemoryFault);
+ N8路T *(*allocate_array)(Extent, N8路Allocate_MemoryFault);
+ void (*deallocate)(N8路T*);
+
+ void (*copy)(N8路T*, N8路T*);
+ void (*bit_and)(N8路T*, N8路T*, N8路T*);
+ void (*bit_or)(N8路T*, N8路T*, N8路T*);
+ void (*bit_complement)(N8路T*, N8路T*);
+ void (*bit_twos_complement)(N8路T*, N8路T*);
+ N8路Order (*compare)(N8路T*, N8路T*);
+ bool (*lt)(N8路T*, N8路T*);
+ bool (*gt)(N8路T*, N8路T*);
+ bool (*eq)(N8路T*, N8路T*);
+ bool (*eq_zero)(N8路T*);
+ N8路Status (*accumulate)(N8路T *accumulator1 ,N8路T *accumulator0 ,...);
+ N8路Status (*add)(N8路T*, N8路T*, N8路T*);
+ bool (*increment)(N8路T *a);
+ N8路Status (*subtract)(N8路T*, N8路T*, N8路T*);
+ N8路Status (*multiply)(N8路T*, N8路T*, N8路T*, N8路T*);
+ N8路Status (*divide)(N8路T*, N8路T*, N8路T*, N8路T*);
+ N8路Status (*modulus)(N8路T*, N8路T*, N8路T*);
+ N8路Status (*shift_left)(Extent, N8路T*, N8路T*, N8路T*);
+ N8路Status (*shift_right)(Extent, N8路T*, N8路T*, N8路T*);
+ N8路Status (*arithmetic_shift_right)(Extent, N8路T*, N8路T*);
+
+ N8路T* (*access)(N8路T*, Extent);
+ void (*from_uint32)(N8路T *destination ,uint32_t value);
+ } N8路螞;
+
+ Local const N8路螞 N8路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N8路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N8路T{
+ Digit d0;
+ };
+
+ N8路T N8路constant[4] = {
+ {.d0 = 0},
+ {.d0 = 1},
+ {.d0 = ~(uint8_t)0},
+ {.d0 = 1 << 7}
+ };
+
+ N8路T *N8路zero = &N8路constant[0];
+ N8路T *N8路one = &N8路constant[1];
+ N8路T *N8路all_one_bit = &N8路constant[2];
+ N8路T *N8路msb = &N8路constant[3];
+ N8路T *N8路lsb = &N8路constant[1];
+
+ // the allocate an array of N8
+ N8路T *N8路allocate_array(Extent extent ,N8路Allocate_MemoryFault memory_fault){
+ N8路T *instance = malloc((extent + 1) * sizeof(N8路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N8路T *N8路allocate_array_zero(Extent extent ,N8路Allocate_MemoryFault memory_fault){
+ N8路T *instance = calloc(extent + 1, sizeof(N8路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N8路deallocate(N8路T *unencumbered){
+ free(unencumbered);
+ }
+
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N8路T{
+ Digit d0;
+ };
+
+ // temporary variables
+ Local N8路T N8路t[4];
+
+ // allocation
+
+ extern N8路T *N8路allocate_array(Extent, N8路Allocate_MemoryFault);
+ extern N8路T *N8路allocate_array_zero(Extent, N8路Allocate_MemoryFault);
+ extern void N8路deallocate(N8路T *);
+
+ // so the user can access numbers in an array allocation
+ Local N8路T* N8路access(N8路T *array ,Extent index){
+ return &array[index];
+ }
+
+ Local void N8路from_uint32(N8路T *destination ,uint32_t value){
+ if(destination == NULL) return;
+ destination->d0 = (uint8_t)(value & 0xFF);
+ }
+
+ // copy, convenience copy
+
+ Local void N8路copy(N8路T *destination ,N8路T *source){
+ if(source == destination) return;
+ *destination = *source;
+ }
+
+ Local void N8路set_to_zero(N8路T *instance){
+ instance->d0 = 0;
+ }
+
+ Local void N8路set_to_one(N8路T *instance){
+ instance->d0 = 1;
+ }
+
+ // bit operations
+
+ Local void N8路bit_and(N8路T *result, N8路T *a, N8路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ Local void N8路bit_or(N8路T *result, N8路T *a, N8路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ Local void N8路bit_complement(N8路T *result, N8路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ Local void N8路bit_twos_complement(N8路T *result ,N8路T *a){
+ result->d0 = (uint8_t)(~a->d0 + 1);
+ }
+
+ // test functions
+
+ Local N8路Order N8路compare(N8路T *a, N8路T *b){
+ if(a->d0 < b->d0) return N8路Order_lt;
+ if(a->d0 > b->d0) return N8路Order_gt;
+ return N8路Order_eq;
+ }
+
+ Local bool N8路lt(N8路T *a ,N8路T *b){
+ return a->d0 < b->d0;
+ }
+
+ Local bool N8路gt(N8路T *a ,N8路T *b){
+ return a->d0 > b->d0;
+ }
+
+ Local bool N8路eq(N8路T *a ,N8路T *b){
+ return a->d0 == b->d0;
+ }
+
+ Local bool N8路eq_zero(N8路T *a){
+ return a->d0 == 0;
+ }
+
+ // arithmetic operations
+
+ Local N8路Status N8路accumulate(N8路T *accumulator1 ,N8路T *accumulator0 ,...){
+
+ va_list args;
+ va_start(args ,accumulator0);
+ uint32_t sum = accumulator0->d0;
+ uint32_t carry = 0;
+ N8路T *current;
+
+ while( (current = va_arg(args ,N8路T*)) ){
+ sum += current->d0;
+ if(sum < current->d0){
+ (carry)++;
+ if(carry == 0){
+ va_end(args);
+ return N8路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ accumulator1->d0 = (uint8_t)carry;
+ return N8路Status路ok;
+ }
+
+ Local N8路Status N8路add(N8路T *sum ,N8路T *a ,N8路T *b){
+ uint32_t result = (uint32_t)a->d0 + (uint32_t)b->d0;
+ sum->d0 = (uint8_t)(result & 0xFF);
+ return (result >> 8) ? N8路Status路carry : N8路Status路ok;
+ }
+
+ Local bool N8路increment(N8路T *a){
+ a->d0++;
+ return (a->d0 == 0);
+ }
+
+ Local N8路Status N8路subtract(N8路T *difference ,N8路T *a ,N8路T *b){
+ uint32_t diff = (uint32_t)a->d0 - (uint32_t)b->d0;
+ difference->d0 = (uint8_t)(diff & 0xFF);
+ return (diff > a->d0) ? N8路Status路borrow : N8路Status路ok;
+ }
+
+ Local N8路Status N8路multiply(N8路T *product1 ,N8路T *product0 ,N8路T *a ,N8路T *b){
+ uint32_t product = (uint32_t)a->d0 * (uint32_t)b->d0;
+ product0->d0 = (uint8_t)(product & 0xFF);
+ product1->d0 = (uint8_t)((product >> 8) & 0xFF);
+
+ if(product1->d0 == 0) return N8路Status路one_word_product;
+ return N8路Status路two_word_product;
+ }
+
+ Local N8路Status N8路divide(N8路T *remainder ,N8路T *quotient ,N8路T *a ,N8路T *b){
+ if(b->d0 == 0) return N8路Status路undefined_divide_by_zero;
+
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ quotient->d0 = (uint8_t)(dividend / divisor);
+ remainder->d0 = (uint8_t)(dividend - (quotient->d0 * divisor));
+
+ return N8路Status路ok;
+ }
+
+ Local N8路Status N8路modulus(N8路T *remainder ,N8路T *a ,N8路T *b){
+ if(b->d0 == 0) return N8路Status路undefined_modulus_zero;
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ uint32_t q = dividend / divisor;
+ remainder->d0 = (uint8_t)(dividend - (q * divisor));
+ return N8路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint8_t (*ShiftOp)(uint8_t, uint8_t);
+
+ Local uint8_t shift_left_op(uint8_t value, uint8_t amount){
+ return (uint8_t)(value << amount);
+ }
+
+ Local uint8_t shift_right_op(uint8_t value, uint8_t amount){
+ return (uint8_t)(value >> amount);
+ }
+
+ Local N8路Status N8路shift
+ (
+ uint8_t shift_count
+ ,N8路T *spill
+ ,N8路T *operand
+ ,N8路T *fill
+ ,ShiftOp shift_op
+ ,ShiftOp complement_shift_op
+ ){
+
+ if(operand == NULL && spill == NULL) return N8路Status路ok;
+
+ if(operand == NULL){
+ operand = &N8路t[0];
+ N8路copy(operand, N8路zero);
+ }
+
+ if(shift_count > 7) return N8路Status路gt_max_shift_count;
+
+ N8路T *given_operand = &N8路t[1];
+ N8路copy(given_operand, operand);
+
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (8 - shift_count));
+ N8路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (8 - shift_count));
+ }
+
+ return N8路Status路ok;
+ }
+
+ Local N8路Status
+ N8路shift_left(uint8_t shift_count, N8路T *spill, N8路T *operand, N8路T *fill){
+ return N8路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N8路Status
+ N8路shift_right(uint8_t shift_count, N8路T *spill, N8路T *operand, N8路T *fill){
+ return N8路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N8路Status
+ N8路arithmetic_shift_right(uint8_t shift_count, N8路T *operand, N8路T *spill){
+
+ if(shift_count > 7) return N8路Status路gt_max_shift_count;
+
+ if(operand == NULL){
+ operand = &N8路t[0];
+ N8路copy(operand, N8路zero);
+ }
+
+ N8路T *fill = (operand->d0 & 0x80) ? N8路all_one_bit : N8路zero;
+ return N8路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N8路螞 N8路位 = {
+
+ .allocate_array = N8路allocate_array
+ ,.allocate_array_zero = N8路allocate_array_zero
+ ,.deallocate = N8路deallocate
+
+ ,.copy = N8路copy
+ ,.bit_and = N8路bit_and
+ ,.bit_or = N8路bit_or
+ ,.bit_complement = N8路bit_complement
+ ,.bit_twos_complement = N8路bit_twos_complement
+ ,.compare = N8路compare
+ ,.lt = N8路lt
+ ,.gt = N8路gt
+ ,.eq = N8路eq
+ ,.eq_zero = N8路eq_zero
+ ,.accumulate = N8路accumulate
+ ,.add = N8路add
+ ,.increment = N8路increment
+ ,.subtract = N8路subtract
+ ,.multiply = N8路multiply
+ ,.divide = N8路divide
+ ,.modulus = N8路modulus
+ ,.shift_left = N8路shift_left
+ ,.shift_right = N8路shift_right
+ ,.arithmetic_shift_right = N8路arithmetic_shift_right
+
+ ,.access = N8路access
+ ,.from_uint32 = N8路from_uint32
+ };
+
+ #endif
+
+#endif
--- /dev/null
+/*
+ N16 - a processor native type
+
+ For binary operations: a op b -> c
+
+ See the document on the proper use of the Natural types.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+*/
+
+#define N16路DEBUG
+
+#ifndef FACE
+#define N16路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N16路FACE
+#define N16路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint16_t Extent;
+ typedef uint16_t Digit;
+
+ typedef struct N16路T N16路T;
+
+ extern N16路T *N16路zero;
+ extern N16路T *N16路one;
+ extern N16路T *N16路all_one_bit;
+ extern N16路T *N16路lsb;
+ extern N16路T *N16路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum{
+ N16路Status路ok = 0
+ ,N16路Status路overflow = 1
+ ,N16路Status路accumulator1_overflow = 2
+ ,N16路Status路carry = 3
+ ,N16路Status路borrow = 4
+ ,N16路Status路undefined_divide_by_zero = 5
+ ,N16路Status路undefined_modulus_zero = 6
+ ,N16路Status路gt_max_shift_count = 7
+ ,N16路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
+ ,N16路Status路one_word_product = 9
+ ,N16路Status路two_word_product = 10
+ } N16路Status;
+
+ typedef enum{
+ N16路Order_lt = -1
+ ,N16路Order_eq = 0
+ ,N16路Order_gt = 1
+ } N16路Order;
+
+ typedef N16路T *( *N16路Allocate_MemoryFault )(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct{
+
+ N16路T *(*allocate_array_zero)(Extent, N16路Allocate_MemoryFault);
+ N16路T *(*allocate_array)(Extent, N16路Allocate_MemoryFault);
+ void (*deallocate)(N16路T*);
+
+ void (*copy)(N16路T*, N16路T*);
+ void (*bit_and)(N16路T*, N16路T*, N16路T*);
+ void (*bit_or)(N16路T*, N16路T*, N16路T*);
+ void (*bit_complement)(N16路T*, N16路T*);
+ void (*bit_twos_complement)(N16路T*, N16路T*);
+ N16路Order (*compare)(N16路T*, N16路T*);
+ bool (*lt)(N16路T*, N16路T*);
+ bool (*gt)(N16路T*, N16路T*);
+ bool (*eq)(N16路T*, N16路T*);
+ bool (*eq_zero)(N16路T*);
+ N16路Status (*accumulate)(N16路T *accumulator1 ,N16路T *accumulator0 ,...);
+ N16路Status (*add)(N16路T*, N16路T*, N16路T*);
+ bool (*increment)(N16路T *a);
+ N16路Status (*subtract)(N16路T*, N16路T*, N16路T*);
+ N16路Status (*multiply)(N16路T*, N16路T*, N16路T*, N16路T*);
+ N16路Status (*divide)(N16路T*, N16路T*, N16路T*, N16路T*);
+ N16路Status (*modulus)(N16路T*, N16路T*, N16路T*);
+ N16路Status (*shift_left)(Extent, N16路T*, N16路T*, N16路T*);
+ N16路Status (*shift_right)(Extent, N16路T*, N16路T*, N16路T*);
+ N16路Status (*arithmetic_shift_right)(Extent, N16路T*, N16路T*);
+
+ N16路T* (*access)(N16路T*, Extent);
+ void (*from_uint32)(N16路T *destination ,uint32_t value);
+ } N16路螞;
+
+ Local const N16路螞 N16路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N16路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N16路T{
+ Digit d0;
+ };
+
+ N16路T N16路constant[4] = {
+ {.d0 = 0},
+ {.d0 = 1},
+ {.d0 = ~(uint16_t)0},
+ {.d0 = 1 << 15}
+ };
+
+ N16路T *N16路zero = &N16路constant[0];
+ N16路T *N16路one = &N16路constant[1];
+ N16路T *N16路all_one_bit = &N16路constant[2];
+ N16路T *N16路msb = &N16路constant[3];
+ N16路T *N16路lsb = &N16路constant[1];
+
+ // the allocate an array of N16
+ N16路T *N16路allocate_array(Extent extent ,N16路Allocate_MemoryFault memory_fault){
+ N16路T *instance = malloc((extent + 1) * sizeof(N16路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N16路T *N16路allocate_array_zero(Extent extent ,N16路Allocate_MemoryFault memory_fault){
+ N16路T *instance = calloc(extent + 1, sizeof(N16路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N16路deallocate(N16路T *unencumbered){
+ free(unencumbered);
+ }
+
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N16路T{
+ Digit d0;
+ };
+
+ // temporary variables
+ Local N16路T N16路t[4];
+
+ // allocation
+
+ extern N16路T *N16路allocate_array(Extent, N16路Allocate_MemoryFault);
+ extern N16路T *N16路allocate_array_zero(Extent, N16路Allocate_MemoryFault);
+ extern void N16路deallocate(N16路T *);
+
+ // so the user can access numbers in an array allocation
+ Local N16路T* N16路access(N16路T *array ,Extent index){
+ return &array[index];
+ }
+
+ Local void N16路from_uint32(N16路T *destination ,uint32_t value){
+ if(destination == NULL) return;
+ destination->d0 = (uint16_t)(value & 0xFFFF);
+ }
+
+ // copy, convenience copy
+
+ Local void N16路copy(N16路T *destination ,N16路T *source){
+ if(source == destination) return;
+ *destination = *source;
+ }
+
+ Local void N16路set_to_zero(N16路T *instance){
+ instance->d0 = 0;
+ }
+
+ Local void N16路set_to_one(N16路T *instance){
+ instance->d0 = 1;
+ }
+
+ // bit operations
+
+ Local void N16路bit_and(N16路T *result, N16路T *a, N16路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ Local void N16路bit_or(N16路T *result, N16路T *a, N16路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ Local void N16路bit_complement(N16路T *result, N16路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ Local void N16路bit_twos_complement(N16路T *result ,N16路T *a){
+ result->d0 = (uint16_t)(~a->d0 + 1);
+ }
+
+ // test functions
+
+ Local N16路Order N16路compare(N16路T *a, N16路T *b){
+ if(a->d0 < b->d0) return N16路Order_lt;
+ if(a->d0 > b->d0) return N16路Order_gt;
+ return N16路Order_eq;
+ }
+
+ Local bool N16路lt(N16路T *a ,N16路T *b){
+ return a->d0 < b->d0;
+ }
+
+ Local bool N16路gt(N16路T *a ,N16路T *b){
+ return a->d0 > b->d0;
+ }
+
+ Local bool N16路eq(N16路T *a ,N16路T *b){
+ return a->d0 == b->d0;
+ }
+
+ Local bool N16路eq_zero(N16路T *a){
+ return a->d0 == 0;
+ }
+
+ // arithmetic operations
+
+ Local N16路Status N16路accumulate(N16路T *accumulator1 ,N16路T *accumulator0 ,...){
+
+ va_list args;
+ va_start(args ,accumulator0);
+ uint32_t sum = accumulator0->d0;
+ uint32_t carry = 0;
+ N16路T *current;
+
+ while( (current = va_arg(args ,N16路T*)) ){
+ sum += current->d0;
+ if(sum < current->d0){
+ (carry)++;
+ if(carry == 0){
+ va_end(args);
+ return N16路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ accumulator1->d0 = (uint16_t)carry;
+ return N16路Status路ok;
+ }
+
+ Local N16路Status N16路add(N16路T *sum ,N16路T *a ,N16路T *b){
+ uint32_t result = (uint32_t)a->d0 + (uint32_t)b->d0;
+ sum->d0 = (uint16_t)(result & 0xFFFF);
+ return (result >> 16) ? N16路Status路carry : N16路Status路ok;
+ }
+
+ Local bool N16路increment(N16路T *a){
+ a->d0++;
+ return (a->d0 == 0);
+ }
+
+ Local N16路Status N16路subtract(N16路T *difference ,N16路T *a ,N16路T *b){
+ uint32_t diff = (uint32_t)a->d0 - (uint32_t)b->d0;
+ difference->d0 = (uint16_t)(diff & 0xFFFF);
+ return (diff > a->d0) ? N16路Status路borrow : N16路Status路ok;
+ }
+
+ Local N16路Status N16路multiply(N16路T *product1 ,N16路T *product0 ,N16路T *a ,N16路T *b){
+ uint32_t product = (uint32_t)a->d0 * (uint32_t)b->d0;
+ product0->d0 = (uint16_t)(product & 0xFFFF);
+ product1->d0 = (uint16_t)((product >> 16) & 0xFFFF);
+
+ if(product1->d0 == 0) return N16路Status路one_word_product;
+ return N16路Status路two_word_product;
+ }
+
+ Local N16路Status N16路divide(N16路T *remainder ,N16路T *quotient ,N16路T *a ,N16路T *b){
+ if(b->d0 == 0) return N16路Status路undefined_divide_by_zero;
+
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ quotient->d0 = (uint16_t)(dividend / divisor);
+ remainder->d0 = (uint16_t)(dividend - (quotient->d0 * divisor));
+
+ return N16路Status路ok;
+ }
+
+ Local N16路Status N16路modulus(N16路T *remainder ,N16路T *a ,N16路T *b){
+ if(b->d0 == 0) return N16路Status路undefined_modulus_zero;
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ uint32_t q = dividend / divisor;
+ remainder->d0 = (uint16_t)(dividend - (q * divisor));
+ return N16路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint16_t (*ShiftOp)(uint16_t, uint16_t);
+
+ Local uint16_t shift_left_op(uint16_t value, uint16_t amount){
+ return value << amount;
+ }
+
+ Local uint16_t shift_right_op(uint16_t value, uint16_t amount){
+ return (uint16_t)(value >> amount);
+ }
+
+ Local N16路Status N16路shift
+ (
+ uint16_t shift_count
+ ,N16路T *spill
+ ,N16路T *operand
+ ,N16路T *fill
+ ,ShiftOp shift_op
+ ,ShiftOp complement_shift_op
+ ){
+
+ if(operand == NULL && spill == NULL) return N16路Status路ok;
+
+ if(operand == NULL){
+ operand = &N16路t[0];
+ N16路copy(operand, N16路zero);
+ }
+
+ if(shift_count > 15) return N16路Status路gt_max_shift_count;
+
+ N16路T *given_operand = &N16路t[1];
+ N16路copy(given_operand, operand);
+
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (16 - shift_count));
+ N16路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (16 - shift_count));
+ }
+
+ return N16路Status路ok;
+ }
+
+ Local N16路Status
+ N16路shift_left(uint16_t shift_count, N16路T *spill, N16路T *operand, N16路T *fill){
+ return N16路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N16路Status
+ N16路shift_right(uint16_t shift_count, N16路T *spill, N16路T *operand, N16路T *fill){
+ return N16路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N16路Status
+ N16路arithmetic_shift_right(uint16_t shift_count, N16路T *operand, N16路T *spill){
+
+ if(shift_count > 15) return N16路Status路gt_max_shift_count;
+
+ if(operand == NULL){
+ operand = &N16路t[0];
+ N16路copy(operand, N16路zero);
+ }
+
+ N16路T *fill = (operand->d0 & 0x8000) ? N16路all_one_bit : N16路zero;
+ return N16路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N16路螞 N16路位 = {
+
+ .allocate_array = N16路allocate_array
+ ,.allocate_array_zero = N16路allocate_array_zero
+ ,.deallocate = N16路deallocate
+
+ ,.copy = N16路copy
+ ,.bit_and = N16路bit_and
+ ,.bit_or = N16路bit_or
+ ,.bit_complement = N16路bit_complement
+ ,.bit_twos_complement = N16路bit_twos_complement
+ ,.compare = N16路compare
+ ,.lt = N16路lt
+ ,.gt = N16路gt
+ ,.eq = N16路eq
+ ,.eq_zero = N16路eq_zero
+ ,.accumulate = N16路accumulate
+ ,.add = N16路add
+ ,.increment = N16路increment
+ ,.subtract = N16路subtract
+ ,.multiply = N16路multiply
+ ,.divide = N16路divide
+ ,.modulus = N16路modulus
+ ,.shift_left = N16路shift_left
+ ,.shift_right = N16路shift_right
+ ,.arithmetic_shift_right = N16路arithmetic_shift_right
+
+ ,.access = N16路access
+ ,.from_uint32 = N16路from_uint32
+ };
+
+ #endif
+
+#endif
--- /dev/null
+/*
+ N32 - a processor native type
+
+ For binary operations: a op b -> c
+
+ See the document on the proper use of the Natural types.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+
+*/
+
+#define N32_1x32路DEBUG
+
+#ifndef FACE
+#define N32_1x32路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N32_1x32路FACE
+#define N32_1x32路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint32_t Extent;
+ typedef uint32_t Digit;
+
+ typedef struct N32_1x32路T N32_1x32路T;
+
+ extern N32_1x32路T *N32_1x32路zero;
+ extern N32_1x32路T *N32_1x32路one;
+ extern N32_1x32路T *N32_1x32路all_one_bit;
+ extern N32_1x32路T *N32_1x32路lsb;
+ extern N32_1x32路T *N32_1x32路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum{
+ N32_1x32路Status路ok = 0
+ ,N32_1x32路Status路overflow = 1
+ ,N32_1x32路Status路accumulator1_overflow = 2
+ ,N32_1x32路Status路carry = 3
+ ,N32_1x32路Status路borrow = 4
+ ,N32_1x32路Status路undefined_divide_by_zero = 5
+ ,N32_1x32路Status路undefined_modulus_zero = 6
+ ,N32_1x32路Status路gt_max_shift_count = 7
+ ,N32_1x32路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
+ ,N32_1x32路Status路one_word_product = 9
+ ,N32_1x32路Status路two_word_product = 10
+ } N32_1x32路Status;
+
+ typedef enum{
+ N32_1x32路Order_lt = -1
+ ,N32_1x32路Order_eq = 0
+ ,N32_1x32路Order_gt = 1
+ } N32_1x32路Order;
+
+ typedef N32_1x32路T *( *N32_1x32路Allocate_MemoryFault )(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct{
+
+ N32_1x32路T *(*allocate_array_zero)(Extent, N32_1x32路Allocate_MemoryFault);
+ N32_1x32路T *(*allocate_array)(Extent, N32_1x32路Allocate_MemoryFault);
+ void (*deallocate)(N32_1x32路T*);
+
+ void (*copy)(N32_1x32路T*, N32_1x32路T*);
+ void (*bit_and)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ void (*bit_or)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ void (*bit_complement)(N32_1x32路T*, N32_1x32路T*);
+ void (*bit_twos_complement)(N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Order (*compare)(N32_1x32路T*, N32_1x32路T*);
+ bool (*lt)(N32_1x32路T*, N32_1x32路T*);
+ bool (*gt)(N32_1x32路T*, N32_1x32路T*);
+ bool (*eq)(N32_1x32路T*, N32_1x32路T*);
+ bool (*eq_zero)(N32_1x32路T*);
+ N32_1x32路Status (*accumulate)(N32_1x32路T *accumulator1 ,N32_1x32路T *accumulator0 ,...);
+ N32_1x32路Status (*add)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ bool (*increment)(N32_1x32路T *a);
+ N32_1x32路Status (*subtract)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*multiply)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*divide)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*modulus)(N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*shift_left)(Extent, N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*shift_right)(Extent, N32_1x32路T*, N32_1x32路T*, N32_1x32路T*);
+ N32_1x32路Status (*arithmetic_shift_right)(Extent, N32_1x32路T*, N32_1x32路T*);
+
+ N32_1x32路T* (*access)(N32_1x32路T*, Extent);
+ void (*from_uint32)(N32_1x32路T *destination ,uint32_t value);
+ } N32_1x32路螞;
+
+ Local const N32_1x32路螞 N32_1x32路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N32_1x32路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N32_1x32路T{
+ Digit d0;
+ };
+
+ N32_1x32路T N32_1x32路constant[4] = {
+ {.d0 = 0},
+ {.d0 = 1},
+ {.d0 = ~(uint32_t)0},
+ {.d0 = 1 << 31}
+ };
+
+ N32_1x32路T *N32_1x32路zero = &N32_1x32路constant[0];
+ N32_1x32路T *N32_1x32路one = &N32_1x32路constant[1];
+ N32_1x32路T *N32_1x32路all_one_bit = &N32_1x32路constant[2];
+ N32_1x32路T *N32_1x32路msb = &N32_1x32路constant[3];
+ N32_1x32路T *N32_1x32路lsb = &N32_1x32路constant[1];
+
+ // the allocate an array of N32
+ N32_1x32路T *N32_1x32路allocate_array(Extent extent ,N32_1x32路Allocate_MemoryFault memory_fault){
+ N32_1x32路T *instance = malloc((extent + 1) * sizeof(N32_1x32路T) );
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N32_1x32路T *N32_1x32路allocate_array_zero(Extent extent ,N32_1x32路Allocate_MemoryFault memory_fault){
+ N32_1x32路T *instance = calloc( extent + 1 ,sizeof(N32_1x32路T) );
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N32_1x32路deallocate(N32_1x32路T *unencumbered){
+ free(unencumbered);
+ }
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N32_1x32路T{
+ Digit d0;
+ };
+
+ // temporary variables
+ // making these LOCAL rather than reserving one block in the library is thread safe
+ // allocating a block once is more efficient
+ // library code writes these, they are not on the interface
+
+ Local N32_1x32路T N32_1x32路t[4];
+
+
+ // allocation
+
+ extern N32_1x32路T *N32_1x32路allocate_array(Extent, N32_1x32路Allocate_MemoryFault);
+ extern N32_1x32路T *N32_1x32路allocate_array_zero(Extent, N32_1x32路Allocate_MemoryFault);
+ extern void N32_1x32路deallocate(N32_1x32路T *);
+
+ // so the user can access numbers in an array allocation
+ Local N32_1x32路T* N32_1x32路access(N32_1x32路T *array ,Extent index){
+ return &array[index];
+ }
+
+ Local void N32_1x32路from_uint32(N32_1x32路T *destination ,uint32_t value){
+ if(destination == NULL) return;
+ destination->d0 = value;
+ }
+
+ // copy, convenience copy
+
+ Local void N32_1x32路copy(N32_1x32路T *destination ,N32_1x32路T *source){
+ if(source == destination) return; // that was easy!
+ *destination = *source;
+ }
+
+ Local void N32_1x32路set_to_zero(N32_1x32路T *instance){
+ instance->d0 = 0;
+ }
+
+ Local void N32_1x32路set_to_one(N32_1x32路T *instance){
+ instance->d0 = 1;
+ }
+
+ // bit operations
+
+ Local void N32_1x32路bit_and(N32_1x32路T *result, N32_1x32路T *a, N32_1x32路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ // result can be one of the operands
+ Local void N32_1x32路bit_or(N32_1x32路T *result, N32_1x32路T *a, N32_1x32路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ // result can the same as the operand
+ Local void N32_1x32路bit_complement(N32_1x32路T *result, N32_1x32路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ // result can the same as the operand
+ Local void N32_1x32路bit_twos_complement(N32_1x32路T *result ,N32_1x32路T *a){
+ result->d0 = ~a->d0 + 1;
+ }
+
+ // test functions
+
+ Local N32_1x32路Order N32_1x32路compare(N32_1x32路T *a, N32_1x32路T *b){
+ if(a->d0 < b->d0) return N32_1x32路Order_lt;
+ if(a->d0 > b->d0) return N32_1x32路Order_gt;
+ return N32_1x32路Order_eq;
+ }
+
+ Local bool N32_1x32路lt(N32_1x32路T *a ,N32_1x32路T *b){
+ return a->d0 < b->d0;
+ }
+
+ Local bool N32_1x32路gt(N32_1x32路T *a ,N32_1x32路T *b){
+ return a->d0 > b->d0;
+ }
+
+ Local bool N32_1x32路eq(N32_1x32路T *a ,N32_1x32路T *b){
+ return a->d0 == b->d0;
+ }
+
+ Local bool N32_1x32路eq_zero(N32_1x32路T *a){
+ return a->d0 == 0;
+ }
+
+
+ // arithmetic operations
+
+ // For a large number of summands for the lower precision Natural implementations, for accumulate/add/sub, the 'overflow' operand could overflow and thus this routine will halt and return N32_1x32路Status路accumulator1_overflow
+ //
+ // When accumulator1 and accumulator0 point to the same location, the result is the accumulator1 value.
+ Local N32_1x32路Status N32_1x32路accumulate(N32_1x32路T *accumulator1 ,N32_1x32路T *accumulator0 ,...){
+
+ va_list args;
+ va_start(args ,accumulator0);
+ uint32_t sum = accumulator0->d0;
+ uint32_t carry = 0;
+ N32_1x32路T *current;
+
+ while( (current = va_arg(args ,N32_1x32路T *)) ){
+ sum += current->d0;
+ if(sum < current->d0){ // Accumulator1 into carry
+ (carry)++;
+ if(carry == 0){
+ va_end(args);
+ return N32_1x32路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ // wipes out prior value of accumulator1
+ accumulator1->d0 = carry;
+
+ return N32_1x32路Status路ok;
+ }
+
+ Local N32_1x32路Status N32_1x32路add(N32_1x32路T *sum ,N32_1x32路T *a ,N32_1x32路T *b){
+ uint64_t result = (uint64_t)a->d0 + (uint64_t)b->d0;
+ sum->d0 = (uint32_t)result;
+ return (result >> 32) ? N32_1x32路Status路carry : N32_1x32路Status路ok;
+ }
+
+ Local bool N32_1x32路increment(N32_1x32路T *a){
+ a->d0++;
+ return a->d0 == 0;
+ }
+
+ Local N32_1x32路Status N32_1x32路subtract(N32_1x32路T *difference ,N32_1x32路T *a ,N32_1x32路T *b){
+ uint64_t diff = (uint64_t) a->d0 - (uint64_t) b->d0;
+ difference->d0 = (uint32_t)diff;
+ return (diff > a->d0) ? N32_1x32路Status路borrow : N32_1x32路Status路ok;
+ }
+
+
+ Local N32_1x32路Status N32_1x32路multiply(N32_1x32路T *product1 ,N32_1x32路T *product0 ,N32_1x32路T *a ,N32_1x32路T *b){
+ uint64_t product = (uint64_t)a->d0 * (uint64_t)b->d0;
+ product0->d0 = (uint32_t)product;
+ product1->d0 = (uint32_t)(product >> 32);
+
+ if(product1->d0 == 0) return N32_1x32路Status路one_word_product;
+ return N32_1x32路Status路two_word_product;
+ }
+
+ Local N32_1x32路Status N32_1x32路divide(N32_1x32路T *remainder ,N32_1x32路T *quotient ,N32_1x32路T *a ,N32_1x32路T *b){
+ if(b->d0 == 0) return N32_1x32路Status路undefined_divide_by_zero;
+
+ quotient->d0 = a->d0 / b->d0;
+ remainder->d0 = a->d0 - (quotient->d0 * b->d0);
+
+ return N32_1x32路Status路ok;
+ }
+
+ Local N32_1x32路Status N32_1x32路modulus(N32_1x32路T *remainder ,N32_1x32路T *a ,N32_1x32路T *b){
+ if(b->d0 == 0) return N32_1x32路Status路undefined_modulus_zero;
+ uint32_t quotient = a->d0 / b->d0;
+ remainder->d0 = a->d0 - (quotient * b->d0);
+ return N32_1x32路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint32_t (*ShiftOp)(uint32_t, uint32_t);
+
+ Local uint32_t shift_left_op(uint32_t value, uint32_t amount){
+ return value << amount;
+ }
+
+ Local uint32_t shift_right_op(uint32_t value, uint32_t amount){
+ return value >> amount;
+ }
+
+ // modifies all three of its operands
+ // in the case of duplicate operands this is the order: first modifies operand, then fill, then spill,
+ Local N32_1x32路Status N32_1x32路shift
+ (
+ uint32_t shift_count
+ ,N32_1x32路T *spill
+ ,N32_1x32路T *operand
+ ,N32_1x32路T *fill
+ ,ShiftOp shift_op
+ ,ShiftOp complement_shift_op
+ ){
+
+ // If no result is needed, return immediately.
+ if(operand == NULL && spill == NULL) return N32_1x32路Status路ok;
+
+ // Treat NULL operand as zero.
+ if(operand == NULL){
+ operand = &N32_1x32路t[0];
+ N32_1x32路copy(operand, N32_1x32路zero);
+ }
+
+ // Shifting more than one word breaks our fill/spill model.
+ if(shift_count > 31) return N32_1x32路Status路gt_max_shift_count;
+
+ // The given operand is still required after it is modified, so we copy it.
+ N32_1x32路T *given_operand = &N32_1x32路t[1];
+ N32_1x32路copy(given_operand, operand);
+
+ // Perform the shift
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (32 - shift_count));
+ N32_1x32路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (32 - shift_count));
+ }
+
+ return N32_1x32路Status路ok;
+ }
+
+ // Define concrete shift functions using valid C function pointers
+ Local N32_1x32路Status
+ N32_1x32路shift_left(uint32_t shift_count, N32_1x32路T *spill, N32_1x32路T *operand, N32_1x32路T *fill){
+ return N32_1x32路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N32_1x32路Status
+ N32_1x32路shift_right(uint32_t shift_count, N32_1x32路T *spill, N32_1x32路T *operand, N32_1x32路T *fill){
+ return N32_1x32路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N32_1x32路Status
+ N32_1x32路arithmetic_shift_right(uint32_t shift_count, N32_1x32路T *operand, N32_1x32路T *spill){
+
+ // Guard against excessive shift counts
+ if(shift_count > 31) return N32_1x32路Status路gt_max_shift_count;
+
+ // A NULL operand is treated as zero
+ if(operand == NULL){
+ operand = &N32_1x32路t[0];
+ N32_1x32路copy(operand, N32_1x32路zero);
+ }
+
+ // Pick the fill value based on the sign bit
+ N32_1x32路T *fill = (operand->d0 & 0x80000000) ? N32_1x32路all_one_bit : N32_1x32路zero;
+
+ // Call shift_right with the appropriate fill
+ return N32_1x32路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N32_1x32路螞 N32_1x32路位 = {
+
+ .allocate_array = N32_1x32路allocate_array
+ ,.allocate_array_zero = N32_1x32路allocate_array_zero
+ ,.deallocate = N32_1x32路deallocate
+
+ ,.copy = N32_1x32路copy
+ ,.bit_and = N32_1x32路bit_and
+ ,.bit_or = N32_1x32路bit_or
+ ,.bit_complement = N32_1x32路bit_complement
+ ,.bit_twos_complement = N32_1x32路bit_twos_complement
+ ,.compare = N32_1x32路compare
+ ,.lt = N32_1x32路lt
+ ,.gt = N32_1x32路gt
+ ,.eq = N32_1x32路eq
+ ,.eq_zero = N32_1x32路eq_zero
+ ,.accumulate = N32_1x32路accumulate
+ ,.add = N32_1x32路add
+ ,.increment = N32_1x32路increment
+ ,.subtract = N32_1x32路subtract
+ ,.multiply = N32_1x32路multiply
+ ,.divide = N32_1x32路divide
+ ,.modulus = N32_1x32路modulus
+ ,.shift_left = N32_1x32路shift_left
+ ,.shift_right = N32_1x32路shift_right
+ ,.arithmetic_shift_right = N32_1x32路arithmetic_shift_right
+
+ ,.access = N32_1x32路access
+ ,.from_uint32 = N32_1x32路from_uint32
+ };
+
+ #endif
+
+#endif
--- /dev/null
+/*
+ N64 - a 64-bit native type
+
+ For binary operations: a op b -> c
+
+ Similar to N32, but now each Digit is 64 bits. Where a 128-bit
+ intermediate is necessary (e.g. multiplication), we handle it
+ manually using two 64-bit parts.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+*/
+
+#define N64路DEBUG
+
+#ifndef FACE
+#define N64路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N64路FACE
+#define N64路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint64_t Extent;
+ typedef uint64_t Digit;
+
+ typedef struct N64路T N64路T;
+
+ extern N64路T *N64路zero;
+ extern N64路T *N64路one;
+ extern N64路T *N64路all_one_bit;
+ extern N64路T *N64路lsb;
+ extern N64路T *N64路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum {
+ N64路Status路ok = 0
+ ,N64路Status路overflow = 1
+ ,N64路Status路accumulator1_overflow = 2
+ ,N64路Status路carry = 3
+ ,N64路Status路borrow = 4
+ ,N64路Status路undefined_divide_by_zero = 5
+ ,N64路Status路undefined_modulus_zero = 6
+ ,N64路Status路gt_max_shift_count = 7
+ ,N64路Status路spill_eq_operand = 8 // not currently signaled, result will be spill value
+ ,N64路Status路one_word_product = 9
+ ,N64路Status路two_word_product = 10
+ } N64路Status;
+
+ typedef enum {
+ N64路Order_lt = -1
+ ,N64路Order_eq = 0
+ ,N64路Order_gt = 1
+ } N64路Order;
+
+ typedef N64路T *(*N64路Allocate_MemoryFault)(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct {
+
+ N64路T *(*allocate_array_zero)(Extent, N64路Allocate_MemoryFault);
+ N64路T *(*allocate_array)(Extent, N64路Allocate_MemoryFault);
+ void (*deallocate)(N64路T*);
+
+ void (*copy)(N64路T*, N64路T*);
+ void (*bit_and)(N64路T*, N64路T*, N64路T*);
+ void (*bit_or)(N64路T*, N64路T*, N64路T*);
+ void (*bit_complement)(N64路T*, N64路T*);
+ void (*bit_twos_complement)(N64路T*, N64路T*);
+ N64路Order (*compare)(N64路T*, N64路T*);
+ bool (*lt)(N64路T*, N64路T*);
+ bool (*gt)(N64路T*, N64路T*);
+ bool (*eq)(N64路T*, N64路T*);
+ bool (*eq_zero)(N64路T*);
+ N64路Status (*accumulate)(N64路T *accumulator1, N64路T *accumulator0, ...);
+ N64路Status (*add)(N64路T*, N64路T*, N64路T*);
+ bool (*increment)(N64路T *a);
+ N64路Status (*subtract)(N64路T*, N64路T*, N64路T*);
+ N64路Status (*multiply)(N64路T*, N64路T*, N64路T*, N64路T*);
+ N64路Status (*divide)(N64路T*, N64路T*, N64路T*, N64路T*);
+ N64路Status (*modulus)(N64路T*, N64路T*, N64路T*);
+ N64路Status (*shift_left)(Extent, N64路T*, N64路T*, N64路T*);
+ N64路Status (*shift_right)(Extent, N64路T*, N64路T*, N64路T*);
+ N64路Status (*arithmetic_shift_right)(Extent, N64路T*, N64路T*);
+
+ N64路T* (*access)(N64路T*, Extent);
+ void (*from_uint64)(N64路T *destination, uint64_t value);
+
+ } N64路螞;
+
+ Local const N64路螞 N64路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N64路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N64路T {
+ Digit d0;
+ };
+
+ // For constants, we store them in an array for convenience
+ // 0, 1, all bits set (~0ULL), and MSB set (1ULL<<63)
+ N64路T N64路constant[4] = {
+ {.d0 = 0ULL},
+ {.d0 = 1ULL},
+ {.d0 = ~(uint64_t)0ULL},
+ {.d0 = 1ULL << 63}
+ };
+
+ N64路T *N64路zero = &N64路constant[0];
+ N64路T *N64路one = &N64路constant[1];
+ N64路T *N64路all_one_bit = &N64路constant[2];
+ N64路T *N64路msb = &N64路constant[3];
+ N64路T *N64路lsb = &N64路constant[1];
+
+ // allocate an array of N64
+ N64路T *N64路allocate_array(Extent extent, N64路Allocate_MemoryFault memory_fault){
+ N64路T *instance = malloc( (extent + 1) * sizeof(N64路T) );
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N64路T *N64路allocate_array_zero(Extent extent, N64路Allocate_MemoryFault memory_fault){
+ N64路T *instance = calloc(extent + 1, sizeof(N64路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N64路deallocate(N64路T *unencumbered){
+ free(unencumbered);
+ }
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N64路T {
+ Digit d0;
+ };
+
+ // local temporary variables
+ Local N64路T N64路t[4];
+
+ // allocation references
+ extern N64路T *N64路allocate_array(Extent, N64路Allocate_MemoryFault);
+ extern N64路T *N64路allocate_array_zero(Extent, N64路Allocate_MemoryFault);
+ extern void N64路deallocate(N64路T *);
+
+ // Access array
+ Local N64路T* N64路access(N64路T *array, Extent index){
+ return &array[index];
+ }
+
+ Local void N64路from_uint64(N64路T *destination, uint64_t value){
+ if(destination == NULL) return;
+ destination->d0 = value;
+ }
+
+ // copy
+ Local void N64路copy(N64路T *destination, N64路T *source){
+ if(source == destination) return;
+ *destination = *source;
+ }
+
+ // bit operations
+
+ Local void N64路bit_and(N64路T *result, N64路T *a, N64路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ Local void N64路bit_or(N64路T *result, N64路T *a, N64路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ Local void N64路bit_complement(N64路T *result, N64路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ Local void N64路bit_twos_complement(N64路T *result, N64路T *a){
+ result->d0 = ~a->d0 + 1ULL;
+ }
+
+ // compare & test functions
+
+ Local N64路Order N64路compare(N64路T *a, N64路T *b){
+ if(a->d0 < b->d0) return N64路Order_lt;
+ if(a->d0 > b->d0) return N64路Order_gt;
+ return N64路Order_eq;
+ }
+
+ Local bool N64路lt(N64路T *a, N64路T *b){
+ return (a->d0 < b->d0);
+ }
+
+ Local bool N64路gt(N64路T *a, N64路T *b){
+ return (a->d0 > b->d0);
+ }
+
+ Local bool N64路eq(N64路T *a, N64路T *b){
+ return (a->d0 == b->d0);
+ }
+
+ Local bool N64路eq_zero(N64路T *a){
+ return (a->d0 == 0ULL);
+ }
+
+ // arithmetic operations
+
+ // accumulate
+ Local N64路Status N64路accumulate(N64路T *accumulator1, N64路T *accumulator0, ...){
+ va_list args;
+ va_start(args, accumulator0);
+
+ uint64_t sum = accumulator0->d0;
+ uint64_t carry = 0;
+ N64路T *current;
+
+ while( (current = va_arg(args, N64路T*)) ){
+ uint64_t prior = sum;
+ sum += current->d0;
+ if(sum < prior){ // indicates carry
+ carry++;
+ // if carry overflowed a 64-bit, that's an accumulator1 overflow
+ if(carry == 0ULL){
+ va_end(args);
+ return N64路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ accumulator1->d0 = carry;
+ return N64路Status路ok;
+ }
+
+ // add
+ Local N64路Status N64路add(N64路T *sum, N64路T *a, N64路T *b){
+ __uint128_t result = ( __uint128_t )a->d0 + ( __uint128_t )b->d0;
+ // But to avoid using a GNU extension, we can do the simpler approach:
+ // Actually let's do it directly with 64-bit since we only need to detect carry out of 64 bits:
+ uint64_t temp = a->d0 + b->d0;
+ sum->d0 = temp;
+ if(temp < a->d0) return N64路Status路carry; // means we overflowed
+ return N64路Status路ok;
+ }
+
+ Local bool N64路increment(N64路T *a){
+ uint64_t old = a->d0;
+ a->d0++;
+ // if it wrapped around to 0, then it was 0xFFFFFFFFFFFFFFFF
+ return (a->d0 < old);
+ }
+
+ // subtract
+ Local N64路Status N64路subtract(N64路T *difference, N64路T *a, N64路T *b){
+ uint64_t tmpA = a->d0;
+ uint64_t tmpB = b->d0;
+ uint64_t diff = tmpA - tmpB;
+ difference->d0 = diff;
+ if(diff > tmpA) return N64路Status路borrow; // indicates we borrowed
+ return N64路Status路ok;
+ }
+
+ // multiply
+ // We'll do a 64x64->128 using two 64-bit accumulators
+ Local N64路Status N64路multiply(N64路T *product1, N64路T *product0, N64路T *a, N64路T *b){
+ uint64_t A = a->d0;
+ uint64_t B = b->d0;
+
+ // Break each operand into high & low 32 bits
+ uint64_t a_lo = (uint32_t)(A & 0xffffffffULL);
+ uint64_t a_hi = A >> 32;
+ uint64_t b_lo = (uint32_t)(B & 0xffffffffULL);
+ uint64_t b_hi = B >> 32;
+
+ // partial products
+ uint64_t low = a_lo * b_lo; // 64-bit
+ uint64_t cross = (a_lo * b_hi) + (a_hi * b_lo); // potentially up to 2 * 32 bits => 64 bits
+ uint64_t high = a_hi * b_hi; // up to 64 bits
+
+ // incorporate cross into low, high
+ // cross is effectively the middle bits, so shift cross by 32 and add to low
+ uint64_t cross_low = (cross & 0xffffffffULL) << 32; // lower part
+ uint64_t cross_high = cross >> 32; // upper part
+
+ // add cross_low to low, capture carry
+ uint64_t old_low = low;
+ low += cross_low;
+ if(low < old_low) cross_high++;
+
+ // final high
+ high += cross_high;
+
+ // store results
+ product0->d0 = low;
+ product1->d0 = high;
+
+ if(high == 0ULL) return N64路Status路one_word_product;
+ return N64路Status路two_word_product;
+ }
+
+ // divide
+ Local N64路Status N64路divide(N64路T *remainder, N64路T *quotient, N64路T *a, N64路T *b){
+ // we do not handle a > 64-bit, just the single 64-bit
+ if(b->d0 == 0ULL) return N64路Status路undefined_divide_by_zero;
+
+ uint64_t divd = a->d0; // dividend
+ uint64_t divs = b->d0; // divisor
+
+ quotient->d0 = divd / divs;
+ remainder->d0 = divd - (quotient->d0 * divs);
+
+ return N64路Status路ok;
+ }
+
+ // modulus
+ Local N64路Status N64路modulus(N64路T *remainder, N64路T *a, N64路T *b){
+ if(b->d0 == 0ULL) return N64路Status路undefined_modulus_zero;
+
+ uint64_t divd = a->d0;
+ uint64_t divs = b->d0;
+ uint64_t q = divd / divs;
+ remainder->d0 = divd - (q * divs);
+
+ return N64路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint64_t (*ShiftOp)(uint64_t, uint64_t);
+
+ Local uint64_t shift_left_op(uint64_t value, uint64_t amount){
+ return (value << amount);
+ }
+
+ Local uint64_t shift_right_op(uint64_t value, uint64_t amount){
+ return (value >> amount);
+ }
+
+ // modifies all three of its operands
+ // in the case of duplicate operands this is the order: first modifies operand, then fill, then spill
+ Local N64路Status N64路shift
+ (
+ uint64_t shift_count,
+ N64路T *spill,
+ N64路T *operand,
+ N64路T *fill,
+ ShiftOp shift_op,
+ ShiftOp complement_shift_op
+ ){
+ if(operand == NULL && spill == NULL) return N64路Status路ok;
+
+ // Treat NULL operand as zero
+ if(operand == NULL){
+ operand = &N64路t[0];
+ N64路copy(operand, N64路zero);
+ }
+
+ // Shifting more than 63 bits breaks fill/spill logic
+ if(shift_count > 63ULL) return N64路Status路gt_max_shift_count;
+
+ N64路T *given_operand = &N64路t[1];
+ N64路copy(given_operand, operand);
+
+ // Perform the shift
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (64ULL - shift_count));
+ N64路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (64ULL - shift_count));
+ }
+
+ return N64路Status路ok;
+ }
+
+ Local N64路Status N64路shift_left(uint64_t shift_count, N64路T *spill, N64路T *operand, N64路T *fill){
+ return N64路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N64路Status N64路shift_right(uint64_t shift_count, N64路T *spill, N64路T *operand, N64路T *fill){
+ return N64路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N64路Status N64路arithmetic_shift_right(uint64_t shift_count, N64路T *operand, N64路T *spill){
+ if(shift_count > 63ULL) return N64路Status路gt_max_shift_count;
+
+ // A NULL operand is treated as zero
+ if(operand == NULL){
+ operand = &N64路t[0];
+ N64路copy(operand, N64路zero);
+ }
+
+ // sign bit check
+ N64路T *fill = (operand->d0 & (1ULL << 63)) ? N64路all_one_bit : N64路zero;
+ return N64路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N64路螞 N64路位 = {
+ .allocate_array = N64路allocate_array
+ ,.allocate_array_zero = N64路allocate_array_zero
+ ,.deallocate = N64路deallocate
+
+ ,.copy = N64路copy
+ ,.bit_and = N64路bit_and
+ ,.bit_or = N64路bit_or
+ ,.bit_complement = N64路bit_complement
+ ,.bit_twos_complement = N64路bit_twos_complement
+ ,.compare = N64路compare
+ ,.lt = N64路lt
+ ,.gt = N64路gt
+ ,.eq = N64路eq
+ ,.eq_zero = N64路eq_zero
+ ,.accumulate = N64路accumulate
+ ,.add = N64路add
+ ,.increment = N64路increment
+ ,.subtract = N64路subtract
+ ,.multiply = N64路multiply
+ ,.divide = N64路divide
+ ,.modulus = N64路modulus
+ ,.shift_left = N64路shift_left
+ ,.shift_right = N64路shift_right
+ ,.arithmetic_shift_right = N64路arithmetic_shift_right
+
+ ,.access = N64路access
+ ,.from_uint64 = N64路from_uint64
+ };
+
+ #endif
+
+#endif
--- /dev/null
+/*
+ N8 - PN = refers to the use of processor native accumulator
+
+ For binary operations: a op b -> c
+
+ See the document on the proper use of the Natural types.
+
+ On the subject of multiple pointers indicating the same location in memory:
+
+ When a routine has multiple results, and one or more of the result location
+ pointers point to the same storage, the routine will either return an error
+ status, or have defined behavior.
+
+ When a routine has multiple operands, in any combination, those
+ pointers can point to the same location, and the routine will
+ function as advertised.
+
+ When an operand functions as both an input and a result, perhaps due
+ to a result pointer pointing to the same place as an operand
+ pointer, the routine will function as advertised. (Internally the
+ routine might make a temporary copy of the operand to accomplish
+ this.)
+*/
+
+#define N8路DEBUG
+
+#ifndef FACE
+#define N8路IMPLEMENTATION
+#define FACE
+#endif
+
+//--------------------------------------------------------------------------------
+// Interface
+
+#ifndef N8路FACE
+#define N8路FACE
+
+ #include <stdint.h>
+ #include <stdbool.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ //----------------------------------------
+ // Instance Data (Declaration Only)
+
+ typedef uint8_t Extent;
+ typedef uint8_t Digit;
+
+ typedef struct N8路T N8路T;
+
+ extern N8路T *N8路zero;
+ extern N8路T *N8路one;
+ extern N8路T *N8路all_one_bit;
+ extern N8路T *N8路lsb;
+ extern N8路T *N8路msb;
+
+ //----------------------------------------
+ // Return/Error Status and handlers
+
+ typedef enum{
+ N8路Status路ok = 0
+ ,N8路Status路overflow = 1
+ ,N8路Status路accumulator1_overflow = 2
+ ,N8路Status路carry = 3
+ ,N8路Status路borrow = 4
+ ,N8路Status路undefined_divide_by_zero = 5
+ ,N8路Status路undefined_modulus_zero = 6
+ ,N8路Status路gt_max_shift_count = 7
+ ,N8路Status路spill_eq_operand = 8
+ ,N8路Status路one_word_product = 9
+ ,N8路Status路two_word_product = 10
+ } N8路Status;
+
+ typedef enum{
+ N8路Order_lt = -1
+ ,N8路Order_eq = 0
+ ,N8路Order_gt = 1
+ } N8路Order;
+
+ typedef N8路T *( *N8路Allocate_MemoryFault )(Extent);
+
+ //----------------------------------------
+ // Interface
+
+ typedef struct{
+
+ N8路T *(*allocate_array_zero)(Extent, N8路Allocate_MemoryFault);
+ N8路T *(*allocate_array)(Extent, N8路Allocate_MemoryFault);
+ void (*deallocate)(N8路T*);
+
+ void (*copy)(N8路T*, N8路T*);
+ void (*bit_and)(N8路T*, N8路T*, N8路T*);
+ void (*bit_or)(N8路T*, N8路T*, N8路T*);
+ void (*bit_complement)(N8路T*, N8路T*);
+ void (*bit_twos_complement)(N8路T*, N8路T*);
+ N8路Order (*compare)(N8路T*, N8路T*);
+ bool (*lt)(N8路T*, N8路T*);
+ bool (*gt)(N8路T*, N8路T*);
+ bool (*eq)(N8路T*, N8路T*);
+ bool (*eq_zero)(N8路T*);
+ N8路Status (*accumulate)(N8路T *accumulator1 ,N8路T *accumulator0 ,...);
+ N8路Status (*add)(N8路T*, N8路T*, N8路T*);
+ bool (*increment)(N8路T *a);
+ N8路Status (*subtract)(N8路T*, N8路T*, N8路T*);
+ N8路Status (*multiply)(N8路T*, N8路T*, N8路T*, N8路T*);
+ N8路Status (*divide)(N8路T*, N8路T*, N8路T*, N8路T*);
+ N8路Status (*modulus)(N8路T*, N8路T*, N8路T*);
+ N8路Status (*shift_left)(Extent, N8路T*, N8路T*, N8路T*);
+ N8路Status (*shift_right)(Extent, N8路T*, N8路T*, N8路T*);
+ N8路Status (*arithmetic_shift_right)(Extent, N8路T*, N8路T*);
+
+ N8路T* (*access)(N8路T*, Extent);
+ void (*from_uint32)(N8路T *destination ,uint32_t value);
+ } N8路螞;
+
+ Local const N8路螞 N8路位; // initialized in the LOCAL section
+
+#endif
+
+//--------------------------------------------------------------------------------
+// Implementation
+
+#ifdef N8路IMPLEMENTATION
+
+ // this part goes into the library
+ #ifndef LOCAL
+
+ #include <stdarg.h>
+ #include <stdlib.h>
+
+ struct N8路T{
+ Digit d0;
+ };
+
+ N8路T N8路constant[4] = {
+ {.d0 = 0},
+ {.d0 = 1},
+ {.d0 = ~(uint8_t)0},
+ {.d0 = 1 << 7}
+ };
+
+ N8路T *N8路zero = &N8路constant[0];
+ N8路T *N8路one = &N8路constant[1];
+ N8路T *N8路all_one_bit = &N8路constant[2];
+ N8路T *N8路msb = &N8路constant[3];
+ N8路T *N8路lsb = &N8路constant[1];
+
+ // the allocate an array of N8
+ N8路T *N8路allocate_array(Extent extent ,N8路Allocate_MemoryFault memory_fault){
+ N8路T *instance = malloc((extent + 1) * sizeof(N8路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ N8路T *N8路allocate_array_zero(Extent extent ,N8路Allocate_MemoryFault memory_fault){
+ N8路T *instance = calloc(extent + 1, sizeof(N8路T));
+ if(!instance){
+ return memory_fault ? memory_fault(extent) : NULL;
+ }
+ return instance;
+ }
+
+ void N8路deallocate(N8路T *unencumbered){
+ free(unencumbered);
+ }
+
+
+ #endif
+
+ // This part is included after the library user's code
+ #ifdef LOCAL
+
+ // instance
+
+ struct N8路T{
+ Digit d0;
+ };
+
+ // temporary variables
+ Local N8路T N8路t[4];
+
+ // allocation
+
+ extern N8路T *N8路allocate_array(Extent, N8路Allocate_MemoryFault);
+ extern N8路T *N8路allocate_array_zero(Extent, N8路Allocate_MemoryFault);
+ extern void N8路deallocate(N8路T *);
+
+ // so the user can access numbers in an array allocation
+ Local N8路T* N8路access(N8路T *array ,Extent index){
+ return &array[index];
+ }
+
+ Local void N8路from_uint32(N8路T *destination ,uint32_t value){
+ if(destination == NULL) return;
+ destination->d0 = (uint8_t)(value & 0xFF);
+ }
+
+ // copy, convenience copy
+
+ Local void N8路copy(N8路T *destination ,N8路T *source){
+ if(source == destination) return;
+ *destination = *source;
+ }
+
+ Local void N8路set_to_zero(N8路T *instance){
+ instance->d0 = 0;
+ }
+
+ Local void N8路set_to_one(N8路T *instance){
+ instance->d0 = 1;
+ }
+
+ // bit operations
+
+ Local void N8路bit_and(N8路T *result, N8路T *a, N8路T *b){
+ result->d0 = a->d0 & b->d0;
+ }
+
+ Local void N8路bit_or(N8路T *result, N8路T *a, N8路T *b){
+ result->d0 = a->d0 | b->d0;
+ }
+
+ Local void N8路bit_complement(N8路T *result, N8路T *a){
+ result->d0 = ~a->d0;
+ }
+
+ Local void N8路bit_twos_complement(N8路T *result ,N8路T *a){
+ result->d0 = (uint8_t)(~a->d0 + 1);
+ }
+
+ // test functions
+
+ Local N8路Order N8路compare(N8路T *a, N8路T *b){
+ if(a->d0 < b->d0) return N8路Order_lt;
+ if(a->d0 > b->d0) return N8路Order_gt;
+ return N8路Order_eq;
+ }
+
+ Local bool N8路lt(N8路T *a ,N8路T *b){
+ return a->d0 < b->d0;
+ }
+
+ Local bool N8路gt(N8路T *a ,N8路T *b){
+ return a->d0 > b->d0;
+ }
+
+ Local bool N8路eq(N8路T *a ,N8路T *b){
+ return a->d0 == b->d0;
+ }
+
+ Local bool N8路eq_zero(N8路T *a){
+ return a->d0 == 0;
+ }
+
+ // arithmetic operations
+
+ Local N8路Status N8路accumulate(N8路T *accumulator1 ,N8路T *accumulator0 ,...){
+
+ va_list args;
+ va_start(args ,accumulator0);
+ uint32_t sum = accumulator0->d0;
+ uint32_t carry = 0;
+ N8路T *current;
+
+ while( (current = va_arg(args ,N8路T*)) ){
+ sum += current->d0;
+ if(sum < current->d0){
+ (carry)++;
+ if(carry == 0){
+ va_end(args);
+ return N8路Status路accumulator1_overflow;
+ }
+ }
+ }
+ va_end(args);
+
+ accumulator1->d0 = (uint8_t)carry;
+ return N8路Status路ok;
+ }
+
+ Local N8路Status N8路add(N8路T *sum ,N8路T *a ,N8路T *b){
+ uint32_t result = (uint32_t)a->d0 + (uint32_t)b->d0;
+ sum->d0 = (uint8_t)(result & 0xFF);
+ return (result >> 8) ? N8路Status路carry : N8路Status路ok;
+ }
+
+ Local bool N8路increment(N8路T *a){
+ a->d0++;
+ return (a->d0 == 0);
+ }
+
+ Local N8路Status N8路subtract(N8路T *difference ,N8路T *a ,N8路T *b){
+ uint32_t diff = (uint32_t)a->d0 - (uint32_t)b->d0;
+ difference->d0 = (uint8_t)(diff & 0xFF);
+ return (diff > a->d0) ? N8路Status路borrow : N8路Status路ok;
+ }
+
+ Local N8路Status N8路multiply(N8路T *product1 ,N8路T *product0 ,N8路T *a ,N8路T *b){
+ uint32_t product = (uint32_t)a->d0 * (uint32_t)b->d0;
+ product0->d0 = (uint8_t)(product & 0xFF);
+ product1->d0 = (uint8_t)((product >> 8) & 0xFF);
+
+ if(product1->d0 == 0) return N8路Status路one_word_product;
+ return N8路Status路two_word_product;
+ }
+
+ Local N8路Status N8路divide(N8路T *remainder ,N8路T *quotient ,N8路T *a ,N8路T *b){
+ if(b->d0 == 0) return N8路Status路undefined_divide_by_zero;
+
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ quotient->d0 = (uint8_t)(dividend / divisor);
+ remainder->d0 = (uint8_t)(dividend - (quotient->d0 * divisor));
+
+ return N8路Status路ok;
+ }
+
+ Local N8路Status N8路modulus(N8路T *remainder ,N8路T *a ,N8路T *b){
+ if(b->d0 == 0) return N8路Status路undefined_modulus_zero;
+ uint32_t dividend = a->d0;
+ uint32_t divisor = b->d0;
+ uint32_t q = dividend / divisor;
+ remainder->d0 = (uint8_t)(dividend - (q * divisor));
+ return N8路Status路ok;
+ }
+
+ // bit motion
+
+ typedef uint8_t (*ShiftOp)(uint8_t, uint8_t);
+
+ Local uint8_t shift_left_op(uint8_t value, uint8_t amount){
+ return (uint8_t)(value << amount);
+ }
+
+ Local uint8_t shift_right_op(uint8_t value, uint8_t amount){
+ return (uint8_t)(value >> amount);
+ }
+
+ Local N8路Status N8路shift
+ (
+ uint8_t shift_count
+ ,N8路T *spill
+ ,N8路T *operand
+ ,N8路T *fill
+ ,ShiftOp shift_op
+ ,ShiftOp complement_shift_op
+ ){
+
+ if(operand == NULL && spill == NULL) return N8路Status路ok;
+
+ if(operand == NULL){
+ operand = &N8路t[0];
+ N8路copy(operand, N8路zero);
+ }
+
+ if(shift_count > 7) return N8路Status路gt_max_shift_count;
+
+ N8路T *given_operand = &N8路t[1];
+ N8路copy(given_operand, operand);
+
+ operand->d0 = shift_op(given_operand->d0, shift_count);
+ if(fill != NULL){
+ fill->d0 = complement_shift_op(fill->d0, (8 - shift_count));
+ N8路bit_or(operand, operand, fill);
+ }
+ if(spill != NULL){
+ spill->d0 = shift_op(spill->d0, shift_count);
+ spill->d0 += complement_shift_op(given_operand->d0, (8 - shift_count));
+ }
+
+ return N8路Status路ok;
+ }
+
+ Local N8路Status
+ N8路shift_left(uint8_t shift_count, N8路T *spill, N8路T *operand, N8路T *fill){
+ return N8路shift(shift_count, spill, operand, fill, shift_left_op, shift_right_op);
+ }
+
+ Local N8路Status
+ N8路shift_right(uint8_t shift_count, N8路T *spill, N8路T *operand, N8路T *fill){
+ return N8路shift(shift_count, spill, operand, fill, shift_right_op, shift_left_op);
+ }
+
+ Local N8路Status
+ N8路arithmetic_shift_right(uint8_t shift_count, N8路T *operand, N8路T *spill){
+
+ if(shift_count > 7) return N8路Status路gt_max_shift_count;
+
+ if(operand == NULL){
+ operand = &N8路t[0];
+ N8路copy(operand, N8路zero);
+ }
+
+ N8路T *fill = (operand->d0 & 0x80) ? N8路all_one_bit : N8路zero;
+ return N8路shift_right(shift_count, spill, operand, fill);
+ }
+
+ Local const N8路螞 N8路位 = {
+
+ .allocate_array = N8路allocate_array
+ ,.allocate_array_zero = N8路allocate_array_zero
+ ,.deallocate = N8路deallocate
+
+ ,.copy = N8路copy
+ ,.bit_and = N8路bit_and
+ ,.bit_or = N8路bit_or
+ ,.bit_complement = N8路bit_complement
+ ,.bit_twos_complement = N8路bit_twos_complement
+ ,.compare = N8路compare
+ ,.lt = N8路lt
+ ,.gt = N8路gt
+ ,.eq = N8路eq
+ ,.eq_zero = N8路eq_zero
+ ,.accumulate = N8路accumulate
+ ,.add = N8路add
+ ,.increment = N8路increment
+ ,.subtract = N8路subtract
+ ,.multiply = N8路multiply
+ ,.divide = N8路divide
+ ,.modulus = N8路modulus
+ ,.shift_left = N8路shift_left
+ ,.shift_right = N8路shift_right
+ ,.arithmetic_shift_right = N8路arithmetic_shift_right
+
+ ,.access = N8路access
+ ,.from_uint32 = N8路from_uint32
+ };
+
+ #endif
+
+#endif
--- /dev/null
+#include <stdio.h>
+#include <stdbool.h>
+#include <signal.h>
+#include <setjmp.h>
+
+// Enable interface section
+#define FACE
+#include "N32.lib.c"
+#undef FACE
+
+// Jump buffer for signal handling
+static sigjmp_buf jump_buffer;
+
+// Signal handler for catching fatal errors
+void signal_handler(int signal){
+ siglongjmp(jump_buffer ,1); // Jump back to test_head on error
+}
+
+// Test function prototypes
+bool test_copy();
+bool test_bitwise_operations();
+bool test_comparisons();
+bool test_arithmetic();
+bool test_shifts();
+
+// Test array (null-terminated)
+typedef bool (*TestFunction)();
+typedef struct{
+ TestFunction function;
+ const char *name;
+}TestEntry;
+
+TestEntry test_list[] = {
+ {test_copy ,"test_copy"}
+ ,{test_bitwise_operations ,"test_bitwise_operations"}
+ ,{test_comparisons ,"test_comparisons"}
+ ,{test_arithmetic ,"test_arithmetic"}
+ ,{test_shifts ,"test_shifts"}
+ ,{NULL ,NULL} // Null termination
+};
+
+// The test runner
+int test_head(){
+ int pass_count = 0;
+ int fail_count = 0;
+
+ // Set up signal handlers
+ signal(SIGSEGV ,signal_handler); // Catch segmentation faults
+ signal(SIGFPE ,signal_handler); // Catch floating point errors
+ signal(SIGABRT ,signal_handler); // Catch abort() calls
+
+ for(TestEntry *entry = test_list; entry->function != NULL; entry++){
+ if( sigsetjmp(jump_buffer ,1) == 0 ){
+ // Run the test normally
+ if( !entry->function() ){
+ printf("Failed: %s\n" ,entry->name);
+ fail_count++;
+ }else{
+ pass_count++;
+ }
+ }else{
+ // If a signal was caught
+ printf("Failed due to signaling: %s\n" ,entry->name);
+ fail_count++;
+ }
+ }
+
+ printf("Tests passed: %d\n" ,pass_count);
+ printf("Tests failed: %d\n" ,fail_count);
+ return (fail_count == 0) ? 0 : 1;
+}
+
+// Main function
+int main(int argc ,char **argv){
+ return test_head();
+}
+
+//------------------------------------------------------------------------------
+// Test Implementations
+//------------------------------------------------------------------------------
+
+bool test_copy(){
+ // Allocate memory
+ N32路T *array = N32路位.allocate_array(2 ,NULL);
+ if( !array ) return false;
+
+ // Access elements via access function
+ N32路T *a = N32路位.access(array ,0);
+ N32路T *b = N32路位.access(array ,1);
+
+ // Assign value and copy
+ N32路位.from_uint32(a ,42);
+ N32路位.copy(b ,a);
+
+ bool success = ( N32路位.compare(b ,a) == N32路Order_eq );
+ N32路位.deallocate(array);
+ return success;
+}
+
+bool test_arithmetic(){
+ // Allocate memory
+ N32路T *array = N32路位.allocate_array(3 ,NULL);
+ if( !array ) return false;
+
+ N32路T *a = N32路位.access(array ,0);
+ N32路T *b = N32路位.access(array ,1);
+ N32路T *result = N32路位.access(array ,2);
+
+ N32路位.from_uint32(a ,20);
+ N32路位.from_uint32(b ,22);
+
+ if( N32路位.add(result ,a ,b) != N32路Status路ok ) return false;
+ if( N32路位.compare(result ,N32路位.access(array ,0)) != N32路Order_gt ) return false;
+
+ if( N32路位.subtract(result ,b ,a) != N32路Status路ok ) return false;
+ if( N32路位.compare(result ,N32路位.access(array ,0)) != N32路Order_lt ) return false;
+
+ N32路位.deallocate(array);
+ return true;
+}
+
+bool test_bitwise_operations(){
+ // Allocate memory
+ N32路T *array = N32路位.allocate_array(3, NULL);
+ if(!array) return false;
+
+ N32路T *a = N32路位.access(array, 0);
+ N32路T *b = N32路位.access(array, 1);
+ N32路T *result = N32路位.access(array, 2);
+
+ // a = 0x0F0F0F0F, b = 0xF0F0F0F0
+ N32路位.from_uint32(a, 0x0F0F0F0F);
+ N32路位.from_uint32(b, 0xF0F0F0F0);
+
+ // bit_and => expect 0x00000000
+ N32路位.bit_and(result, a, b);
+ N32路位.from_uint32(a, 0x00000000);
+ if(N32路位.compare(result, a) != N32路Order_eq){
+ N32路位.deallocate(array);
+ return false;
+ }
+
+ // Reset a to 0x0F0F0F0F for next tests
+ N32路位.from_uint32(a, 0x0F0F0F0F);
+
+ // bit_or => expect 0xFFFFFFFF
+ N32路位.bit_or(result, a, b);
+ N32路位.from_uint32(b, 0xFFFFFFFF);
+ if(N32路位.compare(result, b) != N32路Order_eq){
+ N32路位.deallocate(array);
+ return false;
+ }
+
+ // bit_complement(a=0x0F0F0F0F) => expect 0xF0F0F0F0
+ N32路位.from_uint32(a, 0x0F0F0F0F);
+ N32路位.bit_complement(result, a);
+ N32路位.from_uint32(b, 0xF0F0F0F0);
+ if(N32路位.compare(result, b) != N32路Order_eq){
+ N32路位.deallocate(array);
+ return false;
+ }
+
+ // bit_twos_complement(a=0x0F0F0F0F) => expect 0xF0F0F0F1
+ N32路位.from_uint32(a, 0x0F0F0F0F);
+ N32路位.bit_twos_complement(result, a);
+ N32路位.from_uint32(b, 0xF0F0F0F1);
+ if(N32路位.compare(result, b) != N32路Order_eq){
+ N32路位.deallocate(array);
+ return false;
+ }
+
+ N32路位.deallocate(array);
+ return true;
+}
+
+bool test_comparisons(){
+ // Allocate memory
+ N32路T *array = N32路位.allocate_array(3, NULL);
+ if(!array) return false;
+
+ N32路T *a = N32路位.access(array, 0);
+ N32路T *b = N32路位.access(array, 1);
+ N32路T *c = N32路位.access(array, 2);
+
+ // First set: a=0, b=42, c=42
+ N32路位.from_uint32(a, 0);
+ N32路位.from_uint32(b, 42);
+ N32路位.from_uint32(c, 42);
+
+ // eq_zero(a) => true
+ if(!N32路位.eq_zero(a)){
+ N32路位.deallocate(array);
+ return false;
+ }
+ // eq_zero(b) => false
+ if(N32路位.eq_zero(b)){
+ N32路位.deallocate(array);
+ return false;
+ }
+ // eq(b, c) => true
+ if(!N32路位.eq(b, c)){
+ N32路位.deallocate(array);
+ return false;
+ }
+ // eq(a, b) => false
+ if(N32路位.eq(a, b)){
+ N32路位.deallocate(array);
+ return false;
+ }
+ // compare(a, b) => N32路Order_lt
+ if(N32路位.compare(a, b) != N32路Order_lt){
+ N32路位.deallocate(array);
+ return false;
+ }
+ // compare(b, a) => N32路Order_gt
+ if(N32路位.compare(b, a) != N32路Order_gt){
+ N32路位.deallocate(array);
+ return false;
+ }
+ // compare(b, c) => N32路Order_eq
+ if(N32路位.compare(b, c) != N32路Order_eq){
+ N32路位.deallocate(array);
+ return false;
+ }
+ // lt(a, b) => true, gt(b, a) => true
+ if(!N32路位.lt(a, b) || !N32路位.gt(b, a)){
+ N32路位.deallocate(array);
+ return false;
+ }
+
+ // Second set: a=100, b=50
+ N32路位.from_uint32(a, 100);
+ N32路位.from_uint32(b, 50);
+ if(N32路位.compare(a, b) != N32路Order_gt){
+ N32路位.deallocate(array);
+ return false;
+ }
+ // eq_zero(a) => false
+ if(N32路位.eq_zero(a)){
+ N32路位.deallocate(array);
+ return false;
+ }
+ // eq_zero(b) => false
+ if(N32路位.eq_zero(b)){
+ N32路位.deallocate(array);
+ return false;
+ }
+
+ N32路位.deallocate(array);
+ return true;
+}
+
+bool test_shifts(){
+ // Allocate memory for operand, fill, spill
+ N32路T *array = N32路位.allocate_array(3, NULL);
+ if(!array) return false;
+
+ N32路T *operand = N32路位.access(array, 0);
+ N32路T *fill = N32路位.access(array, 1);
+ N32路T *spill = N32路位.access(array, 2);
+
+ // Subtest A: shift_left(4) with operand=1 => expect operand=16, fill=0, spill=0
+ N32路位.from_uint32(operand, 1);
+ N32路位.from_uint32(fill, 0);
+ N32路位.from_uint32(spill, 0);
+ if(N32路位.shift_left(4, spill, operand, fill) != N32路Status路ok){
+ N32路位.deallocate(array);
+ return false;
+ }
+ N32路T *temp = N32路位.allocate_array(1, NULL);
+ if(!temp){
+ N32路位.deallocate(array);
+ return false;
+ }
+ N32路位.from_uint32(temp, 16);
+ if(N32路位.compare(operand, temp) != N32路Order_eq){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+ if(N32路位.compare(fill, N32路zero) != N32路Order_eq){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+ if(N32路位.compare(spill, N32路zero) != N32路Order_eq){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+
+ // Subtest B: shift_left(1) with operand=0x80000000 => expect operand=0, spill=1
+ N32路位.from_uint32(operand, 0x80000000);
+ N32路位.from_uint32(fill, 0);
+ N32路位.from_uint32(spill, 0);
+ if(N32路位.shift_left(1, spill, operand, fill) != N32路Status路ok){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+ if(!N32路位.eq_zero(operand)){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+ N32路位.from_uint32(temp, 1);
+ if(N32路位.compare(spill, temp) != N32路Order_eq){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+
+ // Subtest C: shift_right(1) with operand=0x80000000 => expect operand=0x40000000, spill=0
+ N32路位.from_uint32(operand, 0x80000000);
+ N32路位.from_uint32(fill, 0);
+ N32路位.from_uint32(spill, 0);
+ if(N32路位.shift_right(1, spill, operand, fill) != N32路Status路ok){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+ N32路位.from_uint32(temp, 0x40000000);
+ if(N32路位.compare(operand, temp) != N32路Order_eq){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+ if(!N32路位.eq_zero(spill)){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+
+ // Subtest D: arithmetic_shift_right(1) with operand=0x80000000 => expect operand=0xC0000000, spill=0
+ N32路位.from_uint32(operand, 0x80000000);
+ N32路位.from_uint32(spill, 0);
+ if(N32路位.arithmetic_shift_right(1, operand, spill) != N32路Status路ok){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+ N32路位.from_uint32(temp, 0xC0000000);
+ if(N32路位.compare(operand, temp) != N32路Order_eq){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+ if(!N32路位.eq_zero(spill)){
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return false;
+ }
+
+ N32路位.deallocate(temp);
+ N32路位.deallocate(array);
+ return true;
+}
+
+
+
+// Include the local section of N32.lib.c for testing
+#define LOCAL
+#include "N32.lib.c"
+#undef LOCAL
--- /dev/null
+#include <stdio.h>
+#include <stdbool.h>
+#include <signal.h>
+#include <setjmp.h>
+
+// Enable interface section
+#define FACE
+#include "N32_1x32.lib.c"
+#undef FACE
+
+// Jump buffer for signal handling
+static sigjmp_buf jump_buffer;
+
+// Signal handler for catching fatal errors
+void signal_handler(int signal){
+ siglongjmp(jump_buffer ,1); // Jump back to test_head on error
+}
+
+// Test function prototypes
+bool test_copy();
+bool test_bitwise_operations();
+bool test_comparisons();
+bool test_arithmetic();
+bool test_shifts();
+
+// Test array (null-terminated)
+typedef bool (*TestFunction)();
+typedef struct{
+ TestFunction function;
+ const char *name;
+}TestEntry;
+
+TestEntry test_list[] = {
+ {test_copy ,"test_copy"}
+ ,{test_bitwise_operations ,"test_bitwise_operations"}
+ ,{test_comparisons ,"test_comparisons"}
+ ,{test_arithmetic ,"test_arithmetic"}
+ ,{test_shifts ,"test_shifts"}
+ ,{NULL ,NULL} // Null termination
+};
+
+// The test runner
+int test_head(){
+ int pass_count = 0;
+ int fail_count = 0;
+
+ // Set up signal handlers
+ signal(SIGSEGV ,signal_handler); // Catch segmentation faults
+ signal(SIGFPE ,signal_handler); // Catch floating point errors
+ signal(SIGABRT ,signal_handler); // Catch abort() calls
+
+ for(TestEntry *entry = test_list; entry->function != NULL; entry++){
+ if( sigsetjmp(jump_buffer ,1) == 0 ){
+ // Run the test normally
+ if( !entry->function() ){
+ printf("Failed: %s\n" ,entry->name);
+ fail_count++;
+ }else{
+ pass_count++;
+ }
+ }else{
+ // If a signal was caught
+ printf("Failed due to signaling: %s\n" ,entry->name);
+ fail_count++;
+ }
+ }
+
+ printf("Tests passed: %d\n" ,pass_count);
+ printf("Tests failed: %d\n" ,fail_count);
+ return (fail_count == 0) ? 0 : 1;
+}
+
+// Main function
+int main(int argc ,char **argv){
+ return test_head();
+}
+
+//------------------------------------------------------------------------------
+// Test Implementations
+//------------------------------------------------------------------------------
+
+bool test_copy(){
+ // Allocate memory
+ N32_1x32路T *array = N32_1x32路位.allocate_array(2 ,NULL);
+ if( !array ) return false;
+
+ // Access elements via access function
+ N32_1x32路T *a = N32_1x32路位.access(array ,0);
+ N32_1x32路T *b = N32_1x32路位.access(array ,1);
+
+ // Assign value and copy
+ N32_1x32路位.from_uint32(a ,42);
+ N32_1x32路位.copy(b ,a);
+
+ bool success = ( N32_1x32路位.compare(b ,a) == N32_1x32路Order_eq );
+ N32_1x32路位.deallocate(array);
+ return success;
+}
+
+bool test_arithmetic(){
+ // Allocate memory
+ N32_1x32路T *array = N32_1x32路位.allocate_array(3 ,NULL);
+ if( !array ) return false;
+
+ N32_1x32路T *a = N32_1x32路位.access(array ,0);
+ N32_1x32路T *b = N32_1x32路位.access(array ,1);
+ N32_1x32路T *result = N32_1x32路位.access(array ,2);
+
+ N32_1x32路位.from_uint32(a ,20);
+ N32_1x32路位.from_uint32(b ,22);
+
+ if( N32_1x32路位.add(result ,a ,b) != N32_1x32路Status路ok ) return false;
+ if( N32_1x32路位.compare(result ,N32_1x32路位.access(array ,0)) != N32_1x32路Order_gt ) return false;
+
+ if( N32_1x32路位.subtract(result ,b ,a) != N32_1x32路Status路ok ) return false;
+ if( N32_1x32路位.compare(result ,N32_1x32路位.access(array ,0)) != N32_1x32路Order_lt ) return false;
+
+ N32_1x32路位.deallocate(array);
+ return true;
+}
+
+bool test_bitwise_operations(){
+ // Allocate memory
+ N32_1x32路T *array = N32_1x32路位.allocate_array(3, NULL);
+ if(!array) return false;
+
+ N32_1x32路T *a = N32_1x32路位.access(array, 0);
+ N32_1x32路T *b = N32_1x32路位.access(array, 1);
+ N32_1x32路T *result = N32_1x32路位.access(array, 2);
+
+ // a = 0x0F0F0F0F, b = 0xF0F0F0F0
+ N32_1x32路位.from_uint32(a, 0x0F0F0F0F);
+ N32_1x32路位.from_uint32(b, 0xF0F0F0F0);
+
+ // bit_and => expect 0x00000000
+ N32_1x32路位.bit_and(result, a, b);
+ N32_1x32路位.from_uint32(a, 0x00000000);
+ if(N32_1x32路位.compare(result, a) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+
+ // Reset a to 0x0F0F0F0F for next tests
+ N32_1x32路位.from_uint32(a, 0x0F0F0F0F);
+
+ // bit_or => expect 0xFFFFFFFF
+ N32_1x32路位.bit_or(result, a, b);
+ N32_1x32路位.from_uint32(b, 0xFFFFFFFF);
+ if(N32_1x32路位.compare(result, b) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+
+ // bit_complement(a=0x0F0F0F0F) => expect 0xF0F0F0F0
+ N32_1x32路位.from_uint32(a, 0x0F0F0F0F);
+ N32_1x32路位.bit_complement(result, a);
+ N32_1x32路位.from_uint32(b, 0xF0F0F0F0);
+ if(N32_1x32路位.compare(result, b) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+
+ // bit_twos_complement(a=0x0F0F0F0F) => expect 0xF0F0F0F1
+ N32_1x32路位.from_uint32(a, 0x0F0F0F0F);
+ N32_1x32路位.bit_twos_complement(result, a);
+ N32_1x32路位.from_uint32(b, 0xF0F0F0F1);
+ if(N32_1x32路位.compare(result, b) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+
+ N32_1x32路位.deallocate(array);
+ return true;
+}
+
+bool test_comparisons(){
+ // Allocate memory
+ N32_1x32路T *array = N32_1x32路位.allocate_array(3, NULL);
+ if(!array) return false;
+
+ N32_1x32路T *a = N32_1x32路位.access(array, 0);
+ N32_1x32路T *b = N32_1x32路位.access(array, 1);
+ N32_1x32路T *c = N32_1x32路位.access(array, 2);
+
+ // First set: a=0, b=42, c=42
+ N32_1x32路位.from_uint32(a, 0);
+ N32_1x32路位.from_uint32(b, 42);
+ N32_1x32路位.from_uint32(c, 42);
+
+ // eq_zero(a) => true
+ if(!N32_1x32路位.eq_zero(a)){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ // eq_zero(b) => false
+ if(N32_1x32路位.eq_zero(b)){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ // eq(b, c) => true
+ if(!N32_1x32路位.eq(b, c)){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ // eq(a, b) => false
+ if(N32_1x32路位.eq(a, b)){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ // compare(a, b) => N32_1x32路Order_lt
+ if(N32_1x32路位.compare(a, b) != N32_1x32路Order_lt){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ // compare(b, a) => N32_1x32路Order_gt
+ if(N32_1x32路位.compare(b, a) != N32_1x32路Order_gt){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ // compare(b, c) => N32_1x32路Order_eq
+ if(N32_1x32路位.compare(b, c) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ // lt(a, b) => true, gt(b, a) => true
+ if(!N32_1x32路位.lt(a, b) || !N32_1x32路位.gt(b, a)){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+
+ // Second set: a=100, b=50
+ N32_1x32路位.from_uint32(a, 100);
+ N32_1x32路位.from_uint32(b, 50);
+ if(N32_1x32路位.compare(a, b) != N32_1x32路Order_gt){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ // eq_zero(a) => false
+ if(N32_1x32路位.eq_zero(a)){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ // eq_zero(b) => false
+ if(N32_1x32路位.eq_zero(b)){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+
+ N32_1x32路位.deallocate(array);
+ return true;
+}
+
+bool test_shifts(){
+ // Allocate memory for operand, fill, spill
+ N32_1x32路T *array = N32_1x32路位.allocate_array(3, NULL);
+ if(!array) return false;
+
+ N32_1x32路T *operand = N32_1x32路位.access(array, 0);
+ N32_1x32路T *fill = N32_1x32路位.access(array, 1);
+ N32_1x32路T *spill = N32_1x32路位.access(array, 2);
+
+ // Subtest A: shift_left(4) with operand=1 => expect operand=16, fill=0, spill=0
+ N32_1x32路位.from_uint32(operand, 1);
+ N32_1x32路位.from_uint32(fill, 0);
+ N32_1x32路位.from_uint32(spill, 0);
+ if(N32_1x32路位.shift_left(4, spill, operand, fill) != N32_1x32路Status路ok){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ N32_1x32路T *temp = N32_1x32路位.allocate_array(1, NULL);
+ if(!temp){
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ N32_1x32路位.from_uint32(temp, 16);
+ if(N32_1x32路位.compare(operand, temp) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ if(N32_1x32路位.compare(fill, N32_1x32路zero) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ if(N32_1x32路位.compare(spill, N32_1x32路zero) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+
+ // Subtest B: shift_left(1) with operand=0x80000000 => expect operand=0, spill=1
+ N32_1x32路位.from_uint32(operand, 0x80000000);
+ N32_1x32路位.from_uint32(fill, 0);
+ N32_1x32路位.from_uint32(spill, 0);
+ if(N32_1x32路位.shift_left(1, spill, operand, fill) != N32_1x32路Status路ok){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ if(!N32_1x32路位.eq_zero(operand)){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ N32_1x32路位.from_uint32(temp, 1);
+ if(N32_1x32路位.compare(spill, temp) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+
+ // Subtest C: shift_right(1) with operand=0x80000000 => expect operand=0x40000000, spill=0
+ N32_1x32路位.from_uint32(operand, 0x80000000);
+ N32_1x32路位.from_uint32(fill, 0);
+ N32_1x32路位.from_uint32(spill, 0);
+ if(N32_1x32路位.shift_right(1, spill, operand, fill) != N32_1x32路Status路ok){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ N32_1x32路位.from_uint32(temp, 0x40000000);
+ if(N32_1x32路位.compare(operand, temp) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ if(!N32_1x32路位.eq_zero(spill)){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+
+ // Subtest D: arithmetic_shift_right(1) with operand=0x80000000 => expect operand=0xC0000000, spill=0
+ N32_1x32路位.from_uint32(operand, 0x80000000);
+ N32_1x32路位.from_uint32(spill, 0);
+ if(N32_1x32路位.arithmetic_shift_right(1, operand, spill) != N32_1x32路Status路ok){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ N32_1x32路位.from_uint32(temp, 0xC0000000);
+ if(N32_1x32路位.compare(operand, temp) != N32_1x32路Order_eq){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+ if(!N32_1x32路位.eq_zero(spill)){
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return false;
+ }
+
+ N32_1x32路位.deallocate(temp);
+ N32_1x32路位.deallocate(array);
+ return true;
+}
+
+
+
+// Include the local section of N32_1x32.lib.c for testing
+#define LOCAL
+#include "N32_1x32.lib.c"
+#undef LOCAL
--- /dev/null
+#include <stdio.h>
+#include <stdbool.h>
+#include <signal.h>
+#include <setjmp.h>
+
+// Enable interface section
+#define FACE
+#include "N32_4x8.lib.c"
+#undef FACE
+
+// Jump buffer for signal handling
+static sigjmp_buf jump_buffer;
+
+// Signal handler for catching fatal errors
+void signal_handler(int signal){
+ siglongjmp(jump_buffer ,1); // Jump back to test_head on error
+}
+
+// Test function prototypes
+bool test_copy();
+bool test_bitwise_operations();
+bool test_comparisons();
+bool test_arithmetic();
+bool test_shifts();
+
+// Test array (null-terminated)
+typedef bool (*TestFunction)();
+typedef struct{
+ TestFunction function;
+ const char *name;
+}TestEntry;
+
+TestEntry test_list[] = {
+ {test_copy ,"test_copy"}
+ ,{test_bitwise_operations ,"test_bitwise_operations"}
+ ,{test_comparisons ,"test_comparisons"}
+ ,{test_arithmetic ,"test_arithmetic"}
+ ,{test_shifts ,"test_shifts"}
+ ,{NULL ,NULL} // Null termination
+};
+
+// The test runner
+int test_head(){
+ int pass_count = 0;
+ int fail_count = 0;
+
+ // Set up signal handlers
+ signal(SIGSEGV ,signal_handler); // Catch segmentation faults
+ signal(SIGFPE ,signal_handler); // Catch floating point errors
+ signal(SIGABRT ,signal_handler); // Catch abort() calls
+
+ for(TestEntry *entry = test_list; entry->function != NULL; entry++){
+ if( sigsetjmp(jump_buffer ,1) == 0 ){
+ // Run the test normally
+ if( !entry->function() ){
+ printf("Failed: %s\n" ,entry->name);
+ fail_count++;
+ }else{
+ pass_count++;
+ }
+ }else{
+ // If a signal was caught
+ printf("Failed due to signaling: %s\n" ,entry->name);
+ fail_count++;
+ }
+ }
+
+ printf("Tests passed: %d\n" ,pass_count);
+ printf("Tests failed: %d\n" ,fail_count);
+ return (fail_count == 0) ? 0 : 1;
+}
+
+// Main function
+int main(int argc ,char **argv){
+ return test_head();
+}
+
+//------------------------------------------------------------------------------
+// Test Implementations
+//------------------------------------------------------------------------------
+
+bool test_copy(){
+ // Allocate memory
+ N32_4x8路T *array = N32_4x8路位.allocate_array(2 ,NULL);
+ if( !array ) return false;
+
+ // Access elements via access function
+ N32_4x8路T *a = N32_4x8路位.access(array ,0);
+ N32_4x8路T *b = N32_4x8路位.access(array ,1);
+
+ // Assign value and copy
+ N32_4x8路位.from_uint32(a ,42);
+ N32_4x8路位.copy(b ,a);
+
+ bool success = ( N32_4x8路位.compare(b ,a) == N32_4x8路Order_eq );
+ N32_4x8路位.deallocate(array);
+ return success;
+}
+
+bool test_arithmetic(){
+ // Allocate memory
+ N32_4x8路T *array = N32_4x8路位.allocate_array(3 ,NULL);
+ if( !array ) return false;
+
+ N32_4x8路T *a = N32_4x8路位.access(array ,0);
+ N32_4x8路T *b = N32_4x8路位.access(array ,1);
+ N32_4x8路T *result = N32_4x8路位.access(array ,2);
+
+ N32_4x8路位.from_uint32(a ,20);
+ N32_4x8路位.from_uint32(b ,22);
+
+ if( N32_4x8路位.add(result ,a ,b) != N32_4x8路Status路ok ) return false;
+ if( N32_4x8路位.compare(result ,N32_4x8路位.access(array ,0)) != N32_4x8路Order_gt ) return false;
+
+ if( N32_4x8路位.subtract(result ,b ,a) != N32_4x8路Status路ok ) return false;
+ if( N32_4x8路位.compare(result ,N32_4x8路位.access(array ,0)) != N32_4x8路Order_lt ) return false;
+
+ N32_4x8路位.deallocate(array);
+ return true;
+}
+
+bool test_bitwise_operations(){
+ // Allocate memory
+ N32_4x8路T *array = N32_4x8路位.allocate_array(3, NULL);
+ if(!array) return false;
+
+ N32_4x8路T *a = N32_4x8路位.access(array, 0);
+ N32_4x8路T *b = N32_4x8路位.access(array, 1);
+ N32_4x8路T *result = N32_4x8路位.access(array, 2);
+
+ // a = 0x0F0F0F0F, b = 0xF0F0F0F0
+ N32_4x8路位.from_uint32(a, 0x0F0F0F0F);
+ N32_4x8路位.from_uint32(b, 0xF0F0F0F0);
+
+ // bit_and => expect 0x00000000
+ N32_4x8路位.bit_and(result, a, b);
+ N32_4x8路位.from_uint32(a, 0x00000000);
+ if(N32_4x8路位.compare(result, a) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+
+ // Reset a to 0x0F0F0F0F for next tests
+ N32_4x8路位.from_uint32(a, 0x0F0F0F0F);
+
+ // bit_or => expect 0xFFFFFFFF
+ N32_4x8路位.bit_or(result, a, b);
+ N32_4x8路位.from_uint32(b, 0xFFFFFFFF);
+ if(N32_4x8路位.compare(result, b) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+
+ // bit_complement(a=0x0F0F0F0F) => expect 0xF0F0F0F0
+ N32_4x8路位.from_uint32(a, 0x0F0F0F0F);
+ N32_4x8路位.bit_complement(result, a);
+ N32_4x8路位.from_uint32(b, 0xF0F0F0F0);
+ if(N32_4x8路位.compare(result, b) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+
+ // bit_twos_complement(a=0x0F0F0F0F) => expect 0xF0F0F0F1
+ N32_4x8路位.from_uint32(a, 0x0F0F0F0F);
+ N32_4x8路位.bit_twos_complement(result, a);
+ N32_4x8路位.from_uint32(b, 0xF0F0F0F1);
+ if(N32_4x8路位.compare(result, b) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+
+ N32_4x8路位.deallocate(array);
+ return true;
+}
+
+bool test_comparisons(){
+ // Allocate memory
+ N32_4x8路T *array = N32_4x8路位.allocate_array(3, NULL);
+ if(!array) return false;
+
+ N32_4x8路T *a = N32_4x8路位.access(array, 0);
+ N32_4x8路T *b = N32_4x8路位.access(array, 1);
+ N32_4x8路T *c = N32_4x8路位.access(array, 2);
+
+ // First set: a=0, b=42, c=42
+ N32_4x8路位.from_uint32(a, 0);
+ N32_4x8路位.from_uint32(b, 42);
+ N32_4x8路位.from_uint32(c, 42);
+
+ // eq_zero(a) => true
+ if(!N32_4x8路位.eq_zero(a)){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ // eq_zero(b) => false
+ if(N32_4x8路位.eq_zero(b)){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ // eq(b, c) => true
+ if(!N32_4x8路位.eq(b, c)){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ // eq(a, b) => false
+ if(N32_4x8路位.eq(a, b)){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ // compare(a, b) => N32_4x8路Order_lt
+ if(N32_4x8路位.compare(a, b) != N32_4x8路Order_lt){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ // compare(b, a) => N32_4x8路Order_gt
+ if(N32_4x8路位.compare(b, a) != N32_4x8路Order_gt){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ // compare(b, c) => N32_4x8路Order_eq
+ if(N32_4x8路位.compare(b, c) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ // lt(a, b) => true, gt(b, a) => true
+ if(!N32_4x8路位.lt(a, b) || !N32_4x8路位.gt(b, a)){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+
+ // Second set: a=100, b=50
+ N32_4x8路位.from_uint32(a, 100);
+ N32_4x8路位.from_uint32(b, 50);
+ if(N32_4x8路位.compare(a, b) != N32_4x8路Order_gt){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ // eq_zero(a) => false
+ if(N32_4x8路位.eq_zero(a)){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ // eq_zero(b) => false
+ if(N32_4x8路位.eq_zero(b)){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+
+ N32_4x8路位.deallocate(array);
+ return true;
+}
+
+bool test_shifts(){
+ // Allocate memory for operand, fill, spill
+ N32_4x8路T *array = N32_4x8路位.allocate_array(3, NULL);
+ if(!array) return false;
+
+ N32_4x8路T *operand = N32_4x8路位.access(array, 0);
+ N32_4x8路T *fill = N32_4x8路位.access(array, 1);
+ N32_4x8路T *spill = N32_4x8路位.access(array, 2);
+
+ // Subtest A: shift_left(4) with operand=1 => expect operand=16, fill=0, spill=0
+ N32_4x8路位.from_uint32(operand, 1);
+ N32_4x8路位.from_uint32(fill, 0);
+ N32_4x8路位.from_uint32(spill, 0);
+ if(N32_4x8路位.shift_left(4, spill, operand, fill) != N32_4x8路Status路ok){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ N32_4x8路T *temp = N32_4x8路位.allocate_array(1, NULL);
+ if(!temp){
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ N32_4x8路位.from_uint32(temp, 16);
+ if(N32_4x8路位.compare(operand, temp) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ if(N32_4x8路位.compare(fill, N32_4x8路zero) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ if(N32_4x8路位.compare(spill, N32_4x8路zero) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+
+ // Subtest B: shift_left(1) with operand=0x80000000 => expect operand=0, spill=1
+ N32_4x8路位.from_uint32(operand, 0x80000000);
+ N32_4x8路位.from_uint32(fill, 0);
+ N32_4x8路位.from_uint32(spill, 0);
+ if(N32_4x8路位.shift_left(1, spill, operand, fill) != N32_4x8路Status路ok){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ if(!N32_4x8路位.eq_zero(operand)){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ N32_4x8路位.from_uint32(temp, 1);
+ if(N32_4x8路位.compare(spill, temp) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+
+ // Subtest C: shift_right(1) with operand=0x80000000 => expect operand=0x40000000, spill=0
+ N32_4x8路位.from_uint32(operand, 0x80000000);
+ N32_4x8路位.from_uint32(fill, 0);
+ N32_4x8路位.from_uint32(spill, 0);
+ if(N32_4x8路位.shift_right(1, spill, operand, fill) != N32_4x8路Status路ok){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ N32_4x8路位.from_uint32(temp, 0x40000000);
+ if(N32_4x8路位.compare(operand, temp) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ if(!N32_4x8路位.eq_zero(spill)){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+
+ // Subtest D: arithmetic_shift_right(1) with operand=0x80000000 => expect operand=0xC0000000, spill=0
+ N32_4x8路位.from_uint32(operand, 0x80000000);
+ N32_4x8路位.from_uint32(spill, 0);
+ if(N32_4x8路位.arithmetic_shift_right(1, operand, spill) != N32_4x8路Status路ok){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ N32_4x8路位.from_uint32(temp, 0xC0000000);
+ if(N32_4x8路位.compare(operand, temp) != N32_4x8路Order_eq){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+ if(!N32_4x8路位.eq_zero(spill)){
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return false;
+ }
+
+ N32_4x8路位.deallocate(temp);
+ N32_4x8路位.deallocate(array);
+ return true;
+}
+
+
+
+// Include the local section of N32_4x8.lib.c for testing
+#define LOCAL
+#include "N32_4x8.lib.c"
+#undef LOCAL
generates `.c` source code from templates
"""
- # base line test N32 test
- type_name = "N32PN"
+ # N32 processor native
+ type_name = "N32"
code = test_N32(namespace = type_name)
write(code ,type_name);
- # a single digit N32
- type_name = "N32"
+ # N32 1 digit of 32 bits
+ type_name = "N32_1x32"
code = test_N32(namespace = type_name)
write(code ,type_name);
+ # N32 2 digits of 16 bits
+ # type_name = "N32_4x8"
+ # code = test_N32(namespace = type_name)
+ # write(code ,type_name);
+
+ # N32 4 digits of 8 bits
+ type_name = "N32_4x8"
+ code = test_N32(namespace = type_name)
+ write(code ,type_name);
if __name__ == "__main__":
cd "$REPO_HOME"/tester || exit 1
+ pushd python
+ ./fill_template
+ popd
+
/bin/make -f tool馃枆/makefile $@
set +x