Help convert C code to Rust

Code below, has produced from c code and everything works well except for gain with
value 0.0 on rust scale value added by 0.5, i included c example to, can someone can
tell me what i'm doing wrong with rust.


const GAIN_TABLE: [f32; 31] = [
    (-50.0), (-45.0), (-40.0), (-35.0),
    (-30.0), (-25.0), (-22.0), (-19.0),
    (-16.0), (-13.0), (-10.0), (-8.0),
    (-6.0),  (-4.0),  (-2.0),  (0.0),
    (2.0),   (4.0),   (6.0),   (8.0),
    (10.0),  (13.0),  (16.0),  (19.0),
    (22.0),  (25.0),  (30.0),  (35.0),
    (40.0),  (45.0),  (50.0)];

const FRACT_BITS: i32 = 16;  /* single precision */
const DFRACT_BITS: i32 = 32; /* double precision */

const MAXVAL_DBL_CONST: i32 = 0x7FFFFFFF;

#[allow(overflowing_literals)]
const MINVAL_DBL_CONST: i32 = 0x80000000;

const FRACT_FIX_SCALE: i64 = (1 as i64) << (FRACT_BITS - 1);
const DFRACT_FIX_SCALE: i64 = (1 as i64) << (DFRACT_BITS -1);


#[allow(unused_parens, non_snake_case, unused_mut, unused_assignments)]
fn fl2fxconst_dbl(a: f32) -> f32 {

    let mut res: f32 = 0.0;
    let val = quant(a);

    if (val.is_sign_positive()) {
        if (( val * (DFRACT_FIX_SCALE as f32) + 0.5) >= MAXVAL_DBL_CONST as f32) {
            res = MAXVAL_DBL_CONST as f32;
         } else {
             res = val * (DFRACT_FIX_SCALE as f32) + 0.5;
         }
    } else {
        if (( val * (DFRACT_FIX_SCALE as f32) - 0.5) <= MINVAL_DBL_CONST as f32) {
            res = MINVAL_DBL_CONST as f32;
        } else {
            res = val * (DFRACT_FIX_SCALE as f32) - 0.5;
        }
    }

    res
}

fn quant(a: f32) -> f32 {
    let val = a / ( 1 << 7) as f32;
    val
}

fn main() {
    println!("MAXVAL_DBL_CONST: {}", MAXVAL_DBL_CONST);
    println!("MINVAL_DBL_CONST: {}", MINVAL_DBL_CONST);
    println!("FRACT_FIX_SCALE:  {}", FRACT_FIX_SCALE);
    println!("DFRACT_FIX_SCALE: {}", DFRACT_FIX_SCALE);
    println!("----------------------------------------");

    for val in GAIN_TABLE.iter(){
        println!("GAIN: {}, Quant: {}, Scale: {}", val, quant(val.clone()), fl2fxconst_dbl(val.clone()));
    }
}

here example c code:

typedef signed long LONG;
typedef unsigned long ULONG;

typedef signed short SHORT;
typedef unsigned short USHORT;
typedef signed char SCHAR;
typedef unsigned char UCHAR;

typedef long long INT64;
typedef unsigned long long UINT64;

#define MAXVAL_DBL ((signed)0x7FFFFFFF)
#define MINVAL_DBL ((signed)0x80000000)

#define FRACT_BITS 16  /* single precision */
#define DFRACT_BITS 32 /* double precision */

typedef SHORT FIXP_SGL;
typedef LONG FIXP_DBL;

#define MINVAL_DBL_CONST MINVAL_DBL

#define FRACT_FIX_SCALE ((INT64(1) << (FRACT_BITS - 1)))
#define DFRACT_FIX_SCALE ((INT64(1) << (DFRACT_BITS - 1)))


#define FL2FXCONST_DBL(val)                                                  \
  (FIXP_DBL)(                                                                \
      ((val) >= 0)                                                           \
          ? ((((double)(val) * (DFRACT_FIX_SCALE) + 0.5) >=                  \
              (double)(MAXVAL_DBL))                                          \
                 ? (LONG)(MAXVAL_DBL)                                        \
                 : (LONG)((double)(val) * (double)(DFRACT_FIX_SCALE) + 0.5)) \
          : ((((double)(val) * (DFRACT_FIX_SCALE)-0.5) <=                    \
              (double)(MINVAL_DBL_CONST))                                    \
                 ? (LONG)(MINVAL_DBL_CONST)                                  \
                 : (LONG)((double)(val) * (double)(DFRACT_FIX_SCALE)-0.5)))


#define MAX_CLD_QUANT_FINE (31)
#define SCALE_CLDE_SF (7)           /* maxVal in Quant tab is +/-  50 */


#define QUANT(a) (a /(float) (1 << SCALE_CLDE_SF))
#define SCALE_CLDE(a) (FL2FXCONST_DBL(a / (float)(1 << SCALE_CLDE_SF)))

static const float QuantTable[MAX_CLD_QUANT_FINE] = {
    (-50.0), (-45.0), (-40.0), (-35.0),
    (-30.0), (-25.0), (-22.0), (-19.0),
    (-16.0), (-13.0), (-10.0), (-8.0),
    (-6.0),  (-4.0),  (-2.0),  (0.0),
    (2.0),   (4.0),   (6.0),   (8.0),
    (10.0),  (13.0),  (16.0),  (19.0),
    (22.0),  (25.0),  (30.0),  (35.0),
    (40.0),  (45.0),  (50.0)};


#include<stdio.h>

int main()
{

  // compile argument g++ fl2fxconst_dbl.c -std=c++0x -Wall -Wno-parentheses -Wunused-variable -o test
  printf("MINVAL_DBL_CONST: %d\n", MINVAL_DBL);
  printf("MAXVAL_DBL_CONST: %d\n", MAXVAL_DBL);
  printf("FRACT_FIX_SCALE: %lld\n", FRACT_FIX_SCALE);
  printf("DFRACT_FIX_SCALE: %lld\n", DFRACT_FIX_SCALE);

  printf("CLD Quant table [SCALE] :\n");

  for (int i=0; i < MAX_CLD_QUANT_FINE; i++){
    printf("GAIN: %2f, Quant:%f, Scale:%ld\n", QuantTable[i], QUANT(QuantTable[i]), SCALE_CLDE(QuantTable[i]));
  }

  return 0;
}

It's casting the result to (LONG) so you need something like

fn fl2fxconst_dbl(a: f32) -> i32 {
    // ...
    res as i32
}
1 Like

thank you @quinedot i forgot with that (long) type.