Skip to content

Instantly share code, notes, and snippets.

@carlosejimenez
Created September 1, 2019 22:01
Show Gist options
  • Select an option

  • Save carlosejimenez/16a3e0855a91b44660c33415b068365d to your computer and use it in GitHub Desktop.

Select an option

Save carlosejimenez/16a3e0855a91b44660c33415b068365d to your computer and use it in GitHub Desktop.
modified random
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
////////////////////////////////
// Constants
////////////////////////////////
/* DP Mantissa Mask Bits */
long long DPMANT = 0x000fffffffffffff;
long long DPEXPO = 0x7ff0000000000000;
/* SP Mantissa Mask Bits */
int SPMANT = 0x007fffff;
int SPEXPO = 0x7f800000;
////////////////////////////////
// Data collection tools
////////////////////////////////
__attribute__((always_inline)) inline unsigned long long doubleMantissa (double in) {
return DPMANT & *(unsigned long long*) &in;
}
__attribute__((always_inline)) inline unsigned long long doubleExponent (double in) {
unsigned long long bits = DPEXPO & *(unsigned long long*) &in;
return bits >> 52;
}
__attribute__((always_inline)) inline unsigned int floatMantissa (float in) {
return SPMANT & *(unsigned int*) &in;
}
__attribute__((always_inline)) inline unsigned int floatExponent (float in) {
unsigned int bits = SPEXPO & *(unsigned int*) &in;
return bits >> 23;
}
__attribute__((always_inline)) inline void printDouble (double in) {
printf("%d,%llx,%llx", in>=0?0:1, doubleExponent(in), doubleMantissa(in));
}
__attribute__((always_inline)) inline void printFloat (float in) {
printf("%d,%x,%x", in>=0?0:1, floatExponent(in), floatMantissa(in));
}
void printHeader() {
printf("Operation,Lhs sign,lhs exp,lhs mant,rhs sign,rhs exp,rhs mant,"
"fp result sign,fp result exp,fp result mant,"
"real result sign,real result exp,real result mant\n");
}
__attribute__((always_inline)) inline void printLine(char operation,
float lhs, float rhs, float fpResult, double realResult) {
printf("%c,", operation);
printFloat(lhs);
printf(",");
printFloat(rhs);
printf(",");
printFloat(fpResult);
printf(",");
printDouble(realResult);
printf("\n");
}
__attribute__((always_inline)) inline float add (float lhs, float rhs) {
double realResult = (double)lhs + (double)rhs;
float fpResult = lhs + rhs;
printLine('+', lhs, rhs, fpResult, realResult);
return fpResult;
}
__attribute__((always_inline)) inline float multiply (float lhs, float rhs) {
double realResult = (double)lhs * (double)rhs;
float fpResult = lhs * rhs;
printLine('*', lhs, rhs, fpResult, realResult);
return fpResult;
}
__attribute__((always_inline)) inline float subtract (float lhs, float rhs) {
double realResult = (double)lhs - (double)rhs;
float fpResult = lhs - rhs;
printLine('-', lhs, rhs, fpResult, realResult);
return fpResult;
}
__attribute__((always_inline)) inline float divide (float lhs, float rhs) {
double realResult = (double)lhs / (double)rhs;
float fpResult = lhs / rhs;
printLine('/', lhs, rhs, fpResult, realResult);
return fpResult;
}
////////////////////////////////
// Main program
////////////////////////////////
void kernal(unsigned long n) {
srand(time(NULL));
printHeader();
for (int i = 0; i < n; i++) {
float x = (float)rand()/(float)(RAND_MAX);
float y = (float)rand()/(float)(RAND_MAX);
if (rand() % 2) {
x *= -1;
}
if (rand() % 2) {
y *= -1;
}
if (i%4 == 0) {
add(x, y);
} else if (i%4 == 1) {
subtract(x, y);
} else if (i%4 == 2) {
multiply(x, y);
} else {
divide(x, y);
}
}
}
int main (int argc, char** argv) {
unsigned long n;
if (argc != 2){
printf("No number argument given.\n");
return 1;
}
sscanf(argv[1], "%llu", &n);
kernal(n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment