使用单个 aarch64 指令获取余数? [英] Obtaining remainder using single aarch64 instruction?

查看:37
本文介绍了使用单个 aarch64 指令获取余数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 ARM8 (aarch64) 编写一些汇编代码.我想做一个除法并使用获得的余数进行进一步计算.在 x86 中,当我使用'div',我知道我的剩余部分保存在 RDX 中.我的问题是 - 是否有与 aarch64 指令集中相同的指令?我知道 'udiv' 和 'sdiv' 做无符号和有符号的除法,并得到商数.是否有一条指令可以给我余数?(我想要 c 中的 % 模运算符).我知道我可以使用代数获得它,只是想确认我没有错过更简单的方法.

I am writing some assembly code for ARM8 (aarch64). I want to do a division and use the remainder obtained for further calculations. In x86 when I use 'div', and I know my remainder is kept in RDX. My question is - is there an equivalent to that in the aarch64 instruction set? I know 'udiv' and 'sdiv' do unsigned and signed divisions and gets me the quotient. Is there a single instruction which would give me remainder? (I want % modulo operator in c). I understand I can obtain it using algebra, just wanted to confirm I am not missing out on a simpler method.

推荐答案

除了可以优化为 and 的恒定 2 次幂除数之外,没有计算余数的指令的一个部门.但是,您可以非常巧妙地将其分为两部分:

Barring constant power-of-two divisors which can be optimised down to an and, there is no instruction that will calculate the remainder of a division. You can, however do it pretty neatly in two:

// input: x0=dividend, x1=divisor
udiv x2, x0, x1
msub x3, x2, x1, x0
// result: x2=quotient, x3=remainder

这篇关于使用单个 aarch64 指令获取余数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆