case BPF_JMP32 | BPF_JSGE | BPF_K:
        case BPF_JMP | BPF_JSLE | BPF_K:
        case BPF_JMP32 | BPF_JSLE | BPF_K:
-       case BPF_JMP | BPF_JSET | BPF_K:
-       case BPF_JMP32 | BPF_JSET | BPF_K:
                rvoff = rv_offset(i, off, ctx);
                s = ctx->ninsns;
                if (imm) {
 
                /* Adjust for extra insns */
                rvoff -= (e - s) << 2;
+               emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
+               break;
 
-               if (BPF_OP(code) == BPF_JSET) {
-                       /* Adjust for and */
-                       rvoff -= 4;
-                       emit(rv_and(rs, rd, rs), ctx);
-                       emit_branch(BPF_JNE, rs, RV_REG_ZERO, rvoff, ctx);
+       case BPF_JMP | BPF_JSET | BPF_K:
+       case BPF_JMP32 | BPF_JSET | BPF_K:
+               rvoff = rv_offset(i, off, ctx);
+               s = ctx->ninsns;
+               if (is_12b_int(imm)) {
+                       emit(rv_andi(RV_REG_T1, rd, imm), ctx);
                } else {
-                       emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
+                       emit_imm(RV_REG_T1, imm, ctx);
+                       emit(rv_and(RV_REG_T1, rd, RV_REG_T1), ctx);
                }
+               /* For jset32, we should clear the upper 32 bits of t1, but
+                * sign-extension is sufficient here and saves one instruction,
+                * as t1 is used only in comparison against zero.
+                */
+               if (!is64 && imm < 0)
+                       emit(rv_addiw(RV_REG_T1, RV_REG_T1, 0), ctx);
+               e = ctx->ninsns;
+               rvoff -= (e - s) << 2;
+               emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
                break;
 
        /* function call */