From patchwork Sat Apr 6 05:37:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 786477 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1101:b0:343:f27d:c44e with SMTP id z1csp489397wrw; Fri, 5 Apr 2024 22:39:14 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCV0xqQJIQ9+biEwtmJnK1avsrpIY80SUOZ2PPBRWXEGAsdADAz2kgAzGg24tzk049GJefnMspjhK++9e3T277f4 X-Google-Smtp-Source: AGHT+IEXA2aT5wdk8u0oJ+8LdXcJeMPb66RkEyEMfeTcZivAB3pMHSSXjerjR33Sz56iO6BWPj2p X-Received: by 2002:a05:6214:d87:b0:699:2f69:ac87 with SMTP id e7-20020a0562140d8700b006992f69ac87mr3592376qve.45.1712381953838; Fri, 05 Apr 2024 22:39:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712381953; cv=none; d=google.com; s=arc-20160816; b=GHYDetss8n/qXgckjWnv+GpFeLj93vu9kNsRSUcqO9fCLVaPTJwYXWhUq2fpbjfExz eVg/Y69MPiXTZJ6pLdXN/WAn6LJJjseYCabaoji4Y4m6BAn1wH7gF1WtbvhIP07/ybmV O4pEipWyFX6LcwO1j9s4VoVJgvW0R89h2+gObjQahdmQ8yVN+l+7F4CLf5YCEwc6W5Bi QZp3EnXyqAZgdtZNztib9zQIgzsnYQKKZYRJMpTqDTRnmToY7ag1T3tlt8s3e/MGikdX Z+UkM9ZGqqpbqie9J+4BLP29aLSBaupmzq1uzOLkR2Vp5D5pmaUBW9rs9157hjLdSXUn cbDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=RcrZGxEnfZgrLqlhciK4hABUGUNHIg16hIoNjeHOda0=; fh=84/uo61yBhOat7tWTwgBWiREzrxU2uSS3JBQDi1oXBY=; b=foE045vLPu7/hvpae4DADT9Vhv0AcRU2/kn/uwfN+bF72eOohBFyX98/dKFDSvWq7v +yWOHtIEg+36S3RxHmF7fQ/Q07IOtSKT9A1QS3UMjdt6p1/G3KEu5pcFhZFnUnhhzlzC b2WjJAs4H9K2eqjB1pdxo/xT9Q1uSv4NP3EUaKdQOgiOlQE/YDszautGd4Xm7o1pXNNz qV7z8X1wngh464uEG+TdEDcjKVHZbqUaorn4OGPR2bQyUJWfQ1MlcW5C9mt9Dt65G16C vsZ7sz4zKus25FKJ/ntDMEa9W6iN9e9sW2YkTyo1iuvmt0+DplsYKfDKcWLAxKc6WTwa hHeA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=U3EGEHfv; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id df17-20020a056214081100b006992047c104si3453844qvb.122.2024.04.05.22.39.13 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 05 Apr 2024 22:39:13 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=U3EGEHfv; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rsykI-0002q6-OA; Sat, 06 Apr 2024 01:37:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rsykG-0002pY-Rz for qemu-devel@nongnu.org; Sat, 06 Apr 2024 01:37:40 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rsykF-0006I4-A5 for qemu-devel@nongnu.org; Sat, 06 Apr 2024 01:37:40 -0400 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-6ecd957f949so2683446b3a.0 for ; Fri, 05 Apr 2024 22:37:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712381857; x=1712986657; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RcrZGxEnfZgrLqlhciK4hABUGUNHIg16hIoNjeHOda0=; b=U3EGEHfvNDT7eFspbsn90ONUSk29KRfcTAPYh5jfE/v1k6mgFSNqiKoCR2AsOOeZid 5LjaBlQsPOrgfFUfAEzJcIVa+g8OKPJyE0yWH1Nf30WUBR3oIOvRhIMz+gnrMrr186Gn uBKXQxLKnPn57rQYu9y71v7TsNc4KPtnv9GFZ4K+B/m08W6eJhKdodGu7G/kdFZV1pqV PEXWuAXzh2L7ZynaJmgSTzQsiAlnDyf4hr82QC4KrdEioTUUuSrp763BryTK373VMlmM 9sLQ7tYKqALWmtBtGjAgQp0XoDv9dGod3XaLHFyZf4UDOhEwBiU91pItA2yqSzMsLC0M acog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712381857; x=1712986657; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RcrZGxEnfZgrLqlhciK4hABUGUNHIg16hIoNjeHOda0=; b=GlYg46P1zjYAUx4QxjsX/dtSIaSkXduPqgd8UV1t1YG9cOT/Mfjzxy83aNSeoRlPP0 vg/T+9pEWTc6j6HYiEKZiR17jbCyFsYnQ/JdrL/Uuv7DJ4gTrNvlUPibiOTtO7xquLsd Z9M97HaZdkEa17xrdQjmsPnXplMgZIYWR1d4X179Cb2VpjaDH8vZ9EWXvNGxEb4FlBNG ql0B2WyHNOBgR8KISH3j0dZR38chft+edmbovYjRwkXkcULjDTsFunHd2knN/kckF+i6 5Mr6Bj0tfBy6kIRWq0UXSJLHicZ0ZLcm1ANgX2jn33dMouhHL8uy91xgwr4ZVL8CV3u6 fEtQ== X-Gm-Message-State: AOJu0YzClfgOmb8K8qoqtbTl/4CQg2wtu36CkKBnkmvTyOAm28h+kqxd /Jr5AtrD7k0X7pSINZA1pS+PxS9V+p5qedjeo1P8BGdGPtD7k1F3+WYJRr28SmjEKyfkHWKSzGJ M X-Received: by 2002:a05:6a20:6d94:b0:1a3:d60c:4889 with SMTP id gl20-20020a056a206d9400b001a3d60c4889mr2909796pzb.56.1712381857428; Fri, 05 Apr 2024 22:37:37 -0700 (PDT) Received: from stoup.. (098-147-007-212.res.spectrum.com. [98.147.7.212]) by smtp.gmail.com with ESMTPSA id u12-20020a170903124c00b001dc05535632sm2589378plh.170.2024.04.05.22.37.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 22:37:37 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: zack@buhman.org, peter.maydell@linaro.org, ysato@users.sourceforge.jp, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v3 1/4] target/sh4: mac.w: memory accesses are 16-bit words Date: Fri, 5 Apr 2024 19:37:29 -1000 Message-Id: <20240406053732.191398-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240406053732.191398-1-richard.henderson@linaro.org> References: <20240406053732.191398-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::431; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x431.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Zack Buhman Before this change, executing a code sequence such as: mova tblm,r0 mov r0,r1 mova tbln,r0 clrs clrmac mac.w @r0+,@r1+ mac.w @r0+,@r1+ .align 4 tblm: .word 0x1234 .word 0x5678 tbln: .word 0x9abc .word 0xdefg Does not result in correct behavior: Expected behavior: first macw : macl = 0x1234 * 0x9abc + 0x0 mach = 0x0 second macw: macl = 0x5678 * 0xdefg + 0xb00a630 mach = 0x0 Observed behavior (qemu-sh4eb, prior to this commit): first macw : macl = 0x5678 * 0xdefg + 0x0 mach = 0x0 second macw: (unaligned longword memory access, SIGBUS) Various SH-4 ISA manuals also confirm that `mac.w` is a 16-bit word memory access, not a 32-bit longword memory access. Signed-off-by: Zack Buhman Reviewed-by: Yoshinori Sato Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20240402093756.27466-1-zack@buhman.org> Signed-off-by: Richard Henderson --- target/sh4/translate.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/target/sh4/translate.c b/target/sh4/translate.c index a9b1bc7524..6643c14dde 100644 --- a/target/sh4/translate.c +++ b/target/sh4/translate.c @@ -816,10 +816,10 @@ static void _decode_opc(DisasContext * ctx) TCGv arg0, arg1; arg0 = tcg_temp_new(); tcg_gen_qemu_ld_i32(arg0, REG(B7_4), ctx->memidx, - MO_TESL | MO_ALIGN); + MO_TESW | MO_ALIGN); arg1 = tcg_temp_new(); tcg_gen_qemu_ld_i32(arg1, REG(B11_8), ctx->memidx, - MO_TESL | MO_ALIGN); + MO_TESW | MO_ALIGN); gen_helper_macw(tcg_env, arg0, arg1); tcg_gen_addi_i32(REG(B11_8), REG(B11_8), 2); tcg_gen_addi_i32(REG(B7_4), REG(B7_4), 2); From patchwork Sat Apr 6 05:37:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 786475 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1101:b0:343:f27d:c44e with SMTP id z1csp489330wrw; Fri, 5 Apr 2024 22:38:56 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXxD888+OO/tBi17B+g079eyDq2SsDcvHAqr/9PszKoBDAVWVYWY6BomrKx8KovxcTzspt3yE4TAi4NxSBGiv4n X-Google-Smtp-Source: AGHT+IH9LclbWoriAGFotss+OoV/gxZIPKGzhTlv697e5EKPxN6qi/CmVImIKV3VPTtiDYh8eVvQ X-Received: by 2002:a05:620a:1a94:b0:78d:54db:21bc with SMTP id bl20-20020a05620a1a9400b0078d54db21bcmr822769qkb.31.1712381936266; Fri, 05 Apr 2024 22:38:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712381936; cv=none; d=google.com; s=arc-20160816; b=miyoLgFT67SjGNO4Z77BKc28O7Xj7T20RMjcmuZ0wKjsI07qsAkrK5YfnwlnIfWZ44 F622Ga59F+DOouvBOt0nAHJOzmLP/WAUCUVMfFfoLa3I60BhYGMJdTog2XF1NJfqh44T W4SVo4ezm7OFonOXyM57zWPwES/HVsnM8HgzAye+YLlwWVPDY0u/vhhMC/Z6fDrOh6PZ Uy3EwDAqVKPbQ+nO+r1VXijQfXTwSiPsn7zbkCuJfL+hkcCfdg9W+SjEBuLV368cH6UI xtGiyCBW+EWlnkK5i9ggr0RP4qc1epAio/IcyNXCwhZSfBPpDpiPJWcKo69xWJpqSdgo b3sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=syERz6mhK860XklS44UTgUTqfyplnj5Xmmc1ns2mrO8=; fh=cuTpXidYakzLJEueohunWHlvXGsa+3AL7M3K5ZUnTnY=; b=K3+A62Cffk/mPROJAQW802iI4fF6q8XIj0SapHdLef6VNTPaJta2AAzxSC8q+Q3Hjg wUAH7osBAsJDPwWKZgnAqzOqN1izA6D9jjRz0bo5MNsVessXyalE90Ofi04NDsUFtHDZ NY86XIubfkmA/XT3TfCENMIplpzC/V/GWxOWCM05gP35F+LZ/Noegjkb1lXCfINsJxxG ip8Mz3IkalbRRZGOFivgauqAPIO2+dzWa8S6YK+MbVgm/YQ1Eqgb5PEDB5T+LfWKbQ6I rXZwMmPM0tH9v3KFJ54+4zX1C0IxOsMzEkZcrhFi51mX9AxlodnD8hi9bVZKOQpegnkj 2Rrg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="CPNo/F9z"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id x9-20020a05620a0b4900b0078d3a8286ebsi3286106qkg.151.2024.04.05.22.38.56 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 05 Apr 2024 22:38:56 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="CPNo/F9z"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rsykI-0002q9-TX; Sat, 06 Apr 2024 01:37:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rsykH-0002px-Lt for qemu-devel@nongnu.org; Sat, 06 Apr 2024 01:37:41 -0400 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rsykG-0006IG-5m for qemu-devel@nongnu.org; Sat, 06 Apr 2024 01:37:41 -0400 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1e2bfac15c2so18883405ad.0 for ; Fri, 05 Apr 2024 22:37:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712381859; x=1712986659; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=syERz6mhK860XklS44UTgUTqfyplnj5Xmmc1ns2mrO8=; b=CPNo/F9zhoyhVnkYBeq9VmdK/Rimnw7PVYt49jBun7UFEl+gPy+Y14RlBXnwFLEHA3 oXJXRmHbuWDnDZeFbyWEmIFJHiM9JRtqo99cod8SlqNIgrxkpitC/6BVej6EmSz3mIx8 BMVmsgqonKzuWhX36aY/1+v1Bdkc2eWHKuCupn8xoGA+vJjIZLr7eBgy92xxns3vE0C1 qSQlcPyxVFXzbzpTCLyZxPntvu1LmE0fwOUKhYYiGt+iJ8C9Qlw9dJKQNJobaJ1Yoxxd DBI1moe2HTbxOVygaCJZE9ocOk0xknYNcEPEIFUIJLJStvzuCBeQxGFvxJw4Oo0A23P+ 3yKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712381859; x=1712986659; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=syERz6mhK860XklS44UTgUTqfyplnj5Xmmc1ns2mrO8=; b=gGTT+Xz3l22XhtJArxYBCFfVAsKGjbRH0/I7TQxX2nv8XQsUVUIe1/Ml6la0mQNP+J BRfMtlZllgFGKbinwov7z5114TjgUnH3OzfMz4TqYdVhgbUpaH7r3i4yOv0xtMAUnPyM XH4uXFhnZYucfSuBm8sNAZw7m3sVg/rnuZpKYX1UEClbZv00iqevjaDhO8pkCrsLo67f uiS9nEzsNbk3eLXYOfOtg1JDt9Lx+Lr/v1vtJg61pip47vTrZu1M6GtERy9xQbkJhZBR I9s/SPYGzzU9NcAVjbS04v+Tuv5JF9zq98T97K62qK1FKjpql6oQjnqrOXfCrLETUQpi 99oA== X-Gm-Message-State: AOJu0Yy7Y6eznN3evc3SdmR606d0vqM9nhcyl7hhQGgy023Ddb0IUuk8 JrNb+0SxRpNBKdrjugNgbZzyju9JzpVTdYhj1o89nta750QxGr0Jln5fd8+h+56uVkTxDLZ29lt 0 X-Received: by 2002:a17:902:c952:b0:1e2:8ec0:90c4 with SMTP id i18-20020a170902c95200b001e28ec090c4mr3301213pla.2.1712381858926; Fri, 05 Apr 2024 22:37:38 -0700 (PDT) Received: from stoup.. (098-147-007-212.res.spectrum.com. [98.147.7.212]) by smtp.gmail.com with ESMTPSA id u12-20020a170903124c00b001dc05535632sm2589378plh.170.2024.04.05.22.37.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 22:37:38 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: zack@buhman.org, peter.maydell@linaro.org, ysato@users.sourceforge.jp Subject: [PATCH v3 2/4] target/sh4: Merge mach and macl into a union Date: Fri, 5 Apr 2024 19:37:30 -1000 Message-Id: <20240406053732.191398-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240406053732.191398-1-richard.henderson@linaro.org> References: <20240406053732.191398-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x635.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Allow host access to the entire 64-bit accumulator. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- target/sh4/cpu.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/target/sh4/cpu.h b/target/sh4/cpu.h index 9211da6bde..d928bcf006 100644 --- a/target/sh4/cpu.h +++ b/target/sh4/cpu.h @@ -155,12 +155,22 @@ typedef struct CPUArchState { uint32_t pc; /* program counter */ uint32_t delayed_pc; /* target of delayed branch */ uint32_t delayed_cond; /* condition of delayed branch */ - uint32_t mach; /* multiply and accumulate high */ - uint32_t macl; /* multiply and accumulate low */ uint32_t pr; /* procedure register */ uint32_t fpscr; /* floating point status/control register */ uint32_t fpul; /* floating point communication register */ + /* multiply and accumulate: high, low and combined. */ + union { + uint64_t mac; + struct { +#if HOST_BIG_ENDIAN + uint32_t mach, macl; +#else + uint32_t macl, mach; +#endif + }; + }; + /* float point status register */ float_status fp_status; From patchwork Sat Apr 6 05:37:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 786478 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1101:b0:343:f27d:c44e with SMTP id z1csp489400wrw; Fri, 5 Apr 2024 22:39:14 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUOPzkird8eaWDCl3IKJk1U1pKh4bq9IQhUMw8g6INq6RRpUckppbiB/50GYwJ2Q77026A0VDRJ6CuBQAPN8MSw X-Google-Smtp-Source: AGHT+IFt0BwP3odl4rCs2vvfmS+eqijCkgGmibZ5Y16Cr3/6MWj+iFJgoA+imCatywwdbN3u0Voq X-Received: by 2002:a0d:f402:0:b0:611:30a2:175b with SMTP id d2-20020a0df402000000b0061130a2175bmr2984767ywf.31.1712381954437; Fri, 05 Apr 2024 22:39:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712381954; cv=none; d=google.com; s=arc-20160816; b=Vn21pbltlv4pAa4tDQAeIm3Fw9f65Tqt8HgZd5Sl3pYDLkkGbOQPr6sPYHcpo/MS4P /IR+vjUSRlVSr1O2WL4WQAfjCk0i1BuEfmsCn2n+uKfLNkAjCrkqoXLxWstJxOB6bhcu nK2Q8uH3tj5RtTSEmIhC9lDR5DvLSGqo4F3eKuwjAXfI7miOuf/DOsVBYcWMmQhes+f8 aYrW++AeTvtq7JWaF/h2Hj/ycuuad/MkZnB5eRxN7LxwAvJnOaueMs0WSd38HVfZV0KC 2CIVOWOhx+kyOf0q+ezn39zr1hu0cUJdWkqWzK/n762dwgp5z24CuPYkmxWiw7Nbl5OS mzVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=eNDwqVcutDiIERmHFyRHN/ZgN4CmsTKIxQUGe7pIcjw=; fh=cuTpXidYakzLJEueohunWHlvXGsa+3AL7M3K5ZUnTnY=; b=KTn7wXNwxfOyFIFP3ELZa6xwu3wIdW1qx9+dbiOmqjavaE1LoSjROfNB2Psl+mSSnv aAhZDEPi9BVFKblXNzgfV4KZ8CpdKuvp/G5vKk0LutjPxe65UxtFefQd7PPphDWZEmK8 9HA91b3zq5P/6yNuUhjDj8XdoUdlzoR1s/nbd3g2ncUMUY6gMnl9jwGLbeiJrKkSIT9V J3lXdTWGSPdn/SyTEO0PFwTITYTrleSbNvKGm22SR9np0Gw2iKV1DJy8Nih+qXQjmD48 FT7zC/k+eWzPN+wv3eh2OY4elhQnS53NXU1aLgcLxo9N2zOKDgOhJgeeVXBnlsY+o1Bf DvDA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yRlIYthN; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id ke11-20020a05622a288b00b00434762b8f5dsi197998qtb.692.2024.04.05.22.39.14 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 05 Apr 2024 22:39:14 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yRlIYthN; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rsykK-0002qt-Hp; Sat, 06 Apr 2024 01:37:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rsykJ-0002qf-Jn for qemu-devel@nongnu.org; Sat, 06 Apr 2024 01:37:43 -0400 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rsykH-0006IY-Rj for qemu-devel@nongnu.org; Sat, 06 Apr 2024 01:37:43 -0400 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1e2b1cd446fso19519865ad.3 for ; Fri, 05 Apr 2024 22:37:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712381860; x=1712986660; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eNDwqVcutDiIERmHFyRHN/ZgN4CmsTKIxQUGe7pIcjw=; b=yRlIYthNpN79MdYRCo2XKjSOzYr/8QIVeMD0zQuOWjiA0HKdcnzxcdej6vPA+q7idh W/ple7ODrlrKxNFI5HCjU91A+s17s8XAQC+27AldXFSFuEQdeA4RZe1lfUGZQmB7JoQ1 s2AjoyPpkbZzUXPYGxxLd+LdhbHjE0RjmFAsMKHU494Aic0iw1nkwcmULvh+yK/i+aBh MkbTm2anYyvolVOAp20Pg/XoYT9VUuBgTulXUpR5to4KFOmFKM6D+TRbwmB3QIlkjroW Ee8YJEOZAOFwmMbk6GOKbBgt7fuRlXnz8Ot91r57oFuuID/nYxp/LMQ/+NpBP4qYJO/p kCMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712381860; x=1712986660; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eNDwqVcutDiIERmHFyRHN/ZgN4CmsTKIxQUGe7pIcjw=; b=YRjf8LInQxAlA/Kdc28Wb8tblrJtgc8hFcNdHKTcOJoERhoC06bHEP4NzM4iLVPkxf xV0MFbs0PMZMTdkY8bp5ps49l9I8pOAOpZmlIrCTMDsk5BIpHfigQO1q4+X+HMHd8wUZ iUFXP6UD2pQENon7OEvVdS4kXULHnY90kW+9CntVJ8DGLEn9q7cCqiQENu+bGfeML68z 4UEM5EQC5IaT+YNV0lEuwanbRjnSI03+8Sb+0N30kH3I+XOS0CP888R4YL/GazKw7Ifr rDOKUg0Qib/HSLBdF8p2B8exDMOq83nLNMRtJYfxaOchO1+M912+AiJqm1ameGCjayyG Miuw== X-Gm-Message-State: AOJu0YwjRGkiXZ+TkSBit9pu3OFCUIWdcJfo+u9a1Ao1k3Ur8hv15gEo hZSFR/3UkLt7r05QkWfUJ5T/oqxtXHDLftwj9l7zgF39PezN7lxEkmUrOHW/D+Lv6Qg9Gr6koBd I X-Received: by 2002:a17:902:a387:b0:1e0:d631:5563 with SMTP id x7-20020a170902a38700b001e0d6315563mr3201583pla.66.1712381860408; Fri, 05 Apr 2024 22:37:40 -0700 (PDT) Received: from stoup.. (098-147-007-212.res.spectrum.com. [98.147.7.212]) by smtp.gmail.com with ESMTPSA id u12-20020a170903124c00b001dc05535632sm2589378plh.170.2024.04.05.22.37.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 22:37:39 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: zack@buhman.org, peter.maydell@linaro.org, ysato@users.sourceforge.jp Subject: [PATCH v3 3/4] target/sh4: Fix mac.l with saturation enabled Date: Fri, 5 Apr 2024 19:37:31 -1000 Message-Id: <20240406053732.191398-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240406053732.191398-1-richard.henderson@linaro.org> References: <20240406053732.191398-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62d; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Zack Buhman The saturation arithmetic logic in helper_macl is not correct. I tested and verified this behavior on a SH7091. Signed-off-by: Zack Buhman Message-Id: <20240404162641.27528-2-zack@buhman.org> [rth: Reformat helper_macl, add a test case.] Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- target/sh4/helper.h | 2 +- target/sh4/op_helper.c | 23 ++++++------ tests/tcg/sh4/test-macl.c | 67 +++++++++++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 5 +++ 4 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 tests/tcg/sh4/test-macl.c diff --git a/target/sh4/helper.h b/target/sh4/helper.h index 8d792f6b55..64056e4a39 100644 --- a/target/sh4/helper.h +++ b/target/sh4/helper.h @@ -11,7 +11,7 @@ DEF_HELPER_3(movcal, void, env, i32, i32) DEF_HELPER_1(discard_movcal_backup, void, env) DEF_HELPER_2(ocbi, void, env, i32) -DEF_HELPER_3(macl, void, env, i32, i32) +DEF_HELPER_3(macl, void, env, s32, s32) DEF_HELPER_3(macw, void, env, i32, i32) DEF_HELPER_2(ld_fpscr, void, env, i32) diff --git a/target/sh4/op_helper.c b/target/sh4/op_helper.c index 4559d0d376..d0bae0cc00 100644 --- a/target/sh4/op_helper.c +++ b/target/sh4/op_helper.c @@ -158,20 +158,23 @@ void helper_ocbi(CPUSH4State *env, uint32_t address) } } -void helper_macl(CPUSH4State *env, uint32_t arg0, uint32_t arg1) +void helper_macl(CPUSH4State *env, int32_t arg0, int32_t arg1) { + const int64_t min = -(1ll << 47); + const int64_t max = (1ll << 47) - 1; + int64_t mul = (int64_t)arg0 * arg1; + int64_t mac = env->mac; int64_t res; - res = ((uint64_t) env->mach << 32) | env->macl; - res += (int64_t) (int32_t) arg0 *(int64_t) (int32_t) arg1; - env->mach = (res >> 32) & 0xffffffff; - env->macl = res & 0xffffffff; - if (env->sr & (1u << SR_S)) { - if (res < 0) - env->mach |= 0xffff0000; - else - env->mach &= 0x00007fff; + if (!(env->sr & (1u << SR_S))) { + res = mac + mul; + } else if (sadd64_overflow(mac, mul, &res)) { + res = mac < 0 ? min : max; + } else { + res = MIN(MAX(res, min), max); } + + env->mac = res; } void helper_macw(CPUSH4State *env, uint32_t arg0, uint32_t arg1) diff --git a/tests/tcg/sh4/test-macl.c b/tests/tcg/sh4/test-macl.c new file mode 100644 index 0000000000..b66c854365 --- /dev/null +++ b/tests/tcg/sh4/test-macl.c @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include +#include +#include + +#define MACL_S_MIN (-(1ll << 47)) +#define MACL_S_MAX ((1ll << 47) - 1) + +int64_t mac_l(int64_t mac, const int32_t *a, const int32_t *b) +{ + register uint32_t macl __asm__("macl") = mac; + register uint32_t mach __asm__("mach") = mac >> 32; + + asm volatile("mac.l @%0+,@%1+" + : "+r"(a), "+r"(b), "+x"(macl), "+x"(mach)); + + return ((uint64_t)mach << 32) | macl; +} + +typedef struct { + int64_t mac; + int32_t a, b; + int64_t res[2]; +} Test; + +__attribute__((noinline)) +void test(const Test *t, int sat) +{ + int64_t res; + + if (sat) { + asm volatile("sets"); + } else { + asm volatile("clrs"); + } + res = mac_l(t->mac, &t->a, &t->b); + + if (res != t->res[sat]) { + fprintf(stderr, "%#llx + (%#x * %#x) = %#llx -- got %#llx\n", + t->mac, t->a, t->b, t->res[sat], res); + abort(); + } +} + +int main() +{ + static const Test tests[] = { + { 0x00007fff12345678ll, INT32_MAX, INT32_MAX, + { 0x40007ffe12345679ll, MACL_S_MAX } }, + { MACL_S_MIN, -1, 1, + { 0xffff7fffffffffffll, MACL_S_MIN } }, + { INT64_MIN, -1, 1, + { INT64_MAX, MACL_S_MIN } }, + { 0x00007fff00000000ll, INT32_MAX, INT32_MAX, + { 0x40007ffe00000001ll, MACL_S_MAX } }, + { 4, 1, 2, { 6, 6 } }, + { -4, -1, -2, { -2, -2 } }, + }; + + for (int i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) { + for (int j = 0; j < 2; ++j) { + test(&tests[i], j); + } + } + return 0; +} diff --git a/tests/tcg/sh4/Makefile.target b/tests/tcg/sh4/Makefile.target index 16eaa850a8..9a11c10924 100644 --- a/tests/tcg/sh4/Makefile.target +++ b/tests/tcg/sh4/Makefile.target @@ -9,3 +9,8 @@ run-signals: signals $(call skip-test, $<, "BROKEN") run-plugin-signals-with-%: $(call skip-test, $<, "BROKEN") + +VPATH += $(SRC_PATH)/tests/tcg/sh4 + +test-macl: CFLAGS += -O -g +TESTS += test-macl From patchwork Sat Apr 6 05:37:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 786476 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1101:b0:343:f27d:c44e with SMTP id z1csp489331wrw; Fri, 5 Apr 2024 22:38:56 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUbSUzgWtQwt3hzeRN1G6a+uTFkjslrkTvVKyDhHX+QyAJCwY8IIzdSArX6TpvNctVGWICAFJ+KquK1XpYmkPgi X-Google-Smtp-Source: AGHT+IFP3BuJgjsknrv30jZsRonMPYW1u+VV73Z36sxhQSZ2auiyVDoBX9GiApGBMG/TRWhzNCRX X-Received: by 2002:a05:6214:c27:b0:690:dce7:76e7 with SMTP id a7-20020a0562140c2700b00690dce776e7mr3568981qvd.25.1712381936298; Fri, 05 Apr 2024 22:38:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712381936; cv=none; d=google.com; s=arc-20160816; b=rzaTy7WAOChrS9pdIuMny0ssH+uQmXhVkOSPv8Mev0ODsXgYFeeqIGmaf9XpH9cjo9 ozBTv29YrGxJQv5K4ZUmkwSvVIZA/IQLGx9e4Rs/QBEH9L/uXQX8ebkr/pfgdK9OaX80 ZltEpn4aKKeTL6SrrNzX+/VX3YMLZDG/2eHIKUj40KUXmoT2ozIzPWQZng9Ufo1r6yzu BoaN/EN58D77WMu2zvQ1akwv0V9PqMU1APUljqU/ukx+k6WCPf1foEqGRI1VKD26q6fF BRRr0wFXaHwNy/RBOZ1brXjkVywJ95FVw1fowBJ8zCZi8LJG9VUsWeEwxrN71PdCHt8w VfhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=BjltTyIUkG4oE0AcOsKz1SbiNuo1Csm0+1nsuSZO9Hk=; fh=cuTpXidYakzLJEueohunWHlvXGsa+3AL7M3K5ZUnTnY=; b=IRgIhtwFvioraQeSNqMjZ/D/klNRsjcPOQYOQo2Ae2Y70E5+Pwu1ALYSu6lFAXQQbh ElfJdSnUIxRbxnjV78bM1rkdympHWulDcS26mbADixxuIVygYm+pWET1JVcZalgB4iZQ c7jMhm0+/zCqnky24S4D+iaw20jSAHUI90Zb1IfgEaC3lbNuWIWJ7dwJEx6/S56YL+tO +fKBIElOjSlW38BKwk237qd3F6SjxnNksAbjsXZZ8h7WkCwQ3QVNvaQTE/RWpyik5s5m pJo2KJX4ZFyzxhHVDc7TJQq+BXkYoPRkeFYC2sJAUHhndfR/59WJiVVNhYMynEI4chE0 tSsw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NaAr0+Nw; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id g4-20020a0562140ac400b006994a9de8ddsi1739330qvi.564.2024.04.05.22.38.56 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 05 Apr 2024 22:38:56 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NaAr0+Nw; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rsykN-0002ra-5v; Sat, 06 Apr 2024 01:37:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rsykK-0002qu-Vg for qemu-devel@nongnu.org; Sat, 06 Apr 2024 01:37:44 -0400 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rsykJ-0006Ij-6y for qemu-devel@nongnu.org; Sat, 06 Apr 2024 01:37:44 -0400 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1e252f2bf23so25175915ad.2 for ; Fri, 05 Apr 2024 22:37:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712381862; x=1712986662; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BjltTyIUkG4oE0AcOsKz1SbiNuo1Csm0+1nsuSZO9Hk=; b=NaAr0+NwTByfYhqBOyloW5DMn5UhJc3ScKGtDmmxIBVQ1i340BsT26oD4NU2pYyUlx a5xaVm/ZYPWmza7MkjVTheD/CBsOxWewUK+zixpDGgif3LcUjn4i904Ssk0+GHSuJ0rf MXPT6+lFvFfh0jGzjgL16U2/0qzZNiHwFYsytxQwH+z8Ouv7rUMZ4CT1wmjNpmWLyGxw HjYJJKiLo5B6fVk6gQCOW2+UcDFQsUhuJT4OkRH/VCF6oT4bnfl1Rp6FaQ7gGs0068ev 0aMNF4SrxgIl34NqV6GvZlIyK5u2s99o+TfoDh6oxKHJJ0s9jlWxmcvZu60zIhVXMMgK 7Www== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712381862; x=1712986662; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BjltTyIUkG4oE0AcOsKz1SbiNuo1Csm0+1nsuSZO9Hk=; b=ZGW4ZHkNA1TiSq+bLlvPezVFwIxK2CPRG+byfNdUOEtF0LR4bdQxCaLzMbZhvLxfbS wxCr4+Hh4G4Byr1DZRcMAzM/uTkKbqx88zF95Ri3oHvp6Pgd90FkFwbKVYTumiJqdLu/ kQW6bu8gItRoB1Z9JJKKhM9BKoZQ7qFE2l8G1ZRua6849hbD06HhtGSyjRYBx0aFgwUJ aAW3smQFair08NVPNMPUDiAO8nsPfdMOlzDhoyFks6G3vF3qT4rJCRPOrwWV0EA18zgZ OXDlbs5y2PBFVxr9qk8r9UTkjzYg77e2MJdRmAuuM7EeyQ35wP+mH93PkaHtD9/6Lk3d mA/A== X-Gm-Message-State: AOJu0YyPnHkAu3CWKjWhiDQ/J3urfVSEegPb9KMpuMt4W4+4iXYH9g+u iO9V9mRrSh/AkbMCvWIRitCgPHILBSQccEuiejn91EIU9nzFMdSMt9Qdxratz7kcTOoUMQVBJA+ V X-Received: by 2002:a17:902:e542:b0:1e0:cdbf:24c2 with SMTP id n2-20020a170902e54200b001e0cdbf24c2mr3895820plf.29.1712381861942; Fri, 05 Apr 2024 22:37:41 -0700 (PDT) Received: from stoup.. (098-147-007-212.res.spectrum.com. [98.147.7.212]) by smtp.gmail.com with ESMTPSA id u12-20020a170903124c00b001dc05535632sm2589378plh.170.2024.04.05.22.37.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 22:37:41 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: zack@buhman.org, peter.maydell@linaro.org, ysato@users.sourceforge.jp Subject: [PATCH v3 4/4] target/sh4: Fix mac.w with saturation enabled Date: Fri, 5 Apr 2024 19:37:32 -1000 Message-Id: <20240406053732.191398-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240406053732.191398-1-richard.henderson@linaro.org> References: <20240406053732.191398-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::631; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x631.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Zack Buhman The saturation arithmetic logic in helper_macw is not correct. I tested and verified this behavior on a SH7091. Signed-off-by: Zack Buhman Message-Id: <20240405233802.29128-3-zack@buhman.org> [rth: Reformat helper_macw, add a test case.] Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- target/sh4/helper.h | 2 +- target/sh4/op_helper.c | 28 +++++++++------- tests/tcg/sh4/test-macw.c | 61 +++++++++++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 3 ++ 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 tests/tcg/sh4/test-macw.c diff --git a/target/sh4/helper.h b/target/sh4/helper.h index 64056e4a39..29011d3dbb 100644 --- a/target/sh4/helper.h +++ b/target/sh4/helper.h @@ -12,7 +12,7 @@ DEF_HELPER_1(discard_movcal_backup, void, env) DEF_HELPER_2(ocbi, void, env, i32) DEF_HELPER_3(macl, void, env, s32, s32) -DEF_HELPER_3(macw, void, env, i32, i32) +DEF_HELPER_3(macw, void, env, s32, s32) DEF_HELPER_2(ld_fpscr, void, env, i32) diff --git a/target/sh4/op_helper.c b/target/sh4/op_helper.c index d0bae0cc00..99394b714c 100644 --- a/target/sh4/op_helper.c +++ b/target/sh4/op_helper.c @@ -177,22 +177,28 @@ void helper_macl(CPUSH4State *env, int32_t arg0, int32_t arg1) env->mac = res; } -void helper_macw(CPUSH4State *env, uint32_t arg0, uint32_t arg1) +void helper_macw(CPUSH4State *env, int32_t arg0, int32_t arg1) { - int64_t res; + /* Inputs are already sign-extended from 16 bits. */ + int32_t mul = arg0 * arg1; - res = ((uint64_t) env->mach << 32) | env->macl; - res += (int64_t) (int16_t) arg0 *(int64_t) (int16_t) arg1; - env->mach = (res >> 32) & 0xffffffff; - env->macl = res & 0xffffffff; if (env->sr & (1u << SR_S)) { - if (res < -0x80000000) { + /* + * In saturation arithmetic mode, the accumulator is 32-bit + * with carry. MACH is not considered during the addition + * operation nor the 32-bit saturation logic. + */ + int32_t res, macl = env->macl; + + if (sadd32_overflow(macl, mul, &res)) { + res = macl < 0 ? INT32_MIN : INT32_MAX; + /* If overflow occurs, the MACH register is set to 1. */ env->mach = 1; - env->macl = 0x80000000; - } else if (res > 0x000000007fffffff) { - env->mach = 1; - env->macl = 0x7fffffff; } + env->macl = res; + } else { + /* In non-saturation arithmetic mode, the accumulator is 64-bit */ + env->mac += mul; } } diff --git a/tests/tcg/sh4/test-macw.c b/tests/tcg/sh4/test-macw.c new file mode 100644 index 0000000000..4eceec8634 --- /dev/null +++ b/tests/tcg/sh4/test-macw.c @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include +#include +#include + +int64_t mac_w(int64_t mac, const int16_t *a, const int16_t *b) +{ + register uint32_t macl __asm__("macl") = mac; + register uint32_t mach __asm__("mach") = mac >> 32; + + asm volatile("mac.w @%0+,@%1+" + : "+r"(a), "+r"(b), "+x"(macl), "+x"(mach)); + + return ((uint64_t)mach << 32) | macl; +} + +typedef struct { + int64_t mac; + int16_t a, b; + int64_t res[2]; +} Test; + +__attribute__((noinline)) +void test(const Test *t, int sat) +{ + int64_t res; + + if (sat) { + asm volatile("sets"); + } else { + asm volatile("clrs"); + } + res = mac_w(t->mac, &t->a, &t->b); + + if (res != t->res[sat]) { + fprintf(stderr, "%#llx + (%#x * %#x) = %#llx -- got %#llx\n", + t->mac, t->a, t->b, t->res[sat], res); + abort(); + } +} + +int main() +{ + static const Test tests[] = { + { 0, 2, 3, { 6, 6 } }, + { 0x123456787ffffffell, 2, -3, + { 0x123456787ffffff8ll, 0x123456787ffffff8ll } }, + { 0xabcdef127ffffffall, 2, 3, + { 0xabcdef1280000000ll, 0x000000017fffffffll } }, + { 0xfffffffffll, INT16_MAX, INT16_MAX, + { 0x103fff0000ll, 0xf3fff0000ll } }, + }; + + for (int i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) { + for (int j = 0; j < 2; ++j) { + test(&tests[i], j); + } + } + return 0; +} diff --git a/tests/tcg/sh4/Makefile.target b/tests/tcg/sh4/Makefile.target index 9a11c10924..4d09291c0c 100644 --- a/tests/tcg/sh4/Makefile.target +++ b/tests/tcg/sh4/Makefile.target @@ -14,3 +14,6 @@ VPATH += $(SRC_PATH)/tests/tcg/sh4 test-macl: CFLAGS += -O -g TESTS += test-macl + +test-macw: CFLAGS += -O -g +TESTS += test-macw