forked from SharpCoder/rpi-kernel
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.h
More file actions
executable file
·141 lines (116 loc) · 2.6 KB
/
Copy pathcommon.h
File metadata and controls
executable file
·141 lines (116 loc) · 2.6 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// *******************************
// FILE: common.h
// AUTHOR: SharpCoder
// DATE: 2012-03-18
// ABOUT: This is the include file for all pages. It defines
// data types and memory locations that should be
// accessible all around.
//
// LICENSE: Provided "AS IS". USE AT YOUR OWN RISK.
// *******************************
#ifndef __COMMON_H_
#define __COMMON_H_
typedef unsigned long ulong;
typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char byte;
// Linked list structure/class
class LinkedList {
public:
LinkedList* next;
LinkedList* previous;
uint32 value;
// Public
LinkedList() {
}
LinkedList* GetNext() {
return this->next;
}
LinkedList* GetPrev() {
return this->previous;
}
void Add( LinkedList* item ) {
LinkedList* end;
// Traverse the linked list.
while( end->next ) { end = end->next; }
// Add to the end.
end->next = item;
}
void Remove() {
if ( this->next && this->previous ) {
// Link em.
this->previous->next = this->next;
this->next->previous = this->previous;
// If there is a previous link...
} else if ( this->previous ) {
// We don't have any forward link.
this->previous->next = 0;
} else if ( this->next ) {
// well...
this->value = this->next->value;
this->next = this->next->next;
this->previous = 0;
}
}
};
int abs( int x ) {
if ( x >= 0 ) return x;
return x * -1;
}
int getNumberLength( int number, int base, int* count ) {
int size = 1;
*count = 0;
while(true) {
if ( number > (size*base) ) {
size *= base;
*(count)++;
} else {
break;
}
}
return size;
}
int getNumberLength( int number, int base ) {
int count = 0;
return getNumberLength( number, base, &count );
}
int getNumberLength( int number ) {
return getNumberLength( number, 10 );
}
void divide ( int top, int bottom, int* result, int* remainder ) {
// Cannot divide by zero.
if ( bottom == 0 ) return;
if ( bottom == 1 ) {
*result = top;
*remainder = 0;
return;
}
// Get the absolute values of everything.
top = abs(top);
bottom = abs(bottom);
// Reset the pointer values.
*result = 0;
*remainder = 0;
// Iterate until we hit zero.
int i;
for ( i = 0; i < top; i++ ) {
if ( (top - bottom) >= 0 ) {
*result = *result + 1;
} else {
*remainder = top;
break;
}
top = top - bottom;
}
}
int divide( int top, int bottom ) {
int result = 0, remainder = 0;
divide( top, bottom, &result, &remainder );
return result;
}
int modulo( int top, int bottom ) {
int result = 0, remainder = 0;
divide( top, bottom, &result, &remainder );
return remainder;
}
#endif