Calling Convention(rv手册18章)有的没的笔记

C compiler datatypes for base RISC-V ISA

在这里插入图片描述

RVG Calling Convention

When possible,参数会放在a0-a7或者fa0-fa7里。

这里没看懂
If the arguments to a function are conceptualized as fields of a C struct, each with pointer alignment, the argument registers are a shadow of the first eight pointer-words of that struct.

  1. 小于指针字 (pointer-word) 的参数被放到寄存器的最低有效位中。假如参数被送进栈中,则会被放到指针字的低地址中。

  2. 假如参数是指针字的两倍。

    • 如果被放进栈中,这个参数是自然对齐的(they are naturally aligned. 8字节的数据起始地址应该是8的倍数);
    • 如果被放进整数寄存器中,会存进奇偶两个对齐的寄存器中,偶数保存最低有效位。比如 In RV32, for example, the function void foo(int, long long) is passed its first argument in a0 and its second in a2 and a3. Nothing is passed in a1.
  3. 超过指针字大小两倍的参数通过引用传递。

  4. 结构体没被传进寄存器的部分会被放在栈中,堆栈指针sp指向第一个没被传到寄存器里的参数。

  5. 返回值放在a0、a1或者fa0、fa1里。

    • 当且仅当浮点数是基本数据类型(float、double、long double)或者作为只有一两个浮点数值的结构体的成员时,才会放在浮点寄存器中返回。
    • Other return values that fit into two pointer-words are returned in a0 and a1.
    • Larger return values are passed entirely in memory; the caller allocates this memory region and passes a pointer to it as an implicit first parameter to the callee.
  6. 在标准RISCV调用约定中,栈向下增长并且堆栈指针sp总是16字节对齐的。

    • t0-t6和ft0-ft11是临时寄存器,在调用过程中易失。must be saved by the caller if later used。
    • s0-s11和fs0-fs11在调用期间被保存,must be saved by the callee if used.
  7. Table 18.2 indicates the role of each integer and floating-point register in the calling convention.
    在这里插入图片描述

Soft-Float Calling Convention

The soft-float calling convention is used on RV32 and RV64 implementations that lack floatingpoint hardware. It avoids all use of instructions in the F, D, and Q standard extensions, and hence the f registers.

  • Integral arguments are passed and returned in the same manner as the RVG convention, and the stack discipline is the same.
  • Floating-point arguments are passed and returned in integer registers, using the rules for integer arguments of the same size.
  • In RV32, for example, the function double foo(int, double, long double) is passed its first argument in a0, its second argument in a2 and a3, and its third argument by reference via a4; its result is returned in a0 and a1.
  • In RV64, the arguments are passed in a0, a1, and the a2-a3 pair, and the result is returned in a0.

The dynamic rounding mode and accrued exception flags are accessed through the routines provided by the C99 header fenv.h.
完全不懂什么是dynamic rounding mode和accrued exception flags,要好好学习一个。

这句话的意思是:在 C99 标准中,你可以通过 fenv.h 头文件中提供的函数来访问和控制浮点运算的动态舍入模式和累积的异常标志。这些功能允许程序在运行时灵活地调整浮点运算的行为和处理异常。

在 C99 标准中,fenv.h 头文件提供了一组函数和宏,用于控制和查询浮点环境。以下是对动态舍入模式和累积异常标志的详细解释:

动态舍入模式

  • 舍入模式决定了浮点运算结果如何舍入到目标精度。

  • C99 提供的函数可以在运行时更改舍入模式,例如:

    • FE_TONEAREST: 向最近的值舍入(默认模式)。
    • FE_DOWNWARD: 向负无穷方向舍入。
    • FE_UPWARD: 向正无穷方向舍入。
    • FE_TOWARDZERO: 向零方向舍入。
  • 相关函数:

    • fesetround(int round): 设置当前舍入模式。
    • fegetround(void): 获取当前舍入模式。

累积异常标志

  • 异常标志表示在浮点运算中发生的特定异常,例如:

    • FE_DIVBYZERO: 除以零。
    • FE_OVERFLOW: 上溢。
    • FE_UNDERFLOW: 下溢。
    • FE_INVALID: 无效操作。
    • FE_INEXACT: 非精确结果。
  • 相关函数:

    • feclearexcept(int excepts): 清除指定异常标志。
    • feraiseexcept(int excepts): 引发指定异常。
    • fetestexcept(int excepts): 测试哪些异常标志被设置。

通过这些函数,程序可以在运行时动态调整浮点运算的行为,并对异常进行检测和处理,从而增强程序的数值稳定性和可靠性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值