Rockbox.org home
release
dev builds
extras
themes manual
wiki
device status forums
mailing lists
IRC bugs
patches
dev guide



Rockbox mail archive

Subject: Optimizing to functions for a plugin

Optimizing to functions for a plugin

From: Joseph Garvin <k04jg02_at_gmail.com>
Date: Wed, 15 Oct 2008 13:09:47 -0500

There don't appear to be any builtin functions for converting strings
to floats or longs, so I wrote a couple, but they're very very rough.
I care less about precision than about speed. Anybody have tips on how
to speed them up?

static unsigned long string_to_long(const char* toConvert)
{
    unsigned long result = 0;
    const char* c;
    unsigned int i;
    unsigned long pow_of_10 = 1;

    for(i = 0; i < rb->strlen(toConvert) - 1; ++i)
        pow_of_10 *= 10;

    for(c = toConvert; *c != '\0'; ++c) {
        result += char_to_long(*c) * pow_of_10;
        pow_of_10 /= 10;
    }

    return result;
}

static float string_to_float(const char* toConvert)
{
    double result = 0.0;
    char convert_buffer[100];
    const char* c;
    int decimal_location = 0;
    int decimal_places = 0;
    unsigned long pow_of_10 = 1;
    int i = 0;

    for(c = toConvert; *c != '.'; ++c)
        decimal_location++;

    rb->memcpy(convert_buffer, toConvert, decimal_location);
    convert_buffer[decimal_location] = '\0';

    result = string_to_long(convert_buffer);

    rb->strcpy(convert_buffer, toConvert + decimal_location + 1);

    decimal_places = rb->strlen(toConvert) - decimal_location - 1;

    for(i = 0; i < decimal_places; ++i)
        pow_of_10 *= 10;

    result += (double)string_to_long(convert_buffer) / (double)pow_of_10;

    return (float)result;
}
Received on 2008-10-15

Page template was last modified "Tue Sep 7 00:00:02 2021" The Rockbox Crew -- Privacy Policy