Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add memset #8

Merged
merged 2 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}