From c311090de0d6d226f240234fe210634b2c363c7a Mon Sep 17 00:00:00 2001 From: coral Date: Fri, 11 Sep 2026 13:45:46 -0700 Subject: [PATCH] =?UTF-8?q?Perform=20phase=20differences=20using=20unsigne?= =?UTF-8?q?d=20arithmetic=20to=20preserve=20modulo-2=C2=B3=C2=B2=20wraparo?= =?UTF-8?q?und=20without=20invoking=20signed-overflow=20undefined=20behavi?= =?UTF-8?q?or.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/v17rx.c | 10 +++++----- src/v27ter_rx.c | 4 ++-- src/v29rx.c | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/v17rx.c b/src/v17rx.c index 8d9fbad8..7ae7e66c 100644 --- a/src/v17rx.c +++ b/src/v17rx.c @@ -679,7 +679,7 @@ static void process_half_baud(v17_rx_state_t *s, const complexf_t *sample) out is the phase. */ /* Check if we just saw A or B */ /* atan(1/3) = 18.433 degrees */ - if ((uint32_t) (angle - s->last_angles[0]) < (uint32_t) DDS_PHASE(180.0f)) + if (((uint32_t) angle - (uint32_t) s->last_angles[0]) < (uint32_t) DDS_PHASE(180.0f)) { angle = s->last_angles[0]; s->last_angles[0] = DDS_PHASE(270.0f + 18.433f); @@ -694,7 +694,7 @@ static void process_half_baud(v17_rx_state_t *s, const complexf_t *sample) /* Make a step shift in the phase, to pull it into line. We need to rotate the equalizer buffer, as well as the carrier phase, for this to play out nicely. */ /* angle is now the difference between where A is, and where it should be */ - phase_step = angle - DDS_PHASE(180.0f + 18.433f); + phase_step = (uint32_t) angle - (uint32_t) DDS_PHASE(180.0f + 18.433f); #if defined(SPANDSP_USE_FIXED_POINTx) ip = phase_step >> 16; span_log(&s->logging, SPAN_LOG_FLOW, "Spin (short) by %d\n", ip); @@ -728,7 +728,7 @@ static void process_half_baud(v17_rx_state_t *s, const complexf_t *sample) /* Look for the initial ABAB sequence to display a phase reversal, which will signal the start of the scrambled CDBA segment */ i = s->training_count + 1; - ang = angle - s->last_angles[i & 1]; + ang = (int32_t) ((uint32_t) angle - (uint32_t) s->last_angles[i & 1]); s->last_angles[i & 1] = angle; s->diff_angles[i & 0xF] = s->diff_angles[(i - 2) & 0xF] + (ang >> 4); if ((ang > DDS_PHASE(90.0f) || ang < DDS_PHASE(-90.0f)) && s->training_count >= 13) @@ -767,7 +767,7 @@ static void process_half_baud(v17_rx_state_t *s, const complexf_t *sample) /* Make a step shift in the phase, to pull it into line. We need to rotate the equalizer buffer, as well as the carrier phase, for this to play out nicely. */ /* angle is now the difference between where C is, and where it should be */ - phase_step = angle - DDS_PHASE(18.433f); + phase_step = (uint32_t) angle - (uint32_t) DDS_PHASE(18.433f); #if defined(SPANDSP_USE_FIXED_POINTx) ip = phase_step >> 16; span_log(&s->logging, SPAN_LOG_FLOW, "Spin (long) by %d\n", ip); @@ -936,7 +936,7 @@ static void process_half_baud(v17_rx_state_t *s, const complexf_t *sample) /* Look for the initial ABAB sequence to display a phase reversal, which will signal the start of the scrambled CDBA segment */ angle = arctan2(z.im, z.re); - ang = angle - s->last_angles[s->training_count & 1]; + ang = (int32_t) ((uint32_t) angle - (uint32_t) s->last_angles[s->training_count & 1]); if (ang > DDS_PHASE(90.0f) || ang < DDS_PHASE(-90.0f)) { /* We seem to have a phase reversal */ diff --git a/src/v27ter_rx.c b/src/v27ter_rx.c index 583468f6..fa5a7b9e 100644 --- a/src/v27ter_rx.c +++ b/src/v27ter_rx.c @@ -601,7 +601,7 @@ static __inline__ void process_half_baud(v27ter_rx_state_t *s, const complexf_t /* Look for the initial ABAB sequence to display a phase reversal, which will signal the start of the scrambled ABAB segment */ i = s->training_count + 1; - ang = angle - s->last_angles[i & 1]; + ang = (int32_t) ((uint32_t) angle - (uint32_t) s->last_angles[i & 1]); s->last_angles[i & 1] = angle; s->diff_angles[i & 0xF] = s->diff_angles[(i - 2) & 0xF] + (ang >> 4); if ((ang > DDS_PHASE(45.0f) || ang < DDS_PHASE(-45.0f)) && s->training_count >= 13) @@ -641,7 +641,7 @@ static __inline__ void process_half_baud(v27ter_rx_state_t *s, const complexf_t /* Make a step shift in the phase, to pull it into line. We need to rotate the equalizer buffer, as well as the carrier phase, for this to play out nicely. */ - angle += DDS_PHASE(180.0f); + angle = (int32_t) ((uint32_t) angle + (uint32_t) DDS_PHASE(180.0f)); #if defined(SPANDSP_USE_FIXED_POINT) z16 = complex_seti16(fixed_cos(angle >> 16), -fixed_sin(angle >> 16)); for (i = 0; i < V27TER_EQUALIZER_LEN; i++) diff --git a/src/v29rx.c b/src/v29rx.c index aa98e577..ba4ba813 100644 --- a/src/v29rx.c +++ b/src/v29rx.c @@ -571,7 +571,7 @@ static void process_half_baud(v29_rx_state_t *s, complexf_t *sample) /* Look for the initial ABAB sequence to display a phase reversal, which will signal the start of the scrambled CDCD segment */ i = s->training_count + 1; - ang = angle - s->last_angles[i & 1]; + ang = (int32_t) ((uint32_t) angle - (uint32_t) s->last_angles[i & 1]); s->last_angles[i & 1] = angle; s->diff_angles[i & 0xF] = s->diff_angles[(i - 2) & 0xF] + (ang >> 4); if ((ang > DDS_PHASE(45.0f) || ang < DDS_PHASE(-45.0f)) && s->training_count >= 13)