-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intrinsic sign_extend / zero_extend / truncate functions #64
Comments
To exand on this further: These intrinsics could have additional validation logic checking that the extended type is actually larger than the source type. If XLEN were smaller than the width of |
Alright, quick update: I'm currently rewriting the RV32I ISA to give you a demonstration of what the language would look like with all of my new proposals applied. I must say that these functions are invaluable for the readability of the code. So here's a proper proposal instead of the rough idea above. 5 new functions:
All of the width parameters must be constant expressions. The extend functions will report an error if the requested width is smaller than that of the input value, the truncate functions if it is larger. The only annoyance is the constantly repeating |
I'm strictly against the introduction of intrinsics for several reasons:
|
Thoughts:
|
PS: The C-style cast syntax is not great. Personally, I'm annoyed that we often need multiple layers of parentheses.
Maybe we should've gone for
instead. |
Yes, none of the existing semantics would be changed, there would just be additional functions to make the operations more explicit and provide additional static validation. These would be entirely optional, so as an implementor you could choose to simply not use them. Ideally, these constructs would be treated as syntactic sugar and eliminated in the fontend, so backends would just see a regular cast (or rather, an even more generic bit swizzle). |
Zero extension is a type-agnostic operation, so as described in the proposal, it can work on bit vectors. Sign extension on the other hand only makes sense on signed values and hence treats its operand as such. Whether we force the users to make this explicit by casting the operand to |
I'm currently looking through the RISCV reference files and I'm already spotting potential for improvement. The very first instruction I looked at (
LUI
) does a sign extension by first castingimm
to a signed type and then to an unsigned type of the correct width. According to the casting rules, there are actually three casts, as one happens implicitly:(unsigned<XLEN>)(signed<XLEN>)(signed<20>)imm
.It works, but in my opinion it's not very intuitive since a cast doesn't clearly communicate which kind of extension occurs. Luckily I built a very flexible system for intrinsic functions, so we can provide a
sign_extend(width, expr)
function to make the intent more explicit.What do you think, is this a direction worth exploring?
The text was updated successfully, but these errors were encountered: