OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [tools/] [riscv-gnu-toolchain-master/] [newlib/] [newlib/] [libc/] [machine/] [riscv/] [strlen.c] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 serginhofr
#include <string.h>
2
#include <stdint.h>
3
 
4
size_t strlen(const char* str)
5
{
6
  const char* start = str;
7
 
8
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
9
  while (*str++)
10
    ;
11
  return str - start - 1;
12
#else
13
  if (__builtin_expect((uintptr_t)str & (sizeof(long)-1), 0)) do
14
  {
15
    char ch = *str;
16
    str++;
17
    if (!ch)
18
      return str - start - 1;
19
  } while ((uintptr_t)str & (sizeof(long)-1));
20
 
21
  unsigned long* ls = (unsigned long*)str;
22
  while (!__libc_detect_null(*ls++))
23
    ;
24
  asm volatile ("" : "+r"(ls)); /* prevent "optimization" */
25
 
26
  str = (const char*)ls;
27
  size_t ret = str - start, sl = sizeof(long);
28
 
29
  char c0 = str[0-sl], c1 = str[1-sl], c2 = str[2-sl], c3 = str[3-sl];
30
  if (c0 == 0)            return ret + 0 - sl;
31
  if (c1 == 0)            return ret + 1 - sl;
32
  if (c2 == 0)            return ret + 2 - sl;
33
  if (sl == 4 || c3 == 0) return ret + 3 - sl;
34
 
35
  c0 = str[4-sl], c1 = str[5-sl], c2 = str[6-sl], c3 = str[7-sl];
36
  if (c0 == 0)            return ret + 4 - sl;
37
  if (c1 == 0)            return ret + 5 - sl;
38
  if (c2 == 0)            return ret + 6 - sl;
39
                          return ret + 7 - sl;
40
#endif /* not PREFER_SIZE_OVER_SPEED */
41
}

powered by: WebSVN 2.1.0

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