1.机器特征寄存器CSR_MISA
????????通过该寄存器可以查询当前处理器是否支持单精度浮点计算,是否支持双精度浮点计算,只要支持其中之一,就表示当前处理器支持浮点计算。
csrr t0, CSR_MISA
andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)
beqz t0, out //直接跳出表示机器硬件不支持浮点计算单元
/*此处代码,表示机器硬件支持浮点计算单元*/
out:
2.机器状态寄存器CSR_STATUS
如果机器硬件上支持浮点计算单元,通过该寄存器可以启用或者关闭浮点计算功能。其中,设置相应位,就是启动浮点计算功能;清零相应位表示关闭浮点计算功能
/* 启用浮点计算功能 */
li t0, SR_FS
csrs CSR_STATUS, t0
/* 关闭浮点计算功能 */
li t0, SR_FS
csrc CSR_STATUS, t0
3. 浮点计算单元
- 浮点控制状态寄存器fcsr
- 32个浮点寄存器f0/f1/.../f31
4.上下文保存
保存
ENTRY(__fstate_save)
li a2, TASK_THREAD_F0
add a0, a0, a2 //此时, a0是线程信息结构首地址
li t1, SR_FS
csrs CSR_STATUS, t1 //确保浮点计算单元启用
frcsr t0 //将浮点控制状态寄存器复制到通用寄存器t0中
fsd f0, TASK_THREAD_F0_F0(a0) //先在线程信息结构体中保存32个浮点寄存器
fsd f1, TASK_THREAD_F1_F0(a0)
fsd f2, TASK_THREAD_F2_F0(a0)
fsd f3, TASK_THREAD_F3_F0(a0)
fsd f4, TASK_THREAD_F4_F0(a0)
fsd f5, TASK_THREAD_F5_F0(a0)
fsd f6, TASK_THREAD_F6_F0(a0)
fsd f7, TASK_THREAD_F7_F0(a0)
fsd f8, TASK_THREAD_F8_F0(a0)
fsd f9, TASK_THREAD_F9_F0(a0)
fsd f10, TASK_THREAD_F10_F0(a0)
fsd f11, TASK_THREAD_F11_F0(a0)
fsd f12, TASK_THREAD_F12_F0(a0)
fsd f13, TASK_THREAD_F13_F0(a0)
fsd f14, TASK_THREAD_F14_F0(a0)
fsd f15, TASK_THREAD_F15_F0(a0)
fsd f16, TASK_THREAD_F16_F0(a0)
fsd f17, TASK_THREAD_F17_F0(a0)
fsd f18, TASK_THREAD_F18_F0(a0)
fsd f19, TASK_THREAD_F19_F0(a0)
fsd f20, TASK_THREAD_F20_F0(a0)
fsd f21, TASK_THREAD_F21_F0(a0)
fsd f22, TASK_THREAD_F22_F0(a0)
fsd f23, TASK_THREAD_F23_F0(a0)
fsd f24, TASK_THREAD_F24_F0(a0)
fsd f25, TASK_THREAD_F25_F0(a0)
fsd f26, TASK_THREAD_F26_F0(a0)
fsd f27, TASK_THREAD_F27_F0(a0)
fsd f28, TASK_THREAD_F28_F0(a0)
fsd f29, TASK_THREAD_F29_F0(a0)
fsd f30, TASK_THREAD_F30_F0(a0)
fsd f31, TASK_THREAD_F31_F0(a0)
sw t0, TASK_THREAD_FCSR_F0(a0) //在线程信息结构体中,保存浮点控制状态寄存器的值
csrc CSR_STATUS, t1 //关闭浮点计算单元
ret
ENDPROC(__fstate_save)
?恢复
ENTRY(__fstate_restore)
li a2, TASK_THREAD_F0
add a0, a0, a2
li t1, SR_FS
lw t0, TASK_THREAD_FCSR_F0(a0)
csrs CSR_STATUS, t1 //启用浮点计算单元
fld f0, TASK_THREAD_F0_F0(a0) //恢复32个浮点寄存器
fld f1, TASK_THREAD_F1_F0(a0)
fld f2, TASK_THREAD_F2_F0(a0)
fld f3, TASK_THREAD_F3_F0(a0)
fld f4, TASK_THREAD_F4_F0(a0)
fld f5, TASK_THREAD_F5_F0(a0)
fld f6, TASK_THREAD_F6_F0(a0)
fld f7, TASK_THREAD_F7_F0(a0)
fld f8, TASK_THREAD_F8_F0(a0)
fld f9, TASK_THREAD_F9_F0(a0)
fld f10, TASK_THREAD_F10_F0(a0)
fld f11, TASK_THREAD_F11_F0(a0)
fld f12, TASK_THREAD_F12_F0(a0)
fld f13, TASK_THREAD_F13_F0(a0)
fld f14, TASK_THREAD_F14_F0(a0)
fld f15, TASK_THREAD_F15_F0(a0)
fld f16, TASK_THREAD_F16_F0(a0)
fld f17, TASK_THREAD_F17_F0(a0)
fld f18, TASK_THREAD_F18_F0(a0)
fld f19, TASK_THREAD_F19_F0(a0)
fld f20, TASK_THREAD_F20_F0(a0)
fld f21, TASK_THREAD_F21_F0(a0)
fld f22, TASK_THREAD_F22_F0(a0)
fld f23, TASK_THREAD_F23_F0(a0)
fld f24, TASK_THREAD_F24_F0(a0)
fld f25, TASK_THREAD_F25_F0(a0)
fld f26, TASK_THREAD_F26_F0(a0)
fld f27, TASK_THREAD_F27_F0(a0)
fld f28, TASK_THREAD_F28_F0(a0)
fld f29, TASK_THREAD_F29_F0(a0)
fld f30, TASK_THREAD_F30_F0(a0)
fld f31, TASK_THREAD_F31_F0(a0)
fscsr t0 //恢复浮点控制状态寄存器
csrc CSR_STATUS, t1 //关闭浮点计算功能
ret
ENDPROC(__fstate_restore)
|