New SYSTEM functions
Posted: Thu Aug 22, 2013 2:12 pm
v4.5 and later versions of Astrobe for Cortex-M3 has some new in-line SYSTEM functions which make use of Thumb-2 instructions that aren't exploited in the Oberon language. Two such instructions are CLZ (count leading zeros) and RBIT (reverse the bits in a word) which both execute in a single cycle.
Examples of their use are two very efficient functions to return the lowest value and the highest value in a set:
e.g.
Special cases which you might want to handle separately are:
The resulting Thumb-2 code for each procedure consists of just 3 executable instructions:
Examples of their use are two very efficient functions to return the lowest value and the highest value in a set:
Code: Select all
PROCEDURE* MaxElement(s: SET): INTEGER;
RETURN 31 - SYSTEM.CLZ(ORD(s))
END MaxElement;
PROCEDURE* MinElement(s: SET): INTEGER;
RETURN SYSTEM.CLZ(SYSTEM.RBIT(ORD(s)))
END MinElement;
Code: Select all
MinElement({5..25}) = 5
MaxElement({2, 4, 6, 8} = 8
Code: Select all
MinElement({}) = 32
MaxElement({}) = -1
Code: Select all
PROCEDURE* MinElement:
rbit r10,r11
clz r11,r10
bx lr
PROCEDURE* MaxElement:
clz r10,r11
rsbs.w r11,r10,31
bx lr