Skip to content

Commit

Permalink
Patching undefined behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Kagamiin committed Oct 22, 2023
1 parent 360f08e commit ac9d853
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
32 changes: 12 additions & 20 deletions esfm.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,11 @@ static int13
ESFM_envelope_calc_sin0(uint10 phase, uint10 envelope)
{
uint16 out = 0;
uint13 neg = 0;
bool inc = 0;
int13 neg = 1;
phase &= 0x3ff;
if (phase & 0x200)
{
neg = ~(0);
inc = 1;
neg = -1;
}
if (phase & 0x100)
{
Expand All @@ -212,7 +210,7 @@ ESFM_envelope_calc_sin0(uint10 phase, uint10 envelope)
{
out = logsinrom[phase & 0xff];
}
return (int13)(((uint13)ESFM_envelope_calc_exp(out + (envelope << 3)) ^ neg) + inc);
return ESFM_envelope_calc_exp(out + (envelope << 3)) * neg;
}

/* ------------------------------------------------------------------------- */
Expand Down Expand Up @@ -275,13 +273,11 @@ static int13
ESFM_envelope_calc_sin4(uint10 phase, uint10 envelope)
{
uint16 out = 0;
int13 neg = 0;
bool inc = 0;
int13 neg = 1;
phase &= 0x3ff;
if ((phase & 0x300) == 0x100)
{
neg = ~(0);
inc = 1;
neg = -1;
}
if (phase & 0x200)
{
Expand All @@ -295,7 +291,7 @@ ESFM_envelope_calc_sin4(uint10 phase, uint10 envelope)
{
out = logsinrom[(phase << 1) & 0xff];
}
return (int13)(((uint13)ESFM_envelope_calc_exp(out + (envelope << 3)) ^ neg) + inc);
return ESFM_envelope_calc_exp(out + (envelope << 3)) * neg;
}

/* ------------------------------------------------------------------------- */
Expand Down Expand Up @@ -323,33 +319,29 @@ ESFM_envelope_calc_sin5(uint10 phase, uint10 envelope)
static int13
ESFM_envelope_calc_sin6(uint10 phase, uint10 envelope)
{
int13 neg = 0;
bool inc = 0;
int13 neg = 1;
phase &= 0x3ff;
if (phase & 0x200)
{
neg = ~(0);
inc = 1;
neg = -1;
}
return (int13)(((uint13)ESFM_envelope_calc_exp(envelope << 3) ^ neg) + inc);
return ESFM_envelope_calc_exp(envelope << 3) * neg;
}

/* ------------------------------------------------------------------------- */
static int13
ESFM_envelope_calc_sin7(uint10 phase, uint10 envelope)
{
uint16 out = 0;
int13 neg = 0;
bool inc = 0;
int13 neg = 1;
phase &= 0x3ff;
if (phase & 0x200)
{
neg = ~(0);
inc = 1;
neg = -1;
phase = (phase & 0x1ff) ^ 0x1ff;
}
out = phase << 3;
return (int13)(((uint13)ESFM_envelope_calc_exp(out + (envelope << 3)) ^ neg) + inc);
return ESFM_envelope_calc_exp(out + (envelope << 3)) * neg;
}

/* ------------------------------------------------------------------------- */
Expand Down
2 changes: 0 additions & 2 deletions esfm.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ typedef uint_fast16_t uint9;
typedef uint_fast16_t uint10;
typedef uint_fast16_t uint11;
typedef uint_fast16_t uint12;
typedef uint_fast16_t uint13;
typedef uint_fast16_t uint16;
typedef uint_fast32_t uint19;
typedef uint_fast32_t uint23;
Expand All @@ -115,7 +114,6 @@ typedef uint16_t uint9;
typedef uint16_t uint10;
typedef uint16_t uint11;
typedef uint16_t uint12;
typedef uint16_t uint13;
typedef uint16_t uint16;
typedef uint32_t uint19;
typedef uint32_t uint23;
Expand Down

0 comments on commit ac9d853

Please sign in to comment.