PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

The Microsoft Compiler ain't too great

I'm writing a short routine to copy a 32-bit mixing buffer to the sound output, and noticed the assembler isn't too great:

Code:

// Copy to output and clip
pm=SndMix; pd=WaveDest; pe=pd+(WaveLen<<1);
do
{
int m=*pm;
// Clip to -0x8000,0x7fff
if (m<-0x8000) m=-0x8000;
if (m> 0x7fff) m= 0x7fff;
*pd=(short)m;

pm++; pd++;
}
while (pd<pe);


; 55 : // Clip to -0x8000,0x7fff
; 56 : if (m<-0x8000) m=-0x8000;
; 57 : if (m> 0x7fff) m= 0x7fff;
; 58 : *pd=(short)m;
; 59 :
; 60 : pm++; pd++;

add r2, r2, #4
cmp r1, r3
movlt r1, r3
cmp r1, r4
movgt r1, r4
mov r1, r1, lsl #16 *****
mov r1, r1, asr #16 *****
strh r1, [r0]
add r0, r0, #2

As far as I can see the two lines I marked with stars are unneeded. Has anyone got any tricks to tell the compiler not to do this?
angedelamort · 14 Jun 2002, 16:05 UTC
The first method : write directly in assembly :)
The second one, go an look in the C++ tab and look for the optimization options. But, because of the compiler, I'm not sure if you can remove those lines explicitely.
The first method : write directly in assembly :)
The second one, go an look in the C++ tab and look for the optimization options. But, because of the compiler, I'm not sure if you can remove those lines explicitely.


no luck - tried all the optimization options and those two weird useless lines of assembly remain... :(
angedelamort · 14 Jun 2002, 19:01 UTC
How many cycles for this instructions:
movgt ra rb???
maybe it needs 2 nop bacause it takes 3 cylces?
and he has to wait for the strh instruction...(put those unusefull mov instead of nop...) I'm not really familiar with ARM assembly :(

That's only a supposistion. But Like I said, you should write directly in assembly this routine. It would be a lot simplier to remove those :P
I know what's happening:

The ARM signed stores and loads for halfwords and bytes are pretty crappy (They are less capable than the unisgned versions, and in some casews slower). The compiler is shifting left, and when it shifts back right, the original sign bit is in bit 15 (the high bit of the short), and an unsigned store can be used. Without the shifts, either the sign would be lost, or the signed halfword instruction would need to be used.

In other words, unless you really need to save 8 bytes in your code, it's not hurting to have those there (I'm not sure how slow the signed store halfword is, but if the compiler does something to work around it, it must be slower than the workaround. Even Microsoft must know about the signed store instructions).
The STRSH (signed store) costs an extra cycle over the straight STRH, so the MS version is slower by a single cycle. In that routine it's not worth worrying about (and if it is, just write the whole fn in assembler and have done with it).

(Can't check now, but if you can use writeback with a STRH then there's an extra cycle to kill at the end).

angeldelamort:

So long as you're not waiting a result from a previous instruction for one of the registers (like a from a MUL):
MOV X,Y

will take one cycle.

MUL R,big_number_1, big_number_2
wait between 1 and 3 cycles for R to arrive
MOV X,R

It depends on the numbers you multiply by - the bigger the more goes the (8 bit, IIRC) Booth multiplier needs. IIRC it's also faster to put the smaller number of the two in big_number_2 (if you know which is smaller at compile-time).

Cheers,

Refractor[/quote]
The ARM signed stores and loads for halfwords and bytes are pretty crappy (They are less capable than the unisgned versions, and in some casews slower). The compiler is shifting left, and when it shifts back right, the original sign bit is in bit 15 (the high bit of the short), and an unsigned store can be used. Without the shifts, either the sign would be lost, or the signed halfword instruction would need to be used.


That's the trick you see - the compiler thinks it needs to store say 0x80001234 as 0x9234 so needs to get the sign bit. However it doesn't: my clipping code before means the sign bit is always fine in 16-bits. But the compiler doesn't know this.

What I'd like to do is tell the compiler: I tried casting as unsigned short (same code output), or even casting a pointer to unsigned short (no luck - the code got even worse then!).

Any other tricks to tell C to forget about treating a value as signed?
I just realized another thing:
the "add r0, r0, #2" after the strh is redundant. If the compiler would just use post-incrementing with the strh (making it "strh r1, [r0], #2"), the add would be unnecessary.

Also, according to my ARM quick-reference card, there is no signed halfword store, only a signed halfword load. So the compiler took the only choice (based on the data on the reference card. If the "strsh" instruction is undocumented, then the designers of the Microsoft compiler would not necessarily have known about it, and the same code would result).

According to the quick-reference card I have (I got it somewhere on the ARM site), the following load/store instructions exist:
Code:

LDR{cond} Rd, <a_mode2> ; Load word
LDR{cond}T Rd, <a_mode2P> ; Load word (user-mode)
LDR{cond}B Rd, <a_mode2> ; Load byte (unsigned)
LDR{cond}BT Rd, <a_mode2P> ; Load byte (user-mode, unsigned)
LDR{cond}SB Rd, <a_mode3> ; Load byte (signed)
LDR{cond}H Rd, <a_mode3> ; Load halfword (unsigned)
LDR{cond}SH Rd, <a_mode3> ; Load haldword (signed)
STR{cond} Rd, <a_mode2> ; Store word
STR{cond}T Rd, <a_mode2P> ; Store word (user-mode)
STR{cond}B Rd, <a_mode2> ; Store byte (unsigned)
STR{cond}BT Rd, <a_mode2P> ; Store byte (user-mode, unsigned)
STR{cond}H Rd, <a_mode3> ; Store halfword (unsigned)

a_mode2 means you can use:
immediate offset (for structs)
zero offset
register offset
scaled register offset
All of these can be used in both pre- and post-indexed modes
a_mode2P means the same but without pre-indexed versions
a_mode3 allows for the following:
immediate offset
zero offset
register offset
all in both pre- and post-indexed modes

In other words, according to the reference card, all the stores are unsigned (except words, which don't count) and halfword and signed byte versions are less capable. The card doesn't list the instruction timing, but I'm guessing all non-word instructions are slower.

This is, of course, assuming that the quickref card I have lists all the instructions (which is pretty likely) and that the StrongARM didn't add signed store instructions (which I'm not sure of, and which wouldn't matter, since the MS compiler is probably written for generic ARM, not StrongARM (probable, but not definately)).
Hummm:

at:
http://www.heyrick.co.uk/assembler/qfinder.html

STRSB
Store a signed byte (from a register)
StrongARM

STRSH
Store a signed half-word (from a register)
StrongARM


I can't find where I found the thing about the extra cycle, but it wasn't a "real" Intel or ARM document.

I'll try compiling them tomorrow and see if anything complains :) I tend not to use half-words myself 'cos they cause this kind of grief.

If you think about it though, then it's sensible NOT to have a signed store, 'cos if the register was negative then bit 15 should be 1 anyway (unless you've overflowed your 15/16-bits, which is entirely your fault anyway :D)

I just realized another thing:
the "add r0, r0, #2" after the strh is redundant.


Me, earlier:

(Can't check now, but if you can use writeback with a STRH then there's an extra cycle to kill at the end).


:P
Yeah, that site says the SRTSH is a StrongArm extension, the quickref card I have is plain ARM only. Which would indicate that the MS compiler isn't StrongARM specific.

As for the overflow, the compiler can't be sure that the programmer is smart enough to prevent overflow, so it has to cover it's bases. Since MS didn't provide us with very much choices for the level of optimization, the compiler has to take the safe road. In this case, the overflow check is done right before the write, but since there are so many ways to check for overflow, it would be difficult for a compiler to check for any possible overflow check. As a result, the only way to tell it not to would be to use a #pragma statement or something like that, but the MS compiler doesn't have one.

The writeback (I wasn't aware of that term for it, which is why I brought it up after you already did) is probably only used by the compiler when you use "*(p++)=a" instead of "*p=a; p++;". Unlike overflow checks, detecting this is relatively easy, so there isn't really any excuse.

My opinion is that the compiler isn't that bad, but it does have a few issues nonetheless. Most of the problems it does have are excusable for various reasons.
I've not had my coffee so far this morning, but I reckon the shifts the compiler does are entirely useless for the store. They in no way help overflow, or check for overflow (in this instance).

"Without the shifts, either the sign would be lost,"


No it wouldn't.

Let's go through this... let's have two 16-bit values, -100 and 100:

A=1111111110011100 (=-100)

B=0000000001100100 (=100)

So what it's doing is a logical shift left (in a 32-bit register):

A=1111111110011100 0000000000000000
B=0000000001100100 0000000000000000

and an arithmetic shift right (carry the top bit across *16* bits only):

A=1111111111111111 1111111110011100
B=0000000000000000 0000000001100100

Which leaves us, err, exactly where we started for the 16-bit store. If the value HAS overflowed the 16-bits then then that still doesn't help us because we're still NOT affecting the bottom 16 bits in any way. So, a:

STRH R1,[R0]

Is *the same* as:

MOV R1,R1,LSL #16
MOV R1,R1,ASR #16
STRH R1,[R0]

We don't give a toss what's in the top 16 bits for the purposes of half-word storage because we don't store them. The only time it would benefit us is when we're switching from a 16-bit representation to a 32-bit representation. If R1 is used afterwards then it's sensible for the compiler to shunt it so it fits a 32-bit register .. if it isn't then MS/the compiler have screwed up (but I think that R1 should've *already* been in a 32-bit representation so it could handle negatives properly...).

Cheers,

Refractor
I should add that it all hinges on the way that the compiler deals with 16-bit values.
Isn't the ARM processor significantly faster when performing ops on 32-bit values over 16-bit values? I seem to recall reading that StrongARM was highly optimized to 32-bit ops.
Yes.

The registers are all 32-bit, for a start. Also, on some ARM architectures writing a 16-bit word necessitated reading a 32-bit word, munging it to put the new 16-bits in there, and storing it again. Not nice.

Try not to use 16-bit values for anything speed-critical. About the only exception is the video bank and other (large) stored data which may NEED to be 16-bit in C to save space or to be the necessary datatype (I shudder to think what the compiler is doing when it's writing a pixel at a time).