-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitflip.c
More file actions
114 lines (99 loc) · 2.22 KB
/
Copy pathbitflip.c
File metadata and controls
114 lines (99 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "bitflip.h"
//#define TEST
int initiated = 0;
int ipow(int base, int exp)
{
int result = 1;
while (exp)
{
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
int generate_bit_position()
{
if(initiated == 0)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
/* using nano-seconds instead of seconds */
srand((time_t)ts.tv_nsec);
// srand(time(NULL));
initiated = 1;
}
#define RANDOM_BITFLIP
#ifndef RANDOM_BITFLIP
//int num;
//srand(time(NULL));
//num = rand();
return (int)(LIMIT);
#else
int num;
// srand(time(NULL));
num = rand();
int num2 = rand();
if(num2%PROPABILITY == 0)
return (num % LIMIT);
else
return 0;
#endif
}
double bitflip_double (double num)
{
double temp_num = num;
long a = *((long*)&temp_num);
long temp_result = bitflip_long (a);
// printf( "\n %ld %ld \n", a , temp_result);
return *((double*)&temp_result);
}
float bitflip_float (float num)
{
float temp_num = num;
long a = *((long*)&temp_num);
long temp_result = bitflip_int (a);
//sprintf( "\n %ld %ld \n", a , temp_result);
return *((float*)&temp_result);
}
int bitflip_int (int num)
{
// return num;
int pos = generate_bit_position();
// printf(" %d ", pos);
if(pos ==0)
return num;
else
return num ^ (1<<pos);
}
long bitflip_long (long num)
{
int pos = generate_bit_position();
//printf(" %d ", pos);
return num ^ (1<<pos);
}
int bitflip_int_at_pos (int num, int pos)
{
//int pos = generate_bit_position();
// printf(" %d ", pos);
return num ^ (1<<pos);
}
int limitprecision_int (int num, int precision) // saturated max value for signed int
{
int limit = ipow(2,precision-1);
if(num > limit-1)
return limit -1;
if (num < (0-limit))
return 0 - limit;
return num;
}
#ifdef TEST
int main()
{
double num = 0.8;
printf("%lf\n",bitflip_float(num));
int num2 = 15;
printf ("%d %d \n", limitprecision_int(num2,4), limitprecision_int(num2,5));
}
#endif