| 18 | constexpr int kCodebookSize = 256; |
| 19 | |
| 20 | inline unsigned char lookup_code_index(const float* codebook, float value) { |
| 21 | value = std::clamp(value, -1.0f, 1.0f); |
| 22 | const float* begin = codebook; |
| 23 | const float* end = codebook + kCodebookSize; |
| 24 | const float* right = std::lower_bound(begin, end, value); |
| 25 | if (right == begin) { |
| 26 | return 0; |
| 27 | } |
| 28 | if (right == end) { |
| 29 | return static_cast<unsigned char>(kCodebookSize - 1); |
| 30 | } |
| 31 | const float* left = right - 1; |
| 32 | const float dist_left = std::fabs(value - *left); |
| 33 | const float dist_right = std::fabs(*right - value); |
| 34 | const unsigned char idx = static_cast<unsigned char>(right - begin); |
| 35 | return dist_right < dist_left ? idx : idx - 1; |
| 36 | } |
| 37 | |
| 38 | } // namespace |
| 39 |
nothing calls this directly
no outgoing calls
no test coverage detected