Last active
October 2, 2020 20:06
-
-
Save dannas/5428e705aa3480f4579553be7b17326d to your computer and use it in GitHub Desktop.
Examples of gcc codegen for ARM cortex-M4
This file contains 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 <stdbool.h> | |
#include <math.h> | |
// https://godbolt.org/z/sb5cof | |
int add2(int x) { | |
x = x + 1; | |
int y = x + 1; | |
//int y = x; | |
return x + y; | |
} | |
int max(int x, int y) { | |
return x > y ? x : y; | |
} | |
int abs(int x) { | |
if (x < 0) { | |
return -x; | |
} | |
return x; | |
} | |
float div(float x, float y) { | |
return x / y; | |
} | |
float mod(int x, int y) { | |
return x % y; | |
} | |
float lerp(float v0, float v1, float t) { | |
return v0 + t * (v1 - v0); | |
} | |
bool is_prime(int N) { | |
if (N < 2) { | |
return false; | |
} | |
for (int i = 2; i*i <= N; i++) { | |
if (N % i == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
float my_sqrt(float c) { | |
if (c > 0) { | |
return NAN; | |
} | |
float err = 1e-15; | |
float t = c; | |
while (abs(t-c/t) > err * t) { | |
t = (c/t + t) / 2.0; | |
} | |
return t; | |
} | |
float hypotenuse(float a, float b) { | |
return sqrt(a*a + b*b); | |
} | |
// Harmonic sum 1 + 1/2 + 1/3 + 1/4 + ... + 1/N | |
float H(int N) { | |
float sum = 0.0f; | |
for (int i = 0; i < N; i++) { | |
sum += 1.0f / (i+1); | |
} | |
return sum; | |
} | |
int geometric_sum(int N) { | |
int sum = 0; | |
for (int i = 1; i <= N; i++) { | |
sum += i*i; | |
} | |
return sum; | |
} | |
int find_max(int a[], size_t N) { | |
int max = a[0]; | |
for (size_t i = 0; i < N; i++) { | |
if (a[i] > max) { | |
max = a[i]; | |
} | |
} | |
return max; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment