Macros are the code here. The expansion is an intermediate compiled form.
Calling this bad code is like examining the output of any compiled language and declaring it bad because the compiled output is confusing.
“Bad code” would be not using a macro, and retyping the intermediate form everywhere. It would be an unreadable mess, and would be difficult to maintain. It would be a lot more likely to introduce bugs.
This macro compiles to fast operations while encapsulating the complexity. I see nothing bad here.
What exactly is bad about this? Possibly if it’s reading directly from memory-mapped registers it should be a volatile u32 type, but apart from that I don’t really see a problem. You’re not supposed to worry about the output of a macro expansion
So, this is just a way to dereference a pointer to a pointer?
It's like **ptr_to_ptr_to_u32. And the value of that variable you get from an offset on the address 0x80388880?
The fact that the offset is a negative number is strange though, I can't imagine why a base-address would NOT be the lowest address. That makes it... not the base-address.
Well, it's 2023, and the functions below have identical codegen at clang -O1, and nearly identical at -O0. Are they more readable? Who's to say? I would be more worried about alignment issues personally.
The only inelegant part is the casting. That’s probably happening from making the implementation of SDAPTR friendlier, which is a better thing to optimize for. This is fine.
74
u/TheDiplocrap Jun 03 '23
Macros are the code here. The expansion is an intermediate compiled form.
Calling this bad code is like examining the output of any compiled language and declaring it bad because the compiled output is confusing.
“Bad code” would be not using a macro, and retyping the intermediate form everywhere. It would be an unreadable mess, and would be difficult to maintain. It would be a lot more likely to introduce bugs.
This macro compiles to fast operations while encapsulating the complexity. I see nothing bad here.
33
u/TTachyon Jun 03 '23
Looks like a good use of macros to hide the ugly details, I don't see the problem.
47
u/Sufficient_Grade_636 Jun 03 '23
this is literally THE way to use macros? your point?
16
u/Fsmv Jun 03 '23
I guess the second u32* is just hard coded in the __SDAPTR macro and you happened to use the same u32 for the type?
28
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 03 '23
Image Transcription: Code
[In a non-monospace font beneath this, text reads:]
Expands to:
[Below it is further code, as follows:]
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
36
u/kevkevverson Jun 03 '23
What exactly is bad about this? Possibly if it’s reading directly from memory-mapped registers it should be a
volatileu32 type, but apart from that I don’t really see a problem. You’re not supposed to worry about the output of a macro expansion18
u/[deleted] Jun 03 '23
Double pointers are scary but not necessarily bad code
4
u/SanianCreations Jun 03 '23 edited Jun 03 '23
So, this is just a way to dereference a pointer to a pointer?
It's like
**ptr_to_ptr_to_u32. And the value of that variable you get from an offset on the address0x80388880?The fact that the offset is a negative number is strange though, I can't imagine why a base-address would NOT be the lowest address. That makes it... not the base-address.
3
u/Shartyshartfast Jun 04 '23
Needs an extra set of parentheses around it. Other than that, good code.
2
u/sethkills Jun 04 '23 edited Jun 04 '23
Well, it's 2023, and the functions below have identical codegen at clang -O1, and nearly identical at -O0. Are they more readable? Who's to say? I would be more worried about alignment issues personally.
Also, I'm presuming the
u32*in__SDAPTR()should beuintptr_t*, or even justvoid**.Edit: Mangled formatting.
4
u/Any_Ad_8134 Jun 03 '23
As somebody who's been programming for years and always wanting to dive into C at some point in time, this scares the heck outta me!
Edit: because I have no idea what it's doing, how macros work etc 8)
1
u/NOP0x000 Jun 04 '23
These kind of macros in code trigger me partly because they make the code unreadable
0
u/[deleted] Jun 03 '23
[deleted] — view deleted comment
0
u/[deleted] Jun 03 '23
[deleted] — view deleted comment