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 the library
|
||||||
add_library(bigint STATIC ${CMAKE_SOURCE_DIR}/bigint/src/bigint.c
|
add_library(bigint STATIC ${CMAKE_SOURCE_DIR}/bigint/src/bigint.c
|
||||||
support.c)
|
support.c
|
||||||
|
heap.c)
|
||||||
|
|
||||||
# Include directories for the bigint library
|
# Include directories for the bigint library
|
||||||
target_include_directories(bigint PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
|
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
|
// Stack
|
||||||
struct stack stack;
|
struct stack stack;
|
||||||
#define SIZE 1000
|
#define SIZE 64
|
||||||
|
|
||||||
//Register
|
//Register
|
||||||
struct stack reg;
|
struct stack reg;
|
||||||
@ -312,18 +312,22 @@ void tests(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
// Initialize the HEAP
|
||||||
|
int hsize = 8 * 1024;
|
||||||
|
int hcurrent = 0;
|
||||||
|
|
||||||
|
|
||||||
// Initialize the Stack
|
// Initialize the Stack
|
||||||
int size = SIZE;
|
int size = SIZE;
|
||||||
int current = 0;
|
int current = 0;
|
||||||
StackSlot s[SIZE];
|
|
||||||
stack.size = &size;
|
stack.size = &size;
|
||||||
stack.current = ¤t;
|
stack.current = ¤t;
|
||||||
stack.stack = s;
|
stack.stack = malloc(SIZE * 1024);
|
||||||
|
|
||||||
// Initialize the registery
|
// Initialize the registery
|
||||||
int rSize = SIZE;
|
int rSize = 1000;
|
||||||
int rCurrent = 0;
|
int rCurrent = 0;
|
||||||
StackSlot r[SIZE];
|
StackSlot r[1000];
|
||||||
reg.size = &rSize;
|
reg.size = &rSize;
|
||||||
reg.current = &rCurrent;
|
reg.current = &rCurrent;
|
||||||
reg.stack = r;
|
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:
|
_subFraction:
|
||||||
asf 4
|
asf 0
|
||||||
pushl -4
|
pushl -4
|
||||||
pushc 0
|
getf 0
|
||||||
lt
|
pushl -3
|
||||||
brf __5
|
getf 1
|
||||||
pushc 0
|
mul
|
||||||
|
pushl -3
|
||||||
|
getf 0
|
||||||
pushl -4
|
pushl -4
|
||||||
|
getf 1
|
||||||
|
mul
|
||||||
sub
|
sub
|
||||||
popl 0
|
|
||||||
jmp __6
|
|
||||||
__5:
|
|
||||||
pushl -4
|
pushl -4
|
||||||
popl 0
|
getf 1
|
||||||
__6:
|
|
||||||
pushl -3
|
pushl -3
|
||||||
pushc 0
|
getf 1
|
||||||
lt
|
mul
|
||||||
brf __7
|
call _newFraction
|
||||||
pushc 0
|
|
||||||
pushl -3
|
|
||||||
sub
|
|
||||||
popl 1
|
|
||||||
jmp __8
|
|
||||||
__7:
|
|
||||||
pushl -3
|
|
||||||
popl 1
|
|
||||||
__8:
|
|
||||||
pushl 0
|
|
||||||
pushl 1
|
|
||||||
call _gcd
|
|
||||||
drop 2
|
drop 2
|
||||||
pushr
|
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
|
popr
|
||||||
jmp __4
|
jmp __14
|
||||||
__4:
|
__14:
|
||||||
rsf
|
rsf
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
|||||||
3
record.c
3
record.c
@ -15,7 +15,7 @@ ObjRef newRecord(int size){
|
|||||||
record->size = (1 << ((sizeof(int) * 8) - 1));
|
record->size = (1 << ((sizeof(int) * 8) - 1));
|
||||||
record->size = record->size + size;
|
record->size = record->size + size;
|
||||||
for(int i = 0; i < size; i++) {
|
for(int i = 0; i < size; i++) {
|
||||||
GET_REFS_PTR(record)[i] = malloc(8);
|
GET_REFS_PTR(record)[i] = NULL;
|
||||||
}
|
}
|
||||||
return record;
|
return record;
|
||||||
}
|
}
|
||||||
@ -32,6 +32,7 @@ void setField(ObjRef arr, int point, ObjRef value){
|
|||||||
if(0 > point || point > getSize(arr)){
|
if(0 > point || point > getSize(arr)){
|
||||||
printf("Index %i out of bounds for length %i\n",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;
|
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user