In risc-v, beq instruction has 12bits of imm[12:1]
.
And PC-relative addressing is Target address = PC + immediate x 2
It says the reason of the multiply with 2
is for the half word instruction.
So, I think the immediate value can represent the range of [-2^12 ~ 2^12-2]
,
because of the hidden bit in imm[0] == 0
.
And multiplying with 2 makes the range as [-2^13 ~ 2^13-2]
.
So, when we think in 2 byte increments instruction, the branch can reach in range of
-2^12 ~ 2^12-2
and in 4 byte increments instruction, the branch can reach in range of
-2^11 ~ 2^11-2
.
But in risc-v org page 29 https://riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf,
it says the conditional branch range is -4KiB ~ 4KiB
,
which means -2^10 ~ 2^10
in 32-bit instructions.
Q1. In PC relative addressing, Is it right we use 13bits result (with hidden 0bit) in calculation?
Q2. If it is right, how can I calculate the range of the branch instruction?