diff --git a/examples/natmod/deepcraft/dc_mp_iface.c b/examples/natmod/deepcraft/dc_mp_iface.c index 3d4c1be6bd3..5bdeb1611cd 100644 --- a/examples/natmod/deepcraft/dc_mp_iface.c +++ b/examples/natmod/deepcraft/dc_mp_iface.c @@ -24,46 +24,75 @@ void *memset(void *s, int c, size_t n) { // ============================================================================= // Helper: bit-cast float - unsigned int without UB -static inline unsigned int _f2u(float f) { union { float f; unsigned int u; } v; v.f = f; return v.u; } -static inline float _u2f(unsigned int u) { union { float f; unsigned int u; } v; v.u = u; return v.f; } +static inline unsigned int _f2u(float f) { + union { float f; + unsigned int u; + } v; + v.f = f; + return v.u; +} +static inline float _u2f(unsigned int u) { + union { float f; + unsigned int u; + } v; + v.u = u; + return v.f; +} // --- expf --- // Degree-5 minimax polynomial + IEEE 754 exponent scaling. // Max error ~1.5 ULP for |x| <= 88. float expf(float x) { - if (x > 88.0f) return 3.40282347e+38f; - if (x < -88.0f) return 0.0f; + if (x > 88.0f) { + return 3.40282347e+38f; + } + if (x < -88.0f) { + return 0.0f; + } int n = (int)(x * 1.4426950409f + (x >= 0.0f ? 0.5f : -0.5f)); float r = x - (float)n * 0.6931471806f; float p = 1.0f + r * (1.0f + r * (0.5f + r * (0.16666667f + - r * (0.041666667f + r * 0.0083333333f)))); + r * (0.041666667f + r * 0.0083333333f)))); return p * _u2f((unsigned int)((n + 127) << 23)); } // --- logf --- // Natural log via exponent extraction + polynomial on [sqrt(0.5), sqrt(2)]. float logf(float x) { - if (x <= 0.0f) return -3.40282347e+38f; + if (x <= 0.0f) { + return -3.40282347e+38f; + } int e = (int)((_f2u(x) >> 23) & 0xFF) - 127; float m = _u2f((_f2u(x) & 0x007FFFFFu) | 0x3F000000u); // m in [0.5, 1) - if (m < 0.70710678f) { m *= 2.0f; e -= 1; } + if (m < 0.70710678f) { + m *= 2.0f; + e -= 1; + } float f = (m - 1.0f) / (m + 1.0f), f2 = f * f; float p = f * (2.0f + f2 * (0.66666667f + f2 * (0.4f + f2 * - (0.28571429f + f2 * 0.22222222f)))); + (0.28571429f + f2 * 0.22222222f)))); return p + (float)e * 0.6931471806f; } // --- log2f --- -float log2f(float x) { return logf(x) * 1.4426950409f; } +float log2f(float x) { + return logf(x) * 1.4426950409f; +} // --- log10f --- -float log10f(float x) { return logf(x) * 0.4342944819f; } +float log10f(float x) { + return logf(x) * 0.4342944819f; +} // --- tanhf --- // tanh(x) = (e^2x - 1)/(e^2x + 1); clamped for |x| > 9 (result = ±1). float tanhf(float x) { - if (x > 9.0f) return 1.0f; - if (x < -9.0f) return -1.0f; + if (x > 9.0f) { + return 1.0f; + } + if (x < -9.0f) { + return -1.0f; + } float e2x = expf(2.0f * x); return (e2x - 1.0f) / (e2x + 1.0f); } @@ -72,7 +101,9 @@ float tanhf(float x) { // Three Newton-Raphson iterations on an IEEE 754 seed estimate (~6 correct bits). // On Cortex-M4F the compiler may emit vsqrt.f32 directly and never call this. float sqrtf(float x) { - if (x <= 0.0f) return 0.0f; + if (x <= 0.0f) { + return 0.0f; + } float r = _u2f(0x1fbb4000u + (_f2u(x) >> 1)); // seed: ~half the exponent r = 0.5f * (r + x / r); r = 0.5f * (r + x / r); @@ -81,20 +112,32 @@ float sqrtf(float x) { } // --- fabsf --- (often compiled to vabs.f32 inline, but shim for safety) -float fabsf(float x) { return _u2f(_f2u(x) & 0x7FFFFFFFu); } +float fabsf(float x) { + return _u2f(_f2u(x) & 0x7FFFFFFFu); +} // --- floorf --- -float floorf(float x) { int i = (int)x; return (float)(i - (x < (float)i)); } +float floorf(float x) { + int i = (int)x; + return (float)(i - (x < (float)i)); +} // --- ceilf --- -float ceilf(float x) { int i = (int)x; return (float)(i + (x > (float)i)); } +float ceilf(float x) { + int i = (int)x; + return (float)(i + (x > (float)i)); +} // --- roundf --- -float roundf(float x) { return (x >= 0.0f) ? floorf(x + 0.5f) : ceilf(x - 0.5f); } +float roundf(float x) { + return (x >= 0.0f) ? floorf(x + 0.5f) : ceilf(x - 0.5f); +} // --- fmodf --- float fmodf(float x, float y) { - if (y == 0.0f) return 0.0f; + if (y == 0.0f) { + return 0.0f; + } float q = x / y; int n = (int)q; return x - (float)n * y; @@ -103,11 +146,17 @@ float fmodf(float x, float y) { // --- powf --- // a^b = exp(b * ln(a)); handles integer-exponent cases first for accuracy. float powf(float a, float b) { - if (b == 0.0f) return 1.0f; - if (a == 0.0f) return 0.0f; + if (b == 0.0f) { + return 1.0f; + } + if (a == 0.0f) { + return 0.0f; + } if (a < 0.0f) { int ib = (int)b; - if ((float)ib != b) return 0.0f; // non-integer exponent of negative base + if ((float)ib != b) { + return 0.0f; // non-integer exponent of negative base + } return (ib & 1) ? -expf(b * logf(-a)) : expf(b * logf(-a)); } return expf(b * logf(a)); @@ -132,10 +181,14 @@ float sinf(float x) { int n = (int)(q + (x >= 0.0f ? 0.5f : -0.5f)); float r = x - (float)n * 1.5707963268f; // r = x - n*(pi/2) switch (n & 3) { - case 0: return _dc_sinf_kernel(r); - case 1: return _dc_cosf_kernel(r); - case 2: return -_dc_sinf_kernel(r); - default: return -_dc_cosf_kernel(r); + case 0: + return _dc_sinf_kernel(r); + case 1: + return _dc_cosf_kernel(r); + case 2: + return -_dc_sinf_kernel(r); + default: + return -_dc_cosf_kernel(r); } } float cosf(float x) { @@ -143,25 +196,35 @@ float cosf(float x) { int n = (int)(q + (x >= 0.0f ? 0.5f : -0.5f)); float r = x - (float)n * 1.5707963268f; switch (n & 3) { - case 0: return _dc_cosf_kernel(r); - case 1: return -_dc_sinf_kernel(r); - case 2: return -_dc_cosf_kernel(r); - default: return _dc_sinf_kernel(r); + case 0: + return _dc_cosf_kernel(r); + case 1: + return -_dc_sinf_kernel(r); + case 2: + return -_dc_cosf_kernel(r); + default: + return _dc_sinf_kernel(r); } } // --- tanf --- float tanf(float x) { float c = cosf(x); - if (c == 0.0f) return 3.40282347e+38f; + if (c == 0.0f) { + return 3.40282347e+38f; + } return sinf(x) / c; } // --- asinf --- // Uses identity: asin(x) = atan(x / sqrt(1 - x*x)) float asinf(float x) { - if (x >= 1.0f) return 1.5707963268f; // pi/2 - if (x <= -1.0f) return -1.5707963268f; + if (x >= 1.0f) { + return 1.5707963268f; // pi/2 + } + if (x <= -1.0f) { + return -1.5707963268f; + } float t = x * x; float s = sqrtf(1.0f - t); // atan(x/sqrt(1-x^2)) via degree-5 minimax on [0,1] @@ -171,29 +234,49 @@ float asinf(float x) { } // --- acosf --- -float acosf(float x) { return 1.5707963268f - asinf(x); } +float acosf(float x) { + return 1.5707963268f - asinf(x); +} // --- atanf --- // Degree-9 minimax polynomial, accurate to ~2 ULP for |x| <= 1. // Larger |x| uses identity: atan(x) = pi/2 - atan(1/x). float atanf(float x) { int inv = 0; - if (x < 0.0f) { x = -x; inv = 2; } // handle sign - if (x > 1.0f) { x = 1.0f / x; inv ^= 1; } + if (x < 0.0f) { + x = -x; + inv = 2; + } // handle sign + if (x > 1.0f) { + x = 1.0f / x; + inv ^= 1; + } float x2 = x * x; float p = x * (1.0f + x2 * (-0.33333334f + x2 * (0.2f + x2 * - (-0.14285715f + x2 * (0.11111111f + x2 * -0.090909094f))))); - if (inv & 1) p = 1.5707963268f - p; + (-0.14285715f + x2 * (0.11111111f + x2 * -0.090909094f))))); + if (inv & 1) { + p = 1.5707963268f - p; + } return (inv & 2) ? -p : p; } // --- atan2f --- float atan2f(float y, float x) { - if (x > 0.0f) return atanf(y / x); - if (x < 0.0f && y >= 0.0f) return atanf(y / x) + 3.14159265359f; - if (x < 0.0f && y < 0.0f) return atanf(y / x) - 3.14159265359f; - if (x == 0.0f && y > 0.0f) return 1.5707963268f; - if (x == 0.0f && y < 0.0f) return -1.5707963268f; + if (x > 0.0f) { + return atanf(y / x); + } + if (x < 0.0f && y >= 0.0f) { + return atanf(y / x) + 3.14159265359f; + } + if (x < 0.0f && y < 0.0f) { + return atanf(y / x) - 3.14159265359f; + } + if (x == 0.0f && y > 0.0f) { + return 1.5707963268f; + } + if (x == 0.0f && y < 0.0f) { + return -1.5707963268f; + } return 0.0f; // x == 0, y == 0 — undefined, return 0 } @@ -205,33 +288,71 @@ float atan2f(float y, float x) { // (cosf, sinf, expf, …). These thin wrappers delegate to our float shims // above so no extra precision logic is needed. // ============================================================================= -double cos(double x) { return (double)cosf((float)x); } -double sin(double x) { return (double)sinf((float)x); } -double tan(double x) { return (double)tanf((float)x); } -double acos(double x) { return (double)acosf((float)x); } -double asin(double x) { return (double)asinf((float)x); } -double atan(double x) { return (double)atanf((float)x); } -double atan2(double y, double x) { return (double)atan2f((float)y, (float)x); } -double exp(double x) { return (double)expf((float)x); } -double log(double x) { return (double)logf((float)x); } -double log2(double x) { return (double)log2f((float)x); } -double log10(double x) { return (double)log10f((float)x); } -double sqrt(double x) { return (double)sqrtf((float)x); } -double pow(double a, double b) { return (double)powf((float)a, (float)b); } -double fabs(double x) { return (double)fabsf((float)x); } -double floor(double x) { return (double)floorf((float)x); } -double ceil(double x) { return (double)ceilf((float)x); } -double round(double x) { return (double)roundf((float)x); } -double fmod(double x, double y) { return (double)fmodf((float)x, (float)y); } -double tanh(double x) { return (double)tanhf((float)x); } +double cos(double x) { + return (double)cosf((float)x); +} +double sin(double x) { + return (double)sinf((float)x); +} +double tan(double x) { + return (double)tanf((float)x); +} +double acos(double x) { + return (double)acosf((float)x); +} +double asin(double x) { + return (double)asinf((float)x); +} +double atan(double x) { + return (double)atanf((float)x); +} +double atan2(double y, double x) { + return (double)atan2f((float)y, (float)x); +} +double exp(double x) { + return (double)expf((float)x); +} +double log(double x) { + return (double)logf((float)x); +} +double log2(double x) { + return (double)log2f((float)x); +} +double log10(double x) { + return (double)log10f((float)x); +} +double sqrt(double x) { + return (double)sqrtf((float)x); +} +double pow(double a, double b) { + return (double)powf((float)a, (float)b); +} +double fabs(double x) { + return (double)fabsf((float)x); +} +double floor(double x) { + return (double)floorf((float)x); +} +double ceil(double x) { + return (double)ceilf((float)x); +} +double round(double x) { + return (double)roundf((float)x); +} +double fmod(double x, double y) { + return (double)fmodf((float)x, (float)y); +} +double tanh(double x) { + return (double)tanhf((float)x); +} #endif -int native_errno=0; +int native_errno = 0; #if defined(__linux__) -int *__errno_location (void) +int *__errno_location(void) #else -int *__errno (void) +int *__errno(void) #endif { return &native_errno; @@ -257,22 +378,22 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a MP_DYNRUNTIME_INIT_ENTRY // Populate type - dc_type.base.type = (void*)&mp_type_type; + dc_type.base.type = (void *)&mp_type_type; dc_type.flags = MP_TYPE_FLAG_NONE; dc_type.name = MP_QSTR_DEEPCRAFT; MP_OBJ_TYPE_SET_SLOT(&dc_type, make_new, dc_make_new, 0); - dc_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_init), MP_OBJ_FROM_PTR(&init_obj) }; - dc_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_enqueue), MP_OBJ_FROM_PTR(&enqueue_obj) }; - dc_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_dequeue), MP_OBJ_FROM_PTR(&dequeue_obj) }; - dc_locals_dict_table[3] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_get_model_input_dim), MP_OBJ_FROM_PTR(&get_model_input_dim_obj) }; - dc_locals_dict_table[4] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_get_model_output_dim), MP_OBJ_FROM_PTR(&get_model_output_dim_obj) }; - - MP_OBJ_TYPE_SET_SLOT(&dc_type, locals_dict, (void*)&dc_locals_dict, 5); + dc_locals_dict_table[0] = (mp_map_elem_t) { MP_OBJ_NEW_QSTR(MP_QSTR_init), MP_OBJ_FROM_PTR(&init_obj) }; + dc_locals_dict_table[1] = (mp_map_elem_t) { MP_OBJ_NEW_QSTR(MP_QSTR_enqueue), MP_OBJ_FROM_PTR(&enqueue_obj) }; + dc_locals_dict_table[2] = (mp_map_elem_t) { MP_OBJ_NEW_QSTR(MP_QSTR_dequeue), MP_OBJ_FROM_PTR(&dequeue_obj) }; + dc_locals_dict_table[3] = (mp_map_elem_t) { MP_OBJ_NEW_QSTR(MP_QSTR_get_model_input_dim), MP_OBJ_FROM_PTR(&get_model_input_dim_obj) }; + dc_locals_dict_table[4] = (mp_map_elem_t) { MP_OBJ_NEW_QSTR(MP_QSTR_get_model_output_dim), MP_OBJ_FROM_PTR(&get_model_output_dim_obj) }; + + MP_OBJ_TYPE_SET_SLOT(&dc_type, locals_dict, (void *)&dc_locals_dict, 5); // Expose constructor as DEEPCRAFT mp_store_global(MP_QSTR_DEEPCRAFT, MP_OBJ_FROM_PTR(&dc_type)); // This must be last, it restores the globals dict MP_DYNRUNTIME_INIT_EXIT - -} \ No newline at end of file + +} diff --git a/examples/natmod/deepcraft/mp_src.c b/examples/natmod/deepcraft/mp_src.c index da65aad95e2..bf6074e4ec8 100644 --- a/examples/natmod/deepcraft/mp_src.c +++ b/examples/natmod/deepcraft/mp_src.c @@ -4,22 +4,22 @@ typedef struct _dc_obj_t { mp_obj_base_t base; - bool model_state; + bool model_state; uint8_t model_in_dim; uint8_t model_out_dim; } dc_obj_t; -mp_obj_t get_model_input_dim(mp_obj_t self_in){ +mp_obj_t get_model_input_dim(mp_obj_t self_in) { dc_obj_t *self = MP_OBJ_TO_PTR(self_in); return MP_OBJ_NEW_SMALL_INT(self->model_in_dim); } -mp_obj_t get_model_output_dim(mp_obj_t self_in){ +mp_obj_t get_model_output_dim(mp_obj_t self_in) { dc_obj_t *self = MP_OBJ_TO_PTR(self_in); return MP_OBJ_NEW_SMALL_INT(self->model_out_dim); } -mp_obj_t init(mp_obj_t self_in){ +mp_obj_t init(mp_obj_t self_in) { dc_obj_t *self = MP_OBJ_TO_PTR(self_in); self->model_state = true; self->model_in_dim = IMAI_DATA_IN_COUNT; @@ -28,11 +28,11 @@ mp_obj_t init(mp_obj_t self_in){ return mp_const_none; } -mp_obj_t enqueue(mp_obj_t self_in, const mp_obj_t data_in_obj){ +mp_obj_t enqueue(mp_obj_t self_in, const mp_obj_t data_in_obj) { dc_obj_t *self = MP_OBJ_TO_PTR(self_in); // Check if model is initialized - if(!self->model_state){ + if (!self->model_state) { mp_raise_ValueError(MP_ERROR_TEXT("Model should be initialized first.")); } @@ -63,7 +63,7 @@ mp_obj_t dequeue(mp_obj_t self_in, mp_obj_t data_out_obj) { if (len != self->model_out_dim) { mp_raise_ValueError("data_out must be a list of floats with size matching to output dimensions of model. Check using get_model_output_dim()."); } - + int result = IMAI_dequeue(data_out); if (result == 0) { return MP_OBJ_NEW_SMALL_INT(result); @@ -100,4 +100,4 @@ static const mp_obj_fun_builtin_fixed_t get_model_input_dim_obj = { static const mp_obj_fun_builtin_fixed_t get_model_output_dim_obj = { .base = { &mp_type_fun_builtin_1 }, .fun._1 = (mp_fun_1_t)get_model_output_dim, -}; \ No newline at end of file +}; diff --git a/examples/usercimaimodule/imagimob/imai_mp_iface.c b/examples/usercimaimodule/imagimob/imai_mp_iface.c index cdd558b6be3..fa981fe9bd3 100644 --- a/examples/usercimaimodule/imagimob/imai_mp_iface.c +++ b/examples/usercimaimodule/imagimob/imai_mp_iface.c @@ -4,13 +4,13 @@ #include "py/mphal.h" #include "model.h" -static mp_obj_t init(void){ +static mp_obj_t init(void) { IMAI_init(); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(init_obj, init); -static mp_obj_t enqueue(const mp_obj_t data_in_obj){ +static mp_obj_t enqueue(const mp_obj_t data_in_obj) { float data_in[IMAI_DATA_IN_COUNT]; mp_obj_t *data_in_items; size_t len; @@ -44,7 +44,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(dequeue_obj, dequeue); static const mp_rom_map_elem_t example_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_imai) }, - + // imagimob fns { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&init_obj) }, { MP_ROM_QSTR(MP_QSTR_enqueue), MP_ROM_PTR(&enqueue_obj) }, diff --git a/ports/psoc6/machine_wdt.c b/ports/psoc6/machine_wdt.c index afa9aa4a069..fe714365570 100644 --- a/ports/psoc6/machine_wdt.c +++ b/ports/psoc6/machine_wdt.c @@ -58,9 +58,9 @@ typedef struct _machine_wdt_obj_t { // singleton WDT object static const machine_wdt_obj_t machine_wdt_obj = {{&machine_wdt_type}}; -static machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) { - if (id != 0) { - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("WDT(%d) doesn't exist"), id); +static machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_obj_t id, mp_int_t timeout_ms) { + if (id != MP_OBJ_NEW_SMALL_INT(0)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("WDT(%d) doesn't exist"), mp_obj_get_int(id)); } if (timeout_ms <= 0) { diff --git a/tools/codeformat.py b/tools/codeformat.py index 1a71ef609b0..e66c9e543e3 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -47,6 +47,8 @@ EXCLUSIONS = [ # Fixups broken by preprocessor macro "shared/readline/*.[ch]", + # Generated example model output. + "examples/usercimaimodule/imagimob/model.[ch]", # The cc3200 port is not fully formatted yet. "drivers/cc3100/*/*.[ch]", "ports/cc3200/*/*.[ch]",