forgot what ive done
This commit is contained in:
parent
82704187f9
commit
9f7cd4aefc
@ -12,7 +12,8 @@ target_include_directories(njvm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build
|
||||
|
||||
# Add the library
|
||||
add_library(bigint STATIC ${CMAKE_SOURCE_DIR}/bigint/src/bigint.c
|
||||
support.c)
|
||||
support.c
|
||||
heap.c)
|
||||
|
||||
# Include directories for the bigint library
|
||||
target_include_directories(bigint PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
|
||||
|
||||
11
heap.c
Normal file
11
heap.c
Normal file
@ -0,0 +1,11 @@
|
||||
//
|
||||
// Created by Nils Polek on 23.01.24.
|
||||
//
|
||||
typedef struct {
|
||||
int size;
|
||||
int hcurrent;
|
||||
}heap;
|
||||
|
||||
//void* alloc(int size){
|
||||
// return {}
|
||||
//}
|
||||
14
njvm.c
14
njvm.c
@ -15,7 +15,7 @@ int debug = 0;
|
||||
|
||||
// Stack
|
||||
struct stack stack;
|
||||
#define SIZE 1000
|
||||
#define SIZE 64
|
||||
|
||||
//Register
|
||||
struct stack reg;
|
||||
@ -312,18 +312,22 @@ void tests(void) {
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Initialize the HEAP
|
||||
int hsize = 8 * 1024;
|
||||
int hcurrent = 0;
|
||||
|
||||
|
||||
// Initialize the Stack
|
||||
int size = SIZE;
|
||||
int current = 0;
|
||||
StackSlot s[SIZE];
|
||||
stack.size = &size;
|
||||
stack.current = ¤t;
|
||||
stack.stack = s;
|
||||
stack.stack = malloc(SIZE * 1024);
|
||||
|
||||
// Initialize the registery
|
||||
int rSize = SIZE;
|
||||
int rSize = 1000;
|
||||
int rCurrent = 0;
|
||||
StackSlot r[SIZE];
|
||||
StackSlot r[1000];
|
||||
reg.size = &rSize;
|
||||
reg.current = &rCurrent;
|
||||
reg.stack = r;
|
||||
|
||||
146
programs/matrix.nj
Normal file
146
programs/matrix.nj
Normal file
@ -0,0 +1,146 @@
|
||||
//
|
||||
// matinv.nj -- invert 2x2 matrices of fractions
|
||||
//
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// greatest common divisor
|
||||
|
||||
Integer gcd(Integer a, Integer b) {
|
||||
local Integer h;
|
||||
while (b != 0) {
|
||||
h = a % b;
|
||||
a = b;
|
||||
b = h;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// fractions
|
||||
|
||||
type Fraction = record {
|
||||
Integer num;
|
||||
Integer den;
|
||||
};
|
||||
|
||||
Fraction newFraction(Integer num, Integer den) {
|
||||
local Integer n;
|
||||
local Integer d;
|
||||
local Integer g;
|
||||
local Fraction r;
|
||||
if (num < 0) {
|
||||
n = -num;
|
||||
} else {
|
||||
n = num;
|
||||
}
|
||||
if (den < 0) {
|
||||
d = -den;
|
||||
} else {
|
||||
d = den;
|
||||
}
|
||||
g = gcd(n, d);
|
||||
r = new(Fraction);
|
||||
if ((num < 0) != (den < 0)) {
|
||||
r.num = -n / g;
|
||||
} else {
|
||||
r.num = n / g;
|
||||
}
|
||||
r.den = d / g;
|
||||
return r;
|
||||
}
|
||||
|
||||
void writeFraction(Fraction f) {
|
||||
writeInteger(f.num);
|
||||
writeString("/");
|
||||
writeInteger(f.den);
|
||||
}
|
||||
|
||||
Fraction negFraction(Fraction f) {
|
||||
return newFraction(-f.num, f.den);
|
||||
}
|
||||
|
||||
Fraction addFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den + f2.num * f1.den, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction subFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den - f2.num * f1.den, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction mulFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.num, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction divFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den, f1.den * f2.num);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// 2x2 matrices of fractions
|
||||
|
||||
type Matrix = Fraction[][];
|
||||
|
||||
Matrix newMatrix(Fraction a00, Fraction a01,
|
||||
Fraction a10, Fraction a11) {
|
||||
local Matrix m;
|
||||
m = new(Fraction[2][]);
|
||||
m[0] = new(Fraction[2]);
|
||||
m[1] = new(Fraction[2]);
|
||||
m[0][0] = a00;
|
||||
m[0][1] = a01;
|
||||
m[1][0] = a10;
|
||||
m[1][1] = a11;
|
||||
return m;
|
||||
}
|
||||
|
||||
void writeMatrix(Matrix m) {
|
||||
local Integer i;
|
||||
local Integer j;
|
||||
i = 0;
|
||||
while (i < sizeof(m)) {
|
||||
j = 0;
|
||||
while (j < sizeof(m[i])) {
|
||||
writeFraction(m[i][j]);
|
||||
writeString(" ");
|
||||
j = j + 1;
|
||||
}
|
||||
writeString("\n");
|
||||
i = i + 1;
|
||||
}
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
Matrix invertMatrix(Matrix m) {
|
||||
local Fraction det;
|
||||
det = subFraction(mulFraction(m[0][0], m[1][1]),
|
||||
mulFraction(m[0][1], m[1][0]));
|
||||
if (det.num == 0) {
|
||||
writeString("error: matrix cannot be inverted\n");
|
||||
exit();
|
||||
}
|
||||
return newMatrix(
|
||||
divFraction(m[1][1], det), divFraction(negFraction(m[0][1]), det),
|
||||
divFraction(negFraction(m[1][0]), det), divFraction(m[0][0], det)
|
||||
);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
void main() {
|
||||
local Matrix matrix;
|
||||
local Matrix result1;
|
||||
local Matrix result2;
|
||||
writeString("\n");
|
||||
matrix = newMatrix(
|
||||
newFraction(7, 1), newFraction(4, 1),
|
||||
newFraction(6, 1), newFraction(5, 1)
|
||||
);
|
||||
writeMatrix(matrix);
|
||||
result1 = invertMatrix(matrix);
|
||||
writeMatrix(result1);
|
||||
result2 = invertMatrix(result1);
|
||||
writeMatrix(result2);
|
||||
}
|
||||
@ -1,71 +1,26 @@
|
||||
_newFraction:
|
||||
asf 4
|
||||
_subFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
pushc 0
|
||||
lt
|
||||
brf __5
|
||||
pushc 0
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -3
|
||||
getf 0
|
||||
pushl -4
|
||||
getf 1
|
||||
mul
|
||||
sub
|
||||
popl 0
|
||||
jmp __6
|
||||
__5:
|
||||
pushl -4
|
||||
popl 0
|
||||
__6:
|
||||
getf 1
|
||||
pushl -3
|
||||
pushc 0
|
||||
lt
|
||||
brf __7
|
||||
pushc 0
|
||||
pushl -3
|
||||
sub
|
||||
popl 1
|
||||
jmp __8
|
||||
__7:
|
||||
pushl -3
|
||||
popl 1
|
||||
__8:
|
||||
pushl 0
|
||||
pushl 1
|
||||
call _gcd
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popl 2
|
||||
new 2
|
||||
popl 3
|
||||
pushl -4
|
||||
pushc 0
|
||||
lt
|
||||
pushl -3
|
||||
pushc 0
|
||||
lt
|
||||
ne
|
||||
brf __9
|
||||
pushl 3
|
||||
pushc 0
|
||||
pushl 0
|
||||
sub
|
||||
pushl 2
|
||||
div
|
||||
putf 0
|
||||
jmp __10
|
||||
__9:
|
||||
pushl 3
|
||||
pushl 0
|
||||
pushl 2
|
||||
div
|
||||
putf 0
|
||||
__10:
|
||||
pushl 3
|
||||
pushl 1
|
||||
pushl 2
|
||||
div
|
||||
putf 1
|
||||
pushl 3
|
||||
popr
|
||||
jmp __4
|
||||
__4:
|
||||
jmp __14
|
||||
__14:
|
||||
rsf
|
||||
ret
|
||||
|
||||
|
||||
3
record.c
3
record.c
@ -15,7 +15,7 @@ ObjRef newRecord(int size){
|
||||
record->size = (1 << ((sizeof(int) * 8) - 1));
|
||||
record->size = record->size + size;
|
||||
for(int i = 0; i < size; i++) {
|
||||
GET_REFS_PTR(record)[i] = malloc(8);
|
||||
GET_REFS_PTR(record)[i] = NULL;
|
||||
}
|
||||
return record;
|
||||
}
|
||||
@ -32,6 +32,7 @@ void setField(ObjRef arr, int point, ObjRef value){
|
||||
if(0 > point || point > getSize(arr)){
|
||||
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
|
||||
}
|
||||
GET_REFS_PTR(arr)[point] = malloc(8);
|
||||
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user