-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.c
More file actions
115 lines (96 loc) · 2.48 KB
/
driver.c
File metadata and controls
115 lines (96 loc) · 2.48 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
115
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include "node.h"
extern Node* reverse_asm(Node *head, unsigned int offset);
#define NUM_NODES 10
int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
{
long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
result->tv_sec = diff / 1000000;
result->tv_usec = diff % 1000000;
return (diff<0);
}
void timeval_print(char *str, struct timeval *tv)
{
printf("%s %ld sec, %06ld micro sec\n", str, tv->tv_sec, tv->tv_usec);
}
Node *get_head(void *p)
{
return p == NULL ?
NULL :
((Node *)((char *)(p)-(unsigned long)(&((Node *)0)->ptr)));
}
Node *gen_rand_list(unsigned int count)
{
Node *head, *tail, *curr;
void *p;
unsigned int i;
head = tail = curr = NULL;
srand(time(NULL));
for(i = 0; i < count; i++) {
curr = (Node *) malloc (sizeof(Node));
memset(curr, 0, sizeof(Node));
curr->var = curr->val.n = rand() % 1000;
if(head == NULL) {
head = tail = curr;
} else {
tail->ptr = &(curr->ptr);
tail = curr;
}
}
return head;
}
void print_node(Node *node)
{
printf("%d", node->var);
}
void print_list(Node *head)
{
while(head) {
print_node(head);
printf(" -> ");
head = get_head(head->ptr);
}
printf(" NULL\n");
}
Node *reverse_C(Node *head)
{
void *headptr, *nextptr, *new_headptr;
headptr = nextptr = new_headptr = NULL;
if(head == NULL) return NULL;
headptr = &(head->ptr);
while (headptr) {
nextptr = (void *)(*((unsigned long *)headptr));
*(unsigned long *)headptr = (unsigned long) new_headptr;
new_headptr = headptr;
headptr = nextptr;
}
return get_head(new_headptr);
}
int main()
{
struct timeval tvDiff, tvStart, tvEnd;
Node *head, *revhead;
/* This is the offset from base of node structure to the ptr field */
unsigned int offset;
/* We ask the compiler: "If 0 were the address of a Node, what is the address of ptr field?" */
offset = (unsigned int)(&((Node *)0)->ptr);
head = gen_rand_list(NUM_NODES);
/*print_list(head);
revhead = reverse_asm(head, offset);
printf("****REV****\n");
print_list(revhead);*/
gettimeofday(&tvStart, NULL);
revhead = reverse_asm(head, offset);
gettimeofday(&tvEnd, NULL);
timeval_subtract(&tvDiff, &tvEnd, &tvStart);
timeval_print("ASM: ", &tvDiff);
gettimeofday(&tvStart, NULL);
head = reverse_C(revhead);
gettimeofday(&tvEnd, NULL);
timeval_subtract(&tvDiff, &tvEnd, &tvStart);
timeval_print("C: ", &tvDiff);
return 0;
}