Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp509299ybl; Thu, 22 Aug 2019 00:06:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqzebfJCBQK/Lb3zX/x8Yl/FhL3sqYgI+Y6P6M1JZbNKEFlPVp9t6wtrQngW4o6CpWvUzRM7 X-Received: by 2002:a17:902:2be6:: with SMTP id l93mr38402665plb.0.1566457595104; Thu, 22 Aug 2019 00:06:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566457595; cv=none; d=google.com; s=arc-20160816; b=EyHs4AAa2kVVveDBT9FdFWJ8czpTiL/ntyK/azvL+V8oQD0VHKAW0M1dpmvXx7VKVU 68jjRgcyWXaHDiUoZHZIvXP45PfEh3e9BQhfQoEb2XmmOGDMWJiQBlwawkeryvS+lQ0w QMrnAMv88k+neWZHCCHrLMsYS0uq0SU8do++6LjislEV8e0pmGFtgBkxk0ReMp05x+gJ F4DqoHVjNzZbMc2bMFI1282RHGSiFSodhfP2A9YA6QtT/kHeiyzNlXWLWlp3NtKm6PbA QejjIxAwzntGPHQjmmXQPnBeX6jSLpa3jqwonM7YkfYo9M/49fkEZmtVeIfKuARMtNpY LWbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=UJs0K6rRKqwRwSTuqOsvwtuUWsudEI9pV/2hEqZh8P8=; b=QlTlvVMCQ1Iij3MRnpFo0KFT/v5bHqFI/JScAxPjrv5BNfXXgf9nUHieHO85p4Qopq +Mxk7TccHlcPrr33nzrHMYwGTeDSxf+G/jqlSwU38gc0LUZfRtMZqBOBBYxW3UgPnVMf 5KNIRZLG2vgR5r2f93zYAGM4yYloGEtIm/p56cpQitTHg5UBiAnjw3PuUTWVPWtMk1S4 5+pj+zQnySO8nXkTUZNizVM79i3G06k2LDq7IrqQ0uDviE2dM3BfSUS6FaToQxYMv85V J0kLW7gEA/XlQfsaUUEwTK7s3/djLp6JK+DYVTKPdv7Q4u6gNoezqzLrBkvqEhaOZdx+ a72A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j16si17770025pfh.0.2019.08.22.00.06.20; Thu, 22 Aug 2019 00:06:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731632AbfHVGGQ (ORCPT + 99 others); Thu, 22 Aug 2019 02:06:16 -0400 Received: from ox4u.de ([212.118.221.216]:45541 "EHLO s1.ox4u.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729455AbfHVGGQ (ORCPT ); Thu, 22 Aug 2019 02:06:16 -0400 Received: by s1.ox4u.de (Postfix, from userid 65534) id 255FA260127; Thu, 22 Aug 2019 08:06:14 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on s1.ox4u.de X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED autolearn=disabled version=3.4.1 Received: from ws-140106.systec.local (unknown [212.185.67.146]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by s1.ox4u.de (Postfix) with ESMTPSA id 7A95C260122; Thu, 22 Aug 2019 08:06:13 +0200 (CEST) From: Alexander Stein To: Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler Cc: Alexander Stein , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/1] iio: core: Fix fractional format generation Date: Thu, 22 Aug 2019 08:06:07 +0200 Message-Id: <20190822060607.25339-1-alexander.stein@systec-electronic.com> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In case the result is -0.3252 tmp0 is 0 after the div_s64_rem, so tmp0 is non-negative which results in an output of 0.3252. Fix this by explicitly handling the negative sign ourselves. Signed-off-by: Alexander Stein --- Changes in v2: * Support vals[0] >= and vals[1] < 0 in IIO_VAL_FRACTIONAL * Note: IIO_VAL_FRACTIONAL is untested, as I lack hardware * Note2: Currently IIO_VAL_FRACTIONAL is only called with vals[1] from in-kernel drivers AFAICS drivers/iio/industrialio-core.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 245b5844028d..247338142c87 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -568,6 +568,7 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type, { unsigned long long tmp; int tmp0, tmp1; + char *sign; bool scale_db = false; switch (type) { @@ -593,11 +594,17 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type, tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); tmp1 = vals[1]; tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1); - return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1)); + if (vals[1] < 0) { + sign = vals[0] >= 0 ? "-" : ""; + } else { + sign = vals[0] < 0 ? "-" : ""; + } + return snprintf(buf, len, "%s%u.%09u", sign, abs(tmp0), abs(tmp1)); case IIO_VAL_FRACTIONAL_LOG2: + sign = vals[0] < 0 ? "-" : ""; tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]); tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1); - return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1)); + return snprintf(buf, len, "%s%u.%09u", sign, abs(tmp0), abs(tmp1)); case IIO_VAL_INT_MULTIPLE: { int i; -- 2.23.0