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 <stdlib.h> | |
#include <stdio.h> | |
void put_in_register(int i); | |
//int read_from_register(void); // conflicting type with definition | |
int read_from_register(); // incomplete declaration | |
int main(int argc, const char *argv[]) { | |
if (argc < 2) { | |
fprintf(stderr, "Provide a number as a command line argument\n"); |
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 <stdio.h> | |
#include <limits.h> | |
#include <stdint.h> // for PTRDIFF_WIDTH | |
#include <stdbool.h> // for pre-C23 | |
#include <float.h> | |
#include <math.h> | |
#include <uchar.h> | |
#if !defined (BOOL_WIDTH) | |
#define BOOL_WIDTH (-1) |
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 process_nonnegative_elements(int a[], int n) { | |
int i; | |
for (i = 0; i < n; i++) { | |
if (a[i] < 0) { /* skip negative elements */ | |
continue; | |
} | |
// ... /* do positive elements */ | |
} | |
} |
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; | |
} |
NewerOlder