Merged
float类型性能优化 #7121
float类型性能优化 #7121
Merged
xuyiyangAdd on 8 days ago
xuyiyang
xuyiyangCollaborators
8 days ago

描述

本次变更针对 impl/simt_api/math_functions_impl.h 中的浮点数学函数进行性能优化,主要消减 atan2f 和 rnorm4df 中的分支路径。atan2f 由条件分支 + 多路选择重构为无分支的共享计算流程,统一用 base + sgn * t 的 __fma 形式还原象限;rnorm4df 将特殊值(inf/NaN/零范数)的提前返回改为计算完成后的结果覆盖,减少提前退出的分支。

主要改动

  • atan2f 去除零分母分支:引入 safe_hi = nz ? hi : 1.0f,使比值 r = lo / safe_hi 与多项式求值始终执行,避免 hi == 0 时的 0/0 路径发散,异常结果由后续选择丢弃。
    无理输入的特殊值处理改为选择:原先对 inf/inf 产生的 NaN 与 |y| == |x| 对角线的特判,改为 t = (t != t) ? pi/4 : t,使仿射公式自然产出 pi/4 与 3pi/4。
  • 象限还原合并为单次 fma:用 xneg = __float_as_uint(x) >> 31 提取符号,按 y_gt_x 与 xneg 选定 base 和 sgn,以 __fma(sgn, t, base) 替换原先三分支的角度拼接。
    零幅值与符号处理重构:hi == 0 时通过 a0 选择 pi、0 或 pi/2,由 nz 决定最终取值;y 的符号改用 __float_as_uint(y) >> 31 按位判断,NaN 仍通过 ax + ay 传播。
  • rnorm4df 特殊值判断后移:删除入口处的 isinf/isnan 提前返回与零范数提前返回,改为在完成倒数范数计算与一次 Newton 迭代后,依次用 ASCRT_INF_F、ASCRT_NAN_F、0.0f 覆盖 result。

关联的Issue

https://gitcode.com/cann/asc-devkit/issues/1782

测试

接口 修改前 修改后
float atan2f(float y, float x) --cce-use-fast-math=true: 233
--cce-use-fast-math=false: 847
--cce-use-fast-math=true: 127
--cce-use-fast-math=false: 820.62
float rnorm4df(float a, float b, float c, float d) --cce-use-fast-math=true: 215
--cce-use-fast-math=false: 544
--cce-use-fast-math=true: 147
--cce-use-fast-math=false: 367

文档更新

impl/simt_api/math_functions_impl.h

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for xuyiyang's contribution.)
xuyiyangxuyiyangCollaborators
8 days ago Created pull request, commit 4c63fadd
atomgit-bot
atomgit-bot
8 days ago Comments:

变更摘要

本次变更针对 impl/simt_api/math_functions_impl.h 中的浮点数学函数进行性能优化,主要消减 atan2f 和 rnorm4df 中的分支路径。atan2f 由条件分支 + 多路选择重构为无分支的共享计算流程,统一用 base + sgn * t 的 __fma 形式还原象限;rnorm4df 将特殊值(inf/NaN/零范数)的提前返回改为计算完成后的结果覆盖,减少提前退出的分支。

主要改动

  • atan2f 去除零分母分支:引入 safe_hi = nz ? hi : 1.0f,使比值 r = lo / safe_hi 与多项式求值始终执行,避免 hi == 0 时的 0/0 路径发散,异常结果由后续选择丢弃。
  • 无理输入的特殊值处理改为选择:原先对 inf/inf 产生的 NaN 与 |y| == |x| 对角线的特判,改为 t = (t != t) ? pi/4 : t,使仿射公式自然产出 pi/4 与 3pi/4。
  • 象限还原合并为单次 fma:用 xneg = __float_as_uint(x) >> 31 提取符号,按 y_gt_x 与 xneg 选定 base 和 sgn,以 __fma(sgn, t, base) 替换原先三分支的角度拼接。
  • 零幅值与符号处理重构:hi == 0 时通过 a0 选择 pi、0 或 pi/2,由 nz 决定最终取值;y 的符号改用 __float_as_uint(y) >> 31 按位判断,NaN 仍通过 ax + ay 传播。
  • rnorm4df 特殊值判断后移:删除入口处的 isinf/isnan 提前返回与零范数提前返回,改为在完成倒数范数计算与一次 Newton 迭代后,依次用 ASCRT_INF_F、ASCRT_NAN_F、0.0f 覆盖 result。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
8 days ago Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
8 days ago Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
8 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
8 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
impl/simt_api ✅ wulinyu, louyuxuan (2/2) ✅ wulinyu (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

xuyiyang0324, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
8 days ago Set zengjuan,zhanghao_0689,suqwe,wulinyu,kong0808,dengjing_aoe,YeZZzzz1,bluesky901,yangbinhua,zhudi22,chenyiyuan,xun_zhuge,XuebinYang,sjtulxh,bianque,houyuyang,Lycheeeee,YANXI_ZHAO,chentianyu19,shi_nanxiang,wuyang_hw,jiangxinyu3,ascendhjh,hujiawen_kaven,wangxiaozhi,DragonBornHD84,louyuxuan,hyolee,yukangchen as approver
CANN-robotCANN-robotCollaborators
8 days ago Set wulinyu,wangxiaozhi,louyuxuan as approver
xuyiyangxuyiyangCollaborators
8 days ago Modify description of pull request
xuyiyangxuyiyangCollaborators
8 days ago Modify description of pull request
xuyiyangxuyiyangCollaborators
8 days ago Modify description of pull request
xuyiyang
xuyiyangCollaborators
8 days ago Comments:

compile

likedislike
xuyiyangxuyiyangCollaborators
8 days ago Pre merge succeeded (commit_id: 621588184a5d25ab6dfb6af99ccdb51ac029f166)
CANN-robot
CANN-robotCollaborators
8 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
8 days ago Added label: ci-pipeline-running
6 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
8 days ago Removed label: ci-pipeline-passed
CANN-robot
CANN-robotCollaborators
8 days ago Comments:

Notification

This pull request has been changed(code update) or closed, so removes the following label(s): ci-pipeline-passed.

likedislike
CANN-robotCANN-robotCollaborators
8 days ago Added label: cann-cla/yes
xuyiyang
xuyiyangCollaborators
8 days ago Comments:

compile

likedislike
xuyiyangxuyiyangCollaborators
8 days ago Pre merge succeeded (commit_id: 49164c9693411b520200a9af4904a074f45c76b1)
CANN-robot
CANN-robotCollaborators
8 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
8 days ago Added label: ci-pipeline-running
6 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
7 days ago Removed label: ci-pipeline-passedstat/needs-squash
CANN-robot
CANN-robotCollaborators
7 days ago Comments:

Notification

This pull request has been changed(code update) or closed, so removes the following label(s): ci-pipeline-passed, stat/needs-squash.

likedislike
CANN-robotCANN-robotCollaborators
7 days ago Added label: cann-cla/yes
xuyiyang
xuyiyangCollaborators
7 days ago Comments:

compile

likedislike
xuyiyangxuyiyangCollaborators
7 days ago Pre merge succeeded (commit_id: 708e941253c54b2db6159780e734678cd97a6f73)
CANN-robot
CANN-robotCollaborators
7 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
7 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
7 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
7 days ago Added label: ci-pipeline-passed
louyuxuanCollaborators
7 days ago Comments:

/lgtm

likedislike
wulinyuCollaborators
7 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
7 days ago Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
7 days ago Merged pull request