Commit c7a68234 authored by Christian Fibich's avatar Christian Fibich
Browse files

initial commit

parents
#include <stdio.h>
#include <stdlib.h>
#define N 128
struct S {
unsigned int a : 3;
unsigned int b : 2;
unsigned int c : 2;
};
int bitfields (int seed) {
int i;
unsigned int cnt = seed;
volatile struct S arr[N];
for (i = 0; i < N; i++) {
unsigned int rand = (cnt&0x80000000) ? (cnt<<1) ^ 0xD2 : (cnt << 1);
arr[i].a = rand&0x7;
arr[i].b = rand&0x3;
arr[i].c = (rand&0xC)>>2;
}
int result = 0;
for (i = 0; i < N; i++) {
result += arr[i].a + arr[i].b - arr[i].c;
}
return result;
}
int main(void) {
int result = bitfields(128);
printf("%d\n",result);
return result;
}
#include <complex.h>
#include <stdio.h>
#define cimagf(x) ((float)(x/I))
#define crealf(x) ((float)(x))
float do_cmplx(float a, float b, float c, float d) {
float complex A = a + I * b;
float complex B = c + I * d;
float complex C = A + B;
printf("%f + %fi + %f + %fi = %f + %fi\n",a,b,c,d,crealf(C),cimagf(C));
return crealf(C) + cimagf(C);
}
int main(void) {
float result = do_cmplx(1,9,100,5);
printf("%f\n",result);
return 0;
}
#include <stdio.h>
#include <stdint.h>
#define TAG_FLOAT 0x1000
#define TAG_INTEGER 0x0100
struct have_u32 {
int tag;
uint32_t data;
};
struct have_f32 {
int tag;
float data;
};
struct have_generic {
int tag;
unsigned char data[4];
};
void add_generic(const struct have_generic *in, struct have_generic *out) {
out->tag += in->tag;
for (int i = 0; i < 4; i++)
out->data[i] += in->data[i];
}
int pointer_cast(void) {
volatile struct have_u32 u = {.tag = TAG_INTEGER, .data = 0xdeadbeef};
volatile struct have_f32 f = {.tag = TAG_FLOAT, .data = 3.1415926f};
volatile struct have_generic sum = {0};
add_generic((struct have_generic *)(&u),&sum);
add_generic((struct have_generic *)(&f),&sum);
int result = sum.tag;
printf("sum.tag = %d\n",sum.tag);
for (int i = 0; i < 4; i++) {
printf("sum.data[%d] = %02x\n",i,sum.data[i]);
result += sum.data[i];
}
printf("%d\n",result-5034);
return result-5034;
}
int main(void) {
return pointer_cast();
}
#include <stdio.h>
#include <stdlib.h>
#define N 128
struct S {
unsigned int data[N];
};
int struct_assign (int seed) {
unsigned int cnt = seed | 1;
int result = 0;
volatile struct S A, B;
int i;
_Bool a = 0,b = 0;
for (i = 0; i < N; i++) {
A.data[i] = (cnt&0x80000000) ? (cnt<<1) ^ 0xD2 : (cnt << 1);
a ^= cnt;
b ^= cnt;
}
B = A;
for (i = 0; i < N; i++) {
result += (A.data[i] != B.data[i]);
}
result += (a != b);
return result;
}
int main(void) {
int result = struct_assign(128);
printf("%d\n",result);
return result;
}
#include <stdio.h>
#include <stdlib.h>
#define N 128
struct S {
unsigned int data[N];
};
int compstruct(struct S A, struct S B) {
int result = 0;
for (int i = 0; i < N; i++) {
result += (A.data[i] != B.data[i]);
}
A.data[0] = 0xDEAD;
B.data[0] = 0xBEEF;
return result;
}
int struct_pass (int seed) {
int result = 0;
struct S A = {0}, B = {0};
unsigned int cnt = seed | 1;
for (int i = 0; i < N; i++) {
A.data[i] = (cnt&0x80000000) ? (cnt<<1) ^ 0xD2 : (cnt << 1);
}
B = A;
result = compstruct(A,B);
result += (A.data[0] != B.data[0]);
printf("%d",A.data[0]);
return result;
}
int main(void) {
int result = struct_pass(128);
printf("%d\n",result);
return result;
}
#include <stdio.h>
#include <stdlib.h>
#define N 128
struct S {
unsigned int data[N];
};
struct S mkstruct(int seed) {
struct S A;
unsigned int cnt = seed | 1;
for (int i = 0; i < N; i++) {
A.data[i] = (cnt&0x80000000) ? (cnt<<1) ^ 0xD2 : (cnt << 1);
}
return A;
}
int compstruct(struct S A, struct S B) {
int result = 0;
for (int i = 0; i < N; i++) {
result += (A.data[i] != B.data[i]);
}
return result;
}
int struct_pass (int seed) {
int result = 0;
struct S A = {0}, B = {0};
A = mkstruct (seed);
B = A;
return compstruct(A,B);
}
int main(void) {
int result = struct_pass(128);
printf("%d\n",result);
return result;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment