Skip to content

Commit 504792e

Browse files
Jerin Jacobborkmann
authored andcommitted
arm64: bpf: optimize modulo operation
Optimize modulo operation instruction generation by using single MSUB instruction vs MUL followed by SUB instruction scheme. Signed-off-by: Jerin Jacob <jerinj@marvell.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 53092f7 commit 504792e

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

‎arch/arm64/net/bpf_jit.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@
171171
/* Rd = Ra + Rn * Rm */
172172
#define A64_MADD(sf, Rd, Ra, Rn, Rm) aarch64_insn_gen_data3(Rd, Ra, Rn, Rm, \
173173
A64_VARIANT(sf), AARCH64_INSN_DATA3_MADD)
174+
/* Rd = Ra - Rn * Rm */
175+
#define A64_MSUB(sf, Rd, Ra, Rn, Rm) aarch64_insn_gen_data3(Rd, Ra, Rn, Rm, \
176+
A64_VARIANT(sf), AARCH64_INSN_DATA3_MSUB)
174177
/* Rd = Rn * Rm */
175178
#define A64_MUL(sf, Rd, Rn, Rm) A64_MADD(sf, Rd, A64_ZR, Rn, Rm)
176179

‎arch/arm64/net/bpf_jit_comp.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
409409
break;
410410
case BPF_MOD:
411411
emit(A64_UDIV(is64, tmp, dst, src), ctx);
412-
emit(A64_MUL(is64, tmp, tmp, src), ctx);
413-
emit(A64_SUB(is64, dst, dst, tmp), ctx);
412+
emit(A64_MSUB(is64, dst, dst, tmp, src), ctx);
414413
break;
415414
}
416415
break;
@@ -516,8 +515,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
516515
case BPF_ALU64 | BPF_MOD | BPF_K:
517516
emit_a64_mov_i(is64, tmp2, imm, ctx);
518517
emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
519-
emit(A64_MUL(is64, tmp, tmp, tmp2), ctx);
520-
emit(A64_SUB(is64, dst, dst, tmp), ctx);
518+
emit(A64_MSUB(is64, dst, dst, tmp, tmp2), ctx);
521519
break;
522520
case BPF_ALU | BPF_LSH | BPF_K:
523521
case BPF_ALU64 | BPF_LSH | BPF_K:

0 commit comments

Comments
 (0)