Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp5545456imm; Tue, 12 Jun 2018 09:23:44 -0700 (PDT) X-Google-Smtp-Source: ADUXVKICENFvACcfh8GAS4PZrZCPrHLsJJunp/yhSj//wDG+w6bySs7coXEJZ5nLb1rNIG4Jv8Wp X-Received: by 2002:a63:7459:: with SMTP id e25-v6mr896672pgn.186.1528820624103; Tue, 12 Jun 2018 09:23:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528820624; cv=none; d=google.com; s=arc-20160816; b=uL4uo514zZBJEaujE67cW7Hi1GwEUOudTGVM2q8NbfRzmBho5N3K4AE46hs7Q1LVCs /gSwfskB11L7RYa7elVwdh8XrLyplMfRq10PysqbhZQaW7bQRXwBrmmy3gd3VkkhRBAi vlBHMm8jZnhFBNE27rpzK8nHE+PevVHgyn7SBfJi1psx3R9T5ML/ZXNe2oG2Xi0cSbDj HoA1BOjhDPhJKTc+Mafskq0JmlNrFpBVkFTuco/9VxQ5mftFcaoNMpKNLUSe9o/pM3xK TrHSJeNCmNOdwt6Zu/8Tw8rxVdKwfPOVEwOK/QjmbfTz9L/hqb/lvxfrCG648a9/7tia 7eHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=SD/vkKHHe7VcwOgHxPuKmedkuT8iyzrA6ySLSwiuTjg=; b=QPSYs4JDNiEy4qCROq/OfzZLv+eIBp9ph34ttnldFZl7c6utYe001oYy+A+JECAjaF vnTiAnnjUIB2nFRYHCjkuAP4ZnqYcFIQNF1OIHbnYBRMnISnwrdGAz7rZfqoE2MISXWG Qu6nHvqEmiPlnupCAmNa0Zw9n8cFD6OrT0rhqPu4g4v2t0RBU7y7WEjga6vIuxxHPFCP C549egv8TGyzMd7ZzvrE0S5U2jcokrBl5jBD/6qbdpvXeYbg+/btHVqkBu/2tXu0Yxl9 CSKHnao0tQY5aTOLcIOjF412yCUfolVJObpX5hGQDRJ5PZjAS2SsimKgthQBbi3xxyl9 4aXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=CeBYaZXt; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i197-v6si416756pgc.161.2018.06.12.09.23.29; Tue, 12 Jun 2018 09:23:44 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=CeBYaZXt; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934297AbeFLQWD (ORCPT + 99 others); Tue, 12 Jun 2018 12:22:03 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:40092 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933792AbeFLQWB (ORCPT ); Tue, 12 Jun 2018 12:22:01 -0400 Received: by mail-wr0-f194.google.com with SMTP id l41-v6so24728090wre.7; Tue, 12 Jun 2018 09:22:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=SD/vkKHHe7VcwOgHxPuKmedkuT8iyzrA6ySLSwiuTjg=; b=CeBYaZXtTxoZnvDWBeFC8jqDZAvf0eDGl/5WVQrRrVKD/H3HN9zuEquL0HkTsaqe6S sN5BCdMJCdKhrPNFugfFDvS+UTCHk/LMkM0hMPtcAttEgoRPy6TPYMeq9PfKSeyiI+lU Z7InJQ9gndcdGV18HcuGV54QkYDWSWdGp9jz8ZxwZFc1IbOBINSzBWg1htOzs6uva+VN 8+IahUTtjVCPnE5cZEM5MlOnynQ6thyIRtB9lWQdkfEIfdza8Agrg2Uxfu9m150pWtPy xsGN9XpE6dZlNurmivjzMruZ/yLLXOtmZc3xfMUT3XJ5vcT5eImVSeeQw+8TG3w9/no1 OSEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=SD/vkKHHe7VcwOgHxPuKmedkuT8iyzrA6ySLSwiuTjg=; b=Sk9VLjnghnJljLamMWnYp2JDw1UdyUPKtvktSKLyKNVPnYJuVNfrLT8Ymdzn34dW6w xztO5KqhOShTTrDnjjyFV3YXuKPo4+vYKovUPITz0sVz9ORd2ojaAcGR0/m7BogG+gcW 0RlD3SrJfW2ddrxlcTlzO4u5w7ptjAbvHbAzmDNmpZe0Aab9z+ie9qFWKEoj+cz2pRaO zW5Q22JuXZB8ie+sbSMJG1GS72/KX/3sxW6Ghv5InpA7nkkquYs2CZcN2jHM9S9W+0Z9 TCX3cTIQ59rvzaejKHlAwVc5d4V2IbaAxGM03zsrDxT0hEkAh+qZ7UzNUrfKKFRwTAEx tYZw== X-Gm-Message-State: APt69E2Y2Dm+U5n8/wpNvFqaabaQtOUFx16bw4X0KJ/zWj4VfAC28fcR +89HvgCod4HPkAahi3IE1HI= X-Received: by 2002:adf:fd88:: with SMTP id d8-v6mr800943wrr.276.1528820519977; Tue, 12 Jun 2018 09:21:59 -0700 (PDT) Received: from localhost.localdomain ([197.54.242.32]) by smtp.gmail.com with ESMTPSA id o13-v6sm557959wmc.8.2018.06.12.09.21.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Jun 2018 09:21:58 -0700 (PDT) From: Karim Eshapa To: lars@metafoo.de Cc: Michael.Hennerich@analog.com, jic23@kernel.org, knaack.h@gmx.de, pmeerw@pmeerw.net, gregkh@linuxfoundation.org, linux-iio@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, Karim Eshapa Subject: [PATCH v2] staging:iio:accel:adis16240: sign extend function avoiding code duplication Date: Tue, 12 Jun 2018 18:21:53 +0200 Message-Id: <20180612162153.21789-1-karim.eshapa@gmail.com> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use sign_extend32 kernel function instead of code duplication, Safe also for 16 bit. and remove declaration of bits variable not needed. Signed-off-by: Karim Eshapa >On Tue, 2018-06-12 at 01:38 +0200, Karim Eshapa wrote: >> Use sign_extend32 kernel function instead of code duplication. >> Safe also for 16 bit. > >Perhaps remove the bits declaration and assignments >and just use 9 directly. > >> diff --git a/drivers/staging/iio/accel/adis16240.c b/drivers/staging/iio/accel/adis16240.c > >> @@ -292,9 +292,7 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, >> ret = adis_read_reg_16(st, addr, &val16); >> if (ret) >> return ret; >> - val16 &= (1 << bits) - 1; >> - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); >> - *val = val16; >> + *val = sign_extend32(val16, bits - 1); >> return IIO_VAL_INT; >> case IIO_CHAN_INFO_PEAK: >> bits = 10; >> @@ -302,9 +300,7 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, >> ret = adis_read_reg_16(st, addr, &val16); >> if (ret) >> return ret; >> - val16 &= (1 << bits) - 1; >> - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); >> - *val = val16; >> + *val = sign_extend32(val16, bits - 1); >> return IIO_VAL_INT; >> } >> return -EINVAL; --- drivers/staging/iio/accel/adis16240.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/drivers/staging/iio/accel/adis16240.c b/drivers/staging/iio/accel/adis16240.c index fff6d99089cc..24e525f1ef25 100644 --- a/drivers/staging/iio/accel/adis16240.c +++ b/drivers/staging/iio/accel/adis16240.c @@ -250,7 +250,6 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, { struct adis *st = iio_priv(indio_dev); int ret; - int bits; u8 addr; s16 val16; @@ -287,24 +286,18 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, *val = 25000 / 244 - 0x133; /* 25 C = 0x133 */ return IIO_VAL_INT; case IIO_CHAN_INFO_CALIBBIAS: - bits = 10; addr = adis16240_addresses[chan->scan_index][0]; ret = adis_read_reg_16(st, addr, &val16); if (ret) return ret; - val16 &= (1 << bits) - 1; - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); - *val = val16; + *val = sign_extend32(val16, 9); return IIO_VAL_INT; case IIO_CHAN_INFO_PEAK: - bits = 10; addr = adis16240_addresses[chan->scan_index][1]; ret = adis_read_reg_16(st, addr, &val16); if (ret) return ret; - val16 &= (1 << bits) - 1; - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); - *val = val16; + *val = sign_extend32(val16, 9); return IIO_VAL_INT; } return -EINVAL; -- 2.17.1