Skip to content

Commit

Permalink
Fixed twos-complement and normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Irreq committed Oct 8, 2023
1 parent d3a5f78 commit fb66762
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/utils.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
#include <stdint.h>
#include <limits.h>

float normalize_int32(int32_t value) {
return value < 0
? -((float)value) / INT_MIN
: ((float)value) / INT_MAX;
#define INT24_MIN (-8388608) // -2^23
#define INT24_MAX (8388607) // 2^23 - 1


float normalize_int32(int32_t value24Bit) {
// Mask to keep only the 24 least significant bits
value24Bit &= 0xFFFFFF;

// Check the sign bit (bit 23)
if (value24Bit & 0x800000) {
// It's a negative number
// Calculate two's complement
value24Bit -= 0x1000000;
}

return value24Bit < 0
? -((float)value24Bit) / INT24_MIN
: ((float)value24Bit) / INT24_MAX;
}

int32_t denormalize_int32(float normalized_value) {
Expand Down

0 comments on commit fb66762

Please sign in to comment.