llvm Bitwise Binary Operations & Conversion Operations

1、‘shl’ Instruction
The ‘shl’ instruction returns the first operand shifted to the left a specified number of bits.
op1是需要左移的数,左移的位数是op2,value = op1 * 2op2 mod 2n,如果左移的位数大于op1本身的bit数,将会报错。

<result> = shl <ty> <op1>, <op2>           ; yields ty:result
<result> = shl nuw <ty> <op1>, <op2>       ; yields ty:result
<result> = shl nsw <ty> <op1>, <op2>       ; yields ty:result
<result> = shl nuw nsw <ty> <op1>, <op2>   ; yields ty:result
If the nuw keyword is present, then the shift produces a poison value if it shifts out any non-zero bits. 
nuw关键字:移位中,将非零值移出去,报错
If the nsw keyword is present, then the shift produces a poison value if it shifts out any bits that disagree with the resultant sign bit.
nsw关键字:经过移位后的数的结果,与目标符号位不一致报错。

example

<result> = shl i32 4, %var   ; yields i32: 4 << %var
<result> = shl i32 4, 2      ; yields i32: 16
<result> = shl i32 1, 10     ; yields i32: 1024
<result> = shl i32 1, 32     ; undefined
<result> = shl <2 x i32> < i32 1, i32 1>, < i32 1, i32 2>   ; yields: result=<2 x i32> < i32 2, i32 4>

2、‘lshr’ Instruction

The ‘lshr’ instruction (logical shift right) returns the first operand shifted to the right a specified number of bits with zero fill.
逻辑右移返回数:op1向右移动op2 个bit数,高位补零。

<result> = lshr <ty> <op1>, <op2>         ; yields ty:result
<result> = lshr exact <ty> <op1>, <op2>   ; yields ty:result
If the exact keyword is present, the result value of the lshr is a poison value if any of the bits shifted out are non-zero.
exact关键字:移位中,将非零值移出去,报错

example

<result> = lshr i32 4, 1   ; yields i32:result = 2
<result> = lshr i32 4, 2   ; yields i32:result = 1
<result> = lshr i8  4, 3   ; yields i8:result = 0
<result> = lshr i8 -2, 1   ; yields i8:result = 0x7F
<result> = lshr i32 1, 32  ; undefined

3、‘ashr’ Instruction

The ‘ashr’ instruction (arithmetic shift right) returns the first operand shifted to the right a specified number of bits with sign extension.
算术右移返回数:op1向右移动op2 个bit数,高位补符号位。

<result> = ashr <ty> <op1>, <op2>         ; yields ty:result
<result> = ashr exact <ty> <op1>, <op2>   ; yields ty:result
If the exact keyword is present, the result value of the ashr is a poison value if any of the bits shifted out are non-zero.
exact关键字:移位中,将非零值移出去,报错

example

<result> = ashr i32 4, 1   ; yields i32:result = 2
<result> = ashr i32 4, 2   ; yields i32:result = 1
<result> = ashr i8  4, 3   ; yields i8:result = 0
<result> = ashr i8 -2, 1   ; yields i8:result = -1
<result> = ashr i32 1, 32  ; undefined

ps:
算术左移和算术右移主要用来进行有符号数的倍增、减半。
逻辑左移和逻辑右移主要用来进行无符号数的倍增、减半。

二 : Conversion Operations 转换指令

1、‘trunc … to’ Instruction

<result> = trunc <ty> <value> to <ty2> ; yields ty2
The ‘trunc’ instruction truncates its operand to the type ty2.
将value由ty截断为ty2

'trunc’指令,截断value中的高位,并将剩余的位转换为ty2。 由于源大小必须大于目标大小,因此trunc不能是无操作转换。 它总是会截断bit。

example

%X = trunc i32 257 to i8                        ; yields i8:1
%Y = trunc i32 123 to i1                        ; yields i1:true
%Z = trunc i32 122 to i1                        ; yields i1:false
%W = trunc <2 x i16> <i16 8, i16 7> to <2 x i8> ; yields <i8 8, i8 7>

2、‘zext … to’ Instruction

<result> = zext <ty> <value> to <ty2> ; yields ty2
The ‘zext’ instruction zero extends its operand to type ty2.
'zext’指令将type为ty 的 value扩展为类型ty2。
zext用零位填充值的高位,直到达到目标类型ty2的大小。

example

%X = zext i32 257 to i64              ; yields i64:257
%Y = zext i1 true to i32              ; yields i32:1
%Z = zext <2 x i16> <i16 8, i16 7> to <2 x i32> ; yields <i32 8, i32 7>

3、‘sext … to’ Instruction

<result> = sext <ty> <value> to <ty2> ; yields ty2

'sext’符号将值扩展为ty2类型。

'sext’指令通过复制值的符号位(最高位)直到达到类型ty2的位大小来执行符号扩展。

当符号从i1延伸时,扩展名总是导致-1或0。

example

%X = sext i8  -1 to i16              ; yields i16   :65535
%Y = sext i1 true to i32             ; yields i32:-1
%Z = sext <2 x i16> <i16 8, i16 7> to <2 x i32> ; yields <i32 8, i32 7>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值