Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/rp2040/pico_platform/include/pico/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,7 @@ return a;
* \param b the second operand
* \return a * b
*/
#define __fast_mul(a, b) __builtin_choose_expr(__builtin_constant_p(b) && !__builtin_constant_p(a), \
(__builtin_popcount(b) >= 2 ? __mul_instruction(a,b) : (a)*(b)), \
(a)*(b))
#define __fast_mul(a, b) (__builtin_constant_p(b) && !__builtin_constant_p(a) && __builtin_popcount(b) >= 2 ? __mul_instruction(a,b) : (a)*(b))

#ifdef __cplusplus
}
Expand Down
4 changes: 1 addition & 3 deletions src/rp2350/pico_platform/include/pico/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ __force_inline static int32_t __mul_instruction(int32_t a, int32_t b) {
* \param b the second operand
* \return a * b
*/
#define __fast_mul(a, b) __builtin_choose_expr(__builtin_constant_p(b) && !__builtin_constant_p(a), \
(__builtin_popcount(b) >= 2 ? __mul_instruction(a,b) : (a)*(b)), \
(a)*(b))
#define __fast_mul(a, b) (__builtin_constant_p(b) && !__builtin_constant_p(a) && __builtin_popcount(b) >= 2 ? __mul_instruction(a,b) : (a)*(b))

#ifdef __cplusplus
}
Expand Down
12 changes: 6 additions & 6 deletions src/rp2_common/pico_float/include/pico/float.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ uint64_t float2ufix64(float f, int e);
// or call the original function
#if PICO_FLOAT_HAS_FIX32_TO_FLOAT_CONVERSIONS
// a bit of a hack to inline VFP fixed-point conversion when exponent is constant and in range 1-32
#define fix2float(m, e) __builtin_choose_expr(__builtin_constant_p(e), (e) >= 1 && (e) <= 32 ? _fix2float_inline(m, e) : fix2 ## float(m, e), fix2 ## float(m, e))
#define ufix2float(m, e) __builtin_choose_expr(__builtin_constant_p(e), (e) >= 1 && (e) <= 32 ? _ufix2float_inline(m, e) : ufix2 ## float(m, e), ufix2 ## float(m, e))
#define fix2float(m, e) (__builtin_constant_p(e) && (e) >= 1 && (e) <= 32 ? _fix2float_inline(m, e) : fix2 ## float(m, e))
#define ufix2float(m, e) (__builtin_constant_p(e) && (e) >= 1 && (e) <= 32 ? _ufix2float_inline(m, e) : ufix2 ## float(m, e))

#define _fix2float_inline(m, e) ({ \
int32_t _m = m; \
Expand All @@ -385,8 +385,8 @@ uint64_t float2ufix64(float f, int e);

#endif
#if PICO_FLOAT_HAS_FLOAT_TO_FIX32_Z_CONVERSIONS
#define float2fix_z(f, e) __builtin_choose_expr(__builtin_constant_p(e), (e) >= 1 && (e) <= 32 ? _float2fix_z_inline(f, e) : float2 ## fix_z(f, e), float2 ## fix_z(f, e))
#define float2ufix_z(f, e) __builtin_choose_expr(__builtin_constant_p(e), (e) >= 1 && (e) <= 32 ? _float2ufix_z_inline(f, e) : float2 ## ufix_z(f, e), float2 ## ufix_z(f, e))
#define float2fix_z(f, e) (__builtin_constant_p(e) && (e) >= 1 && (e) <= 32 ? _float2fix_z_inline(f, e) : float2 ## fix_z(f, e))
#define float2ufix_z(f, e) (__builtin_constant_p(e) && (e) >= 1 && (e) <= 32 ? _float2ufix_z_inline(f, e) : float2 ## ufix_z(f, e))

#define _float2fix_z_inline(f, e) ({ \
int32_t _m; \
Expand All @@ -413,8 +413,8 @@ uint64_t float2ufix64(float f, int e);

#endif
#if PICO_FLOAT_HAS_FLOAT_TO_FIX32_M_CONVERSIONS
#define float2fix(f, e) __builtin_choose_expr(__builtin_constant_p(e), (e) >= 1 && (e) <= 32 ? _float2fix_inline(f, e) : float2 ## fix(f, e), float2 ## fix(f, e))
#define float2ufix(f, e) __builtin_choose_expr(__builtin_constant_p(e), (e) >= 1 && (e) <= 32 ? _float2ufix_inline(f, e) : float2 ## ufix(f, e), float2 ## ufix(f, e))
#define float2fix(f, e) (__builtin_constant_p(e) && (e) >= 1 && (e) <= 32 ? _float2fix_inline(f, e) : float2 ## fix(f, e))
#define float2ufix(f, e) (__builtin_constant_p(e) && (e) >= 1 && (e) <= 32 ? _float2ufix_inline(f, e) : float2 ## ufix(f, e))

#define _float2fix_inline(f, e) ({ \
union { float _f; int32_t _i; } _u; \
Expand Down
40 changes: 40 additions & 0 deletions test/kitchen_sink/kitchen_sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,42 @@ float __attribute__((noinline)) foox(float x, float b) {
return x * b;
}

uint __noinline const_funcs_returning_int(__unused float x, int n) {
return __fast_mul(n, 7)
#if PICO_FLOAT_HAS_FLOAT_TO_FIX32_M_CONVERSIONS
+ float2fix(x, 7)
+ float2ufix(x, 7)
#endif
#if PICO_FLOAT_HAS_FLOAT_TO_FIX32_Z_CONVERSIONS
+ float2fix_z(x, 7)
+ float2ufix_z(x, 7)
#endif
;
}

uint __noinline non_const_funcs_returning_int(__unused float x, int n) {
return __fast_mul(n, n)
#if PICO_FLOAT_HAS_FLOAT_TO_FIX32_M_CONVERSIONS
+ float2fix(x, n)
+ float2ufix(x, n)
#endif
#if PICO_FLOAT_HAS_FLOAT_TO_FIX32_Z_CONVERSIONS
+ float2fix_z(x, n)
+ float2ufix_z(x, n)
#endif
;
}

#if PICO_FLOAT_HAS_FIX32_TO_FLOAT_CONVERSIONS
float __noinline const_funcs_returning_float(int n) {
return fix2float(n, 7) + ufix2float(n, 7);
}

float __noinline non_const_funcs_returning_float(int n) {
return fix2float(n, n) + ufix2float(n, n);
}
#endif

void svc_call(void) {
puts("PASSED");
exit(0);
Expand Down Expand Up @@ -93,6 +129,10 @@ int main(void) {
hard_assert(recursive_mutex_try_enter(&recursive_mutex, NULL));
hard_assert(recursive_mutex_try_enter(&recursive_mutex, NULL));
printf("%f\n", foox(1.3f, 2.6f));
printf("%u %u\n", const_funcs_returning_int(3.7f, 7), non_const_funcs_returning_int(3.7f, 7));
#if PICO_FLOAT_HAS_FIX32_TO_FLOAT_CONVERSIONS
printf("%f %f\n", const_funcs_returning_float(7), non_const_funcs_returning_float(7));
#endif
#ifdef EXTRA_DATA_SECTION
extern uint32_t __extra_end_variable__;
printf("__extra_end_variable__ = %p\n", (void *)&__extra_end_variable__);
Expand Down
Loading