macro #defines and __riscv_xlen

1,478 views
Skip to first unread message

Manuel A. Fernandez Montecelo

unread,
Feb 7, 2017, 3:26:57 PM2/7/17
to sw-...@groups.riscv.org
Hi,

So after __riscv32/64 were removed a while ago, and GCC support for
RISC-V upstreamed, I guess that the canonical way for supporting RISC-V
in upstream software will be something like:

#if defined (__riscv_xlen)
#if (__riscv_xlen == 32)
//...
#elif (__riscv_xlen == 64)
//...
#elif (__riscv_xlen == 128)
// ... in a galaxy, far far away...
#else
// invalid length
#endif
#endif


Or is there a better alternative pattern to achieve this?


Cheers.
--
Manuel A. Fernandez Montecelo <manuel.m...@gmail.com>

Stefan O'Rear

unread,
Feb 7, 2017, 3:52:09 PM2/7/17
to Manuel A. Fernandez Montecelo, RISC-V SW Dev
On Tue, Feb 7, 2017 at 12:26 PM, Manuel A. Fernandez Montecelo
<manuel.m...@gmail.com> wrote:
> Or is there a better alternative pattern to achieve this?

If you're using __riscv_xlen at all you're probably using it incorrectly.

The correct check for RISC-V is #ifdef __riscv

The correct way to determine pointer size is sizeof(void*) (ANSI C) or
__SIZEOF_POINTER__ (gcc extension).

The only correct use of __riscv_xlen is when you are interacting with
the instruction set at the assembly/machine code level and need to
know whether "sd" and "sq" can be used.

-s

Manuel A. Fernandez Montecelo

unread,
Feb 7, 2017, 4:59:12 PM2/7/17
to RISC-V SW Dev
2017-02-07 21:52 Stefan O'Rear:
Thanks for the reply.

My interest in using something like __riscv_xlen is that I think that
checking "sizeof(void*)" looks a bit ugly/obcure, specially if it's in
the middle of other #ifdef blocks for another dozen architectures --
which is a common case in which this kind of code would be inserted,
e.g.:

http://sources.debian.net/src/texlive-bin/2016.20160513.41080.dfsg-1/libs/luajit/LuaJIT-src/src/lj_arch.h/?hl=46#L46


Most other arches have something like __aarch64__ or __x86_64__ that is
clear/convenient enough, *and easy to search* in web search engines.

I was hoping that, if __riscv32/64 are not available, some other
easy-to-understand string would be made available.

Would something like __riscv_pointer_size defined in terms of
"sizeof(void*)" make sense, defined at the same level as __riscv_xlen
(that is, the compiler)? Or is it definitely out of question?


Cheers.
--

Andrew Waterman

unread,
Feb 7, 2017, 5:07:21 PM2/7/17
to Manuel A. Fernandez Montecelo, RISC-V SW Dev
Hi Manuel,

I think Stefan's point is that XLEN and the size of a pointer are not
necessarily equal. __riscv and __riscv_xlen will be defined on RISC-V
systems, but even if __riscv_xlen == 64, it may not be the case that
__SIZEOF_POINTER__ == 8. Similarly, __aarch64__ may be defined when
pointers are 4 bytes, because of their ILP32 ABI.

So use __SIZEOF_POINTER__ to condition on pointer size, and
__riscv_xlen to condition on ISA, and their conjunction to condition
on the overall ABI.

Andrew

On Tue, Feb 7, 2017 at 1:59 PM, Manuel A. Fernandez Montecelo
> --
> You received this message because you are subscribed to the Google Groups
> "RISC-V SW Dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sw-dev+un...@groups.riscv.org.
> To post to this group, send email to sw-...@groups.riscv.org.
> Visit this group at
> https://groups.google.com/a/groups.riscv.org/group/sw-dev/.
> To view this discussion on the web visit
> https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/20170207215907.x4dfuu3vm26z6wai%40reva.itsari.org.

Manuel A. Fernandez Montecelo

unread,
Feb 7, 2017, 6:06:56 PM2/7/17
to RISC-V SW Dev

(Thanks for the replies, and sorry if I'm a bother... I am asking all of
this in detail because it's not for a single project that I want to
support -- it's to have these details sorted out before I start to send
patches to upstream projects.

The easier to understand and the more similar to what other arches do
the better, in order to get upstream to accept them with as little
friction as possible.

And if this changes later and I/we have to repeat the process, is a
mess.)


2017-02-07 23:06 Andrew Waterman:
>Hi Manuel,
>
>I think Stefan's point is that XLEN and the size of a pointer are not
>necessarily equal. __riscv and __riscv_xlen will be defined on RISC-V
>systems, but even if __riscv_xlen == 64, it may not be the case that
>__SIZEOF_POINTER__ == 8. Similarly, __aarch64__ may be defined when
>pointers are 4 bytes, because of their ILP32 ABI.

Yeah, I got that.

The vast majority of arch-specific support that I have seen in upstream
projects starts with checks of self-explanatory strings like "#ifdef
__aarch64__", for all architectures/ABIs, including recent ones like
this aarch64 or aarch64_ilp32, or __x86_64__ and x32.

So even if that solution is more correct, I think that it will cause
some friction to integrate in the projects.

But if it's the only solution, well, it'll have to be that way :)


>So use __SIZEOF_POINTER__ to condition on pointer size, and
>__riscv_xlen to condition on ISA, and their conjunction to condition
>on the overall ABI.

Unfortunately, I think that __SIZEOF_POINTER__ is out of question as a
general pattern if it's a GCC-only extension.


Cheers and thanks for the advice.

Andrew Waterman

unread,
Feb 7, 2017, 6:17:48 PM2/7/17
to Manuel A. Fernandez Montecelo, RISC-V SW Dev
For the ISA itself, yeah, #if defined(__riscv) && __riscv_xlen == 64.

>
>> So use __SIZEOF_POINTER__ to condition on pointer size, and
>> __riscv_xlen to condition on ISA, and their conjunction to condition
>> on the overall ABI.
>
>
> Unfortunately, I think that __SIZEOF_POINTER__ is out of question as a
> general pattern if it's a GCC-only extension.

We plan to include __SIZEOF_POINTER__ in the official list of list of
baked-in defines, so you can rely on it if __riscv is defined.

>
>
> Cheers and thanks for the advice.
>
> --
> Manuel A. Fernandez Montecelo <manuel.m...@gmail.com>
>
> --
> You received this message because you are subscribed to the Google Groups
> "RISC-V SW Dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sw-dev+un...@groups.riscv.org.
> To post to this group, send email to sw-...@groups.riscv.org.
> Visit this group at
> https://groups.google.com/a/groups.riscv.org/group/sw-dev/.
> To view this discussion on the web visit
> https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/20170207230653.ywk4vss4ii7qtiyv%40reva.itsari.org.

Manuel A. Fernandez Montecelo

unread,
Feb 7, 2017, 6:46:00 PM2/7/17
to RISC-V SW Dev
2017-02-08 00:17 Andrew Waterman:
>On Tue, Feb 7, 2017 at 3:06 PM, Manuel A. Fernandez Montecelo
>> Unfortunately, I think that __SIZEOF_POINTER__ is out of question as a
>> general pattern if it's a GCC-only extension.
>
>We plan to include __SIZEOF_POINTER__ in the official list of list of
>baked-in defines, so you can rely on it if __riscv is defined.

I see. Stefan mentioned that it was a GCC extension, so I got confused.

If it's guaranteed to be defined for __riscv in all platforms/compiled
(I assume that it's already there in the upstreamed GCC patches), it
looks like an excellent solution for most cases.

Thanks!
Reply all
Reply to author
Forward
0 new messages