User:Strife Onizuka/int2hex

From Second Life Wiki
< User:Strife Onizuka
Revision as of 12:44, 9 March 2023 by Gwyneth Llewelyn (talk | contribs) (Replaced <source> with <syntaxhighlight>)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

I wrote these more for the fun of it, an exercise in cleverness, than to be useful. The design goal was to not use a loop, to make the function operate in constant time. I expect they are worse in performance than Int2Hex. The lesson to be learned from this code is that data can be transformed in non-obvious ways.

I am of the mind that you don't need the dedicated functions of Hex2Int but they do show how to reverse the process, which is good. It's more costly to call the function then it is to inline the functions functionality.

string int2hex(integer I)
{//0x prefix version
    //0xABCDEFGH
    integer c = llCeil(llLog((I | 15 | (I >> 1)) & 0x7FFFFFFF) / 2.7725887222397812376689284858327) + 1;
    I = I << ((9 - c) << 2);
    integer A = (I >> 14) & 0x0003C000;
    integer B = (I >> 16) & 0x00000F00;
    integer C = (I >> 18) & 0x0000003C;
    integer D = (I << 10) & 0x3C000000;
    integer E = (I <<  8) & 0x00F00000;
    integer F = (I <<  6) & 0x0003C000;
    integer G = (I <<  4) & 0x00000F00;
    integer H = (I <<  2) & 0x0000003C;
 
    return 
        llGetSubString(
            llInsertString(
                llIntegerToBase64(
                    A + B + C + 0xD31D34D3//lowercase-x=0xD31D34D3, uppercase-X=0xD17D34D3
                    - (0x000F8000 * (A / 0x00028000))//lowercase=0x00090000, uppercase=0x000F8000
                    - (0x00003E00 * (B / 0x00000A00))//lowercase=0x00002400, uppercase=0x00003E00
                    - (0x000000F8 * (C / 0x00000028))//lowercase=0x00000090, uppercase=0x000000F8
                ),
                5,
                llIntegerToBase64(
                    D + E + F + G + H + 0xD34D34D3
                    - (0xF8000000 * (D / 0x28000000))//lowercase=0x90000000, uppercase=0xF8000000
                    - (0x03E00000 * (E / 0x00A00000))//lowercase=0x02400000, uppercase=0x03E00000
                    - (0x000F8000 * (F / 0x00028000))//lowercase=0x00090000, uppercase=0x000F8000
                    - (0x00003E00 * (G / 0x00000A00))//lowercase=0x00002400, uppercase=0x00003E00
                    - (0x000000F8 * (H / 0x00000028))//lowercase=0x00000090, uppercase=0x000000F8
                )
            ),
            0,
            c
        );
}

string int2hex(integer I)
{//pure hex version
    //ABCDEFGH
    integer A = (I >>  2) & 0x3C000000;
    integer B = (I >>  4) & 0x00F00000;
    integer C = (I >>  6) & 0x0003C000;
    integer D = (I >>  8) & 0x00000F00;
    integer E = (I << 14) & 0x3C000000;
    integer F = (I << 12) & 0x00F00000;
    integer G = (I << 10) & 0x0003C000;
    integer H = (I <<  8) & 0x00000F00;
 
    return llGetSubString(
            llInsertString(
                llIntegerToBase64(
                    A + B + C + D + 0xD34D3400
                    - (0xF8000000 * (A / 0x28000000))//lowercase=0x90000000, uppercase=0xF8000000
                    - (0x03E00000 * (B / 0x00A00000))//lowercase=0x02400000, uppercase=0x03E00000
                    - (0x000F8000 * (C / 0x00028000))//lowercase=0x00090000, uppercase=0x000F8000
                    - (0x00003E00 * (D / 0x00000A00))//lowercase=0x00002400, uppercase=0x00003E00
                ),
                4,
                llIntegerToBase64(
                    E + F + G + H + 0xD34D3400
                    - (0xF8000000 * (E / 0x28000000))//lowercase=0x90000000, uppercase=0xF8000000
                    - (0x03E00000 * (F / 0x00A00000))//lowercase=0x02400000, uppercase=0x03E00000
                    - (0x000F8000 * (G / 0x00028000))//lowercase=0x00090000, uppercase=0x000F8000
                    - (0x00003E00 * (H / 0x00000A00))//lowercase=0x00002400, uppercase=0x00003E00
                )
            ),
            8 - llCeil(llLog((I | 15 | (I >> 1)) & 0x7FFFFFFF) / 2.7725887222397812376689284858327),
            7
        );
}