OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [include/] [asm-i960/] [string.h] - Blame information for rev 1777

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1633 jcastillo
#ifndef _I960_STRING_H_
2
#define _I960_STRING_H_
3
 
4
/* FIXME we should probably do this, since the i960 can move 16 bytes in
5
   only a few cycles if in cache, whereas stock strcpy is going to SUCK */
6
 
7
#include <linux/config.h>
8
#include <asm/page.h>
9
 
10
#if 0
11
 
12
#define __HAVE_ARCH_STRCPY
13
extern inline char * strcpy(char * dest,const char *src)
14
{
15
  char *xdest = dest;
16
 
17
  __asm__ __volatile__
18
       ("1:\tmoveb %1@+,%0@+\n\t"
19
        "jne 1b"
20
        : "=a" (dest), "=a" (src)
21
        : "0" (dest), "1" (src) : "memory");
22
  return xdest;
23
}
24
 
25
#define __HAVE_ARCH_STRNCPY
26
extern inline char * strncpy(char *dest, const char *src, size_t n)
27
{
28
  char *xdest = dest;
29
 
30
  if (n == 0)
31
    return xdest;
32
 
33
  __asm__ __volatile__
34
       ("1:\tmoveb %1@+,%0@+\n\t"
35
        "jeq 2f\n\t"
36
        "subql #1,%2\n\t"
37
        "jne 1b\n\t"
38
        "2:"
39
        : "=a" (dest), "=a" (src), "=d" (n)
40
        : "0" (dest), "1" (src), "2" (n)
41
        : "memory");
42
  return xdest;
43
}
44
 
45
#define __HAVE_ARCH_STRCAT
46
extern inline char * strcat(char * dest, const char * src)
47
{
48
        char *tmp = dest;
49
 
50
        while (*dest)
51
                dest++;
52
        while ((*dest++ = *src++))
53
                ;
54
 
55
        return tmp;
56
}
57
 
58
#define __HAVE_ARCH_STRNCAT
59
extern inline char * strncat(char *dest, const char *src, size_t count)
60
{
61
        char *tmp = dest;
62
 
63
        if (count) {
64
                while (*dest)
65
                        dest++;
66
                while ((*dest++ = *src++)) {
67
                        if (--count == 0) {
68
                                *dest++='\0';
69
                                break;
70
                        }
71
                }
72
        }
73
 
74
        return tmp;
75
}
76
 
77
#define __HAVE_ARCH_STRCHR
78
extern inline char * strchr(const char * s, int c)
79
{
80
  const char ch = c;
81
 
82
  for(; *s != ch; ++s)
83
    if (*s == '\0')
84
      return( NULL );
85
  return( (char *) s);
86
}
87
 
88
#define __HAVE_ARCH_STRPBRK
89
extern inline char * strpbrk(const char * cs,const char * ct)
90
{
91
  const char *sc1,*sc2;
92
 
93
  for( sc1 = cs; *sc1 != '\0'; ++sc1)
94
    for( sc2 = ct; *sc2 != '\0'; ++sc2)
95
      if (*sc1 == *sc2)
96
        return((char *) sc1);
97
  return( NULL );
98
}
99
 
100
#define __HAVE_ARCH_STRSPN
101
extern inline size_t strspn(const char *s, const char *accept)
102
{
103
  const char *p;
104
  const char *a;
105
  size_t count = 0;
106
 
107
  for (p = s; *p != '\0'; ++p)
108
    {
109
      for (a = accept; *a != '\0'; ++a)
110
        if (*p == *a)
111
          break;
112
      if (*a == '\0')
113
        return count;
114
      else
115
        ++count;
116
    }
117
 
118
  return count;
119
}
120
 
121
#define __HAVE_ARCH_STRTOK
122
extern inline char * strtok(char * s,const char * ct)
123
{
124
  char *sbegin, *send;
125
 
126
  sbegin  = s ? s : ___strtok;
127
  if (!sbegin) {
128
          return NULL;
129
  }
130
  sbegin += strspn(sbegin,ct);
131
  if (*sbegin == '\0') {
132
    ___strtok = NULL;
133
    return( NULL );
134
  }
135
  send = strpbrk( sbegin, ct);
136
  if (send && *send != '\0')
137
    *send++ = '\0';
138
  ___strtok = send;
139
  return (sbegin);
140
}
141
 
142
/* strstr !! */
143
 
144
#define __HAVE_ARCH_STRLEN
145
extern inline size_t strlen(const char * s)
146
{
147
  const char *sc;
148
  for (sc = s; *sc != '\0'; ++sc) ;
149
  return(sc - s);
150
}
151
 
152
/* strnlen !! */
153
 
154
#define __HAVE_ARCH_STRCMP
155
extern inline int strcmp(const char * cs,const char * ct)
156
{
157
  char __res;
158
 
159
  __asm__
160
       ("1:\tmoveb %0@+,%2\n\t" /* get *cs */
161
        "cmpb %1@+,%2\n\t"      /* compare a byte */
162
        "jne  2f\n\t"           /* not equal, break out */
163
        "tstb %2\n\t"           /* at end of cs? */
164
        "jne  1b\n\t"           /* no, keep going */
165
        "jra  3f\n\t"           /* strings are equal */
166
        "2:\tsubb %1@-,%2\n\t"  /* *cs - *ct */
167
        "3:"
168
        : "=a" (cs), "=a" (ct), "=d" (__res)
169
        : "0" (cs), "1" (ct));
170
  return __res;
171
}
172
 
173
#define __HAVE_ARCH_STRNCMP
174
extern inline int strncmp(const char * cs,const char * ct,size_t count)
175
{
176
  char __res;
177
 
178
  if (!count)
179
    return 0;
180
  __asm__
181
       ("1:\tmovb %0@+,%3\n\t"          /* get *cs */
182
        "cmpb   %1@+,%3\n\t"            /* compare a byte */
183
        "jne    3f\n\t"                 /* not equal, break out */
184
        "tstb   %3\n\t"                 /* at end of cs? */
185
        "jeq    4f\n\t"                 /* yes, all done */
186
        "subql  #1,%2\n\t"              /* no, adjust count */
187
        "jne    1b\n\t"                 /* more to do, keep going */
188
        "2:\tmoveq #0,%3\n\t"           /* strings are equal */
189
        "jra    4f\n\t"
190
        "3:\tsubb %1@-,%3\n\t"          /* *cs - *ct */
191
        "4:"
192
        : "=a" (cs), "=a" (ct), "=d" (count), "=d" (__res)
193
        : "0" (cs), "1" (ct), "2" (count));
194
  return __res;
195
}
196
 
197
#define __HAVE_ARCH_MEMSET
198
/*
199
 * This is really ugly, but its highly optimizatiable by the
200
 * compiler and is meant as compensation for gcc's missing
201
 * __builtin_memset(). For the 680[23]0 it might be worth considering
202
 * the optimal number of misaligned writes compared to the number of
203
 * tests'n'branches needed to align the destination address. The
204
 * 680[46]0 doesn't really care due to their copy-back caches.
205
 *                                              10/09/96 - Jes Sorensen
206
 */
207
extern inline void * __memset_g(void * s, int c, size_t count)
208
{
209
  void *xs = s;
210
  size_t temp;
211
 
212
  if (!count)
213
    return xs;
214
 
215
  c &= 0xff;
216
  c |= c << 8;
217
  c |= c << 16;
218
 
219
  if (count < 36){
220
          long *ls = s;
221
 
222
          switch(count){
223
          case 32: case 33: case 34: case 35:
224
                  *ls++ = c;
225
          case 28: case 29: case 30: case 31:
226
                  *ls++ = c;
227
          case 24: case 25: case 26: case 27:
228
                  *ls++ = c;
229
          case 20: case 21: case 22: case 23:
230
                  *ls++ = c;
231
          case 16: case 17: case 18: case 19:
232
                  *ls++ = c;
233
          case 12: case 13: case 14: case 15:
234
                  *ls++ = c;
235
          case 8: case 9: case 10: case 11:
236
                  *ls++ = c;
237
          case 4: case 5: case 6: case 7:
238
                  *ls++ = c;
239
                  break;
240
          default:
241
                  break;
242
          }
243
          s = ls;
244
          if (count & 0x02){
245
                  short *ss = s;
246
                  *ss++ = c;
247
                  s = ss;
248
          }
249
          if (count & 0x01){
250
                  char *cs = s;
251
                  *cs++ = c;
252
                  s = cs;
253
          }
254
          return xs;
255
  }
256
 
257
  if ((long) s & 1)
258
    {
259
      char *cs = s;
260
      *cs++ = c;
261
      s = cs;
262
      count--;
263
    }
264
  if (count > 2 && (long) s & 2)
265
    {
266
      short *ss = s;
267
      *ss++ = c;
268
      s = ss;
269
      count -= 2;
270
    }
271
  temp = count >> 2;
272
  if (temp)
273
    {
274
      long *ls = s;
275
      temp--;
276
      do
277
        *ls++ = c;
278
      while (temp--);
279
      s = ls;
280
    }
281
  if (count & 2)
282
    {
283
      short *ss = s;
284
      *ss++ = c;
285
      s = ss;
286
    }
287
  if (count & 1)
288
    {
289
      char *cs = s;
290
      *cs = c;
291
    }
292
  return xs;
293
}
294
 
295
/*
296
 * __memset_page assumes that data is longword aligned. Most, if not
297
 * all, of these page sized memsets are performed on page aligned
298
 * areas, thus we do not need to check if the destination is longword
299
 * aligned. Of course we suffer a serious performance loss if this is
300
 * not the case but I think the risk of this ever happening is
301
 * extremely small. We spend a lot of time clearing pages in
302
 * get_empty_page() so I think it is worth it anyway. Besides, the
303
 * 680[46]0 do not really care about misaligned writes due to their
304
 * copy-back cache.
305
 *
306
 * The optimized case for the 680[46]0 is implemented using the move16
307
 * instruction. My tests showed that this implementation is 35-45%
308
 * faster than the original implementation using movel, the only
309
 * caveat is that the destination address must be 16-byte aligned.
310
 *                                            01/09/96 - Jes Sorensen
311
 */
312
extern inline void * __memset_page(void * s,int c,size_t count)
313
{
314
  unsigned long data, tmp;
315
  void *xs, *sp;
316
 
317
  xs = sp = s;
318
 
319
  c = c & 255;
320
  data = c | (c << 8);
321
  data |= data << 16;
322
 
323
#if defined(CONFIG_OPTIMIZE_040) || defined(CONFIG_OPTIMIZE_060)
324
 
325
  if (((unsigned long) s) & 0x0f)
326
          memset(s, c, count);
327
  else{
328
          *((unsigned long *)(s))++ = data;
329
          *((unsigned long *)(s))++ = data;
330
          *((unsigned long *)(s))++ = data;
331
          *((unsigned long *)(s))++ = data;
332
 
333
          __asm__ __volatile__("1:\t"
334
                               "move16 %2@+,%0@+\n\t"
335
                               "subqw  #8,%2\n\t"
336
                               "subqw  #8,%2\n\t"
337
                               "dbra   %1,1b\n\t"
338
                               : "=a" (s), "=d" (tmp)
339
                               : "a" (sp), "0" (s), "1" ((count - 16) / 16 - 1)
340
                               );
341
  }
342
 
343
#else
344
  __asm__ __volatile__("1:\t"
345
                       "movel %2,%0@+\n\t"
346
                       "movel %2,%0@+\n\t"
347
                       "movel %2,%0@+\n\t"
348
                       "movel %2,%0@+\n\t"
349
                       "movel %2,%0@+\n\t"
350
                       "movel %2,%0@+\n\t"
351
                       "movel %2,%0@+\n\t"
352
                       "movel %2,%0@+\n\t"
353
                       "dbra  %1,1b\n\t"
354
                       : "=a" (s), "=d" (tmp)
355
                       : "d" (data), "0" (s), "1" (count / 32 - 1)
356
                       );
357
#endif
358
 
359
  return xs;
360
}
361
 
362
#define __memset_const(s,c,count) \
363
((count==PAGE_SIZE) ? \
364
  __memset_page((s),(c),(count)) : \
365
  __memset_g((s),(c),(count)))
366
 
367
#define memset(s, c, count) \
368
(__builtin_constant_p(count) ? \
369
 __memset_const((s),(c),(count)) : \
370
 memset((s),(c),(count)))
371
 
372
#define __HAVE_ARCH_MEMCPY
373
/*
374
 * __builtin_memcpy() does not handle page-sized memcpys very well,
375
 * thus following the same assumptions as for page-sized memsets, this
376
 * function copies page-sized areas using an unrolled loop, without
377
 * considering alignment.
378
 *
379
 * For the 680[46]0 only kernels we use the move16 instruction instead
380
 * as it writes through the data-cache, invalidating the cache-lines
381
 * touched. In this way we do not use up the entire data-cache (well,
382
 * half of it on the 68060) by copying a page. An unrolled loop of two
383
 * move16 instructions seem to the fastest. The only caveat is that
384
 * both source and destination must be 16-byte aligned, if not we fall
385
 * back to the generic memcpy function.  - Jes
386
 */
387
extern inline void * __memcpy_page(void * to, const void * from, size_t count)
388
{
389
  unsigned long tmp;
390
  void *xto = to;
391
 
392
#if defined(CONFIG_OPTIMIZE_040) || defined(CONFIG_OPTIMIZE_060)
393
 
394
  if (((unsigned long) to | (unsigned long) from) & 0x0f)
395
          return memcpy(to, from, count);
396
 
397
  __asm__ __volatile__("1:\t"
398
                       "move16 %1@+,%0@+\n\t"
399
                       "move16 %1@+,%0@+\n\t"
400
                       "dbra  %2,1b\n\t"
401
                       : "=a" (to), "=a" (from), "=d" (tmp)
402
                       : "0" (to), "1" (from) , "2" (count / 32 - 1)
403
                       );
404
#else
405
  __asm__ __volatile__("1:\t"
406
                       "movel %1@+,%0@+\n\t"
407
                       "movel %1@+,%0@+\n\t"
408
                       "movel %1@+,%0@+\n\t"
409
                       "movel %1@+,%0@+\n\t"
410
                       "movel %1@+,%0@+\n\t"
411
                       "movel %1@+,%0@+\n\t"
412
                       "movel %1@+,%0@+\n\t"
413
                       "movel %1@+,%0@+\n\t"
414
                       "dbra  %2,1b\n\t"
415
                       : "=a" (to), "=a" (from), "=d" (tmp)
416
                       : "0" (to), "1" (from) , "2" (count / 32 - 1)
417
                       );
418
#endif
419
  return xto;
420
}
421
 
422
#define __memcpy_const(to, from, n) \
423
((n==PAGE_SIZE) ? \
424
  __memcpy_page((to),(from),(n)) : \
425
  __builtin_memcpy((to),(from),(n)))
426
 
427
#define memcpy(to, from, n) \
428
(__builtin_constant_p(n) ? \
429
 __memcpy_const((to),(from),(n)) : \
430
 memcpy((to),(from),(n)))
431
 
432
#define __HAVE_ARCH_MEMMOVE
433
extern inline void * memmove(void * dest,const void * src, size_t n)
434
{
435
  void *xdest = dest;
436
  size_t temp;
437
 
438
  if (!n)
439
    return xdest;
440
 
441
  if (dest < src)
442
    {
443
      if ((long) dest & 1)
444
        {
445
          char *cdest = dest;
446
          const char *csrc = src;
447
          *cdest++ = *csrc++;
448
          dest = cdest;
449
          src = csrc;
450
          n--;
451
        }
452
      if (n > 2 && (long) dest & 2)
453
        {
454
          short *sdest = dest;
455
          const short *ssrc = src;
456
          *sdest++ = *ssrc++;
457
          dest = sdest;
458
          src = ssrc;
459
          n -= 2;
460
        }
461
      temp = n >> 2;
462
      if (temp)
463
        {
464
          long *ldest = dest;
465
          const long *lsrc = src;
466
          temp--;
467
          do
468
            *ldest++ = *lsrc++;
469
          while (temp--);
470
          dest = ldest;
471
          src = lsrc;
472
        }
473
      if (n & 2)
474
        {
475
          short *sdest = dest;
476
          const short *ssrc = src;
477
          *sdest++ = *ssrc++;
478
          dest = sdest;
479
          src = ssrc;
480
        }
481
      if (n & 1)
482
        {
483
          char *cdest = dest;
484
          const char *csrc = src;
485
          *cdest = *csrc;
486
        }
487
    }
488
  else
489
    {
490
      dest = (char *) dest + n;
491
      src = (const char *) src + n;
492
      if ((long) dest & 1)
493
        {
494
          char *cdest = dest;
495
          const char *csrc = src;
496
          *--cdest = *--csrc;
497
          dest = cdest;
498
          src = csrc;
499
          n--;
500
        }
501
      if (n > 2 && (long) dest & 2)
502
        {
503
          short *sdest = dest;
504
          const short *ssrc = src;
505
          *--sdest = *--ssrc;
506
          dest = sdest;
507
          src = ssrc;
508
          n -= 2;
509
        }
510
      temp = n >> 2;
511
      if (temp)
512
        {
513
          long *ldest = dest;
514
          const long *lsrc = src;
515
          temp--;
516
          do
517
            *--ldest = *--lsrc;
518
          while (temp--);
519
          dest = ldest;
520
          src = lsrc;
521
        }
522
      if (n & 2)
523
        {
524
          short *sdest = dest;
525
          const short *ssrc = src;
526
          *--sdest = *--ssrc;
527
          dest = sdest;
528
          src = ssrc;
529
        }
530
      if (n & 1)
531
        {
532
          char *cdest = dest;
533
          const char *csrc = src;
534
          *--cdest = *--csrc;
535
        }
536
    }
537
  return xdest;
538
}
539
 
540
#define __HAVE_ARCH_MEMCMP
541
#define memcmp(cs, ct, n) \
542
(__builtin_constant_p(n) ? \
543
 __builtin_memcmp((cs),(ct),(n)) : \
544
 memcmp((cs),(ct),(n)))
545
 
546
#endif
547
 
548
#endif /* _I960_STRING_H_ */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.