!869 chore:精简冗余语句

Merge pull request !869 from Zhaotianyu/0326libc_opt
This commit is contained in:
openharmony_ci 2022-04-11 02:53:21 +00:00 committed by Gitee
commit b90531e366
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 14 additions and 20 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. * Copyright (c) 2021-2022 Huawei Device Co., Ltd. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -31,6 +31,9 @@
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#define SIZE_U64 (sizeof(uint64_t))
#define SIZE_U32 (sizeof(uint32_t))
int memcmp(const void *str1, const void *str2, size_t n) int memcmp(const void *str1, const void *str2, size_t n)
{ {
@ -38,26 +41,26 @@ int memcmp(const void *str1, const void *str2, size_t n)
const unsigned char *s2 = str2; const unsigned char *s2 = str2;
size_t num = n; size_t num = n;
while (num >= 8) { /* 8, compare size, the number of chars of one uint64_t data */ while (num >= SIZE_U64) {
if (*(const uint64_t *)(s1) != *(const uint64_t *)(s2)) { if (*(const uint64_t *)(s1) != *(const uint64_t *)(s2)) {
goto L8_byte_diff; goto L4_byte_cmp;
} }
s1 += 8; /* 8, compare size, the number of chars of one uint64_t data */ s1 += SIZE_U64;
s2 += 8; /* 8, compare size, the number of chars of one uint64_t data */ s2 += SIZE_U64;
num -= 8; /* 8, compare size, the number of chars of one uint64_t data */ num -= SIZE_U64;
} }
if (num == 0) { if (num == 0) {
return 0; return 0;
} }
/* L4_byte_cmp */ L4_byte_cmp:
if (num >= 4) { /* 4, compare size, the number of chars of one uint32_t data */ if (num >= SIZE_U32) {
if (*(const uint32_t *)(s1) != *(const uint32_t *)(s2)) { if (*(const uint32_t *)(s1) != *(const uint32_t *)(s2)) {
goto L4_byte_diff; goto L4_byte_diff;
} }
s1 += 4; /* 4, compare size, the number of chars of one uint32_t data */ s1 += SIZE_U32;
s2 += 4; /* 4, compare size, the number of chars of one uint32_t data */ s2 += SIZE_U32;
num -= 4; /* 4, compare size, the number of chars of one uint32_t data */ num -= SIZE_U32;
} }
if (num == 0) { if (num == 0) {
return 0; return 0;
@ -66,13 +69,4 @@ L4_byte_diff:
for (; num && (*s1 == *s2); num--, s1++, s2++) { for (; num && (*s1 == *s2); num--, s1++, s2++) {
} }
return num ? *s1 - *s2 : 0; return num ? *s1 - *s2 : 0;
L8_byte_diff:
if (*(const uint32_t *)(s1) != *(const uint32_t *)(s2)) {
goto L4_byte_diff;
}
s1 += 4; /* 4, compare size, the number of chars of one uint32_t data */
s2 += 4; /* 4, compare size, the number of chars of one uint32_t data */
num -= 4; /* 4, compare size, the number of chars of one uint32_t data */
goto L4_byte_diff;
} }