As I said GPP uses CLZ instruction in several places. This instr counts number of leading zero bits in its operand.
So CLZ R0, R1 will set ARM register R0 to the number of leading zeros in the register R1.
ARM architecture also supports conditional prefixes on each instruction so CLZNE r0, r1 would execute CLZ only if Z flag is set to 0. Also all ARM instructions are 32-bits in size.
So the trick is to trap illegal operation exception (using Win32 Structured Exception Handling) and modify code memory to replace CLZ with another instruction that will perform the same operation on StrongARM, and then continue execution from the same place.
There is no equivalent instr on a StrongARM, but because all instrs are 32-bit we could use BL (branch with link, a subroutine call) to call appropriate routine to count zeroes. We need several routines to support all register combinations (e.g. CLZ r0, r1; CLZ r5, r3 etc.).
These routines must be coded in assembler because their parameters and return values are arbitrary registers, whereas C code uses ARM procedure call standard - params in R0-R3, results in R0-R1.
Also these routines must save/restore CPU flags.
So at the higher level you have code like this, there handle_ex modifies the code etc.
- Code:
__try {
your_code_that_uses_GPP();
} __except(GetExceptionCode() == STATUS_ILLEGAL_INSTRUCTION
? (handle_ex(GetExceptionInformation()), EXCEPTION_CONTINUE_EXECUTION) : 0) {}
Performance:
Exception processing overhead is only present the first time instr is executed.
Because ARM istrs all have conditional prefixes, we replace CLZcc with BLcc (i.e. call with the same conditional code).
This is not trivial as you can see, so I could release the code for such emulation. Actual code I use is slightly different, because StrongARM routines to calc bits are not hard-coded but instead generated on demand (this is all part of a larger library). Just to support GPP it's possible to use simpler code than that.
In my tests after CLZ replacement performance of the DivHP, InvHP, SqrtHP routines are still faster than straightforward C implementation.
In fact I only use GPP to compare its performance with my own library for fix/float calcs. That library has optimized routines for RISC CPUs and emulated GPP on StrongARM is only marginally slower on those routines (div, inv, sqrt) optimized for StrongARM. So it's okay I guess to use such hack if you want to use GPP and at the same time maintain compatibility with older machines.
SC