This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
int switch_lg(uint32_t power_of_two) { | |
switch (power_of_two) { | |
case 0x1 : | |
return 0; | |
case 0x2 : | |
return 1; | |
case 0x4 : | |
return 2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
int trim_k_and_r(char s[]) { | |
int n; | |
for (n = strlen(s) - 1; n >= 0; n--) { | |
if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n') { | |
break; | |
} | |
} | |
s[n + 1] = '\0'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void multiply_add(int *accumulator, int multiplier, int addend) { | |
*accumulator *= multiplier; | |
*accumulator += addend; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The ternary operator version and the bit manipuation version is *not* equivalent to the "if" version | |
// since they'll have a memory access regardless of array[i]'s value | |
void clamp_if(int *array, int length) { | |
for (int i = 0; i < length; i++) { | |
if (array[i] < 0) { | |
array[i] = 0; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// extracting the load in each conditional path or each half of the conditional assignment was necessary for vectorization | |
// duplicate loads is the impediment to vectorization, not if/else statements | |
void loop_feedback_if(int *restrict levels, int *restrict deviations, int length, int feedback) { | |
for (int i = 0; i < length; i++) { | |
int level = levels[i]; | |
if (deviations[i] < 0) { | |
levels[i] = level + feedback; | |
} else { | |
levels[i] = level - feedback; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int apply_feedback_if(int level, int feedback, int deviation) { | |
if (deviation < 0) { | |
return level + feedback; | |
} else { | |
return level - feedback; | |
} | |
} | |
int apply_feedback_ternary(int level, int feedback, int deviation) { | |
return deviation < 0 ? level + feedback : level - feedback; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int values[] = {5, 6, 2, 9, 8, 2, 9, 7}; | |
long basic_loop(unsigned int count) { | |
long sum = 0; | |
for(unsigned int i = 0; i < count; i++) { | |
sum += values[i % 8]; | |
} | |
return sum; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void simple_memory_copy(char *restrict destination, char const *restrict source, unsigned int length) { | |
for (unsigned int i = 0; i < length; i++) { | |
destination[i] = source[i]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void nonoptimizable_accumulate(int *accumulator, int const *array, int length) { | |
for (int i = 0; i < length; i++) { | |
*accumulator += array[i]; | |
} | |
} | |
void optimizable_accumulate(int *restrict accumulator, int const *restrict array, int length) { | |
for (int i = 0; i < length; i++) { | |
*accumulator += array[i]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int compute_triangle_number(int base) { | |
int base_incremented = base + 1; | |
int numerator = base * base_incremented; | |
return numerator/2; | |
} | |
long simple_bonus_threshold(unsigned int number) { | |
int triangle_number = compute_triangle_number(number); | |
return 3 * triangle_number; | |
} |
NewerOlder