From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm2-f13.google.com (mail-wm2-f13.google.com [74.125.225.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD80A380FD3 for ; Sun, 27 Sep 2026 06:31:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.141 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790490680; cv=none; b=ZOgJu0gKLwle63OBTcg4Dq4h5mD8CXSnYhi5hGmMvqEP7yrea04+uvUA99lkIXQ9BcJzFIW2qfUrCtaDQG1dA8F+Jy+w3zztjL8OKttoXQWAWsqUyfLLJQvj8O96eTd5271phnMwTrF+h6E74aWjWpdI8d5ruUz0ViH+dB3lvgw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790490680; c=relaxed/simple; bh=AFVICFN4chX62VI8nPVeK5uJPzcrbrWeDjC5hZXZc+Q=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dAU8oT/dzwkvyG8q/DhUlRPWooBnKIKW0WLQ/3OyfdhAbrcKzMz+iWa2k5znZdHTxPMOJAHraoHsXBYM0Sgka/Ai57Vg6oc+w1evPSrVyKANKCM/SEoVgP9a3y626gm6V16U0zz1KP0CTUzhCDJR5rI3PW5PFMnOO9dYweDYaYU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ihFU0X74; arc=none smtp.client-ip=74.125.225.141 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ihFU0X74" Received: by mail-wm2-f13.google.com with SMTP id 5b1f17b1804b1-49d1fb0cf5eso18882015e9.3 for ; Sat, 26 Sep 2026 23:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790490677; x=1791095477; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=G98IODEmjwEPHfQZb5jDWlDo2HGYU0z05SG8co7Iw9U=; b=ihFU0X74tUij6HHnWANbCm2IrL6ZXvnx05GenDJYNXJ6srIgqtGGS6CCHBai6S2Buw F/vsGzZJX1a7uIEJDFh2xy2Hti12QQRRwUEFfyigA/RAV83LsmEKG0mLptv15ibd9QXr M0VJSUvq7FzTJTgpX5IWt8oIC4xN24RY4wJa6UXo88a/9cmfgVx8YrccDcALQZlJotiy C5QAe3wcVjMQ96fLJwcY9/KU0QzM+5JVHSlf1iqu+9oq5G7iaMRIB9YmTWUzkCID6gkq 5bX4TdYsmhhMDIPryBqNtJI0pZTmBKIiG6BYx1aKQwF5WquhhfaN7F8A3y0Qh7BshjdZ K/Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790490677; x=1791095477; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=G98IODEmjwEPHfQZb5jDWlDo2HGYU0z05SG8co7Iw9U=; b=pXv7KQeh7t1Ifp7DfeS8YSfvSkRfDvu08Zho+Xw3na9PnpV2yl6WinfQrjpJn2gzHi XNPTahIsc1aGEKu31ubNeH9AJAkXCRTWRK7qOwB5HhapcwGhAiifg8GQiuTJwUrtgpuL zc3IRCc5Yhf3f3LUF/ogu2BwWNx7AiPd0Ma176hKCHddAX/CBJ0LOe6LijI3KxdxVOOf F4ZGULgXMwL+o/z4KZiGbenMeD8nYOV3VUy87RugOqyftqCq1wTh+A2thFGSy+QpgDck OT4JlEbXb/qWvmcyzDMDkiCv71DSiBCai/5bEmSfDHlZj2WS13aqL/61tBWQgh+t2Bmz cGOQ== X-Forwarded-Encrypted: i=1; AKwUvBy1mONmDPLxYzgv5aiOP2ciqTsFOwQvfoFIw/yZbtx/kaXR/mOLYOE5ljWxuJZZT1+0aWEaXrmFWFpmN5A=@vger.kernel.org X-Gm-Message-State: AFuF++k6oeKGgO8iiCniV4elE7oJTroarur77e8+rCHPaM0R8MlGKoxJ g/G4mpvnDtV6Gvy7SiWRVYT80Yt4iwxrVAB/SCSbXytXEdYLbTeXSRfx X-Gm-Gg: AYBFou3Xo+sz9X/dAG/V3NQkmFRuQPKIC3h/KNZZt2WabZEbxzvaOFnRVz6As0avECE Ml44JOdrQxQWmlUk5LCAylNZHR7V7VheTlc763m0qNM/ncsrGQas/WL/fODo3xkE4Mpi2KdXRSv EsqMXwB8lIP86T1hI8+psKL2hDYRVkJtEfI/69SQ1c2Jvv9dId5usamoIeqwy8ZpL1FL5yc7Fc8 UAHOvME0flLRB0UAlYrVPRirng/PotfzGvKqMHz7KNnGKMf6K43roLM9g2tLHNyCLBvws/csY97 ppmuZFJEMhix7tU6b3t+3MRYb6QpuIRQ93U0jq3XtYhIaasm5szQg/JdvLLm0vG3QGTizK9DQxB rTsAGjy6em4LGglqIFoxPrNWigrChAXcquvB+EMO6wms2uX/Gb3gwAuYaE6dvO8zy7IZSkBabTp s+7r/k7AtEFn9iB4N2RO7m6M3V2KANnMaud8nE5Xh40+BIU20peve9BSjwrU9nf9EB9euCJ7O2h 4JQIcaG+iJsuYM27GmLl4933WMnHAnXeIo= X-Received: by 2002:a05:600c:4f41:b0:49f:fe5a:522f with SMTP id 5b1f17b1804b1-49ffe5a56c9mr49760925e9.23.1790490676934; Sat, 26 Sep 2026 23:31:16 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49fef6186a6sm142933845e9.4.2026.09.26.23.31.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Sep 2026 23:31:16 -0700 (PDT) Date: Sun, 27 Sep 2026 07:31:15 +0100 From: David Laight To: Christian Lamparter Cc: Ridham Khurana , Guenter Roeck , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, Christian Lamparter , Alexander Sverdlin , Nikita Shubin , Shuah Khan , Jori Koolstra , Brigham Campbell , linux-kernel-mentees@lists.linux.dev Subject: Re: [PATCH] hwmon: (lm70) Fix rounding of negative temperatures Message-ID: <20260927073115.15110623@pumpkin> In-Reply-To: References: <20260924205326.1739229-1-khurana.ridham222@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 25 Sep 2026 20:03:06 +0200 Christian Lamparter wrote: > Hi, > > On 9/24/26 10:53 PM, Ridham Khurana wrote: > > temp1_input_show() drops the low bits of the temperature register by > > dividing the raw value (raw / 32 on the LM70). These bits are not part > > of the temperature: the LM70, LM71, LM74 and TMP122/TMP124 always > > return some of them as 1, and the TMP125 fills them with copies of the > > temperature LSB. Division rounds toward zero, so a negative temperature > > with any of these bits set is reported one LSB too high. > > > > For example, the TMP122 returns 0xffff for -0.0625 degrees C, which the > > driver reports as 0. The LM70 returns 0xf39f for -25 degrees C, which > > the driver reports as -24750. > > > > Use an arithmetic right shift instead, which drops the low bits and > > rounds down. tmp421 had a similar problem with negative values, fixed > > by commit 724e8af85854 ("hwmon: (tmp421) fix rounding for negative > > values"). > > For the TMP125 yes: > > Reviewed-by: Christian Lamparter > > > > Fixes: e1a8e913f97e ("[PATCH] lm70: New hardware monitoring driver") > > Fixes: a86e94dc946d ("hwmon: (lm70) Add support for LM71 and LM74") > > Fixes: cd929672a9ef ("hwmon: (lm70) Add ti,tmp125 support") > > Signed-off-by: Ridham Khurana > > ---- > I wrote a little test program by hand (see below) and ran it on x64. > No idea if different archs behave differently or if > a clever unsafe math optimizing compiler flag will > convert the "/ 32" to a " >> 5" but I doubt that... A compiler will convert an unsigned divide to a shift, but can't do so for signed divides. But you'll also get flagged because C doesn't define right shifts for negative values. I think they are actually 'undefined behaviour' (UB) if clang detects that your code is UB it silently just stops generating object code. (int)((val + 0x8000u) / 16u - 0x8000) might safely generate the correct value. David