Describe the bug
si_floorf() and si_ceilf() return the wrong results for negative numbers.
Also, si_ceilf() returns the wrong results for integers.
For example,
floorf(-0.5) should be -1.0, but si_floorf() returns 0.
ceilf(-0.5) should be 0, but si_ceilf() returns 1.
ceilf(2) should be 2, but si_ceilf() returns 3.
/** Floor function
*/
static inline __attribute__((optimize("Ofast"),always_inline))
float si_floorf(float x)
{
return (float)((uint32_t)x);
}
/** Ceiling function
*/
static inline __attribute__((optimize("Ofast"),always_inline))
float si_ceilf(float x)
{
return (float)((uint32_t)x + 1);
}
Test code:
#include<stdio.h>
#include<math.h>
#include "../common/utils/float_math.h"
int main(int argc, char **argv) {
printf("floorf(-0.5) = %f\n", floorf(-0.5));
printf("si_floorf(-0.5) = %f\n", si_floorf(-0.5));
printf("ceilf(-0.5) = %f\n", ceilf(-0.5));
printf("si_ceilf(-0.5) = %f\n", si_ceilf(-0.5));
printf("ceilf(2) = %f\n", ceilf(2));
printf("si_ceilf(2) = %f\n", si_ceilf(2));
}
Results:
floorf(-0.5) = -1.000000
si_floorf(-0.5) = 0.000000
ceilf(-0.5) = -0.000000
si_ceilf(-0.5) = 1.000000
ceilf(2) = 2.000000
si_ceilf(2) = 3.000000