/*
 * File: look1_iu8lu32n24_binlcse.c
 *
 * Code generated for Simulink model 'BCMR_AcChrg'.
 *
 * Model version                  : 1.226F
 * Simulink Coder version         : 8.11F (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Mon Dec 10 19:10:29 2018
 */

#include "rtwtypes.h"
#include "look1_iu8lu32n24_binlcse.h"

uint8_T look1_iu8lu32n24_binlcse(uint8_T u0, const uint8_T bp0[], const uint8_T
  table[], uint32_T maxIndex)
{
  uint8_T y;
  uint32_T frac;
  uint32_T iRght;
  uint32_T iLeft;

  /* 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'
     Rounding mode: 'simplest'
   */
  /* 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'
     Rounding mode: 'simplest'
   */
  if (u0 <= bp0[0U]) {
    iLeft = 0U;
    frac = 0U;
  } else if (u0 < bp0[maxIndex]) {
    /* Binary Search */
    frac = maxIndex >> 1U;
    iLeft = 0U;
    iRght = maxIndex;
    while (iRght - iLeft > 1U) {
      if (u0 < bp0[frac]) {
        iRght = frac;
      } else {
        iLeft = frac;
      }

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

    frac = ((uint32_T)(uint8_T)((uint32_T)u0 - bp0[iLeft]) << 24) / (uint8_T)
      ((uint32_T)bp0[iLeft + 1U] - bp0[iLeft]);
  } else {
    iLeft = maxIndex - 1U;
    frac = 16777216U;
  }

  /* Interpolation 1-D
     Interpolation method: 'Linear'
     Use last breakpoint for index at or above upper limit: 'off'
     Rounding mode: 'simplest'
     Overflow mode: 'wrapping'
   */
  if (table[iLeft + 1U] >= table[iLeft]) {
    y = (uint8_T)((uint32_T)(uint8_T)((uint8_T)((uint32_T)table[iLeft + 1U] -
      table[iLeft]) * frac >> 24) + table[iLeft]);
  } else {
    y = (uint8_T)((uint32_T)table[iLeft] - (uint8_T)((uint8_T)((uint32_T)
      table[iLeft] - table[iLeft + 1U]) * frac >> 24));
  }

  return y;
}

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