• sin measurments

    From fir@profesor.fir@gmail.com to comp.lang.c on Mon Sep 28 19:27:45 2026
    From Newsgroup: comp.lang.c

    i lonk time not measured a code times but today i felt
    i may dosome amll testing session becouse i forgot my previous
    conclusions (even thought the previous was on teh same old weak i3 4150)

    (its to old now and i will but some new pc i hope in this or next year)

    so

    float sin_tests()
    {
    InitSinTable();

    float sum = 0;

    if(w_pressed) for(int i=0; i<1000000; i++) { sum += sin ((TAU*i/1000000.)); }
    else for(int i=0; i<1000000; i++) { sum += fast_sin ((TAU*i/1000000.)); }


    return sum;

    }


    for normal sin its 70 ms (to 70 ns per one - quite long)


    the taylor series


    float fast_sin(float x)
    {
    x = fmodf(x, 6.28318531f);

    if (x > 3.14159265f)
    x -= 6.28318531f;
    else if (x < -3.14159265f)
    x += 6.28318531f;

    if (x > 1.57079633f)
    x = 3.14159265f - x;
    else if (x < -1.57079633f)
    x = -3.14159265f - x;

    float x2 = x * x;

    return x * (1.0f - x2 * (1.0f / 6.0f -
    x2 * (1.0f / 120.0f -
    x2 * (1.0f / 5040.0f -
    x2 * (1.0f / 362880.0f -
    x2 * (1.0f / 39916800.0f))))));
    }

    20 ms - sad this fmod is so heavy..

    (out of suriosity i replaced fmod line by x = x - floorf(x); just to see the difference and its 14 ms then - but i was not iding full test in
    such case maybe later)

    replacing fmod by 2 whiles


    float fast_sin(float x)
    {

    while (x > 3.14159265f) x -= 6.28318531f;
    while (x < -3.14159265f) x += 6.28318531f;


    if (x > 3.14159265f)
    x -= 6.28318531f;
    else if (x < -3.14159265f)
    x += 6.28318531f;

    if (x > 1.57079633f)
    x = 3.14159265f - x;
    else if (x < -1.57079633f)
    x = -3.14159265f - x;

    float x2 = x * x;

    return x * (1.0f - x2 * (1.0f / 6.0f -
    x2 * (1.0f / 120.0f -
    x2 * (1.0f / 5040.0f -
    x2 * (1.0f / 362880.0f -
    x2 * (1.0f / 39916800.0f))))));
    }

    10.5 ms - its bagan to be not bad

    for shorter taylor

    float fast_sin(float x)
    {
    while (x > 3.14159265f) x -= 6.28318531f;
    while (x < -3.14159265f) x += 6.28318531f;

    if (x > 1.57079633f)
    x = 3.14159265f - x;
    else if (x < -1.57079633f)
    x = -3.14159265f - x;

    float x2 = x * x;

    return x * (1.0f - x2 * (1.0f / 6.0f -
    x2 * (1.0f / 120.0f - x2 / 5040.0f)));
    }

    9 ms


    tabelarized with arbitrary table size nad linear interpolation
    (said to have very good quality of results


    enum { SIN_TABLE_MAX = 10000 };

    static float sin_table[SIN_TABLE_MAX];

    void InitSinTable()
    {
    static initialised = 0; if(initialised) return;
    initialised = 1;

    int i;
    for (i = 0; i < SIN_TABLE_MAX; i++)
    sin_table[i] = sinf(TAU * i / (float)SIN_TABLE_MAX);
    }

    float fast_sin(float x)
    {
    float p = x * (float)SIN_TABLE_MAX / TAU;

    int i = (int)p;
    float f = p - i;

    i %= SIN_TABLE_MAX;
    if (i < 0) i += SIN_TABLE_MAX;

    int j = i + 1;
    if (j == SIN_TABLE_MAX) j = 0;

    return sin_table[i] + (sin_table[j] - sin_table[i]) * f;
    }


    8 ms - better than taylor


    for table being power of 2

    enum { SIN_TABLE_BITS = 14, SIN_TABLE_MAX = 1 << SIN_TABLE_BITS };

    static float sin_table[SIN_TABLE_MAX];
    static float sin_table_scale;

    void InitSinTable()
    {
    static int initialised = 0;
    if (initialised) return;
    initialised = 1;

    sin_table_scale = SIN_TABLE_MAX / TAU;

    int i;
    for (i = 0; i < SIN_TABLE_MAX; i++)
    sin_table[i] = sinf(TAU * i / (float)SIN_TABLE_MAX);
    }

    float fast_sin(float x)
    {
    float p = x * sin_table_scale;
    int i = (int)p;
    float f = p - i;
    i &= SIN_TABLE_MAX - 1;
    int j = (i + 1) & (SIN_TABLE_MAX - 1);
    return sin_table[i] + (sin_table[j] - sin_table[i]) * f;
    }


    5.2 ms (my test framework has overhead about 0.5 so its more like 4.7 ms strictly per loop turn



    quite nice....seems not dependant on how many bits i set ( set 8 and 20)


    no linear factor

    float fast_sin(float x)
    {
    int i = (int)(x * sin_table_scale + 0.5f);
    return sin_table[i & (SIN_TABLE_MAX - 1)];
    }

    3.2 ms (about as it moves like 2.9 to 3.6 or about) (-.5 ms fo framework
    it makes neat 2.7 ns for turn)


    the winners are last two - fot no time critical i would probably use the linear for typical - time critical the last one)

    (ao helped in tests, tnx ai)




    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From fir@profesor.fir@gmail.com to comp.lang.c on Mon Sep 28 19:45:22 2026
    From Newsgroup: comp.lang.c

    i measured also this hypot (i was not aware of this)


    float sin_tests()
    {

    float sum = 0;


    if(w_pressed) for(int i=0; i<1000000; i++)
    {
    float a = 3*i/1000000.;
    float b = 7*i/1000000.;

    sum += sqrt (a*a+b*b);
    }
    else
    for(int i=0; i<1000000; i++)
    {
    float a = 3*i/1000000.;
    float b = 7*i/1000000.;

    sum += hypot (a, b);

    }

    return sum;

    }

    and what it showed sqrt being unexpectedly slower than sin (1) about 100
    ms and hypot quite fast 5.5 ms

    this makes all bothsin and sqrt logically almost unusable in my applications..wonder how with atan2..but im to weary to test now



    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From fir@profesor.fir@gmail.com to comp.lang.c on Mon Sep 28 21:46:18 2026
    From Newsgroup: comp.lang.c

    i yet checked

    typedef struct { float x, y; } float2;

    float2 fast_cos_sin(float a)
    {
    int i = (int)(a * sin_table_scale + 0.5f) & (SIN_TABLE_MAX - 1);
    int j = (i + SIN_TABLE_MAX / 4) & (SIN_TABLE_MAX - 1);

    return (float2) {sin_table[j], sin_table[i]};
    }

    for
    // for(int i=0; i<1000000; i++)
    // sum += fast_cos_sin(TAU*i/1000000.) .x +
    fast_cos_sin(TAU*i/1000000.) .y;

    cos this would be probably most effective t count 2 at once and
    to chck if only some uses .x it would bring overhead of .y

    and the result using only .x or .y is same fast as sole cos/sin
    (for soem reason it was even like faster this tme more like 2.9 than 3.2)

    so its optimised and using both was like 3.8 (-.5 overhead of framework
    its 1.6 ns per one effectively so this is surisingly fast now)
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From fir@profesor.fir@gmail.com to comp.lang.c on Mon Sep 28 23:28:00 2026
    From Newsgroup: comp.lang.c

    fir pisze:
    i measured also this hypot (i was not aware of this)


    float-a sin_tests()
    {

    -a-a-a-afloat sum-a = 0;


    -a-a-a-a if(w_pressed)-a-a-a-a-a for(int i=0;-a i<1000000; i++)
    -a-a-a-a {
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float a = 3*i/1000000.;
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float b = 7*i/1000000.;

    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a sum += sqrt (a*a+b*b);
    -a-a-a-a }
    -a-a-a-a else
    -a-a-a-a-a-a-a-a for(int i=0;-a i<1000000; i++)
    -a-a-a-a-a-a-a-a {
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float a = 3*i/1000000.;
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float b = 7*i/1000000.;

    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a sum += hypot (a, b);

    -a-a-a-a-a-a-a-a }

    -a-a-a-a-a return sum;

    }

    and what it showed sqrt being unexpectedly slower than sin (1) about 100
    ms and hypot quite fast 5.5 ms



    well bug mistake here sqrtf showed to be very fast (its even faster than
    this 5.5 ms now its about 2.5 here) in turn what is tragically slow it
    this hypot so this 100 ms is this hypot not sqrt..sqrt showed totally
    fast (85 now)



    this makes all bothsin and sqrt logically almost unusable in my applications..wonder how with atan2..but im to weary to test now




    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From fir@profesor.fir@gmail.com to comp.lang.c on Mon Sep 28 23:29:13 2026
    From Newsgroup: comp.lang.c

    fir pisze:
    fir pisze:
    i measured also this hypot (i was not aware of this)


    float-a sin_tests()
    {

    -a-a-a-a-afloat sum-a = 0;


    -a-a-a-a-a if(w_pressed)-a-a-a-a-a for(int i=0;-a i<1000000; i++)
    -a-a-a-a-a {
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float a = 3*i/1000000.;
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float b = 7*i/1000000.;

    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a sum += sqrt (a*a+b*b);
    -a-a-a-a-a }
    -a-a-a-a-a else
    -a-a-a-a-a-a-a-a-a for(int i=0;-a i<1000000; i++)
    -a-a-a-a-a-a-a-a-a {
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float a = 3*i/1000000.;
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float b = 7*i/1000000.;

    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a sum += hypot (a, b);

    -a-a-a-a-a-a-a-a-a }

    -a-a-a-a-a-a return sum;

    }

    and what it showed sqrt being unexpectedly slower than sin (1) about
    100 ms and hypot quite fast 5.5 ms



    well bug mistake here sqrtf showed to be very fast (its even faster than this 5.5 ms now its about 2.5 here) in turn what is tragically slow it
    this hypot so this 100 ms is this hypot not sqrt..sqrt showed totally
    fast (85 now)

    i mean hypot 85 sqrt 2.5




    this makes all bothsin and sqrt logically almost unusable in my
    applications..wonder how with atan2..but im to weary to test now





    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From fir@profesor.fir@gmail.com to comp.lang.c on Tue Sep 29 00:03:32 2026
    From Newsgroup: comp.lang.c

    fir pisze:
    fir pisze:
    fir pisze:
    i measured also this hypot (i was not aware of this)


    float-a sin_tests()
    {

    -a-a-a-a-afloat sum-a = 0;


    -a-a-a-a-a if(w_pressed)-a-a-a-a-a for(int i=0;-a i<1000000; i++)
    -a-a-a-a-a {
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float a = 3*i/1000000.;
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float b = 7*i/1000000.;

    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a sum += sqrt (a*a+b*b);
    -a-a-a-a-a }
    -a-a-a-a-a else
    -a-a-a-a-a-a-a-a-a for(int i=0;-a i<1000000; i++)
    -a-a-a-a-a-a-a-a-a {
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float a = 3*i/1000000.;
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float b = 7*i/1000000.;

    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a sum += hypot (a, b);

    -a-a-a-a-a-a-a-a-a }

    -a-a-a-a-a-a return sum;

    }

    and what it showed sqrt being unexpectedly slower than sin (1) about
    100 ms and hypot quite fast 5.5 ms



    well bug mistake here sqrtf showed to be very fast (its even faster
    than this 5.5 ms now its about 2.5 here) in turn what is tragically
    slow it this hypot so this 100 ms is this hypot not sqrt..sqrt showed
    totally fast (85 now)

    i mean hypot 85 sqrt 2.5



    ok the mystery soewhat resolved why i got sqrt before 5.5 and now 2.5 it
    seem just be difference among sqrt and sqrtf )i didnt realized
    probably double version is called when i pass a float

    atan2 has even more difference aor atan2 65 ms/ns here
    45 ms/ns for atan2f


    those both fast atans


    float fast_atan2_(float y, float x)
    {
    float ax = fabsf(x);
    float ay = fabsf(y);
    float a;
    float r;

    if(ax > ay)
    {
    r = ay / ax;
    a = r * (1.0f - 0.2146018f * r * r);
    }
    else
    {
    r = ax / ay;
    a = 1.5707963f - r * (1.0f - 0.2146018f * r * r);
    }

    if(x < 0) a = 3.14159265f - a;
    if(y < 0) a = -a;

    return a;
    }

    float fast_atan2(float y, float x)
    {
    // float PI = 3.14159265358979323846f;
    float PI2 = 1.57079632679489661923f;

    float ay = fabsf(y) + 1e-10f;
    float r;

    if(x >= 0)
    {
    r = (x - ay) / (x + ay);
    r = PI2 * 0.5f - r * (0.9817f - 0.1963f * r * r);
    }
    else
    {
    r = (x + ay) / (ay - x);
    r = 3.0f * PI2 * 0.5f
    - r * (0.9817f - 0.1963f * r * r);
    }

    return y < 0 ? -r : r;
    }


    around 5.5 ms/ns


    it seems i need to recheck the sin and now it is

    sin 57 ms sinf 37 ms (about as its slightly oscillating)

    previously it was around 70 ms as hypit was around 100 not 85

    maybe it was becouse i was playin youtube in bacground while doin tests
    i assumed it is on other core coz it didnt seem to interfere but
    maybe that ould explain those differences

    i also like got an observation like cpu would hold some global state
    that changes but im not sure as sometimes like those primitives
    could have a somewhat different times..not quite as i said this
    suspiction on using second core slow things down but YET some global
    state maybe fpu flags..i dont know


    but the results are as i said (at least for my old cpu)

    sqrtf - VERY FAST - USE
    sinf/sin - SLOW - DONT USE (sinf eventually 37 is maybe not a tragedy
    but not for time valuable code)
    atan2/atanf - SLOW DONT USE
    hypot - TERRIBLY SlOW DONT USE

    tabelarized sin - VERY FAST - USE
    algebraic atan2 - FAST - USE (not checked for error but those
    sccumulative sums like 0.1 % difference, so not bad)










    this makes all bothsin and sqrt logically almost unusable in my
    applications..wonder how with atan2..but im to weary to test now






    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From fir@profesor.fir@gmail.com to comp.lang.c on Tue Sep 29 00:48:40 2026
    From Newsgroup: comp.lang.c

    overally those test also gives me to think how to hold angle values

    my prsent opinion is angles shuld be 0...1.0

    previously i used degrees and normalized it to -180 .. 180 but 0..1
    seem smuch more logical fmods are quite heavy here it seems about 10 ns
    per lloop turn those

    float frac(float x) { return x - floorf(x);}

    also heavy like 5 ns but cheeper and simpler.. and maybe this taylor
    series for sin would even more like -0.5 to 0.5 (i dont remember but
    some codes i run seemd so) the 0..1 is more logical and one logical
    option i guess

    it gas advantage you want radian you multiply f*TAU, you want degrees
    use f*360..


    so if i will store those functions in my small library i will need them
    to be revritten this way

    there is also problem of angle orientation in mathematics you got axis
    x,y x right y up which is wrong in programming it is x right y down
    which is right here..in math angle is in left side (CCW) which is imo
    wrong but in computing it is in right (CW) which is right...

    the problem is i wanted 0 degrees to be up like in a clock but now i
    think it would bring too much trouble..spo probably 0 degrees must be
    right and 90 degrees must be down

    its maybenot pleasant but it seems it must be this way (though maybe im
    wrong)
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From fir@profesor.fir@gmail.com to comp.lang.c on Tue Sep 29 01:30:04 2026
    From Newsgroup: comp.lang.c

    i yet checked those of taylor series if its faster on 0..1 angles
    ('turns') than on radians

    thiose taylor are all slower than tabelarized but turn versions showed
    to be faster




    float fast_sin1M(float x) //14.7 ns
    {
    x = fmodf(x + 3.14159265f, 6.28318531f);
    if(x < 0) x += 6.28318531f;
    x -= 3.14159265f;

    if(x > 1.57079633f)
    x = 3.14159265f - x;
    else if(x < -1.57079633f)
    x = -3.14159265f - x;

    float x2 = x * x;

    return x * (1.0f - x2 * (1.0f / 6.0f -
    x2 * (1.0f / 120.0f - x2 / 5040.0f)));
    }

    float fast_sin1oM(float x) //6.9 ns
    {
    x -= (int)x;
    if(x < 0) x += 1.0f;

    if(x > 0.5f) x -= 1.0f;

    if(x > 0.25f)
    x = 0.5f - x;
    else if(x < -0.25f)
    x = -0.5f - x;

    x *= 6.28318531f;

    float x2 = x * x;

    return x * (1.0f - x2 * (1.0f / 6.0f -
    x2 * (1.0f / 120.0f - x2 / 5040.0f)));
    }



    float fast_sin1o(float x) //5.9 ns
    {
    while(x > 0.5f) x -= 1.0f;
    while(x < -0.5f) x += 1.0f;

    if(x > 0.25f)
    x = 0.5f - x;
    else if(x < -0.25f)
    x = -0.5f - x;

    x *= 6.28318531f;

    float x2 = x * x;

    return x * (1.0f - x2 * (1.0f / 6.0f -
    x2 * (1.0f / 120.0f - x2 / 5040.0f)));
    }

    float fast_sin1(float x) // 6.2 ns
    {
    while (x > 3.14159265f) x -= 6.28318531f;
    while (x < -3.14159265f) x += 6.28318531f;

    if (x > 1.57079633f)
    x = 3.14159265f - x;
    else if (x < -1.57079633f)
    x = -3.14159265f - x;

    float x2 = x * x;

    return x * (1.0f - x2 * (1.0f / 6.0f -
    x2 * (1.0f / 120.0f - x2 / 5040.0f)));
    }


    if im not wrong it proves that radians suck as to efficiency
    and turns rule
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Chris M. Thomasson@chris.m.thomasson.1@gmail.com to comp.lang.c on Tue Sep 29 16:41:36 2026
    From Newsgroup: comp.lang.c

    On 9/28/2026 10:27 AM, fir wrote:
    [...]

    Play with sqrt(2) a square and some circles.
    --- Synchronet 3.22a-Linux NewsLink 1.2