-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimewindow.c
58 lines (51 loc) · 1.05 KB
/
timewindow.c
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
/*
* Build: cc timewindow.c -o timewindow
* Run ./timewindow <Y> <Z> <TU>
*/
#include <stdio.h>
#include <stdlib.h>
double TW_Optimize(unsigned short Y, unsigned short Z, double TU)
{
return (((1 << Y) * (4 + Z)) >> 2) * TU;
}
double TW_Manufacturer(unsigned short Y, unsigned short Z, double TU)
{
return (1 << Y) * (1.0 + Z / 4.0) * TU;
}
int main(int argc, char *argv[])
{
int rc = 0;
unsigned short Y, Z;
double TU = 0.000976562;
switch (argc) {
case 2:
{
unsigned short bits = atoi(argv[1]);
Y = (bits >> 5) & 0b11;
Z = bits & 0b11111;
break;
}
case 4:
TU = atof(argv[3]);
/* fallthrough */
case 3:
Y = atoi(argv[1]);
Z = atoi(argv[2]);
break;
default:
rc = 1;
break;
}
if (rc == 0)
{
double X[2] = {
[0] = TW_Optimize(Y, Z, TU),
[1] = TW_Manufacturer(Y, Z, TU)
};
printf("TimeWindow@(%u, %u) > TW_Optimize()=%f > TW_Manufacturer()=%f" \
" > %s\n", Y, Z, X[0], X[1], X[0] == X[1] ? "OK" : "KO");
} else {
printf( "Usage:\t%s <Y> <Z> [TU]\nor\t%s <bits>\n", argv[0], argv[0]);
}
return rc;
}