[fpc-devel] Fast ascii upper/lowercase

Christian Iversen chrivers at iversen-net.dk
Sat Jun 11 20:27:32 CEST 2005


With all the discussion about speed of lowercase/uppercase recently, I thought 
I'd chip in. I don't have the time to actually implement and test this, but I 
hope somebody else will. 

This semi-pseudocode should be a bit faster than what is currently done:

function lowercase(const value: string): pchar;
var
  X,L,C: Integer;
  B: Char;
begin
  C := length-of(value); 
  set-length(result, C);
  for X := 0 to C/4-1 do
  begin
    L = PIntegers(value)[X];
    // Char 1/4
    B := char(L and $FF);
    if (B in ['A'..'Z']) then
      result[x shl 2] := B or $20 else
      result[x shl 2] := B;
    // Char 2/4
    B := char((L shr 8) and $FF);
    if (B in ['A'..'Z']) then
      result[(x shl 2)+1] := B or $20 else
      result[(x shl 2)+1] := B;
    ..etc..
  end;
  ..fix remainging 0-3 chars..
end;

Basically, a simple loop unroll, with the twist that the fetch is collapsed to 
every 4th iteration. This, combined with the dynamic scheduling of modern 
CPUs, should increase the speed considerably. Since each iteration is very 
collapsable in terms of data-dependencies, this could work out to be quite 
fast. 

I could be wrong, of course :-)

Does somebody want to try this?

-- 
Regards,
Christian Iversen




More information about the fpc-devel mailing list