imlib/fmath: Fix fmath function inlining.

This commit is contained in:
Kwabena W. Agyeman 2024-02-18 16:22:29 -08:00 committed by iabdalkader
parent 3a2ffc5106
commit 99d22d4eb0
2 changed files with 81 additions and 85 deletions

View File

@ -9,9 +9,6 @@
* Fast approximate math functions.
*/
#include "fmath.h"
#include "omv_common.h"
#include CMSIS_MCU_H
#include "arm_math.h"
const float __atanf_lut[4] = {
-0.0443265554792128f, //p7
@ -20,83 +17,6 @@ const float __atanf_lut[4] = {
+0.9997878412794807f //p1
};
#if (__ARM_ARCH < 7)
#include <math.h>
float OMV_ATTR_ALWAYS_INLINE fast_sqrtf(float x) {
return sqrtf(x);
}
int OMV_ATTR_ALWAYS_INLINE fast_floorf(float x) {
return floorf(x);
}
int OMV_ATTR_ALWAYS_INLINE fast_ceilf(float x) {
return ceilf(x);
}
int OMV_ATTR_ALWAYS_INLINE fast_roundf(float x) {
return roundf(x);
}
float OMV_ATTR_ALWAYS_INLINE fast_fabsf(float x) {
return fabsf(x);
}
#else
float OMV_ATTR_ALWAYS_INLINE fast_sqrtf(float x) {
asm volatile (
"vsqrt.f32 %[r], %[x]\n"
: [r] "=t" (x)
: [x] "t" (x));
return x;
}
int OMV_ATTR_ALWAYS_INLINE fast_floorf(float x) {
int i;
asm volatile (
#if (__CORTEX_M > 4)
"vcvtm.S32.f32 %[r], %[x]\n"
#else
"vcvt.S32.f32 %[r], %[x]\n"
#endif
: [r] "=t" (i)
: [x] "t" (x));
return i;
}
int OMV_ATTR_ALWAYS_INLINE fast_ceilf(float x) {
int i;
#if (__CORTEX_M > 4)
asm volatile (
"vcvtp.S32.f32 %[r], %[x]\n"
#else
uint32_t max = 0x3f7fffff;
x += *((float *) &max);
asm volatile (
"vcvt.S32.f32 %[r], %[x]\n"
#endif
: [r] "=t" (i)
: [x] "t" (x));
return i;
}
int OMV_ATTR_ALWAYS_INLINE fast_roundf(float x) {
int i;
asm volatile (
"vcvtr.S32.F32 %[r], %[x]\n"
: [r] "=t" (i)
: [x] "t" (x));
return i;
}
float OMV_ATTR_ALWAYS_INLINE fast_fabsf(float x) {
asm volatile (
"vabs.f32 %[r], %[x]\n"
: [r] "=t" (x)
: [x] "t" (x));
return x;
}
#endif
typedef union {
uint32_t l;
struct {

View File

@ -13,19 +13,95 @@
#include <stdlib.h>
#include <stdint.h>
#include <float.h>
float fast_sqrtf(float x);
int fast_floorf(float x);
int fast_ceilf(float x);
int fast_roundf(float x);
#include CMSIS_MCU_H
#include "arm_math.h"
float fast_atanf(float x);
float fast_atan2f(float y, float x);
float fast_expf(float x);
float fast_cbrtf(float d);
float fast_fabsf(float d);
float fast_log(float x);
float fast_log2(float x);
float fast_powf(float a, float b);
void fast_get_min_max(float *data, size_t data_len, float *p_min, float *p_max);
static inline float fast_sqrtf(float x) {
#if (__ARM_ARCH >= 7)
__asm__ volatile (
"vsqrt.f32 %[r], %[x]\n"
: [r] "=t" (x)
: [x] "t" (x));
return x;
#else
return sqrtf(x);
#endif
}
static inline int fast_floorf(float x) {
#if (__ARM_ARCH >= 7)
int i;
__asm__ volatile (
#if (__CORTEX_M > 4)
"vcvtm.S32.f32 %[r], %[x]\n"
#else
"vcvt.S32.f32 %[r], %[x]\n"
#endif
: [r] "=t" (i)
: [x] "t" (x));
return i;
#else
return floorf(x);
#endif
}
static inline int fast_ceilf(float x) {
#if (__ARM_ARCH >= 7)
int i;
#if (__CORTEX_M > 4)
__asm__ volatile (
"vcvtp.S32.f32 %[r], %[x]\n"
#else
union {
uint32_t i; float f;
}
max = { 0x3f7fffff };
x += max.f;
__asm__ volatile (
"vcvt.S32.f32 %[r], %[x]\n"
#endif
: [r] "=t" (i)
: [x] "t" (x));
return i;
#else
return ceilf(x);
#endif
}
static inline int fast_roundf(float x) {
#if (__ARM_ARCH >= 7)
int i;
__asm__ volatile (
"vcvtr.S32.F32 %[r], %[x]\n"
: [r] "=t" (i)
: [x] "t" (x));
return i;
#else
return roundf(x);
#endif
}
static inline float fast_fabsf(float x) {
#if (__ARM_ARCH >= 7)
__asm__ volatile (
"vabs.f32 %[r], %[x]\n"
: [r] "=t" (x)
: [x] "t" (x));
return x;
#else
return fabsf(x);
#endif
}
extern const float cos_table[360];
extern const float sin_table[360];
#endif // __FMATH_H__