From a1a62865004a7956314ee265153588974e31c618 Mon Sep 17 00:00:00 2001 From: arvinzzz Date: Fri, 8 Apr 2022 15:28:17 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E9=AB=98=E9=A2=91=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 精简冗余语句 Signed-off-by: arvinzzz Change-Id: If7716d6ce6b751b3525c41fe3c95ff608ef64136 --- lib/libc/src/memcmp.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/lib/libc/src/memcmp.c b/lib/libc/src/memcmp.c index fce4dab7..1583eff8 100644 --- a/lib/libc/src/memcmp.c +++ b/lib/libc/src/memcmp.c @@ -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, * are permitted provided that the following conditions are met: @@ -31,6 +31,9 @@ #include #include +#define SIZE_U64 (sizeof(uint64_t)) +#define SIZE_U32 (sizeof(uint32_t)) + 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; 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)) { - goto L8_byte_diff; + goto L4_byte_cmp; } - s1 += 8; /* 8, compare size, the number of chars of one uint64_t data */ - s2 += 8; /* 8, compare size, the number of chars of one uint64_t data */ - num -= 8; /* 8, compare size, the number of chars of one uint64_t data */ + s1 += SIZE_U64; + s2 += SIZE_U64; + num -= SIZE_U64; } if (num == 0) { return 0; } - /* L4_byte_cmp */ - if (num >= 4) { /* 4, compare size, the number of chars of one uint32_t data */ +L4_byte_cmp: + if (num >= SIZE_U32) { 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 */ + s1 += SIZE_U32; + s2 += SIZE_U32; + num -= SIZE_U32; } if (num == 0) { return 0; @@ -66,13 +69,4 @@ L4_byte_diff: for (; num && (*s1 == *s2); num--, s1++, s2++) { } 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; } \ No newline at end of file