]> Joshua Wise's Git repositories - netwatch.git/blame_incremental - lib/minilib.c
An optimized memcpy() is fine too.
[netwatch.git] / lib / minilib.c
... / ...
CommitLineData
1#include "console.h"
2#include <minilib.h>
3#include <output.h>
4
5/* We have both _memcpy and memcpy, because gcc might be able to do better in lwip.
6 * For small things, gcc inlines its memcpy, but for large things, we call out
7 * to this memcpy.
8 */
9inline void _memcpy(void *dest, const void *src, int bytes)
10{
11 /* I hate everyone */
12 /* Since we otherwise compile with -O0, we might as well manually speed this up a bit. */
13
14 char *cdest = dest;
15 const char *csrc = src;
16 int *idest;
17 const int *isrc;
18 int nwords;
19
20 /* Align to src (picked arbitrarily; might as well align to something) */
21 while (bytes && ((unsigned int)csrc & 3))
22 {
23 *(cdest++) = *(csrc++);
24 bytes--;
25 }
26
27 idest = (int *)cdest;
28 isrc = (const int *)csrc;
29
30 nwords = bytes / 4;
31 bytes -= bytes & ~3;
32 if (nwords != 0)
33 switch(nwords % 8) /* They see me Duff'sin'. They hatin'. */
34 do {
35 case 0: nwords--; *(idest++) = *(isrc++);
36 case 7: nwords--; *(idest++) = *(isrc++);
37 case 6: nwords--; *(idest++) = *(isrc++);
38 case 5: nwords--; *(idest++) = *(isrc++);
39 case 4: nwords--; *(idest++) = *(isrc++);
40 case 3: nwords--; *(idest++) = *(isrc++);
41 case 2: nwords--; *(idest++) = *(isrc++);
42 case 1: nwords--; *(idest++) = *(isrc++);
43 } while (nwords);
44
45 cdest = (char *)idest;
46 csrc = (const char *)isrc;
47 while (bytes) /* Clean up the remainder */
48 {
49 *(cdest++) = *(csrc++);
50 bytes--;
51 }
52}
53
54void memcpy(void *dest, const void *src, int bytes)
55{
56 _memcpy(dest, src, bytes);
57}
58
59void memset(void *dest, int data, int bytes)
60{
61 unsigned char *cdest = dest;
62 while (bytes--)
63 *(cdest++) = (unsigned char)data;
64}
65
66void memmove(void *dest, void *src, int bytes)
67{
68 char * cdest = dest;
69 char * csrc = src;
70 if ((cdest > csrc) && (cdest <= (csrc + bytes)))
71 {
72 /* do it backwards! */
73 cdest += bytes;
74 csrc += bytes;
75 while (bytes--)
76 *(--cdest) = *(--csrc);
77 } else
78 memcpy(dest, src, bytes);
79}
80
81int memcmp (const char *a2, const char *a1, int bytes) {
82 while (bytes--)
83 {
84 if (*(a2++) != *(a1++))
85 return 1;
86 }
87 return 0;
88}
89
90int strcmp (const char *a2, const char *a1) {
91 while (1) {
92 if (*a2 != *a1) return 1;
93 if (*a2 == 0) return 0;
94 a1++;
95 a2++;
96 }
97}
98
99int strncmp (const char *a2, const char *a1, int n) {
100 while (n--) {
101 if (*a2 != *a1) return 1;
102 if (*a2 == 0) return 0;
103 a1++;
104 a2++;
105 }
106 return 0;
107}
108
109int strlen(const char *c)
110{
111 int l = 0;
112 while (*(c++))
113 l++;
114 return l;
115}
116
117void strcpy(char *a2, const char *a1)
118{
119 do {
120 *(a2++) = *a1;
121 } while (*(a1++));
122}
123
124void strcat(char *dest, char *src)
125{
126 while (*dest)
127 dest++;
128 while (*src)
129 *(dest++) = *(src++);
130 *(dest++) = *(src++);
131}
132
133void puts(const char *c)
134{
135 putbytes(c, strlen(c));
136}
137
138static char hexarr[] = "0123456789ABCDEF";
139void tohex(char *s, unsigned long l)
140{
141 int i;
142 for (i = 0; i < 8; i++)
143 {
144 s[i] = hexarr[l >> 28];
145 l <<= 4;
146 }
147}
148
149void puthex(unsigned long l)
150{
151 char d[9];
152 d[8] = 0;
153 tohex(d, l);
154 puts(d);
155}
156
157unsigned short htons(unsigned short in)
158{
159 return (in >> 8) | (in << 8);
160}
161
162unsigned int htonl(unsigned int in)
163{
164 return ((in & 0xff) << 24) |
165 ((in & 0xff00) << 8) |
166 ((in & 0xff0000UL) >> 8) |
167 ((in & 0xff000000UL) >> 24);
168}
This page took 0.024309 seconds and 4 git commands to generate.