Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef BRANCH_PREDICTION
- #define ARRAY_SIZE 256
- #define MIN 2 //sorted - 0x261a, random - 0x2620
- #define MID ARRAY_SIZE/2 //sorted - 0x2716, random - 0x272c
- #define MAX ARRAY_SIZE-2 //sorted - 0x2812, random - 0x2812
- volatile uint32_t time_unsorted;
- volatile uint32_t time_sort_min, time_sort_max, time_sort_mid;
- volatile uint32_t start_time, end_time;
- volatile uint8_t sorted_array[ARRAY_SIZE];
- volatile uint8_t unsorted_array[ARRAY_SIZE];
- volatile uint32_t cnt_tick;
- volatile uint32_t dummy = 0;
- void SysTick_Init(void){
- SysTick->SR = 0;
- SysTick->CNT = 0;
- SysTick->CTLR = 5;
- }
- static uint32_t random_seed = 123456789;
- uint16_t rand_xorshift() {
- random_seed ^= random_seed << 13;
- random_seed ^= random_seed >> 17;
- random_seed ^= random_seed << 5;
- return (uint16_t)(random_seed & 0x00FF);
- }
- uint32_t measure_time(volatile uint8_t *array, uint16_t treshold) {
- start_time = SysTick->CNT;
- for (uint32_t i = 0; i < ARRAY_SIZE; i++) {
- if (array[i] > (uint8_t)treshold){
- dummy++;
- } else {
- asm volatile("nop");
- dummy--;
- }
- }
- end_time = SysTick->CNT;
- return end_time - start_time;
- }
- int main() {
- SysTick_Init();
- for (uint32_t i = 0; i < ARRAY_SIZE; i++) {
- sorted_array[i] = (uint8_t)i;
- //unsorted_array[i] = (uint16_t)rand_xorshift();
- unsorted_array[i] = 1;
- }
- while (1){
- time_unsorted = measure_time(unsorted_array,(uint16_t)MID);
- time_sort_min = measure_time(sorted_array, (uint16_t)MIN);
- time_sort_mid = measure_time(sorted_array, (uint16_t)MID);
- time_sort_max = measure_time(sorted_array, (uint16_t)MAX);
- cnt_tick = SysTick->CNT;
- asm("nop");
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement