implemented avg() asm

This commit is contained in:
Irmen de Jong 2018-12-30 00:24:28 +01:00
parent 6e8cf7e1d3
commit 9ee1628901
2 changed files with 91 additions and 133 deletions

View File

@ -4,27 +4,26 @@
~ main { ~ main {
; @todo fix floating point number corruption ; @todo fix floating point number corruption
; @todo add avg implementations
word[10] xword = [1,2,3,4,5,6,7,8,9,-1111] byte[10] xbyte = [1,2,3,4,5,6,7,8,9,-110]
word[10] yword = [11,22,33,44,55,66,77,88,99,-1111 ] byte[10] ybyte = [11,22,33,44,55,-66,-77,-88,-99,-110 ]
word[10] zword = [1,2,3,4,5,6,7,8,9,-9999] byte[10] zbyte = [1,2,3,4,5,6,7,8,9,-99]
uword[10] xuword = [1,2,3,4,5,6,7,88,99,1111]
uword[10] yuword = [11,22,33,44,55,66,77,88,99,1111 ]
uword[10] zuword = [1,2,3,4,5,6,77,88,99,9999]
byte[10] xbyte = [1,2,3,4,5,6,7,8,9,-110]
byte[10] ybyte = [ 11,22,33,44,55,-66,-77,-88,-99,-110 ]
byte[10] zbyte = [1,2,3,4,5,6,7,8,9,-99]
ubyte[10] xubyte = [1,2,3,4,5,6,77,88,99,111] ubyte[10] xubyte = [1,2,3,4,5,6,77,88,99,111]
ubyte[10] yubyte = [11,22,33,44,55,66,77,88,99,111] ubyte[10] yubyte = [11,22,33,44,55,66,77,88,99,111]
ubyte[10] zubyte = [1,2,3,4,5,66,7,88,99,111] ubyte[10] zubyte = [1,2,3,4,5,66,7,88,99,111]
float[10] xcoor = [ 1,2,3,4,5,6,7,8,9.9,11.11 ] word[10] xword = [1,2,3,4,5,6,7,8,9,-1111]
float[10] ycoor = [ 11,22,33,44,55,66,77,88,99.9,111.11 ] word[10] yword = [11,22,33,44,55,66,77,88,99,-1111 ]
float[10] zcoor = [ 111,222,333,444,555,666,777,888,999.9,1001.11 ] word[10] zword = [1,2,3,4,5,6,7,8,9,-9999]
uword[10] xuword = [1,2,3,4,5,6,7,88,99,1111]
uword[10] yuword = [11,22,33,44,55,66,77,88,99,1111 ]
uword[10] zuword = [1,2,3,4,5,6,77,88,99,9999]
float[10] xcoor = [1,2,3,4,5,6,7,8,9.9,11.11 ]
float[10] ycoor = [11,22,33,44,55,66,77,88,99.9,111.11 ]
float[10] zcoor = [111,222,333,444,555,666,777,888,999.9,1001.11 ]
sub start() { sub start() {
; c64scr.print("\nxword:\n") ; c64scr.print("\nxword:\n")
@ -56,148 +55,77 @@
c64.CHROUT('X') c64.CHROUT('X')
c64scr.print_ub(X) c64scr.print_ub(X)
c64.CHROUT('\n') c64.CHROUT('\n')
word sumbx = min(xbyte) ; -65 float avgbx = avg(xbyte)
word sumby = min(ybyte) ; -275 float avgby = avg(ybyte)
word sumbz = min(zbyte) ; -54 float avgbz = avg(zbyte)
uword sumubx = min(xubyte) ; 396 float avgubx = avg(xubyte)
uword sumuby = min(yubyte) ; 606 float avguby = avg(yubyte)
uword sumubz = min(zubyte) ; 386 float avgubz = avg(zubyte)
word sumwx = min(xword) ; -1066 float avgwx = avg(xword)
word sumwy = min(yword) ; -616 float avgwy = avg(yword)
word sumwz = min(zword) ; -9954 float avgwz = avg(zword)
uword sumuwx = min(xuword) ; 1326 float avguwx = avg(xuword)
uword sumuwy = min(yuword) ; 1606 float avguwy = avg(yuword)
uword sumuwz = min(zuword) ; 10284 float avguwz = avg(zuword)
float sumfx = min(xcoor) ; 57.01 float avgfx = avg(xcoor)
float sumfy = min(ycoor) ; 607.01 float avgfy = avg(ycoor)
float sumfz = min(zcoor) ; 5997.01 float avgfz = avg(zcoor)
c64.CHROUT('X') c64.CHROUT('X')
c64scr.print_ub(X) c64scr.print_ub(X)
c64.CHROUT('\n') c64.CHROUT('\n')
; float avgbx = avg(xbyte)
; float avgby = avg(ybyte)
; float avgbz = avg(zbyte)
; float avgubx = avg(xubyte)
; float avguby = avg(yubyte)
; float avgubz = avg(zubyte)
; float avgwx = avg(xword)
; float avgwy = avg(yword)
; float avgwz = avg(zword)
; float avguwx = avg(xuword)
; float avguwy = avg(yuword)
; float avguwz = avg(zuword)
; float avgfx = avg(xcoor)
; float avgfy = avg(ycoor)
; float avgfz = avg(zcoor)
; c64.CHROUT('X')
; c64scr.print_ub(X)
; c64.CHROUT('\n')
c64scr.print("sumbx=") c64scr.print("avgbx=")
c64scr.print_w(sumbx) c64flt.print_f(avgbx)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumby=") c64scr.print("avgby=")
c64scr.print_w(sumby) c64flt.print_f(avgby)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumbz=") c64scr.print("avgbz=")
c64scr.print_w(sumbz) c64flt.print_f(avgbz)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumubx=") c64scr.print("avgubx=")
c64scr.print_uw(sumubx) c64flt.print_f(avgubx)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumuby=") c64scr.print("avguby=")
c64scr.print_uw(sumuby) c64flt.print_f(avguby)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumubz=") c64scr.print("avgubz=")
c64scr.print_uw(sumubz) c64flt.print_f(avgubz)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumwx=") c64scr.print("avgwx=")
c64scr.print_w(sumwx) c64flt.print_f(avgwx)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumwy=") c64scr.print("avgwy=")
c64scr.print_w(sumwy) c64flt.print_f(avgwy)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumwz=") c64scr.print("avgwz=")
c64scr.print_w(sumwz) c64flt.print_f(avgwz)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumuwx=") c64scr.print("avguwx=")
c64scr.print_uw(sumuwx) c64flt.print_f(avguwx)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumuwy=") c64scr.print("avguwy=")
c64scr.print_uw(sumuwy) c64flt.print_f(avguwy)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumuwz=") c64scr.print("avguwz=")
c64scr.print_uw(sumuwz) c64flt.print_f(avguwz)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumfx=") c64scr.print("avgfx=")
c64flt.print_f(sumfx) c64flt.print_f(avgfx)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumfy=") c64scr.print("avgfy=")
c64flt.print_f(sumfy) c64flt.print_f(avgfy)
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print("sumfz=") c64scr.print("avgfz=")
c64flt.print_f(sumfz) c64flt.print_f(avgfz)
c64.CHROUT('\n') c64.CHROUT('\n')
return return
; c64scr.print("avgbx=")
; c64flt.print_f(avgbx)
; c64.CHROUT('\n')
; c64scr.print("avgby=")
; c64flt.print_f(avgby)
; c64.CHROUT('\n')
; c64scr.print("avgbz=")
; c64flt.print_f(avgbz)
; c64.CHROUT('\n')
;
; c64scr.print("avgubx=")
; c64flt.print_f(avgubx)
; c64.CHROUT('\n')
; c64scr.print("avguby=")
; c64flt.print_f(avguby)
; c64.CHROUT('\n')
; c64scr.print("avgubz=")
; c64flt.print_f(avgubz)
; c64.CHROUT('\n')
;
; c64scr.print("avgwx=")
; c64flt.print_f(avgwx)
; c64.CHROUT('\n')
; c64scr.print("avgwy=")
; c64flt.print_f(avgwy)
; c64.CHROUT('\n')
; c64scr.print("avgwz=")
; c64flt.print_f(avgwz)
; c64.CHROUT('\n')
;
; c64scr.print("avguwx=")
; c64flt.print_f(avguwx)
; c64.CHROUT('\n')
; c64scr.print("avguwy=")
; c64flt.print_f(avguwy)
; c64.CHROUT('\n')
; c64scr.print("avguwz=")
; c64flt.print_f(avguwz)
; c64.CHROUT('\n')
;
; c64scr.print("avgfx=")
; c64flt.print_f(avgfx)
; c64.CHROUT('\n')
; c64scr.print("avgfy=")
; c64flt.print_f(avgfy)
; c64.CHROUT('\n')
; c64scr.print("avgfz=")
; c64flt.print_f(avgfz)
; c64.CHROUT('\n')
;
; return
c64scr.print("\nseparated i=2\n") c64scr.print("\nseparated i=2\n")
c64scr.print(" x[2]=") c64scr.print(" x[2]=")

View File

@ -721,7 +721,38 @@ private class StatementTranslator(private val prog: IntermediateProgram,
else -> throw CompilerException("wrong datatype for $funcname()") else -> throw CompilerException("wrong datatype for $funcname()")
} }
} }
"min", "max", "avg", "sum" -> { "avg" -> {
// 1 array argument, type determines the exact syscall to use
val arg=args.single() as IdentifierReference
val target=arg.targetStatement(namespace) as VarDecl
val length=Value(DataType.UBYTE, target.arrayspec!!.size()!!)
val arrayDt=arg.resultingDatatype(namespace, heap)
prog.instr(Opcode.PUSH_BYTE, length)
when (arrayDt) {
DataType.ARRAY_UB -> {
createSyscall("sum_ub")
prog.instr(Opcode.CAST_UW_TO_F) // result of sum(ubyte) is uword, so cast
}
DataType.ARRAY_B -> {
createSyscall("sum_b")
prog.instr(Opcode.CAST_W_TO_F) // result of sum(byte) is word, so cast
}
DataType.ARRAY_UW -> {
createSyscall("sum_uw")
prog.instr(Opcode.CAST_UW_TO_F) // result of sum(uword) is uword, so cast
}
DataType.ARRAY_W -> {
createSyscall("sum_w")
prog.instr(Opcode.CAST_W_TO_F) // result of sum(word) is word, so cast
}
DataType.ARRAY_F -> createSyscall("sum_f")
else -> throw CompilerException("wrong datatype for avg")
}
// divide by the number of elements
prog.instr(opcodePush(DataType.FLOAT), Value(DataType.FLOAT, length.numericValue()))
prog.instr(Opcode.DIV_F)
}
"min", "max", "sum" -> {
// 1 array argument, type determines the exact syscall to use // 1 array argument, type determines the exact syscall to use
val arg=args.single() as IdentifierReference val arg=args.single() as IdentifierReference
val target=arg.targetStatement(namespace) as VarDecl val target=arg.targetStatement(namespace) as VarDecl
@ -735,7 +766,6 @@ private class StatementTranslator(private val prog: IntermediateProgram,
DataType.ARRAY_F -> createSyscall("${funcname}_f") DataType.ARRAY_F -> createSyscall("${funcname}_f")
else -> throw CompilerException("wrong datatype for $funcname()") else -> throw CompilerException("wrong datatype for $funcname()")
} }
} }
"abs" -> { "abs" -> {
// 1 argument, type determines the exact opcode to use // 1 argument, type determines the exact opcode to use