9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Thumb compiler oddity
@ 2023-09-24 20:26 David Boddie
  2023-09-27 12:32 ` Richard Miller
  0 siblings, 1 reply; 5+ messages in thread
From: David Boddie @ 2023-09-24 20:26 UTC (permalink / raw)
  To: inferno-os; +Cc: 9fans

I'm using the Inferno Thumb compiler, tc, to compile Inferno (unsurprisingly)
and encountered a problem. Since 9front appears to have the same compiler, I
thought that I should mention it on both 9fans and Inferno mailing lists.

The problem is with the libmath/fdlibm/e_exp.c when compiled using tc,
5c (arm), 0c (spim), and maybe others. The relevant chunk of code is the
first branch of this:

  if(k >= -1021) {
      __HI(y) += (k<<20);       /* add k to y's exponent */
      return y;
  } else {
      __HI(y) += ((k+1000)<<20);/* add k to y's exponent */
      return y*twom1000;
  }

where __HI is defined as

  #define __HI(x) *(1+(int*)&x)

for little-endian doubles.

The code generated by tc for the k >= -1021 branch is this, which I've
annotated:

        MOVD    F0,F3               y
        CMP     $-1021,R4,         compare k to -1021
        BLT     ,9(PC)             k < -1021, skip past RET below
        MOVW    R4,R1
        SLL     $20,R1             k << 20
        MOVW    y-4(SP),R2         load the high word of y in memory
        ADD     R1,R2              add k << 20 to it
        MOVW    R2,y-4(SP)         store the result back to memory
        MOVD    F3,F0              the unmodified y is the return value
        RET     ,

So, the compiler operates on the value in memory without updating the value
in register F3.

Turning off registerization results in this code instead:

        MOVD    F0,y-8(SP)         update y in memory
        MOVW    k-44(SP),R1        load k
        CMP     $-1021,R1,         compare k to -1021
        BLT     ,10(PC)            k < -1021, skip past RET below
        MOVW    k-44(SP),R1
        SLL     $20,R1             k << 20
        MOVW    y-4(SP),R2         load the high word of y in memory
        ADD     R1,R2              add k << 20 to it
        MOVW    R2,y-4(SP)         store the result back to memory
        MOVD    y-8(SP),F0         load the modified y to be returned
        NOP     ,R0
        RET     ,

Is the problem with the compiler or with the C code? Although I get the
feeling that the compiler should figure out that the value of y is being
modified, I don't know if it's being done in a valid way, or a way that
compilers are expected to handle.

It could be an endianness-related thing: vc appears to do the right thing.
Having said that, so does 8c, as far as I can tell.

Any thoughts?

David



------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tf0ca316e4ef39fda-M6139e88f459a94c80f155e6b
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 5+ messages in thread
* Re: [9fans] Thumb compiler oddity
@ 2023-10-01 21:17 David Boddie
  2023-10-02  9:07 ` Richard Miller
  0 siblings, 1 reply; 5+ messages in thread
From: David Boddie @ 2023-10-01 21:17 UTC (permalink / raw)
  To: 9fans

On Wed, 27 Sep 2023 13:32:17 +0100, Richard Miller wrote:

>> Is the problem with the compiler or with the C code?
> 
> It's a compiler error. The function mkvar() in reg.c is keeping track
> of unique variables without taking address aliasing into account.

Thanks for the diagnosis. I'll dig into the problem when I next get a 
chance.

In the meantime, I turned off "registerization" for files that could
potentially have similar code.

David

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te51716f70e2f8d84-M74e4cf1605468696bd1b428f
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 5+ messages in thread
* Re: [9fans] Thumb compiler oddity
@ 2023-10-11 20:30 David Boddie
  0 siblings, 0 replies; 5+ messages in thread
From: David Boddie @ 2023-10-11 20:30 UTC (permalink / raw)
  To: 9fans; +Cc: inferno-os

On Mon Oct 02 09:07:41 2023, Richard Miller wrote:

> > In the meantime, I turned off "registerization" for files that could
> > potentially have similar code.
> 
> If you want a more targeted workaround without a performance cost,
> in the example you posted you can force the compiler to reload the
> register by replacing
> 
>         return y;
> 
> with
> 
>         return *(&y);\0

Thanks for the workaround. The FDLIBM readme file suggests hacking on the
__HI, __LO, __HIp and __LOp macros, so I'll see what I can come up with.

David
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tc59953c9719ea4ee-M546a8a287af7affc28a2547b
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-10-11 20:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-24 20:26 [9fans] Thumb compiler oddity David Boddie
2023-09-27 12:32 ` Richard Miller
2023-10-01 21:17 David Boddie
2023-10-02  9:07 ` Richard Miller
2023-10-11 20:30 David Boddie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).