Skip to content

Commit 8a5ead5

Browse files
committed
Use 'dst' as the CBRANCH condition register
See issue tevador#43
1 parent 1276d67 commit 8a5ead5

10 files changed

+139
-174
lines changed

doc/program.asm

+58-58
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ randomx_isn_4:
1919
mov ecx, r13d
2020
ror r11, cl
2121
randomx_isn_5:
22-
; CBRANCH -1891017657, COND 15
23-
add r10, -1886823353
24-
test r10, 2139095040
22+
; CBRANCH r7, -1891017657, COND 15
23+
add r15, -1886823353
24+
test r15, 2139095040
2525
jz randomx_isn_0
2626
randomx_isn_6:
2727
; ISUB_M r3, L1[r7-1023302103]
@@ -41,10 +41,10 @@ randomx_isn_10:
4141
; FADD_R f0, a3
4242
addpd xmm0, xmm11
4343
randomx_isn_11:
44-
; CBRANCH -1981570318, COND 4
45-
add r8, -1981566222
46-
test r8, 1044480
47-
jz randomx_isn_6
44+
; CBRANCH r3, -1981570318, COND 4
45+
add r11, -1981566222
46+
test r11, 1044480
47+
jz randomx_isn_10
4848
randomx_isn_12:
4949
; FSUB_R f0, a1
5050
subpd xmm0, xmm9
@@ -55,9 +55,9 @@ randomx_isn_14:
5555
; FSQRT_R e2
5656
sqrtpd xmm6, xmm6
5757
randomx_isn_15:
58-
; CBRANCH -1278791788, COND 14
59-
add r11, -1278791788
60-
test r11, 1069547520
58+
; CBRANCH r5, -1278791788, COND 14
59+
add r13, -1278791788
60+
test r13, 1069547520
6161
jz randomx_isn_12
6262
randomx_isn_16:
6363
; ISUB_R r3, -1310797453
@@ -167,10 +167,10 @@ randomx_isn_42:
167167
; ISUB_R r4, r3
168168
sub r12, r11
169169
randomx_isn_43:
170-
; CBRANCH 335851892, COND 5
171-
add r13, 335847796
172-
test r13, 2088960
173-
jz randomx_isn_16
170+
; CBRANCH r6, 335851892, COND 5
171+
add r14, 335847796
172+
test r14, 2088960
173+
jz randomx_isn_25
174174
randomx_isn_44:
175175
; IADD_RS r7, r5, SHFT 3
176176
lea r15, [r15+r13*8]
@@ -194,10 +194,10 @@ randomx_isn_48:
194194
; IMUL_R r0, r5
195195
imul r8, r13
196196
randomx_isn_49:
197-
; CBRANCH -272659465, COND 15
198-
add r9, -272659465
199-
test r9, 2139095040
200-
jz randomx_isn_44
197+
; CBRANCH r2, -272659465, COND 15
198+
add r10, -272659465
199+
test r10, 2139095040
200+
jz randomx_isn_48
201201
randomx_isn_50:
202202
; ISTORE L1[r6+1414933948], r5
203203
lea eax, [r14d+1414933948]
@@ -212,9 +212,9 @@ randomx_isn_52:
212212
; FSCAL_R f1
213213
xorps xmm1, xmm15
214214
randomx_isn_53:
215-
; CBRANCH -2143810604, COND 1
216-
add r12, -2143810860
217-
test r12, 130560
215+
; CBRANCH r6, -2143810604, COND 1
216+
add r14, -2143810860
217+
test r14, 130560
218218
jz randomx_isn_50
219219
randomx_isn_54:
220220
; ISUB_M r3, L1[r1-649360673]
@@ -241,7 +241,7 @@ randomx_isn_58:
241241
; IADD_RS r4, r2, SHFT 1
242242
lea r12, [r12+r10*2]
243243
randomx_isn_59:
244-
; CBRANCH -704407571, COND 10
244+
; CBRANCH r6, -704407571, COND 10
245245
add r14, -704276499
246246
test r14, 66846720
247247
jz randomx_isn_54
@@ -263,9 +263,9 @@ randomx_isn_64:
263263
and eax, 2097144
264264
mov qword ptr [rsi+rax], r8
265265
randomx_isn_65:
266-
; CBRANCH -67701844, COND 5
267-
add r15, -67705940
268-
test r15, 2088960
266+
; CBRANCH r1, -67701844, COND 5
267+
add r9, -67705940
268+
test r9, 2088960
269269
jz randomx_isn_60
270270
randomx_isn_66:
271271
; IROR_R r3, r1
@@ -345,20 +345,20 @@ randomx_isn_86:
345345
mul r12
346346
mov r11, rdx
347347
randomx_isn_87:
348-
; CBRANCH -1821955951, COND 5
349-
add r8, -1821955951
350-
test r8, 2088960
351-
jz randomx_isn_66
348+
; CBRANCH r3, -1821955951, COND 5
349+
add r11, -1821955951
350+
test r11, 2088960
351+
jz randomx_isn_87
352352
randomx_isn_88:
353353
; FADD_R f2, a3
354354
addpd xmm2, xmm11
355355
randomx_isn_89:
356356
; IXOR_R r6, r3
357357
xor r14, r11
358358
randomx_isn_90:
359-
; CBRANCH -1780348372, COND 15
360-
add r9, -1784542676
361-
test r9, 2139095040
359+
; CBRANCH r4, -1780348372, COND 15
360+
add r12, -1784542676
361+
test r12, 2139095040
362362
jz randomx_isn_88
363363
randomx_isn_91:
364364
; IROR_R r4, 55
@@ -403,10 +403,10 @@ randomx_isn_102:
403403
; IXOR_R r4, r7
404404
xor r12, r15
405405
randomx_isn_103:
406-
; CBRANCH -607792642, COND 4
407-
add r11, -607792642
408-
test r11, 1044480
409-
jz randomx_isn_91
406+
; CBRANCH r7, -607792642, COND 4
407+
add r15, -607792642
408+
test r15, 1044480
409+
jz randomx_isn_99
410410
randomx_isn_104:
411411
; FMUL_R e1, a1
412412
mulpd xmm5, xmm9
@@ -484,10 +484,10 @@ randomx_isn_123:
484484
; FSQRT_R e2
485485
sqrtpd xmm6, xmm6
486486
randomx_isn_124:
487-
; CBRANCH -1807592127, COND 12
488-
add r15, -1806543551
489-
test r15, 267386880
490-
jz randomx_isn_104
487+
; CBRANCH r1, -1807592127, COND 12
488+
add r9, -1806543551
489+
test r9, 267386880
490+
jz randomx_isn_118
491491
randomx_isn_125:
492492
; IADD_RS r4, r4, SHFT 0
493493
lea r12, [r12+r12*1]
@@ -703,10 +703,10 @@ randomx_isn_180:
703703
and eax, 16376
704704
xor r15, qword ptr [rsi+rax]
705705
randomx_isn_181:
706-
; CBRANCH -759703940, COND 2
707-
add r14, -759704452
708-
test r14, 261120
709-
jz randomx_isn_144
706+
; CBRANCH r2, -759703940, COND 2
707+
add r10, -759704452
708+
test r10, 261120
709+
jz randomx_isn_175
710710
randomx_isn_182:
711711
; FADD_R f1, a2
712712
addpd xmm1, xmm10
@@ -781,9 +781,9 @@ randomx_isn_202:
781781
; FSUB_R f0, a0
782782
subpd xmm0, xmm8
783783
randomx_isn_203:
784-
; CBRANCH -1282235504, COND 2
785-
add r12, -1282234992
786-
test r12, 261120
784+
; CBRANCH r1, -1282235504, COND 2
785+
add r9, -1282234992
786+
test r9, 261120
787787
jz randomx_isn_182
788788
randomx_isn_204:
789789
; IMUL_M r1, L3[176744]
@@ -792,9 +792,9 @@ randomx_isn_205:
792792
; FSWAP_R e1
793793
shufpd xmm5, xmm5, 1
794794
randomx_isn_206:
795-
; CBRANCH -1557284726, COND 14
796-
add r10, -1555187574
797-
test r10, 1069547520
795+
; CBRANCH r0, -1557284726, COND 14
796+
add r8, -1555187574
797+
test r8, 1069547520
798798
jz randomx_isn_204
799799
randomx_isn_207:
800800
; IADD_M r3, L1[r0+72267507]
@@ -945,10 +945,10 @@ randomx_isn_246:
945945
mov rax, 9887096364157721599
946946
imul r12, rax
947947
randomx_isn_247:
948-
; CBRANCH -722123512, COND 2
949-
add r13, -722123512
950-
test r13, 261120
951-
jz randomx_isn_217
948+
; CBRANCH r3, -722123512, COND 2
949+
add r11, -722123512
950+
test r11, 261120
951+
jz randomx_isn_246
952952
randomx_isn_248:
953953
; ISMULH_R r7, r6
954954
mov rax, r15
@@ -977,7 +977,7 @@ randomx_isn_254:
977977
; FMUL_R e3, a2
978978
mulpd xmm7, xmm10
979979
randomx_isn_255:
980-
; CBRANCH -2007380935, COND 9
981-
add r9, -2007315399
982-
test r9, 33423360
983-
jz randomx_isn_248
980+
; CBRANCH r7, -2007380935, COND 9
981+
add r15, -2007315399
982+
test r15, 33423360
983+
jz randomx_isn_249

doc/specs.md

+10-19
Original file line numberDiff line numberDiff line change
@@ -598,51 +598,42 @@ There are 2 control instructions.
598598

599599
|frequency|instruction|dst|src|operation|
600600
|-|-|-|-|-|
601-
|1/256|CFROUND|`fprc`|R|`fprc = src >>> imm32`
602-
|16/256|CBRANCH|-|-|(conditional jump)
601+
|1/256|CFROUND|-|R|`fprc = src >>> imm32`
602+
|16/256|CBRANCH|R|-|`dst = dst + cimm`, conditional jump
603603

604604
#### 5.4.1 CFROUND
605605
This instruction calculates a 2-bit value by rotating the source register right by `imm32` bits and taking the 2 least significant bits (the value of the source register is unaffected). The result is stored in the `fprc` register. This changes the rounding mode of all subsequent floating point instructions.
606606

607607
#### 5.4.2 CBRANCH
608608

609-
This instruction performs a conditional jump in the Program Buffer. It uses an implicit integer register operand `creg`. This register is determined based on preceding instructions. For this purpose, the VM assigns each integer register two tag values:
609+
This instruction adds an immediate value `cimm` (constructed from `imm32`, see below) to the destination register and then performs a conditional jump in the Program Buffer based on the value of the destination register. The target of the jump is the instruction following the instruction when register `dst` was last modified.
610610

611-
* `lastUsed` - the index of the instruction when the register was last modified. The initial value at the start of each program iteration is `-1`, meaning the register is unmodified.
612-
* `count` - the number of times the register has been selected as the operand of a CBRANCH instruction. The initial value at the start of each program iteration is `0`.
613-
614-
A register is considered as modified by an instruction in the following cases:
611+
At the beginning of each program iteration, all registers are considered to be unmodified. A register is considered as modified by an instruction in the following cases:
615612

616613
* It is the destination register of an integer instruction except IMUL_RCP and ISWAP_R.
617614
* It is the destination register of IMUL_RCP and `imm32` is not zero or a power of 2.
618615
* It is the source or the destination register of ISWAP_R and the destination and source registers are distinct.
619616
* The CBRANCH instruction is considered to modify all integer registers.
620617

621-
There are 3 rules for the selection of the `creg` register, evaluated in this order:
622-
623-
1. The register with the lowest value of `lastUsed` tag is selected.
624-
1. In case multiple registers have the same value of the `lastUsed` tag, the register with the lowest value of the `count` tag is selected from them.
625-
1. In case multiple registers have the same values of both `lastUsed` and `count` tags, the register with the lowest index is selected (`r0` before `r1` etc.) from them.
626-
627-
Whenever a register is selected as the operand of a CBRANCH instruction, its `count` tag is increased by 1.
618+
If register `dst` has not been modified yet, the jump target is the first instruction in the Program Buffer.
628619

629620
The CBRANCH instruction performs the following steps:
630621

631622
1. A constant `b` is calculated as `mod.cond + RANDOMX_JUMP_OFFSET`.
632623
1. A constant `cimm` is constructed as sign-extended `imm32` with bit `b` set to 1 and bit `b-1` set to 0 (if `b > 0`).
633-
1. `cimm` is added to `creg`.
634-
1. If bits `b` to `b + RANDOMX_JUMP_BITS - 1` of `creg` are zero, execution jumps to instruction `creg.lastUsed + 1` (the instruction following the instruction where `creg` was last modified).
624+
1. `cimm` is added to the destination register.
625+
1. If bits `b` to `b + RANDOMX_JUMP_BITS - 1` of the destination register are zero, the jump is executed (target is the instruction following the instruction where `dst` was last modified).
635626

636627
Bits in immediate and register values are numbered from 0 to 63 with 0 being the least significant bit. For example, for `b = 10` and `RANDOMX_JUMP_BITS = 8`, the bits are arranged like this:
637628

638629
```
639630
cimm = SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSMMMMMMMMMMMMMMMMMMMMM10MMMMMMMMM
640-
creg = ..............................................XXXXXXXX..........
631+
dst = ..............................................XXXXXXXX..........
641632
```
642633

643-
`S` is a copied sign bit from `imm32`. `M` denotes bits of `imm32`. The 9th bit is set to 0 and the 10th bit is set to 1. This value would be added to `creg`.
634+
`S` is a copied sign bit from `imm32`. `M` denotes bits of `imm32`. The 9th bit is set to 0 and the 10th bit is set to 1. This value will be added to `dst`.
644635

645-
The second line uses `X` to mark bits of `creg` that would be checked by the condition. If all these bits are 0 after adding `cimm`, the jump is executed.
636+
The second line uses `X` to mark bits of `dst` that will be checked by the condition. If all these bits are 0 after adding `cimm`, the jump is executed.
646637

647638
The construction of the CBRANCH instruction ensures that no inifinite loops are possible in the program.
648639

0 commit comments

Comments
 (0)