Skip to content

Commit 1d7f693

Browse files
guoren83palmer-dabbelt
authored andcommitted
riscv: atomic: Optimize dec_if_positive functions
Current implementation wastes another register to pass the argument, but we only need addi to calculate the result. Optimize the code with minimize the usage of registers. Signed-off-by: Guo Ren <[email protected]> Signed-off-by: Guo Ren <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent dd8437c commit 1d7f693

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

arch/riscv/include/asm/atomic.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,47 +310,47 @@ ATOMIC_OPS()
310310
#undef ATOMIC_OPS
311311
#undef ATOMIC_OP
312312

313-
static __always_inline int arch_atomic_sub_if_positive(atomic_t *v, int offset)
313+
static __always_inline int arch_atomic_dec_if_positive(atomic_t *v)
314314
{
315315
int prev, rc;
316316

317317
__asm__ __volatile__ (
318318
"0: lr.w %[p], %[c]\n"
319-
" sub %[rc], %[p], %[o]\n"
319+
" addi %[rc], %[p], -1\n"
320320
" bltz %[rc], 1f\n"
321321
" sc.w.rl %[rc], %[rc], %[c]\n"
322322
" bnez %[rc], 0b\n"
323323
" fence rw, rw\n"
324324
"1:\n"
325325
: [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
326-
: [o]"r" (offset)
326+
:
327327
: "memory");
328-
return prev - offset;
328+
return prev - 1;
329329
}
330330

331-
#define arch_atomic_dec_if_positive(v) arch_atomic_sub_if_positive(v, 1)
331+
#define arch_atomic_dec_if_positive arch_atomic_dec_if_positive
332332

333333
#ifndef CONFIG_GENERIC_ATOMIC64
334-
static __always_inline s64 arch_atomic64_sub_if_positive(atomic64_t *v, s64 offset)
334+
static __always_inline s64 arch_atomic64_dec_if_positive(atomic64_t *v)
335335
{
336336
s64 prev;
337337
long rc;
338338

339339
__asm__ __volatile__ (
340340
"0: lr.d %[p], %[c]\n"
341-
" sub %[rc], %[p], %[o]\n"
341+
" addi %[rc], %[p], -1\n"
342342
" bltz %[rc], 1f\n"
343343
" sc.d.rl %[rc], %[rc], %[c]\n"
344344
" bnez %[rc], 0b\n"
345345
" fence rw, rw\n"
346346
"1:\n"
347347
: [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
348-
: [o]"r" (offset)
348+
:
349349
: "memory");
350-
return prev - offset;
350+
return prev - 1;
351351
}
352352

353-
#define arch_atomic64_dec_if_positive(v) arch_atomic64_sub_if_positive(v, 1)
353+
#define arch_atomic64_dec_if_positive arch_atomic64_dec_if_positive
354354
#endif
355355

356356
#endif /* _ASM_RISCV_ATOMIC_H */

0 commit comments

Comments
 (0)