Cleaning up a little, and possibly a speed increase
[tpg/acess2.git] / Kernel / arch / x86 / lib.c
1 /*
2  * AcessOS Microkernel Version
3  * lib.c
4  */
5 #include <acess.h>
6
7 // === CODE ===
8 void Spinlock(int *lock)
9 {
10          int    v = 1;
11         while(v)        __asm__ __volatile__ ("lock xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(lock));
12 }
13
14 void Release(int *lock)
15 {
16         __asm__ __volatile__ ("lock andl $0, (%0)"::"r"(lock));
17 }
18
19 // === IO Commands ===
20 void outb(Uint16 Port, Uint8 Data)
21 {
22         __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
23 }
24 void outw(Uint16 Port, Uint16 Data)
25 {
26         __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
27 }
28 void outd(Uint16 Port, Uint32 Data)
29 {
30         __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
31 }
32 Uint8 inb(Uint16 Port)
33 {
34         Uint8   ret;
35         __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
36         return ret;
37 }
38 Uint16 inw(Uint16 Port)
39 {
40         Uint16  ret;
41         __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
42         return ret;
43 }
44 Uint32 ind(Uint16 Port)
45 {
46         Uint32  ret;
47         __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
48         return ret;
49 }
50
51 /**
52  * \fn void *memset(void *Dest, int Val, size_t Num)
53  * \brief Do a byte granuality set of Dest
54  */
55 void *memset(void *Dest, int Val, size_t Num)
56 {
57         Uint32  val = Val&0xFF;
58         val |= val << 8;
59         val |= val << 16;
60         __asm__ __volatile__ (
61                 "rep stosl;\n\t"
62                 "mov %3, %%ecx;\n\t"
63                 "rep stosb"
64                 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
65         return Dest;
66 }
67 /**
68  * \brief Set double words
69  */
70 void *memsetd(void *Dest, Uint32 Val, size_t Num)
71 {
72         __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
73         return Dest;
74 }
75
76 /**
77  * \fn int memcmp(const void *m1, const void *m2, size_t Num)
78  * \brief Compare two pieces of memory
79  */
80 int memcmp(const void *m1, const void *m2, size_t Num)
81 {
82         while(Num--)
83         {
84                 if(*(Uint8*)m1 != *(Uint8*)m2)  break;
85                 m1 ++;
86                 m2 ++;
87         }
88         return *(Uint8*)m1 - *(Uint8*)m2;
89 }
90
91 /**
92  * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
93  * \brief Copy \a Num bytes from \a Src to \a Dest
94  */
95 void *memcpy(void *Dest, const void *Src, size_t Num)
96 {
97         if( ((Uint)Dest & 3) || ((Uint)Src & 3) )
98                 __asm__ __volatile__ ("rep movsb" :: "D" (Dest), "S" (Src), "c" (Num));
99         else {
100                 __asm__ __volatile__ (
101                         "rep movsl;\n\t"
102                         "mov %3, %%ecx;\n\t"
103                         "rep movsb"
104                         :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
105         }
106         return Dest;
107 }
108 /**
109  * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
110  * \brief Copy \a Num DWORDs from \a Src to \a Dest
111  */
112 void *memcpyd(void *Dest, const void *Src, size_t Num)
113 {
114         __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
115         return Dest;
116 }
117
118 /**
119  * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
120  * \brief Divide two 64-bit integers
121  */
122 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
123 {
124         Uint64  ret = 0;
125         
126         if(Den == 0)    __asm__ __volatile__ ("int $0x0");      // Call Div by Zero Error
127         if(Den == 1)    return Num;     // Speed Hacks
128         if(Den == 2)    return Num >> 1;        // Speed Hacks
129         if(Den == 4)    return Num >> 2;        // Speed Hacks
130         if(Den == 8)    return Num >> 3;        // Speed Hacks
131         if(Den == 16)   return Num >> 4;        // Speed Hacks
132         if(Den == 32)   return Num >> 5;        // Speed Hacks
133         if(Den == 1024) return Num >> 10;       // Speed Hacks
134         if(Den == 2048) return Num >> 11;       // Speed Hacks
135         if(Den == 4096) return Num >> 12;
136         
137         if(Num >> 32 == 0 && Den >> 32 == 0)
138                 return (Uint32)Num / (Uint32)Den;
139         
140         //Log("__udivdi3: (Num={0x%x:%x}, Den={0x%x:%x})",
141         //      Num>>32, Num&0xFFFFFFFF,
142         //      Den>>32, Den&0xFFFFFFFF);
143         
144         while(Num > Den) {
145                 ret ++;
146                 Num -= Den;
147         }
148         return ret;
149 }
150
151 /**
152  * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
153  * \brief Get the modulus of two 64-bit integers
154  */
155 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
156 {
157         if(Den == 0)    __asm__ __volatile__ ("int $0x0");      // Call Div by Zero Error
158         if(Den == 1)    return 0;       // Speed Hacks
159         if(Den == 2)    return Num & 1; // Speed Hacks
160         if(Den == 4)    return Num & 3; // Speed Hacks
161         if(Den == 8)    return Num & 7; // Speed Hacks
162         if(Den == 16)   return Num & 15;        // Speed Hacks
163         if(Den == 32)   return Num & 31;        // Speed Hacks
164         if(Den == 1024) return Num & 1023;      // Speed Hacks
165         if(Den == 2048) return Num & 2047;      // Speed Hacks
166         if(Den == 4096) return Num & 4095;      // Speed Hacks
167         
168         if(Num >> 32 == 0 && Den >> 32 == 0)
169                 return (Uint32)Num % (Uint32)Den;
170         
171         while(Num > Den)
172                 Num -= Den;
173         return Num;
174 }
175
176 Uint16 LittleEndian16(Uint16 Val)
177 {
178         return Val;
179 }
180 Uint16 BigEndian16(Uint16 Val)
181 {
182         return ((Val&0xFF)<<8) | ((Val>>8)&0xFF);
183 }
184 Uint32 LittleEndian32(Uint32 Val)
185 {
186         return Val;
187 }
188 Uint32 BigEndian32(Uint32 Val)
189 {
190         return ((Val&0xFF)<<24) | ((Val&0xFF00)<<8) | ((Val>>8)&0xFF00) | ((Val>>24)&0xFF);
191 }
192
193 // --- EXPORTS ---
194 EXPORT(memcpy); EXPORT(memset);
195 EXPORT(memcmp);
196 //EXPORT(memcpyw);      EXPORT(memsetw);
197 EXPORT(memcpyd);        EXPORT(memsetd);
198 EXPORT(inb);    EXPORT(inw);    EXPORT(ind);
199 EXPORT(outb);   EXPORT(outw);   EXPORT(outd);
200 EXPORT(__udivdi3);      EXPORT(__umoddi3);
201
202 EXPORT(LittleEndian16); EXPORT(BigEndian16);
203 EXPORT(LittleEndian32); EXPORT(BigEndian32);

UCC git Repository :: git.ucc.asn.au