Skip to content

Additional (scalar) helper functions. #125

@astewart-xmos

Description

@astewart-xmos

I've repeatedly found myself needing to write helper functions such as the following:

int32_t ashr32(int32_t x, right_shift_t shr){
  int32_t y;
  if(shr >= 0) y = (x >> ( shr) );
  else         y = (x << (-shr) );
  return y;
}

int64_t ashr64(int64_t x, right_shift_t shr){
  int64_t y;
  if(shr >= 0) y = (x >> ( shr) );
  else         y = (x << (-shr) );
  return y;
}

int32_t float_s32_to_fixed(float_s32_t v, exponent_t output_exp){
  right_shift_t shr = output_exp - v.exp;
  return ashr32(v.mant, shr); 
}

int32_t float_s64_to_fixed(float_s64_t v, exponent_t output_exp){
  right_shift_t shr = output_exp - v.exp;
  return ashr64(v.mant, shr); 
}

int32_t float_to_fixed(float x, exponent_t output_exp){
  float_s32_t y = f32_to_float_s32(x);
  return float_s32_to_fixed(y, output_exp);
}

Including within lib_xcore_math. I believe there are already macros for the arithmetic shifts, but they aren't exposed as part of the public API. These should probably be added.

Metadata

Metadata

Assignees

No one assigned

    Labels

    size:Stype:enhancementNew feature or requestversion: minor incrMinor version number increment required. (Change adds new features)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions