Re: this girl calls c ugly

Liste des GroupesRevenir à cl c  
Sujet : Re: this girl calls c ugly
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 31. May 2026, 21:14:48
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <10vi4ro$1p77r$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13
User-Agent : Mozilla Thunderbird
On 31/05/2026 20:25, BGB wrote:
On 5/31/2026 4:14 AM, David Brown wrote:
On 30/05/2026 22:48, BGB wrote:
On 5/30/2026 6:52 AM, David Brown wrote:
On 29/05/2026 22:16, BGB wrote:
On 5/29/2026 6:22 AM, David Brown wrote:
On 29/05/2026 12:20, BGB wrote:
On 5/29/2026 2:52 AM, Janis Papanagnou wrote:
On 2026-05-28 11:57, BGB wrote:
On 5/28/2026 2:18 AM, Janis Papanagnou wrote:
On 2026-05-28 01:49, BGB wrote:
[...]

Also functions with large static arrays.
>
>
void SomeFunc()
{
   static char buf[4096];
   ...
}
>
Where, say, eliminating SomeFunc does not necessarily eliminate buf.
>
Yes, if you have such code but want to eliminate it, then -fdata- sections would definitely benefit.  I have not seen such code in practice (at least not with very big static arrays, and that also was not an essential part of the program).  But of course I have only seen a microscopic part of all C code written - if you come across this sort of thing, then I appreciate your point.
>
(There are several ways to make this more "friendly" to builds that need to be compact, such as putting the buffer and/or SomeFunc in a separate file or giving it a specific section of its own.)
>
 I have seen this pattern sometimes, though usually in "medium old" code, with newer code more often assuming that the stack is really big and so can handle putting 1MB or more in a local array. Though, this is not great on a target which doesn't have a huge stack.
 In my case, I usually had 128K as the default stack size in my project.
 
OK.  My code typically has a stack of 1 KB or less per thread.  It is not inconceivable that I would have a static array like this, but it would not be in code that is likely to be unused.

>
Where section anchors shine - and where -fdata-sections therefore has cost - is when a function needs to access more than one piece of static lifetime data defined in the same translation unit (or another translation unit if you are using LTO).  That happens a lot in embedded ARM programming at least.  I don't know about RISC-V.  If the target normally uses a "small data section" for ram (I know this is common on PowerPC), then there is, in effect, a program-wide section anchor already.  So it is possible that it relatively few targets have section anchors - but the 32-bit ARM on gcc is a vastly popular choice in the embedded world, so it is important to understand the cost of this compiler flag for that target at least.
>
>
It depends on the way it is built.
>
>
A lot of times though (for non-relocatable static-linked binaries) it mostly tends to use AUIPC+LD or AUIPC+ST pairs to access global variables. There is a Global Pointer that needs to be loaded when the binary is started, unclear what it is used for exactly.
>
>
If you have a global pointer, then it will probably be used for gp+offset access to global data, eliminating the need for section anchors.
>
I have not used RISC-V, and am not familiar with its details.  I can see from godbolt that when -fdata-sections is in action and you are loading from static lifetime variables, the compiler generates instructions like
>
     lw a5, a_variable
     lw a4, b_variable
     lw a0, c_variable
>
When you do not have "-fdata-sections", it uses anchors :
>
     lla a4, .LANCHOR0
     lw a5, 0(a4)
     lw a3, 4(a4)
     lw a0, 8(a4)
>
 From my (limited) understanding, RISC-V cannot use 32-bit absolute addressing.  So the "lw a5, a_variable" must be a pseudo-instruction - using register + offset addressing.  If there is a global pointer, then presumably that is used here.  Alternatively, the pseudo instruction might assemble to two real instruction to support the 32- bit address.  I know both techniques are used in some targets, but don't know about RISC-V.
>
 It can use one of two strategies for these (after breaking up pseudo- instructions):
   LUI    a5, HiAddr      //Abs32, Low 2GB only
   LW     a5, LoAddr(a5)
Or:
   AUIPC  a5, HiAddr      //PC-Rel
   LW     a5, LoAddr(a5)
 IIRC, LLA is similar, just using an ADDI as the second instruction.
But, yeah, the latter sequence would be more efficient.
 
Thanks.  That clears things up for me.  And in particular, it shows that section anchors (and therefore no "-fdata-sections") can make a significant difference to gcc code for RISC-V.

 I would expect something different if building with -fPIC or -fPIE, but this depends on if it is a version of GCC built with support for these (if using a version of GCC built for non-hosted targets, it ignores these). Where, one effectively needs different GCC builds for bare-metal (like OS kernels) and for hosted Linux development, for whatever bizarre reason...
 
Certainly it would surprise me if the "lw a5, a_variable" version were more efficient than using anchors - otherwise why would gcc generate code with anchors when given a free choice?  (Perhaps gcc is not well tuned for RISC-V code generation - I am wary of making too many assumptions about the processor just from some simple compiler outputs.)
>
 It is not, it is a 2-op sequence usually.
 Plain RISC-V has a bigger problem with 64-bit constants though, generally needs to either load these from memory (more typical in GCC) or build them in-place (which needs roughly 6 instructions in RISC-V).
 Say (possible, but GCC doesn't do this):
   LUI   t0, ValHiA
   LUI   t1, valHiB
   ADDI  t0, t0, valLoA
   ADDI  t1, t1, valLoB
   SLLI  t1, t1, 32
   ADD   a0, t0, t1
  In my case, I have extensions for RV that can turn a lot of this stuff into single instructions (albeit with larger 8 and 12 byte encodings).
 In some cases, it can save bytes, for example:
   LW   a1, Disp33s(a0)
As a 64-bit / 8-byte encoding, vs:
   LUI  t0, DispHi
   ADD  t0, t0, a0
   LW   a1, DispLo(a0)
Needing 12 bytes.
  My own (more drastic) extensions can save more, by having a few Disp16 instructions, which can access 256K or 512K past GP within a single 32- bit instruction.
  But, if/when any of this would end up in mainline RISC-V is uncertain.
Weirdly, there is a lot more emphasis there on big/fancy features (with niche applicability), rather than on smaller things that can improve the properties of the base ISA (and that could more generally benefit nearly all code built for the ISA).
 
[...]

>
Cygwin has its own wide range of complications.  If you want to use gcc targeting native Windows, msys2 and mingw-64 are probably your best bet, either compiled natively under msys2 or as a cross-compile from Linux. But don't place too much emphasis on my advice, as I very rarely compile C or C++ code for Windows - most of my PC target (Linux or Windows) coding is in Python.
>
 Yes, I had used MinGW for a while, before mostly moving over to MSVC for native Windows.
 The tradeoff is mostly:
MinGW is closer to native for Windows;
Cygwin could give a closer approximation of Linux on Windows, so one can build a lot of Linux software and use "./configure" scripts and similar.
 
Note that MinGW and Mingw-w64 are very, very different.  (And the corresponding environments and utility collections, msys and msys2, are equally different.)  Mingw-w64, as I understand it, is somewhat of a balance between old MinGW and Cygwin in being close to native for most purposes, but providing more POSIX compliance than MinGW.  It is also much newer, much better maintained, with modern language support in its tools (last I heard, with MinGW you did not even get C99 support in the standard library).  And of course it has 64-bit support.
You may well find WSL or MSVC to be a better choice for your requirements, but don't mistake Mingw-w64 for MinGW.

 But, as noted, Cygwin's role was mostly displaced by WSL, which effectively runs a Linux userland on Windows.
  There was WSL1, which basically mapped Linux syscalls over to the Windows kernel, and WSL2, which runs the Linux kernel in a VM.
  Though, in my case I was using WSL1 as seemingly MS had decided that my PC can't do virtualization (and sees it as necessary for WSL2), even despite having a CPU that can do so, and it is enabled in the BIOS.
 

Date Sujet#  Auteur
27 May 26 * this girl calls c ugly365fir
27 May 26 `* Re: this girl calls c ugly364fir
28 May 26  `* Re: this girl calls c ugly363BGB
28 May 26   +* Re: this girl calls c ugly5Lawrence D’Oliveiro
28 May 26   i+* Re: this girl calls c ugly3BGB
29 May 26   ii`* Re: this girl calls c ugly2Lawrence D’Oliveiro
29 May 26   ii `- Re: this girl calls c ugly1BGB
28 May 26   i`- Re: this girl calls c ugly1Bonita Montero
28 May 26   +* Re: this girl calls c ugly19Janis Papanagnou
28 May 26   i+* Re: this girl calls c ugly15BGB
29 May 26   ii+- Re: this girl calls c ugly1Lawrence D’Oliveiro
29 May 26   ii`* Re: this girl calls c ugly13Janis Papanagnou
29 May 26   ii `* Re: this girl calls c ugly12BGB
29 May 26   ii  +* Re: this girl calls c ugly9David Brown
29 May 26   ii  i`* Re: this girl calls c ugly8BGB
30 May 26   ii  i `* Re: this girl calls c ugly7David Brown
30 May 26   ii  i  +* Re: this girl calls c ugly2Janis Papanagnou
30 May 26   ii  i  i`- Re: this girl calls c ugly1David Brown
30 May 26   ii  i  `* Re: this girl calls c ugly4BGB
31 May 26   ii  i   `* Re: this girl calls c ugly3David Brown
31 May 26   ii  i    `* Re: this girl calls c ugly2BGB
31 May 26   ii  i     `- Re: this girl calls c ugly1David Brown
29 May 26   ii  +- Re: this girl calls c ugly1Janis Papanagnou
30 May 26   ii  `- Re: this girl calls c ugly1Lawrence D’Oliveiro
28 May 26   i`* Re: this girl calls c ugly3Chris M. Thomasson
29 May 26   i `* Re: this girl calls c ugly2Janis Papanagnou
29 May 26   i  `- Re: this girl calls c ugly1Chris M. Thomasson
28 May 26   `* Re: this girl calls c ugly338fir
28 May 26    `* Re: this girl calls c ugly337BGB
29 May 26     +* Re: this girl calls c ugly330Lawrence D’Oliveiro
29 May 26     i`* Re: this girl calls c ugly329Janis Papanagnou
29 May 26     i `* Re: this girl calls c ugly328Bart
29 May 26     i  +* Re: this girl calls c ugly312Janis Papanagnou
29 May 26     i  i`* Re: this girl calls c ugly311Bart
29 May 26     i  i +* Re: this girl calls c ugly9Janis Papanagnou
29 May 26     i  i i+* Re: this girl calls c ugly2Bart
29 May 26     i  i ii`- Re: this girl calls c ugly1Janis Papanagnou
29 May 26     i  i i`* Re: this girl calls c ugly6Bart
29 May 26     i  i i +* Re: this girl calls c ugly4Janis Papanagnou
29 May 26     i  i i i`* Re: this girl calls c ugly3Bart
29 May 26     i  i i i `* Re: this girl calls c ugly2Janis Papanagnou
29 May 26     i  i i i  `- Re: this girl calls c ugly1Bart
29 May 26     i  i i `- Re: this girl calls c ugly1Keith Thompson
29 May 26     i  i `* Re: this girl calls c ugly301tTh
29 May 26     i  i  `* Re: this girl calls c ugly300Bart
29 May 26     i  i   +* Re: this girl calls c ugly298Keith Thompson
29 May 26     i  i   i`* Re: this girl calls c ugly297Bart
29 May 26     i  i   i +- Re: this girl calls c ugly1Janis Papanagnou
29 May 26     i  i   i `* Re: this girl calls c ugly295Keith Thompson
29 May 26     i  i   i  `* Re: this girl calls c ugly294Bart
29 May 26     i  i   i   +* Re: this girl calls c ugly5Keith Thompson
30 May 26     i  i   i   i`* Re: this girl calls c ugly4James Kuyper
30 May 26     i  i   i   i `* Re: this girl calls c ugly3Bart
30 May 26     i  i   i   i  `* Re: this girl calls c ugly2Keith Thompson
30 May 26     i  i   i   i   `- Re: this girl calls c ugly1Bart
30 May 26     i  i   i   `* Re: this girl calls c ugly288Dan Cross
30 May 26     i  i   i    +* Re: this girl calls c ugly284Bart
31 May 26     i  i   i    i+* Re: this girl calls c ugly282Keith Thompson
31 May 26     i  i   i    ii+* Re: this girl calls c ugly5Janis Papanagnou
31 May 26     i  i   i    iii+* Re: this girl calls c ugly2Keith Thompson
2 Jun 26     i  i   i    iiii`- Re: this girl calls c ugly1Janis Papanagnou
31 May 26     i  i   i    iii`* Re: this girl calls c ugly2David Brown
2 Jun 26     i  i   i    iii `- Re: this girl calls c ugly1Janis Papanagnou
31 May 26     i  i   i    ii`* Re: this girl calls c ugly276Richard Harnden
31 May 26     i  i   i    ii +* Re: this girl calls c ugly171David Brown
31 May 26     i  i   i    ii i+* Re: this girl calls c ugly168Bart
31 May 26     i  i   i    ii ii+* Re: this girl calls c ugly142David Brown
31 May 26     i  i   i    ii iii`* Re: this girl calls c ugly141James Kuyper
31 May 26     i  i   i    ii iii `* Re: this girl calls c ugly140David Brown
31 May 26     i  i   i    ii iii  +* Re: this girl calls c ugly4James Kuyper
31 May 26     i  i   i    ii iii  i`* Re: this girl calls c ugly3David Brown
31 May 26     i  i   i    ii iii  i `* Re: this girl calls c ugly2James Kuyper
1 Jun 26     i  i   i    ii iii  i  `- Re: this girl calls c ugly1David Brown
31 May 26     i  i   i    ii iii  `* Re: this girl calls c ugly135Keith Thompson
1 Jun 26     i  i   i    ii iii   +* Re: this girl calls c ugly2David Brown
1 Jun 26     i  i   i    ii iii   i`- Re: this girl calls c ugly1Keith Thompson
2 Jun 26     i  i   i    ii iii   +* Re: this girl calls c ugly131Janis Papanagnou
2 Jun 26     i  i   i    ii iii   i+- Re: this girl calls c ugly1James Kuyper
2 Jun 26     i  i   i    ii iii   i+* Constants and undefined behavior84Tim Rentsch
2 Jun 26     i  i   i    ii iii   ii`* Re: Constants and undefined behavior83Dan Cross
4 Jun 26     i  i   i    ii iii   ii `* Re: Constants and undefined behavior82Tim Rentsch
4 Jun 26     i  i   i    ii iii   ii  `* Re: Constants and undefined behavior81Dan Cross
4 Jun 26     i  i   i    ii iii   ii   +* Re: Constants and undefined behavior31Keith Thompson
5 Jun 26     i  i   i    ii iii   ii   i+* Re: Constants and undefined behavior28Dan Cross
5 Jun 26     i  i   i    ii iii   ii   ii+* Re: Constants and undefined behavior24Keith Thompson
6 Jun 26     i  i   i    ii iii   ii   iii+* Re: Constants and undefined behavior19Dan Cross
6 Jun 26     i  i   i    ii iii   ii   iiii`* Re: Constants and undefined behavior18Keith Thompson
8 Jun 26     i  i   i    ii iii   ii   iiii `* Re: Constants and undefined behavior17Dan Cross
8 Jun 26     i  i   i    ii iii   ii   iiii  +* Re: Constants and undefined behavior5Keith Thompson
9 Jun 26     i  i   i    ii iii   ii   iiii  i`* Re: Constants and undefined behavior4Dan Cross
9 Jun 26     i  i   i    ii iii   ii   iiii  i `* Re: Constants and undefined behavior3Keith Thompson
9 Jun 26     i  i   i    ii iii   ii   iiii  i  `* Re: Constants and undefined behavior2Dan Cross
9 Jun 26     i  i   i    ii iii   ii   iiii  i   `- Re: Constants and undefined behavior1Keith Thompson
9 Jun 26     i  i   i    ii iii   ii   iiii  `* Re: Constants and undefined behavior11Waldek Hebisch
9 Jun 26     i  i   i    ii iii   ii   iiii   +* Re: Constants and undefined behavior3James Kuyper
10 Jun 26     i  i   i    ii iii   ii   iiii   i`* Re: Constants and undefined behavior2Keith Thompson
10 Jun 26     i  i   i    ii iii   ii   iiii   i `- Re: Constants and undefined behavior1Dan Cross
11 Jun 26     i  i   i    ii iii   ii   iiii   `* Re: Constants and undefined behavior7Janis Papanagnou
11 Jun 26     i  i   i    ii iii   ii   iiii    +* Re: Constants and undefined behavior2Dan Cross
11 Jun 26     i  i   i    ii iii   ii   iiii    i`- Re: Constants and undefined behavior1Janis Papanagnou
11 Jun 26     i  i   i    ii iii   ii   iiii    `* Re: Constants and undefined behavior4Waldek Hebisch
6 Jun 26     i  i   i    ii iii   ii   iii`* Re: Constants and undefined behavior4Tim Rentsch
5 Jun 26     i  i   i    ii iii   ii   ii`* Re: Constants and undefined behavior3Janis Papanagnou
7 Jun 26     i  i   i    ii iii   ii   i`* Re: Constants and undefined behavior2Tim Rentsch
9 Jun 26     i  i   i    ii iii   ii   `* Re: Constants and undefined behavior49Tim Rentsch
2 Jun 26     i  i   i    ii iii   i`* Re: this girl calls c ugly45Keith Thompson
2 Jun 26     i  i   i    ii iii   `- Re: this girl calls c ugly1Chris M. Thomasson
2 Jun 26     i  i   i    ii ii`* Re: this girl calls c ugly25Dan Cross
31 May 26     i  i   i    ii i`* Re: this girl calls c ugly2James Kuyper
31 May 26     i  i   i    ii +* Re: this girl calls c ugly2Keith Thompson
31 May 26     i  i   i    ii `* Re: this girl calls c ugly102Tim Rentsch
31 May 26     i  i   i    i`- Re: this girl calls c ugly1Dan Cross
1 Jun 26     i  i   i    `* Re: this girl calls c ugly3Tim Rentsch
30 May 26     i  i   `- Re: this girl calls c ugly1David Brown
29 May 26     i  +* Re: this girl calls c ugly6Janis Papanagnou
30 May 26     i  `* Re: this girl calls c ugly9Lawrence D’Oliveiro
29 May 26     `* Re: this girl calls c ugly6Bonita Montero

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal