Skip to content

Commit

Permalink
Merge pull request #8 from linshire/add_memset
Browse files Browse the repository at this point in the history
add memset
  • Loading branch information
BernardXiong authored Mar 11, 2023
2 parents d136362 + 451d5fe commit af963b9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
3 changes: 3 additions & 0 deletions arch/riscv64/typedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define __TYPEDEF_H__

#define NULL 0
#define opt_size 8

typedef unsigned long long size_t;
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
Expand All @@ -28,4 +30,5 @@ typedef long _fpos_t;
typedef int64_t intmax_t;
typedef uint64_t uintmax_t;


#endif
1 change: 1 addition & 0 deletions include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __STDIO_H__

#include <typedef.h>
#include <stdarg.h>

#define EOF (-1)

Expand Down
8 changes: 1 addition & 7 deletions include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
#include <typedef.h>

size_t strlen(const char *s);







void *memset (void *dstpp, int c, size_t len);

#endif
2 changes: 1 addition & 1 deletion src/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int vfprintf(FILE *stream, const char *format, va_list arg)
switch (*ptr_string)
{
case ' ':
putchar(*ptr_string);
putchar((char)*ptr_string);
ret_num++;
break;
case '\t':
Expand Down
74 changes: 73 additions & 1 deletion src/string.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,84 @@
/*
* Copyright (c) 2023
*
* SPDX-License-Identifier: MIT
*
* Change Logs:
* Date Author Notes
* 2023/2/1 linshire the first version
* 2023/3/11 linshire add memset
*/

#include <string.h>
#include <typedef.h>


size_t strlen(const char *s)
{
const char *a = s;
for (; *s; s++);
return s-a;
}

void *memset (void *dstpp, int c, size_t len)
{
long int dstp = (long int) dstpp;

if (len >= 8)
{
size_t xlen;
size_t cccc;

cccc = (unsigned char) c;
cccc |= cccc << 8;
cccc |= cccc << 16;
if (opt_size > 4)
/* Do the shift in two steps to avoid warning if long has 32 bits. */
cccc |= (cccc << 16) << 16;

/* There are at least some bytes to set.
No need to test for LEN == 0 in this alignment loop. */
while (dstp % opt_size != 0)
{
((uint8_t *) dstp)[0] = c;
dstp += 1;
len -= 1;
}

/* Write 8 `size_t' per iteration until less than 8 `size_t' remain. */
xlen = len / (opt_size * 8);
while (xlen > 0)
{
((size_t *) dstp)[0] = cccc;
((size_t *) dstp)[1] = cccc;
((size_t *) dstp)[2] = cccc;
((size_t *) dstp)[3] = cccc;
((size_t *) dstp)[4] = cccc;
((size_t *) dstp)[5] = cccc;
((size_t *) dstp)[6] = cccc;
((size_t *) dstp)[7] = cccc;
dstp += 8 * opt_size;
xlen -= 1;
}
len %= opt_size * 8;

/* Write 1 `size_t' per iteration until less than opt_size bytes remain. */
xlen = len / opt_size;
while (xlen > 0)
{
((size_t *) dstp)[0] = cccc;
dstp += opt_size;
xlen -= 1;
}
len %= opt_size;
}

/* Write the last few bytes. */
while (len > 0)
{
((uint8_t *) dstp)[0] = c;
dstp += 1;
len -= 1;
}

return dstpp;
}

0 comments on commit af963b9

Please sign in to comment.