Advertisement
petrdynin

Static_branch_prediction_CH32V003

Feb 26th, 2025 (edited)
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #ifdef BRANCH_PREDICTION
  2.  
  3. #define ARRAY_SIZE 256
  4. #define MIN   2               //sorted - 0x261a, random - 0x2620
  5. #define MID   ARRAY_SIZE/2    //sorted - 0x2716, random - 0x272c
  6. #define MAX   ARRAY_SIZE-2    //sorted - 0x2812, random - 0x2812
  7.  
  8.  
  9. volatile uint32_t time_unsorted;
  10. volatile uint32_t time_sort_min, time_sort_max, time_sort_mid;
  11.  
  12. volatile uint32_t start_time, end_time;
  13.  
  14. volatile uint16_t sorted_array[ARRAY_SIZE];
  15. volatile uint16_t unsorted_array[ARRAY_SIZE];
  16. volatile uint32_t cnt_tick;
  17.  
  18.  
  19. volatile uint32_t dummy = 0;
  20.  
  21. void SysTick_Init(void){
  22.    SysTick->SR = 0;
  23.    SysTick->CNT = 0;
  24.    SysTick->CTLR = 5;
  25. }
  26.  
  27.  
  28. static uint32_t random_seed = 123456789;
  29.  
  30. uint16_t rand_xorshift() {
  31.     random_seed ^= random_seed << 13;
  32.     random_seed ^= random_seed >> 17;
  33.     random_seed ^= random_seed << 5;
  34.     return (uint16_t)(random_seed & 0x00FF);
  35. }
  36.  
  37. uint32_t measure_time(volatile uint16_t *array, uint16_t treshold) {
  38.  
  39.    start_time = SysTick->CNT;
  40.    for (uint32_t i = 0; i < ARRAY_SIZE; i++) {
  41.         if (array[i] > treshold){
  42.             dummy++;
  43.          } else {
  44.            asm volatile("nop");
  45.            dummy--;
  46.          }
  47.     }
  48.     end_time = SysTick->CNT;
  49.     return end_time - start_time;
  50. }
  51.  
  52. int main() {
  53.  
  54.    SysTick_Init();
  55.  
  56.    for (uint32_t i = 0; i < ARRAY_SIZE; i++) {
  57.         sorted_array[i] = i;
  58.         //unsorted_array[i] = (uint16_t)rand_xorshift();
  59.         unsorted_array[i] = 1;
  60.        }
  61.  
  62.    while (1){
  63.       time_unsorted = measure_time(unsorted_array,(uint16_t)MID);
  64.       time_sort_min = measure_time(sorted_array,  (uint16_t)MIN);
  65.       time_sort_mid = measure_time(sorted_array,  (uint16_t)MID);
  66.       time_sort_max = measure_time(sorted_array,  (uint16_t)MAX);
  67.  
  68.       cnt_tick = SysTick->CNT;
  69.       asm("nop");
  70.     }
  71. }
  72. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement