Skip to content

Commit

Permalink
Improved floating point functions performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
rombrew committed Apr 6, 2024
1 parent e559a60 commit c3ffb8c
Show file tree
Hide file tree
Showing 22 changed files with 325 additions and 323 deletions.
4 changes: 2 additions & 2 deletions doc/TroubleShooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ example there was no speed maneuver during the moment of inertia probing.
`PM_ERROR_INVALID_OPERATION` - Numerical instability inside PMC control code or
deliberately invalid operation was requested.

`PM_ERROR_SENSOR_HALL_FAULT` - Forbidden value of Hall code was detected. Or
`PM_ERROR_SENSOR_HALL_FAULT` - Forbidden value of Hall code was detected or
result of Hall self-adjustment shows an inadequacy of sensor measurements.

`PM_ERROR_SENSOR_EABI_FAULT` - Result of EABI self-adjustment shows an
Expand Down Expand Up @@ -82,7 +82,7 @@ computationally expensive features.
`PM_ERROR_HW_OVERCURRENT` - Overcurrent accident detected by hardware.

`PM_ERROR_HW_OVERTEMPERATURE` - PCB overheat condition has been reached.
Apparently temperature regulation by current derate is not functional.
Apparently temperature regulation by current limiting is not functional.

`PM_ERROR_HW_EMERGENCY_STOP` - Emergency situation detected by emergency
endstop signal.
Expand Down
2 changes: 1 addition & 1 deletion phobia/gp/lse.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ lse_qrupdate(lse_t *ls, lse_upper_t *rm, lse_float_t *xz, int nz)
}
#else /* LSE_FAST_TRANSFORM */

/* WARNING: We use NAIVE hypot implementation as it is
/* WARNING: We use naive hypot implementation as it is
* the fastest one and quite ulp-accurate.
*/
alpa = lse_sqrtf(x0 * x0 + xi * xi);
Expand Down
19 changes: 7 additions & 12 deletions phobia/gp/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -1597,23 +1597,18 @@ configParseFSM(read_t *rd, parse_t *pa)
do {
r = configToken(rd, pa);

if (r < 0) break;
else if (r == 1) {

failed = 0;
continue;
}
else if (r == 0 && pa->newline != 0) {
if (r == 0 && pa->newline != 0) {

sprintf(msg_tbuf, "unable to parse \"%.80s\"", tbuf);

if (tbuf[0] == '#') {

failed = 0;
while (configToken(rd, pa) == 0) ;
}
else if (strcmp(tbuf, "include") == 0) {

failed = 1;

r = configToken(rd, pa);

if (r == 0) {
Expand All @@ -1631,9 +1626,10 @@ configParseFSM(read_t *rd, parse_t *pa)
if (fd == NULL) {

ERROR("fopen(\"%s\"): %s\n", tbuf, strerror(errno));
failed = 1;
}
else {
failed = 0;

strcpy(rpa.file, tbuf);

rpa.path = pa->path;
Expand All @@ -1650,9 +1646,6 @@ configParseFSM(read_t *rd, parse_t *pa)
fclose(fd);
}
}
else {
failed = 1;
}
}
else if (strcmp(tbuf, "gpconfig") == 0) {

Expand Down Expand Up @@ -3162,6 +3155,8 @@ configParseFSM(read_t *rd, parse_t *pa)

pa->newline = 0;
}
else if (r < 0)
break;
}
while (1);
}
Expand Down
53 changes: 20 additions & 33 deletions phobia/phobia.c
Original file line number Diff line number Diff line change
Expand Up @@ -2777,9 +2777,9 @@ page_diagnose(struct public *pub)
reg->update = 1000;
}

reg_float_prog_um(pub, "hal.CNT_diag0", "IRQ diagnostic 0", 0.f, maximal[0], 1);
reg_float_prog_um(pub, "hal.CNT_diag1", "IRQ diagnostic 1", 0.f, maximal[1], 1);
reg_float_prog_um(pub, "hal.CNT_diag2", "IRQ diagnostic 2", 0.f, maximal[2], 1);
reg_float_prog_um(pub, "hal.CNT_diag0", "IRQ entry time", 0.f, maximal[0], 1);
reg_float_prog_um(pub, "hal.CNT_diag1", "IRQ priority load", 0.f, maximal[1], 1);
reg_float_prog_um(pub, "hal.CNT_diag2", "IRQ total load", 0.f, maximal[2], 1);

nk_layout_row_dynamic(ctx, 0, 1);
nk_spacer(ctx);
Expand Down Expand Up @@ -3696,22 +3696,9 @@ page_thermal(struct public *pub)

reg_enum_combo(pub, "ap.ntc_EXT.type", "NTC type on EXT", 1);
reg_float(pub, "ap.ntc_EXT.balance", "NTC balance");

reg = link_reg_lookup(lp, "ap.ntc_EXT.type");

if ( reg != NULL
&& (reg->lval >= 1 || reg->lval <= 2)) {

reg_float(pub, "ap.ntc_EXT.ntc0", "NTC resistance at Ta");
reg_float(pub, "ap.ntc_EXT.ta0", "NTC Ta");
reg_float(pub, "ap.ntc_EXT.betta", "NTC betta");
}
else {
nk_layout_row_dynamic(ctx, 0, 1);
nk_spacer(ctx);
nk_spacer(ctx);
nk_spacer(ctx);
}
reg_float(pub, "ap.ntc_EXT.ntc0", "NTC resistance at Ta");
reg_float(pub, "ap.ntc_EXT.ta0", "NTC Ta");
reg_float(pub, "ap.ntc_EXT.betta", "NTC betta");

nk_layout_row_dynamic(ctx, 0, 1);
nk_spacer(ctx);
Expand All @@ -3732,13 +3719,13 @@ page_thermal(struct public *pub)
nk_layout_row_dynamic(ctx, 0, 1);
nk_spacer(ctx);

reg_float(pub, "ap.heat_PCB_temp_halt", "PCB halt threshold");
reg_float(pub, "ap.heat_PCB_temp_derate", "PCB derate threshold");
reg_float(pub, "ap.heat_PCB_temp_FAN", "PCB fan ON threshold");
reg_float(pub, "ap.heat_EXT_temp_derate", "EXT derate threshold");
reg_float(pub, "ap.heat_maximal_PCB", "PCB maximal current");
reg_float(pub, "ap.heat_maximal_EXT", "EXT maximal current");
reg_float(pub, "ap.heat_temp_recovery", "Recovery hysteresis");
reg_float(pub, "ap.otp_PCB_halt", "PCB halt threshold");
reg_float(pub, "ap.otp_PCB_derate", "PCB derate threshold");
reg_float(pub, "ap.otp_PCB_fan", "PCB fan ON threshold");
reg_float(pub, "ap.otp_EXT_derate", "EXT derate threshold");
reg_float(pub, "ap.otp_maximal_PCB", "PCB maximal current");
reg_float(pub, "ap.otp_maximal_EXT", "EXT maximal current");
reg_float(pub, "ap.otp_recovery", "Recovery hysteresis");

nk_layout_row_dynamic(ctx, 0, 1);
nk_spacer(ctx);
Expand Down Expand Up @@ -4007,14 +3994,14 @@ page_lu_flux(struct public *pub)
nk_layout_row_dynamic(ctx, 0, 1);
nk_spacer(ctx);

reg_float(pub, "pm.detach_threshold", "Detached threshold");
reg_float(pub, "pm.detach_trip_AP", "Detached trip point");
reg_float(pub, "pm.detach_threshold", "Detached voltage threshold");
reg_float(pub, "pm.detach_trip_tol", "Detached trip tolerance");
reg_float(pub, "pm.detach_gain_SF", "Detached speed loop gain");

nk_layout_row_dynamic(ctx, 0, 1);
nk_spacer(ctx);

reg_float(pub, "pm.flux_trip_AP", "Ortega speed trip point");
reg_float(pub, "pm.flux_trip_tol", "Ortega trip tolerance");
reg_float(pub, "pm.flux_gain_IN", "Ortega initial gain");
reg_float(pub, "pm.flux_gain_LO", "Ortega flux gain LO");
reg_float(pub, "pm.flux_gain_HI", "Ortega flux gain HI");
Expand Down Expand Up @@ -4241,7 +4228,7 @@ page_lu_hall(struct public *pub)
nk_layout_row_dynamic(ctx, 0, 1);
nk_spacer(ctx);

reg_float(pub, "pm.hall_trip_AP", "Hall speed trip point");
reg_float(pub, "pm.hall_trip_tol", "Hall trip tolerance");
reg_float(pub, "pm.hall_gain_LO", "Hall speed gain LO");
reg_float(pub, "pm.hall_gain_SF", "Hall speed loop gain");
reg_float(pub, "pm.hall_gain_IF", "Torque acceleration");
Expand Down Expand Up @@ -4382,7 +4369,7 @@ page_lu_eabi(struct public *pub)
reg_float(pub, "pm.eabi_const_EP", "EABI pulse resolution");
reg_float(pub, "pm.eabi_const_Zs", "Gear teeth number S");
reg_float(pub, "pm.eabi_const_Zq", "Gear teeth number Q");
reg_float(pub, "pm.eabi_trip_AP", "EABI speed trip point");
reg_float(pub, "pm.eabi_trip_tol", "EABI trip tolerance");
reg_float(pub, "pm.eabi_gain_LO", "EABI speed gain LO");
reg_float(pub, "pm.eabi_gain_SF", "EABI speed loop gain");
reg_float(pub, "pm.eabi_gain_IF", "Torque acceleration");
Expand Down Expand Up @@ -4740,8 +4727,8 @@ page_lp_location(struct public *pub)

reg_float_um(pub, "pm.x_maximal", "Maximal location limit", um_def);
reg_float_um(pub, "pm.x_minimal", "Minimal location limit", um_def);
reg_float_um(pub, "pm.x_damping", "Damping distance", 0);
reg_float_um(pub, "pm.x_residual_tol", "Residual tolerance", 0);
reg_float_um(pub, "pm.x_boost_tol", "Regulation tolerance", 0);
reg_float_um(pub, "pm.x_track_tol", "Tracking tolerance", 0);
reg_float_um(pub, "pm.x_gain_P", "Proportional gain", 0);
reg_float(pub, "pm.x_gain_D", "Damped gain");

Expand Down
14 changes: 10 additions & 4 deletions src/hal/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,12 @@ void ADC_startup()
float ADC_get_sample(int xGPIO)
{
int xCH, xADC;
float um = 0.f;

union {
uint32_t l;
float f;
}
um = { 0xFFF80000U };

if (xSemaphoreTake(priv_ADC.mutex_sem, (TickType_t) 10) == pdTRUE) {

Expand All @@ -268,19 +273,20 @@ float ADC_get_sample(int xGPIO)
}

ADC1->SR = ~ADC_SR_EOC;

xADC = ADC1->DR;

if (xCH == XGPIO_GET_CH(GPIO_ADC_TEMPINT)) {

um = (float) (xADC) * hal.const_ADC.TS[1] + hal.const_ADC.TS[0];
um.f = (float) (xADC) * hal.const_ADC.TS[1] + hal.const_ADC.TS[0];
}
else {
um = (float) (xADC) * hal.const_ADC.GS;
um.f = (float) (xADC) * hal.const_ADC.GS;
}

xSemaphoreGive(priv_ADC.mutex_sem);
}

return um;
return um.f;
}

20 changes: 10 additions & 10 deletions src/hal/hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ LOG_t log LD_NOINIT;

typedef struct {

uint32_t bootload;
uint32_t crystal;
uint32_t bootload_flag;
uint32_t crystal_disabled;
}
priv_HAL_t;

Expand All @@ -27,7 +27,7 @@ void irq_NMI()

if (RCC->CIR & RCC_CIR_CSSF) {

noinit_HAL.crystal = HAL_FLAG_SIGNATURE;
noinit_HAL.crystal_disabled = HAL_FLAG_SIGNATURE;

RCC->CIR |= RCC_CIR_CSSC;

Expand Down Expand Up @@ -103,7 +103,7 @@ core_startup()
RCC->DCKCFGR2 = 0;
#endif /* STM32F7 */

if (noinit_HAL.crystal != HAL_FLAG_SIGNATURE) {
if (noinit_HAL.crystal_disabled != HAL_FLAG_SIGNATURE) {

int N = 0;

Expand All @@ -124,12 +124,12 @@ core_startup()

log_TRACE("HSE not ready" EOL);

noinit_HAL.crystal = HAL_FLAG_SIGNATURE;
noinit_HAL.crystal_disabled = HAL_FLAG_SIGNATURE;

#ifdef STM32F7
/* D-Cache Clean and Invalidate.
* */
SCB->DCCIMVAC = (uint32_t) &noinit_HAL.crystal;
SCB->DCCIMVAC = (uint32_t) &noinit_HAL.crystal_disabled;

__DSB();
__ISB();
Expand Down Expand Up @@ -159,7 +159,7 @@ core_startup()
* */
RCC->CFGR |= RCC_CFGR_HPRE_DIV1 | RCC_CFGR_PPRE1_DIV4 | RCC_CFGR_PPRE2_DIV2;

if (noinit_HAL.crystal != HAL_FLAG_SIGNATURE) {
if (noinit_HAL.crystal_disabled != HAL_FLAG_SIGNATURE) {

CLOCK = HW_CLOCK_CRYSTAL_HZ;

Expand Down Expand Up @@ -299,9 +299,9 @@ void hal_bootload()
{
const uint32_t *sysmem;

if (noinit_HAL.bootload == HAL_FLAG_SIGNATURE) {
if (noinit_HAL.bootload_flag == HAL_FLAG_SIGNATURE) {

noinit_HAL.bootload = 0U;
noinit_HAL.bootload_flag = 0U;

#if defined(STM32F4)
sysmem = (const uint32_t *) 0x1FFF0000U;
Expand Down Expand Up @@ -362,7 +362,7 @@ void hal_system_reset()

void hal_bootload_jump()
{
noinit_HAL.bootload = HAL_FLAG_SIGNATURE;
noinit_HAL.bootload_flag = HAL_FLAG_SIGNATURE;

hal_system_reset();
}
Expand Down
11 changes: 4 additions & 7 deletions src/libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ const char *stof(float *x, const char *s)
return s;
}

uint32_t crc32b(const void *s, int n)
uint32_t crc32b(const void *s, size_t n)
{
const uint32_t *ls = (const uint32_t *) s;
uint32_t crc, buf;
Expand All @@ -835,10 +835,10 @@ uint32_t crc32b(const void *s, int n)

crc = 0xFFFFFFFFU;

while (n >= 4) {
while (n >= 4U) {

buf = *ls++;
n += - 4;
n += - 4U;

crc = crc ^ buf;

Expand All @@ -857,11 +857,8 @@ uint32_t crc32b(const void *s, int n)

uint32_t urand()
{
uint32_t rval;

rseed = rseed * 17317U + 1U;
rval = rseed >> 16;

return rval;
return rseed >> 16;
}

2 changes: 1 addition & 1 deletion src/libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const char *stoi(int *x, const char *s);
const char *htoi(int *x, const char *s);
const char *stof(float *x, const char *s);

uint32_t crc32b(const void *s, int n);
uint32_t crc32b(const void *s, size_t n);
uint32_t urand();

#endif /* _H_LIBC_ */
Expand Down
Loading

0 comments on commit c3ffb8c

Please sign in to comment.