[webkit-dev] Proposal for serializing alpha channel values; request for algorithm help

Gavin Barraclough barraclough at apple.com
Mon Nov 2 02:34:17 PST 2015


> On Nov 1, 2015, at 7:40 PM, Darin Adler <darin at apple.com> wrote:
> 
> 3) Can you help me come up with a super-efficient algorithm to do this serialization?

Oh – I guess if you just store the fractions as BCD & unpack you can get the table size down to 512 bytes.

If you decide against truncating the values & still want to store 6 digits of precision you could do so in a 1K table (and should be faster serialization than normal float to ASCII conversion).

cheers,
G.

void unsignedCharToFloatString(unsigned char x, char* out)
{
    unsigned short data = unsignedCharToFloatData[x];

    if (data > 1) {
        *out++ = '0';
        *out++ = '.';
        for (unsigned i = 0; i < 4; ++i) {
            unsigned value = (data >> 12) & 0xf;
            if (value != 0xf)
                *out++ = '0' + value;
            data <<= 4;
        }
    } else
        *out++ = '0' + data;

    *out = '\0';
    return;
}

static const unsigned short unsignedCharToFloatData[256] = {
    0,
    0xf003,
    0xf007,
    0xff01,
    0xf015,
    0xf019,
    0xf023,
    0xf027,
    0xff03,
    0xf035,
    0xf039,
    0xf043,
    0xf047,
    0xff05,
    0xf054,
    0xf058,
    0xf062,
    0xf066,
    0xff07,
    0xf074,
    0xf078,
    0xf082,
    0xf086,
    0xff09,
    0xf094,
    0xf098,
    0xfff1,
    0xf105,
    0xf109,
    0xf113,
    0xf117,
    0xff12,
    0xf125,
    0xf129,
    0xf133,
    0xf137,
    0xff14,
    0xf145,
    0xf149,
    0xf152,
    0xf156,
    0xff16,
    0xf164,
    0xf168,
    0xf172,
    0xf176,
    0xff18,
    0xf184,
    0xf188,
    0xf192,
    0xf196,
    0xfff2,
    0xf203,
    0xf207,
    0xff21,
    0xf215,
    0xf219,
    0xf223,
    0xf227,
    0xff23,
    0xf235,
    0xf239,
    0xf243,
    0xf247,
    0xff25,
    0xf254,
    0xf258,
    0xf262,
    0xf266,
    0xff27,
    0xf274,
    0xf278,
    0xf282,
    0xf286,
    0xff29,
    0xf294,
    0xf298,
    0xfff3,
    0xf305,
    0xf309,
    0xf313,
    0xf317,
    0xff32,
    0xf325,
    0xf329,
    0xf333,
    0xf337,
    0xff34,
    0xf345,
    0xf349,
    0xf352,
    0xf356,
    0xff36,
    0xf364,
    0xf368,
    0xf372,
    0xf376,
    0xff38,
    0xf384,
    0xf388,
    0xf392,
    0xf396,
    0xfff4,
    0xf403,
    0xf407,
    0xff41,
    0xf415,
    0xf419,
    0xf423,
    0xf427,
    0xff43,
    0xf435,
    0xf439,
    0xf443,
    0xf447,
    0xff45,
    0xf454,
    0xf458,
    0xf462,
    0xf466,
    0xff47,
    0xf474,
    0xf478,
    0xf482,
    0xf486,
    0xff49,
    0xf494,
    0xf498,
    0xfff5,
    0xf505,
    0xf509,
    0xf513,
    0xf517,
    0xff52,
    0xf525,
    0xf529,
    0xf533,
    0xf537,
    0xff54,
    0xf545,
    0xf549,
    0xf552,
    0xf556,
    0xff56,
    0xf564,
    0xf568,
    0xf572,
    0xf576,
    0xff58,
    0xf584,
    0xf588,
    0xf592,
    0xf596,
    0xfff6,
    0xf603,
    0xf607,
    0xff61,
    0xf615,
    0xf619,
    0xf623,
    0xf627,
    0xff63,
    0xf635,
    0xf639,
    0xf643,
    0xf647,
    0xff65,
    0xf654,
    0xf658,
    0xf662,
    0xf666,
    0xff67,
    0xf674,
    0xf678,
    0xf682,
    0xf686,
    0xff69,
    0xf694,
    0xf698,
    0xfff7,
    0xf705,
    0xf709,
    0xf713,
    0xf717,
    0xff72,
    0xf725,
    0xf729,
    0xf733,
    0xf737,
    0xff74,
    0xf745,
    0xf749,
    0xf752,
    0xf756,
    0xff76,
    0xf764,
    0xf768,
    0xf772,
    0xf776,
    0xff78,
    0xf784,
    0xf788,
    0xf792,
    0xf796,
    0xfff8,
    0xf803,
    0xf807,
    0xff81,
    0xf815,
    0xf819,
    0xf823,
    0xf827,
    0xff83,
    0xf835,
    0xf839,
    0xf843,
    0xf847,
    0xff85,
    0xf854,
    0xf858,
    0xf862,
    0xf866,
    0xff87,
    0xf874,
    0xf878,
    0xf882,
    0xf886,
    0xff89,
    0xf894,
    0xf898,
    0xfff9,
    0xf905,
    0xf909,
    0xf913,
    0xf917,
    0xff92,
    0xf925,
    0xf929,
    0xf933,
    0xf937,
    0xff94,
    0xf945,
    0xf949,
    0xf952,
    0xf956,
    0xff96,
    0xf964,
    0xf968,
    0xf972,
    0xf976,
    0xff98,
    0xf984,
    0xf988,
    0xf992,
    0xf996,
    1
};

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.webkit.org/pipermail/webkit-dev/attachments/20151102/8e611106/attachment-0001.html>


More information about the webkit-dev mailing list