PocketMatrix forums

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

Optimum boolean type for ARM / XScale

Can someone familiar with the ARM / XScale processor please provide feedback on this? I have boolean values I wish to store, which will have boolean conditional evaluation performed on them ( if (a) do_something; ). I wish to use minimal storage, but not by sacrificing performance.

Is there a substantial difference in processing time between evaulating long, short and char as a boolean?

Does signed / unsigned make a difference?

How much more expensive is masking individual bits in a long compared to the fastest method above? (if (val & 0x0010)) do_something;)

Thanks!

Dan East
Write a simple program to test which is the fastest. Like performing 100 000 if:s with each different method and time them.
Malmer wrote:
Write a simple program to test which is the fastest. Like performing 100 000 if:s with each different method and time them.


That is a really bad way to measure what is the fastest method. Extracting the profile info you seek from the context (program) rarely gives meaningful results.


Dan: How many of these booleans to you have? If you've got a few hundreds, then char might be good enough. But testing a char might be actually slower.

I would expect the clock cycle for a compare with 0 and a bit test to be the same. The difference is that the bit test might require an extra parameter (and thus increase the instruction size). I wish I knew more about the ARM instruction set (didn't have time too look at it yet).

One benefit of grouping multiple booleans in one integer is that you can test 32 of them at the same time. I don't know if this is of any use to you.

Refractor?
Sergey Chaban · 18 Dec 2002, 18:57 UTC
Masking: for mask values that are powers of 2 or something else that is possible ot encode in a single ARM opcode, it's no more expensive than direct compare (i.e. one instruction to load val and another for bittest). But expression like (val & 0xAAAAAAAA) would generate approx. 5 instructions.

If your booleans are members of a struct/class I think it would be better to just use int32-size storage, if they are an array, then byte/short could be more efficient. If you use byte-sized data-members in your struct, it will only provide some benefit (size decrease) if you specify 1-byte packing - in such case you would need to take care of alignment issues for adjacent members.

HTH
I would expect the clock cycle for a compare with 0 and a bit test to be the same.


Indeed.

Compare the TST and CMP instructions (TEQ also if I'm being complete).

TST R0,#1<<10

for example (1 cycle).

The difference is that the bit test might require an extra parameter (and thus increase the instruction size).


I'm not sure you mean size there - all ARM instructions are 32-bit on the StrongARM (and other ARMs with Thumb support still have fixed-length instructions of 32 or 16 bits for ARM and Thumb respectively).

Back to the original question:
I wish to use minimal storage, but not by sacrificing performance.


It really depends on what you're doing. A bit-field using 32-bit words (unsigned is more intuitive for bit-fields) should cover you nicely and you can do it cheaply using the ARM's "free" (until XScale) barrel-shifter, I imagine.

Is there a substantial difference in processing time between evaulating long, short and char as a boolean?


My guess would be:
No difference for the compare.

However, it takes the same amount of time to load each item through the cache-line and the "larger" your padding (for a byte you're wasting 7 bits, for a 32-bit word you're wasting 31 bits), the less "efficient it is cache/memory-wise.

Does signed / unsigned make a difference?


Loading and storing signed bytes / half-words might give you an unwelcome performance penalty. Unsigned is always ok.

How much more expensive is masking individual bits in a long compared to the fastest method above? (if (val & 0x0010)) do_something;)


Not much - I'd go for that method every time. If you have a bit-field then to get a bit you'd just do something like:

Code:

; R0 = number of bit in field
; R1 = address of bit-field
; R2 = 1

; Find out which word we want to load
mov r4,r0,LSR #5
; load the word ('extra' penalty on XScale for scaled register I think)
ldr r3,[r1,r4,LSL #2]
and r4,r0,#31 ; which bit in the word do we want?

tst r3,r2,lsl r4 ; (might be a penalty for a register shift)
; processor flags are now set accordingly if your bit is set or not


(A compiler should make a fairly good job of optimising C code to do the same thing).

Obviously it's much cleaner if you want to iterate sequentially over the bitfield.

Cheers,

Ref.