From patchwork Sat Nov 1 13:55:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Dubiec X-Patchwork-Id: 123279 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 22D153858C51 for ; Sat, 1 Nov 2025 13:56:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 22D153858C51 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=o2.pl header.i=@o2.pl header.a=rsa-sha256 header.s=20241105 header.b=1X/shvn8 X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from mx-out.tlen.pl (mx-out.tlen.pl [193.222.135.175]) by sourceware.org (Postfix) with ESMTPS id 53E3D384F015 for ; Sat, 1 Nov 2025 13:55:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53E3D384F015 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=o2.pl Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=o2.pl ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 53E3D384F015 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=193.222.135.175 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1762005351; cv=none; b=YCrEi690WEMc7k/yKH+mYm14AZFZC9d2ezFcJ2TRnXCWwAdvs4M6swX36wd8lLp4uD0AIqFdDppELHqSLahnO2G2EYl9kxMap7QUUPGfbxNMgzXFyVmaxv6nJdka6QpHZaEBUQ6v10h3E0mhktw9UVzSnjg9WX3hRBH7wk7APlY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1762005351; c=relaxed/simple; bh=bGZ2omgewd4sAJdyAiyLDCSDO96sNwstGQ9vrt+8drA=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=EVUAw2hh4vDApbrBHHzh0RkB/PMAoVnP5Zgdl0l9J0v48UFiZMb0RcvoW/alZ0xTw9RKdcOVdx1UGbtpCjKZH+G0OoyN7cbV3JK8TqZnKt2i1lON4Lu25GcXidt7eDpRkmZg1ki3lnARP4VzS+BZbdJZiqIJL0DGeSER2YBOzYU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 53E3D384F015 Received: (wp-smtpd smtp.tlen.pl 40505 invoked from network); 1 Nov 2025 14:55:49 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=o2.pl; s=20241105; t=1762005349; bh=36xmLKSqT1oKRaCW+dfxDZyG5hQ6XaKHkFlJS9VMzOE=; h=To:From:Subject; b=1X/shvn84GaXUZXC3ULK6LbtcdL/1xhWORtagkpG9ieThile5ZQcLFtsHOhSQ+deV pi3hlrUYuH3QvZRQTdho7jeEPJhSOLYvt0N+ijxU33lBpqyHhmjwW/IySCuvcDWQsm hlwaVBjbkazNrhjddurJBdtk+gy7KmVHw1AtXvFNq9OZ9GISfkgSFaJzzg+jExIkiJ hiujy3bh9yaJrzBxmARDLRcmcFzvtGP0EojOf3NqSlWJy2MNSrOp3gfcTgeOho+/2l 4XwiQDaXZGC81/+eQpGzi+rC1gz5Bz8VwVOjHKFGHVRVHIozIrqCefxnPjF+0630c0 JHaIdvisVW0TA== Received: from ip-94-42-228-142.multimo.pl (HELO [192.168.0.1]) (jdx@o2.pl@[94.42.228.142]) (envelope-sender ) by smtp.tlen.pl (WP-SMTPD) with ECDHE-RSA-AES256-GCM-SHA384 encrypted SMTP for ; 1 Nov 2025 14:55:49 +0100 Message-ID: Date: Sat, 1 Nov 2025 14:55:31 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: newlib@sourceware.org Content-Language: pl, en-US From: Jan Dubiec Subject: [PATCH] H8/300: Fix strcmp() behaviour in some rare corner cases. X-WP-MailID: 01846091b0580305e22e26da6be5a2b3 X-WP-AV: skaner antywirusowy Poczty o2 X-WP-SPAM: NO 0000000 [4TOx] X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org H8 port uses "optimized" version of strcmp() written in assembly language. Unfortunately, the function fails in a few corner cases mentioned at the end of newlib/testsuite/newlib.string/strcmp-1.c, i.e. when the result of comparison exceeds the range of 8 bit signed integer (aka "char"). The existing code first compares two 8 bit character codes and then extends the result to 16 integer (or 32 bit in case of -mint32). If the result of comparison is <= -128 or >= 127 the extension gives wrong result, i.e. not what a human would expect. This patch fixes the problem – first it extends 8 bit character codes to 16 bit integers and then performs comparison. 2025-11-01 Jan Dubiec newlib/ChangeLog: * libc/machine/h8300/strcmp.S (_strcmp): First extend 8 bit character codes to 16 bit integers and then compare them. newlib/libc/machine/h8300/strcmp.S | 39 ++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/newlib/libc/machine/h8300/strcmp.S b/newlib/libc/machine/h8300/strcmp.S index c5d709405..c16bc1cfd 100644 --- a/newlib/libc/machine/h8300/strcmp.S +++ b/newlib/libc/machine/h8300/strcmp.S @@ -3,20 +3,31 @@ #include "defines.h" #if defined (__H8300SX__) + .section .text .global _strcmp _strcmp: mov.l er0,er2 loop: mov.b @er2+,r0l + extu.w r0 beq eos - sub.b @er1+,r0l + mov.b @er1+,r3l + extu.w r3 + sub.w r3,r0 beq loop - exts.l #2,er0 +#if (__INT_MAX__ > 32767) + exts.l er0 +#endif rts eos: - sub.b @er1,r0l - exts.l #2,er0 + mov.b @er1,r3l + extu.w r3 + sub.w r3,r0 +#if (__INT_MAX__ > 32767) + exts.l er0 +#endif rts + .end #else .section .text .align 2 @@ -38,22 +49,14 @@ _strcmp: .L3: mov.b @(-1,A2P),A0L mov.b @A3P,A1L - sub.b A1L,A0L + sub.b A0H,A0H ; H8/300 does not have EXTU.W instruction + sub.b A1H,A1H ; so we just zero higher byte of the word + sub.w A1,A0 ; We have to sign extend the result to 32bits just in case - ; we are using 32bit integers. -#ifdef __H8300H__ - exts.w r0 - exts.l er0 -#else -#ifdef __H8300S__ - exts.w r0 + ; we are using 32bit integers. H8/300 does not support 32bit + ; integers (-mint32), so we can just use EXTS.L here. +#if (__INT_MAX__ > 32767) exts.l er0 -#else - bld #7,r0l - subx r0h,r0h - subx r1l,r1l - subx r1h,r1h -#endif #endif rts .end