# [fpc-devel] Patch to speed up Uppercase/Lowercase functions

Martin Schreiber fpmse at bluewin.ch
Sun Jun 12 14:06:35 CEST 2005

```On Sunday 12 June 2005 08.48, DaniĆ«l Mantione wrote:
> I can't figure out...

loop:

//flags for chars \$80..\$ff are removed later so we have
// only to care about chars \$00..\$7f

ch1:=pinteger(p)^;    //original char
ch3:=\$7F7F7F7F;       //msb remove mask

ch2:=ch1;             //copy of original
ch3:=ch3 and ch1;     //originalchars with msb 0 -> <= \$7f

ch2:=ch2 xor (-1);    //complement

ch3:=ch3 + \$25252525; //msb 1 for all chars > \$7f - \$25 = \$5a -> >'Z'
//\$00..\$5a -> \$25..\$7f
//\$5b..\$7f -> \$80..\$a4

ch2:=ch2 and \$80808080; //msb 1 for all chars < \$80
//to remove flags of chars > \$7f

ch3:=ch3 and \$7F7F7F7F; //(chars mod \$80 + \$25) mod \$80
//\$00..\$5a -> \$25..\$7f
//\$5b..\$7f -> \$80..\$a4 -> \$00..\$24

ch3:=ch3 + \$1A1A1A1A;   //original + \$25 + \$1a = original + \$3f
//-> msb 1 for all chars > \$7f - \$3f = \$40 -> >='A'
//\$00..\$5a -> \$25..\$7f -> \$3f..\$99

//     '@'
//\$00..\$40 -> \$25..\$65 -> \$3f..\$7f             msb 0

//'A'  'Z'
//\$41..\$5a -> \$66..\$7f -> \$80..\$99             msb 1

//'['
//\$5b..\$7f -> \$80..\$a4 -> \$00..\$24 ->\$1a..\$3e  msb 0
inc(p,4);

ch3:=ch3 and ch2;
//remove flags for chars > \$7f, remove bit 0..6

if cardinal(p)>=cardinal(term) then goto last;

ch3:=ch3 shr 2;        //\$80 -> \$20 -> shift 'A' to 'a'
ch1:=ch1 + ch3;        //'A'..'Z' -> 'a'..'z'
pinteger(p+dist)^:=ch1;
goto loop;

Ingeniously!

Martin

```