Skip to content

Commit

Permalink
Fix fsw and fmv.x.s + add fcvt.s.d
Browse files Browse the repository at this point in the history
When adding the doubles I failed to reliaze that some floating point
operations needed to be able to access the unboxed float value.

I also missed that there were float to double conversion operations.
  • Loading branch information
TheThirdOne committed Nov 24, 2019
1 parent 696ef9e commit 5675921
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/PseudoOps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ fcvt.d.wu f1, t1 ; fcvt.d.wu RG1, RG2, dyn ;#Convert double from
fcvt.d.w f1, t1 ; fcvt.d.w RG1, RG2, dyn ;#Convert double from signed integer: Assigns the value of t1 to f1
fcvt.w.d t1, f1 ; fcvt.w.d RG1, RG2, dyn ;#Convert signed integer from double: Assigns the value of f1 (rounded) to t1
fcvt.wu.d t1, f1 ; fcvt.wu.d RG1, RG2, dyn ;#Convert unsigned integer from double: Assigns the value of f1 (rounded) to t1
fcvt.s.d f1, f2 ; fcvt.s.d RG1, RG2, dyn ;#Convert double to float: Assigned the value of f2 to f1
fcvt.d.s f1, f2 ; fcvt.d.s RG1, RG2 , dyn ;#Convert float to double: Assigned the value of f2 to f1

### TODO: maybe it makes sense to rename the instructions and flip these pseudo-instructions
fmv.x.w t1, f1 ; fmv.x.s RG1, RG2 ;#Move float (New mnemonic): move bits representing a float to an integer register
Expand Down
28 changes: 28 additions & 0 deletions src/rars/riscv/instructions/FCVTDS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package rars.riscv.instructions;

import jsoftfloat.Environment;
import jsoftfloat.types.Float32;
import jsoftfloat.types.Float64;
import rars.ProgramStatement;
import rars.SimulationException;
import rars.riscv.BasicInstruction;
import rars.riscv.BasicInstructionFormat;
import rars.riscv.hardware.FloatingPointRegisterFile;

public class FCVTDS extends BasicInstruction {
public FCVTDS() {
super("fcvt.d.s t1, f1, dyn", "Convert a float to a double: Assigned the value of f2 to f1",
BasicInstructionFormat.R4_FORMAT,"0100001 00000 sssss ttt fffff 1010011");
}

public void simulate(ProgramStatement statement) throws SimulationException {
int[] operands = statement.getOperands();
Environment e = new Environment();
e.mode = Floating.getRoundingMode(operands[3],statement);
Float32 in = new Float32(FloatingPointRegisterFile.getValue(operands[1]));
Float64 out = new Float64(0);
out = FCVTSD.convert(in,out,e);
Floating.setfflags(e);
FloatingPointRegisterFile.updateRegisterLong(operands[0],out.bits);
}
}
42 changes: 42 additions & 0 deletions src/rars/riscv/instructions/FCVTSD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package rars.riscv.instructions;

import jsoftfloat.Environment;
import jsoftfloat.types.Float32;
import jsoftfloat.types.Float64;
import rars.ProgramStatement;
import rars.SimulationException;
import rars.riscv.BasicInstruction;
import rars.riscv.BasicInstructionFormat;
import rars.riscv.hardware.FloatingPointRegisterFile;

public class FCVTSD extends BasicInstruction {
public FCVTSD() {
super("fcvt.s.d t1, f1, dyn", "Convert a double to a float: Assigned the value of f2 to f1",
BasicInstructionFormat.R4_FORMAT,"0100000 00001 sssss ttt fffff 1010011");
}

public void simulate(ProgramStatement statement) throws SimulationException {
int[] operands = statement.getOperands();
Environment e = new Environment();
e.mode = Floating.getRoundingMode(operands[3],statement);
Float64 in = new Float64(FloatingPointRegisterFile.getValueLong(operands[1]));
Float32 out = new Float32(0);
out = convert(in,out,e);
Floating.setfflags(e);
FloatingPointRegisterFile.updateRegister(operands[0],out.bits);
}
// Kindof a long type, but removes duplicate code and would make it easy for quads to be implemented.
public static <S extends jsoftfloat.types.Floating<S>,D extends jsoftfloat.types.Floating<D>>
S convert(D toconvert, S constructor, Environment e){
if(toconvert.isInfinite()){
return toconvert.isSignMinus() ? constructor.NegativeInfinity() : constructor.Infinity();
}
if(toconvert.isZero()){
return toconvert.isSignMinus() ? constructor.NegativeZero() : constructor.Zero();
}
if(toconvert.isNaN()){
return constructor.NaN();
}
return constructor.fromExactFloat(toconvert.toExactFloat(),e);
}
}
2 changes: 1 addition & 1 deletion src/rars/riscv/instructions/FMVXS.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public FMVXS() {

public void simulate(ProgramStatement statement) {
int[] operands = statement.getOperands();
RegisterFile.updateRegister(operands[0], FloatingPointRegisterFile.getValue(operands[1]));
RegisterFile.updateRegister(operands[0], (int)FloatingPointRegisterFile.getValueLong(operands[1]));
}
}
2 changes: 1 addition & 1 deletion src/rars/riscv/instructions/FSW.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public FSW() {
public void simulate(ProgramStatement statement) throws SimulationException {
int[] operands = statement.getOperands();
try {
Globals.memory.setWord(RegisterFile.getValue(operands[2]) + operands[1], FloatingPointRegisterFile.getValue(operands[0]));
Globals.memory.setWord(RegisterFile.getValue(operands[2]) + operands[1], (int)FloatingPointRegisterFile.getValueLong(operands[0]));
} catch (AddressErrorException e) {
throw new SimulationException(statement, e);
}
Expand Down

0 comments on commit 5675921

Please sign in to comment.