| 1 | /* |
| 2 | * File: look2_iflf_binlcapw.c |
| 3 | * |
| 4 | * Code generated for Simulink model 'BSER_BattSocCalc'. |
| 5 | * |
| 6 | * Model version : 'M1.5.7' |
| 7 | * Simulink Coder version : 8.13 (R2017b) 24-Jul-2017 |
| 8 | * C/C++ source code generated on : Wed Apr 28 19:42:29 2021 |
| 9 | */ |
| 10 | |
| 11 | #include "rtwtypes.h" |
| 12 | #include "look2_iflf_binlcapw.h" |
| 13 | |
| 14 | real32_T look2_iflf_binlcapw(real32_T u0, real32_T u1, const real32_T bp0[], |
| 15 | const real32_T bp1[], const real32_T table[], const uint32_T maxIndex[], |
| 16 | uint32_T stride) |
| 17 | { |
| 18 | real32_T y; |
| 19 | real32_T frac; |
| 20 | uint32_T bpIndices[2]; |
| 21 | real32_T fractions[2]; |
| 22 | real32_T yR_1d; |
| 23 | uint32_T iRght; |
| 24 | uint32_T bpIdx; |
| 25 | uint32_T iLeft; |
| 26 | |
| 27 | /* Lookup 2-D |
| 28 | Search method: 'binary' |
| 29 | Use previous index: 'off' |
| 30 | Interpolation method: 'Linear' |
| 31 | Extrapolation method: 'Clip' |
| 32 | Use last breakpoint for index at or above upper limit: 'on' |
| 33 | Remove protection against out-of-range input in generated code: 'off' |
| 34 | */ |
| 35 | /* Prelookup - Index and Fraction |
| 36 | Index Search method: 'binary' |
| 37 | Extrapolation method: 'Clip' |
| 38 | Use previous index: 'off' |
| 39 | Use last breakpoint for index at or above upper limit: 'on' |
| 40 | Remove protection against out-of-range input in generated code: 'off' |
| 41 | */ |
| 42 | if (u0 <= bp0[0U]) { |
| 43 | iLeft = 0U; |
| 44 | frac = 0.0F; |
| 45 | } else if (u0 < bp0[maxIndex[0U]]) { |
| 46 | /* Binary Search */ |
| 47 | bpIdx = maxIndex[0U] >> 1U; |
| 48 | iLeft = 0U; |
| 49 | iRght = maxIndex[0U]; |
| 50 | while (iRght - iLeft > 1U) { |
| 51 | if (u0 < bp0[bpIdx]) { |
| 52 | iRght = bpIdx; |
| 53 | } else { |
| 54 | iLeft = bpIdx; |
| 55 | } |
| 56 | |
| 57 | bpIdx = (iRght + iLeft) >> 1U; |
| 58 | } |
| 59 | |
| 60 | frac = (u0 - bp0[iLeft]) / (bp0[iLeft + 1U] - bp0[iLeft]); |
| 61 | } else { |
| 62 | iLeft = maxIndex[0U]; |
| 63 | frac = 0.0F; |
| 64 | } |
| 65 | |
| 66 | fractions[0U] = frac; |
| 67 | bpIndices[0U] = iLeft; |
| 68 | |
| 69 | /* Prelookup - Index and Fraction |
| 70 | Index Search method: 'binary' |
| 71 | Extrapolation method: 'Clip' |
| 72 | Use previous index: 'off' |
| 73 | Use last breakpoint for index at or above upper limit: 'on' |
| 74 | Remove protection against out-of-range input in generated code: 'off' |
| 75 | */ |
| 76 | if (u1 <= bp1[0U]) { |
| 77 | iLeft = 0U; |
| 78 | frac = 0.0F; |
| 79 | } else if (u1 < bp1[maxIndex[1U]]) { |
| 80 | /* Binary Search */ |
| 81 | bpIdx = maxIndex[1U] >> 1U; |
| 82 | iLeft = 0U; |
| 83 | iRght = maxIndex[1U]; |
| 84 | while (iRght - iLeft > 1U) { |
| 85 | if (u1 < bp1[bpIdx]) { |
| 86 | iRght = bpIdx; |
| 87 | } else { |
| 88 | iLeft = bpIdx; |
| 89 | } |
| 90 | |
| 91 | bpIdx = (iRght + iLeft) >> 1U; |
| 92 | } |
| 93 | |
| 94 | frac = (u1 - bp1[iLeft]) / (bp1[iLeft + 1U] - bp1[iLeft]); |
| 95 | } else { |
| 96 | iLeft = maxIndex[1U]; |
| 97 | frac = 0.0F; |
| 98 | } |
| 99 | |
| 100 | /* Interpolation 2-D |
| 101 | Interpolation method: 'Linear' |
| 102 | Use last breakpoint for index at or above upper limit: 'on' |
| 103 | Overflow mode: 'portable wrapping' |
| 104 | */ |
| 105 | bpIdx = iLeft * stride + bpIndices[0U]; |
| 106 | if (bpIndices[0U] == maxIndex[0U]) { |
| 107 | y = table[bpIdx]; |
| 108 | } else { |
| 109 | y = (table[bpIdx + 1U] - table[bpIdx]) * fractions[0U] + table[bpIdx]; |
| 110 | } |
| 111 | |
| 112 | if (iLeft == maxIndex[1U]) { |
| 113 | } else { |
| 114 | bpIdx += stride; |
| 115 | if (bpIndices[0U] == maxIndex[0U]) { |
| 116 | yR_1d = table[bpIdx]; |
| 117 | } else { |
| 118 | yR_1d = (table[bpIdx + 1U] - table[bpIdx]) * fractions[0U] + table[bpIdx]; |
| 119 | } |
| 120 | |
| 121 | y += (yR_1d - y) * frac; |
| 122 | } |
| 123 | |
| 124 | return y; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * File trailer for generated code. |
| 129 | * |
| 130 | * [EOF] |
| 131 | */ |
| 132 |