ARM asm 中的快速饱和和移位两个半字
我在 32 位字中有两个带符号的 16 位值,我需要将它们右移(除)常数值(可以是从 1 到 6)并饱和到字节(0..0xFF)。
例如,
- 0x FFE1 00AA 与shift=5 必须变为0x 0000 0005;
- 0x 2345 1234 必须变成 0x 00FF 0091
我试图同时使这些值饱和,就像这样的伪代码:
AND RT, R0, 0x80008000; - mask high bits to get negatives
ORR RT, RT, LSR #1
ORR RT, RT, LSR #2
ORR RT, RT, LSR #4
ORR RT, RT, LSR #8; - now its expanded signs in each halfword
MVN RT, RT
AND R0, RT; now negative values are zero
; here something to saturate high overflow and shift after
但我得到的代码非常丑陋且缓慢。 :) 我现在拥有的最好(最快)的东西是每一半的单独饱和,如下所示:
MOV RT, R0, LSL #16
MOVS RT, RT, ASR #16+5
MOVMI RT, #0
CMP RT, RT, #256
MOVCS RT, #255
MOVS R0, R0, ASR #16+5
MOVMI R0, #0
CMP R0, R0, #256
MOVCS R0, #255
ORR R0, RT, R0, LSL #16
但它是 10 个周期。 :( 可以更快吗?
ps: 后来我找到了 USAT16 指令,但它仅适用于 ARMv6。我需要代码才能在 ARMv5TE 和 ARMv4 上工作。
编辑:现在我重写了我的第一个代码:
ANDS RT, 0x10000, R0 << 1; // 0x10000 is in register. Sign (HI) moves to C flag, Sign (LO) is masked
SUBNE RT, RT, 1; // Mask LO with 0xFFFF if it's negative
SUBCS RT, RT, 0x10000; // Mask HI with 0xFFFF if it's negative
BIC R0, R0, RT; // Negatives are 0 now. The mask can be used as XOR too
TST R0, 0xE0000000; // check HI overflow
ORRNE R0, R0, 0x1FE00000 // set HI to 0xFF (shifted) if so
TST R0, 0x0000E000 // check LO overflow
ORRNE R0, R0, 0x00001FE0 // set LO to 0xFF if so
AND R0, 0x00FF00FF, R0 >> 5; // 0x00FF00FF is in register
但它并不漂亮。
I have two signed 16-bit values in a 32-bit word, and I need to shift them right (divide) on constant value (it can be from 1 to 6) and saturate to byte (0..0xFF).
For example,
- 0x FFE1 00AA with shift=5 must become 0x 0000 0005;
- 0x 2345 1234 must become 0x 00FF 0091
I'm trying to saturate the values simultaneously, something like this pseudo-code:
AND RT, R0, 0x80008000; - mask high bits to get negatives
ORR RT, RT, LSR #1
ORR RT, RT, LSR #2
ORR RT, RT, LSR #4
ORR RT, RT, LSR #8; - now its expanded signs in each halfword
MVN RT, RT
AND R0, RT; now negative values are zero
; here something to saturate high overflow and shift after
but code I get is very ugly and slow. :)
The best (fastest) thing I have now is separate saturation of each half, like this:
MOV RT, R0, LSL #16
MOVS RT, RT, ASR #16+5
MOVMI RT, #0
CMP RT, RT, #256
MOVCS RT, #255
MOVS R0, R0, ASR #16+5
MOVMI R0, #0
CMP R0, R0, #256
MOVCS R0, #255
ORR R0, RT, R0, LSL #16
But it's 10 cycles. :( Can it be faster?
p.s.: Later I found USAT16 instruction for this, but it's only for ARMv6. And I need code to work on ARMv5TE and ARMv4.
Edit: now I rewrite my first code:
ANDS RT, 0x10000, R0 << 1; // 0x10000 is in register. Sign (HI) moves to C flag, Sign (LO) is masked
SUBNE RT, RT, 1; // Mask LO with 0xFFFF if it's negative
SUBCS RT, RT, 0x10000; // Mask HI with 0xFFFF if it's negative
BIC R0, R0, RT; // Negatives are 0 now. The mask can be used as XOR too
TST R0, 0xE0000000; // check HI overflow
ORRNE R0, R0, 0x1FE00000 // set HI to 0xFF (shifted) if so
TST R0, 0x0000E000 // check LO overflow
ORRNE R0, R0, 0x00001FE0 // set LO to 0xFF if so
AND R0, 0x00FF00FF, R0 >> 5; // 0x00FF00FF is in register
but it isn't beautiful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所拥有的与解决所述问题所需要做的差不多。如果您在紧密循环中对大量数据执行此操作,并且可以负担一些寄存器来保存掩码,那么您可能可以节省一两个周期,但这不会是一个很大的改进。在 v6 架构之前,ARM 上对这种类型的“小向量”饱和运算没有很好的支持。
基本上,除非这是程序中唯一的瓶颈,否则是时候把它放在一边并转向下一个热点了。
What you have is about as good as you're going to do for the problem as stated. If you're doing this for a lot of data in a tight loop, and can afford a few registers to hold masks, you may be able to save a cycle or two, but it's not going to be a big improvement. There just isn't great support for this type of "small-vector" saturation operation on ARM before the v6 architecture.
Basically, unless this is the only bottleneck in your program, it's time to put this away and move on to the next hotspot.
使用一次检查来为两项操作设置标志是一个好主意。但第二部分我做不到。我可以做其他事情:) 这是通用变体,可用于从 1 到 6 的任何转变:
所以现在是 7 个周期。 :)
可以更好吗?
编辑: 看起来溢出很罕见,所以最好添加如下内容:
It was a good idea to use one check to set flags for two operations. But I can't do it for second part. I can do something else :) Here is universal variant to use with any shift from 1 to 6:
So it's 7 cycles now. :)
Can it be better?
Edit: looks like overflows are rare enough, so it's a good idea to add something like this: