diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-08-28 13:04:06 +0000 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-12-05 16:02:41 +0100 |
| commit | 1a36e6211674f1a39c7cc0ddff705357a4add65a (patch) | |
| tree | 26d82fc4f6c7cc444a5121bb4d0cb71681485bb1 /basics/float.S | |
| parent | 6f0ebbad84419de65a03a0ea6e41cbc6b49d6eb0 (diff) | |
| download | farga-1a36e6211674f1a39c7cc0ddff705357a4add65a.tar.gz farga-1a36e6211674f1a39c7cc0ddff705357a4add65a.zip | |
basics: Add an example on floating instructions
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'basics/float.S')
| -rw-r--r-- | basics/float.S | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/basics/float.S b/basics/float.S new file mode 100644 index 0000000..f481f3e --- /dev/null +++ b/basics/float.S @@ -0,0 +1,40 @@ +.text +.globl greater_than_ten +.type greater_than_ten, @function + +// bool greater_than_ten(double a, uint64_t b); +greater_than_ten: + // Upon entry a -> fa0; and b -> a0. That is, the ABI increments the + //(f)a<n> register per argument type. + + // Change the rounding to "Rounds Towards Zero" (bits: 0b001). + li t0, 0b001 + fsrm t0 + + // Convert the second parameter to double. + fcvt.d.lu ft0, a0 + + // ret = a + b + fadd.d fa0, fa0, ft0 + + // ret = (ret * b) + b + fmv.d ft1, ft0 + fmadd.d fa0, fa0, ft0, ft1 + + // Now we need to compare the number with 10.0. There are multiple ways to + // load a floating point immediate: + // 1. Loading an integer immediate and then convert it into a double. This + // is discouraged on the RISC-V assembly manual. + // 2. Load a double with `fld` from a memory location on the data section. + // 3. Load a floating point immediate with `fli` (requires the `Zfa` + // extension). + // I don't have the `Zfa` extension on my board, so I'm opting for the + // second way. + fld ft1, .TEN, t0 + fle.d a0, ft1, fa0 + + ret + +.data +.TEN: + .double 10.0 |
