readcyclecounter.S (1023B)
1 // RISC-V rv32 cycle-counter read for kit's libkit_rt.a. 2 // License: Apache-2.0 WITH LLVM-exception (see lib/LICENSE-compiler-rt.txt). 3 // 4 // unsigned long long __kit_readcyclecounter(void); 5 // 6 // The 64-bit cycle counter is split across two CSRs on rv32: `cycle` (low 32) 7 // and `cycleh` (high 32). A naive hi/lo read races when the low half wraps 8 // between the two reads, so re-read the high half and retry if it changed. 9 // Returns the 64-bit value in the ilp32 pair a0 (low) : a1 (high). Backs the 10 // frontend's __builtin_readcyclecounter() on rv32 (rv64 reads it inline with a 11 // single RDCYCLE). See src/cg/arith.c (kit_cg_intrinsic). 12 13 .text 14 .globl __kit_readcyclecounter 15 .type __kit_readcyclecounter,@function 16 __kit_readcyclecounter: 17 .Lretry: 18 csrrs a1, cycleh, zero // a1 = high 19 csrrs a0, cycle, zero // a0 = low 20 csrrs a2, cycleh, zero // a2 = high (re-read) 21 bne a1, a2, .Lretry // retry if the high half advanced 22 ret 23 .size __kit_readcyclecounter, .-__kit_readcyclecounter