Skip to content

cycleclock: Fix type conversion to match function return type on riscv64 - #1802

Merged
dmah42 merged 1 commit into
google:mainfrom
kraj:kraj/riscv64
Jun 11, 2024
Merged

dmah42 merged 1 commit into
google:mainfrom
kraj:kraj/riscv64

Conversation

@kraj

@kraj kraj commented Jun 11, 2024

Copy link
Copy Markdown
Contributor

Fixes builds with clang

src/cycleclock.h:213:10: error: implicit conversion changes signedness: 'uint64_t' (aka 'unsigned long') to 'int64_t' (aka 'long') [-Werror,-Wsign-conversion]
213 | return cycles;
| ~~~~~~ ^~~~~~
1 error generated.

Fixes builds with clang

src/cycleclock.h:213:10: error: implicit conversion changes signedness: 'uint64_t' (aka 'unsigned long') to 'int64_t' (aka 'long') [-Werror,-Wsign-conversion]
     213 |   return cycles;
         |   ~~~~~~ ^~~~~~
   1 error generated.
Comment thread src/cycleclock.h
uint64_t cycles;
asm volatile("rdtime %0" : "=r"(cycles));
return cycles;
return static_cast<int64_t>(cycles);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should check the range before the cast?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't do that anywhere else though. I'm not sure what we would even do in that situation?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert i guess? the C++ style guide talks about this a bit in discussing avoidance of unsigned types ("In particular, do not use unsigned types to say a number will never be negative. Instead, use assertions for this.")

but i agree we don't currently do it anywhere else. happy to punt this to another PR.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://riscv.org/wp-content/uploads/2016/06/riscv-spec-v2.1.pdf, page 32:

The RDTIME pseudo-instruction reads the low XLEN bits of the time CSR, which counts wall-clock
real time that has passed from an arbitrary start time in the past. RDTIMEH is an RV32I-only instruction that reads bits 63–32 of the same real-time counter. The underlying 64-bit counter should
never overflow in practice.

At 10GHz clock rate, it would take ~30 years of uptime to overflow.

@dmah42
dmah42 merged commit 8e1823d into google:main Jun 11, 2024
@dmah42

dmah42 commented Jun 11, 2024

Copy link
Copy Markdown
Member

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants