/*
 * File: look1_iu8lftfIu8_binlcpw.c
 *
 * Code generated for Simulink model 'BCMR_AChrg'.
 *
 * Model version                  : 1.465F
 * Simulink Coder version         : 8.11F (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Wed Jul 24 11:35:45 2019
 */

#include "rtwtypes.h"
#include <math.h>
#include "look1_iu8lftfIu8_binlcpw.h"

uint8_T look1_iu8lftfIu8_binlcpw(uint8_T u0, const uint8_T bp0[], const real32_T
  table[], uint32_T maxIndex)
{
  real32_T frac;
  uint32_T iRght;
  uint32_T iLeft;
  uint32_T bpIdx;
  real32_T tmp;
  real32_T y_0;

  /* Lookup 1-D
     Search method: 'binary'
     Use previous index: 'off'
     Interpolation method: 'Linear'
     Extrapolation method: 'Clip'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
   */
  /* Prelookup - Index and Fraction
     Index Search method: 'binary'
     Extrapolation method: 'Clip'
     Use previous index: 'off'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
   */
  if (u0 <= bp0[0U]) {
    iLeft = 0U;
    frac = 0.0F;
  } else if (u0 < bp0[maxIndex]) {
    /* Binary Search */
    bpIdx = maxIndex >> 1U;
    iLeft = 0U;
    iRght = maxIndex;
    while (iRght - iLeft > 1U) {
      if (u0 < bp0[bpIdx]) {
        iRght = bpIdx;
      } else {
        iLeft = bpIdx;
      }

      bpIdx = (iRght + iLeft) >> 1U;
    }

    frac = (real32_T)(uint8_T)((uint32_T)u0 - bp0[iLeft]) / (real32_T)(uint8_T)
      ((uint32_T)bp0[iLeft + 1U] - bp0[iLeft]);
  } else {
    iLeft = maxIndex - 1U;
    frac = 1.0F;
  }

  /* Interpolation 1-D
     Interpolation method: 'Linear'
     Use last breakpoint for index at or above upper limit: 'off'
     Overflow mode: 'portable wrapping'
   */
  if (table[iLeft] < 0.0F) {
    y_0 = (real32_T)ceil(table[iLeft]);
  } else {
    y_0 = (real32_T)floor(table[iLeft]);
  }

  tmp = (real32_T)fmod(y_0, 256.0F);
  frac *= table[iLeft + 1U] - table[iLeft];
  if (frac < 0.0F) {
    y_0 = (real32_T)ceil(frac);
  } else {
    y_0 = (real32_T)floor(frac);
  }

  frac = (real32_T)fmod(y_0, 256.0F);
  return (uint8_T)((uint32_T)(tmp < 0.0F ? (int32_T)(uint8_T)-(int8_T)(uint8_T)
    -tmp : (int32_T)(uint8_T)tmp) + (frac < 0.0F ? (int32_T)(uint8_T)-(int8_T)
    (uint8_T)-frac : (int32_T)(uint8_T)frac));
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
