1 // SPDX-License-Identifier: BSD-3-Clause
2 //
3 // Copyright(c) 2016 Intel Corporation. All rights reserved.
4 //
5 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
6 
7 #include <sof/string.h>
8 #include <sof/compiler_info.h>
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 /* Not needed for host or Zephyr or CC uses LIBC */
14 #if !CONFIG_LIBRARY && !__ZEPHYR__ && !defined(CC_USE_LIBC)
15 
16 /* used by gcc - but uses arch_memcpy internally */
memcpy(void * dest,const void * src,size_t n)17 void *memcpy(void *dest, const void *src, size_t n)
18 {
19 	arch_memcpy(dest, src, n);
20 	return dest;
21 }
22 
23 /* generic memset */
memset(void * s,int c,size_t n)24 void *memset(void *s, int c, size_t n)
25 {
26 	uint8_t *d8 = s;
27 	uint8_t v = c;
28 	int i;
29 
30 	for (i = 0; i <	n; i++)
31 		d8[i] = v;
32 
33 	return s;
34 }
35 
memcmp(const void * p,const void * q,size_t count)36 int memcmp(const void *p, const void *q, size_t count)
37 {
38 	uint8_t *s1 = (uint8_t *)p;
39 	uint8_t *s2 = (uint8_t *)q;
40 
41 	while (count) {
42 		if (*s1 != *s2)
43 			return *s1 < *s2 ? -1 : 1;
44 		s1++;
45 		s2++;
46 		count--;
47 	}
48 	return 0;
49 }
50 
51 #endif
52 
memcpy_s(void * dest,size_t dest_size,const void * src,size_t src_size)53 int memcpy_s(void *dest, size_t dest_size,
54 	     const void *src, size_t src_size)
55 {
56 	return arch_memcpy_s(dest, dest_size, src, src_size);
57 }
58 
memset_s(void * dest,size_t dest_size,int data,size_t count)59 int memset_s(void *dest, size_t dest_size, int data, size_t count)
60 {
61 	return arch_memset_s(dest, dest_size, data, count);
62 }
63 
64 #if !__XCC || !XCHAL_HAVE_HIFI3 || !CONFIG_LIBRARY
__vec_memcpy(void * dst,const void * src,size_t len)65 void *__vec_memcpy(void *dst, const void *src, size_t len)
66 {
67 	return memcpy(dst, src, len);
68 }
69 
__vec_memset(void * dest,int data,size_t src_size)70 void *__vec_memset(void *dest, int data, size_t src_size)
71 {
72 	return memset(dest, data, src_size);
73 }
74 #endif
75 
76 /* generic strlen - TODO: can be optimsed for ARCH ? */
rstrlen(const char * s)77 int rstrlen(const char *s)
78 {
79 	const char *p = s;
80 
81 	while (*p++ != 0)
82 		;
83 	return (p - s) - 1;
84 }
85 
86 /* generic string compare */
rstrcmp(const char * s1,const char * s2)87 int rstrcmp(const char *s1, const char *s2)
88 {
89 	while (*s1 != 0 && *s2 != 0) {
90 		if (*s1 < *s2)
91 			return -1;
92 		if (*s1 > *s2)
93 			return 1;
94 		s1++;
95 		s2++;
96 	}
97 
98 	/* did both string end */
99 	if (*s1 != 0)
100 		return 1;
101 	if (*s2 != 0)
102 		return -1;
103 
104 	/* match */
105 	return 0;
106 }
107