util.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /**
  3. ******************************************************************************
  4. * @file util.c
  5. * @author StarFive Technology
  6. * @version V1.0
  7. * @date 07/24/2020
  8. * @brief
  9. ******************************************************************************
  10. * @copy
  11. *
  12. * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STARFIVE SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * COPYRIGHT 2020 Shanghai StarFive Technology Co., Ltd.
  20. */
  21. #include "ctype.h"
  22. #include "util.h"
  23. #include "comdef.h"
  24. #include "encoding.h"
  25. /*===========================================================================
  26. FUNCTION: sys_memcpy
  27. DESCRIPTION:
  28. copy one block memory to another position
  29. INPUT PARAMETER:
  30. void *p_des: destination address
  31. const void * p_src: source address
  32. unsigned long size: length of memory to copy (unit is byte)
  33. OUTPUT PARAMETER:
  34. RETURN VALUE:
  35. ===========================================================================*/
  36. void * sys_memcpy(void *p_des,const void * p_src,unsigned long size)
  37. {
  38. char *tmp = p_des;
  39. const char *s = p_src;
  40. while (size--)
  41. *tmp++ = *s++;
  42. return p_des;
  43. }
  44. int sys_memcmp(const void * cs,const void * ct,unsigned int count)
  45. {
  46. const unsigned char *su1, *su2;
  47. int res = 0;
  48. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  49. if ((res = *su1 - *su2) != 0)
  50. break;
  51. return res;
  52. }
  53. void * _memcpy(void * dest,const void *src,unsigned int count)
  54. {
  55. char *tmp = (char *) dest;
  56. const char *s = (char *) src;
  57. while (count--)
  58. *tmp++ = *s++;
  59. return dest;
  60. }
  61. /*===========================================================================
  62. FUNCTION: sys_memcpy_32
  63. DESCRIPTION:
  64. copy one block memory to another position
  65. caller guarantee the src/des address are DWORD allign
  66. INPUT PARAMETER:
  67. void *p_des: destination address
  68. const void * p_src: source address
  69. unsigned long size: length of memory to copy (unit is DWORD)
  70. OUTPUT PARAMETER:
  71. RETURN VALUE:
  72. ===========================================================================*/
  73. void sys_memcpy_32(void *p_des,const void * p_src,unsigned long size)
  74. {
  75. unsigned long i;
  76. for (i=0;i<size;i++)
  77. *((unsigned long*)p_des+i) = *((unsigned long*)p_src+i);
  78. }
  79. /*===========================================================================
  80. FUNCTION: sys_memset
  81. DESCRIPTION:
  82. fill memory with specifed value
  83. INPUT PARAMETER:
  84. void *p_des: destination address
  85. int c: value to set
  86. unsigned long size: length of memory
  87. OUTPUT PARAMETER:
  88. RETURN VALUE:
  89. ===========================================================================*/
  90. void sys_memset(void *p_des,unsigned char c,unsigned long size)
  91. {
  92. unsigned long i;
  93. for (i=0;i<size;i++)
  94. *((char*)p_des+i) = c;
  95. }
  96. /*===========================================================================
  97. FUNCTION: sys_memset32
  98. DESCRIPTION:
  99. fill memory with specifed value
  100. INPUT PARAMETER:
  101. void *p_des: destination address
  102. int c: value to set
  103. unsigned long size: length of memory in word(32bit)
  104. OUTPUT PARAMETER:
  105. RETURN VALUE:
  106. ===========================================================================*/
  107. void sys_memset32(void *p_des,int c,unsigned long size)
  108. {
  109. unsigned long i;
  110. for(i=0; i< size; i++)
  111. ((unsigned long*)p_des)[i] = c;
  112. }
  113. int atoi(const char *nptr)
  114. {
  115. int c; /* current char */
  116. int total; /* current total */
  117. int sign; /* if '-', then negative, otherwise positive */
  118. /* skip whitespace */
  119. while ( isspace((int)(unsigned char)*nptr) )
  120. ++nptr;
  121. c = (int)(unsigned char)*nptr++;
  122. sign = c; /* save sign indication */
  123. if (c == '-' || c == '+')
  124. c = (int)(unsigned char)*nptr++; /* skip sign */
  125. total = 0;
  126. while (isdigit(c)) {
  127. total = 10 * total + (c - '0'); /* accumulate digit */
  128. c = (int)(unsigned char)*nptr++; /* get next char */
  129. }
  130. if (sign == '-')
  131. return -total;
  132. else
  133. return total; /* return result, negated if necessary */
  134. }