• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <unistd.h>  // For usleep function
20 
21 typedef uint32_t UInt32;
22 typedef uint16_t UInt16;
23 
cond_branch_example_function(uint16_t * prob,uint8_t * buf)24 void cond_branch_example_function(uint16_t* prob, uint8_t* buf) {
25   UInt32 range = 0xFFFFFFFF;
26   UInt32 code = 0;
27   UInt32 bound;
28   UInt16 ttt;
29   UInt32 symbol = 0;
30 
31   do {
32     ttt = *(prob + symbol);
33     if (range < ((UInt32)1 << 24)) {
34       range <<= 8;
35       code = (code << 8) | (*buf++);
36     }
37     bound = (range >> 11) * ttt;
38     if (code < bound) {  // <== This is mispredicted branch (conditional branch)
39       range = bound;
40       *(prob + symbol) = (UInt16)(ttt + (((1 << 11) - ttt) >> 5));
41       symbol = (symbol + symbol);
42     } else {
43       range -= bound;
44       code -= bound;
45       *(prob + symbol) = (UInt16)(ttt - (ttt >> 5));
46       symbol = (symbol + symbol) + 1;
47     }
48   } while (symbol < 0x100);  // conditional branch
49 }
50 
main()51 int main() {
52   uint16_t prob[256] = {0};
53   uint8_t buf[256] = {0};
54 
55   usleep(15000000);
56 
57   // Call the conditional branch example function
58   cond_branch_example_function(prob, buf);
59 
60   return 0;
61 }
62