New SYSTEM functions

Topics related to the use of Oberon language features
Locked
cfbsoftware
Site Admin
Posts: 525
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

New SYSTEM functions

Post by cfbsoftware » 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:

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;
e.g.

Code: Select all

MinElement({5..25}) = 5
MaxElement({2, 4, 6, 8} = 8
Special cases which you might want to handle separately are:

Code: Select all

MinElement({}) = 32
MaxElement({}) = -1
The resulting Thumb-2 code for each procedure consists of just 3 executable instructions:

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

Locked