From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753404AbbIWUES (ORCPT ); Wed, 23 Sep 2015 16:04:18 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:56079 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752760AbbIWUER (ORCPT ); Wed, 23 Sep 2015 16:04:17 -0400 Date: Wed, 23 Sep 2015 13:04:15 -0700 From: Andrew Morton To: Alexey Dobriyan Cc: Linux Kernel , Michal Nazarewicz , Peter Zijlstra , john.stultz@linaro.org, Masami Hiramatsu , Ingo Molnar , Peter Zijlstra , Steven Rostedt , Linus Torvalds Subject: Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree Message-Id: <20150923130415.561f5f13ed8fa64e2c79e2f2@linux-foundation.org> In-Reply-To: References: <55fb3266.yHuY6+DaYTJdIL75%akpm@linux-foundation.org> X-Mailer: Sylpheed 3.4.1 (GTK+ 2.24.23; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 23 Sep 2015 16:44:29 +0300 Alexey Dobriyan wrote: > On Fri, Sep 18, 2015 at 12:36 AM, wrote: > > > > -#define abs(x) ({ \ > > - long ret; \ > > - if (sizeof(x) == sizeof(long)) { \ > > - long __x = (x); \ > > - ret = (__x < 0) ? -__x : __x; \ > > - } else { \ > > - int __x = (x); \ > > - ret = (__x < 0) ? -__x : __x; \ > > - } \ > > - ret; \ > > - }) > > +#define abs(x) __builtin_choose_expr(sizeof(x) == sizeof(s64), ({ \ > > + s64 __x = (x); \ > > + (__x < 0) ? -__x : __x; \ > > + }), ({ \ > > + long ret; \ > > + if (sizeof(x) == sizeof(long)) { \ > > + long __x = (x); \ > > + ret = (__x < 0) ? -__x : __x; \ > > + } else { \ > > + int __x = (x); \ > > + ret = (__x < 0) ? -__x : __x; \ > > + } \ > > + ret; \ > > + })) > > 1. "char" should be banned, because it's signedness is not well defined. > 2. there is unnecessary expansion to long. > > I've sent kabs() before which didn't go in because it didn't work for > INT_MAX et al > (don't worry, this abs() doens't as well) but it is nicer that this > version in other aspects > (hopefully). > > [PATCH v2] Add kabs() > http://marc.info/?l=linux-kernel&m=133518745522740&w=4 Looks nice. Care to send along a patch which updates the abs() in linux-next? > As for INT_MIN, it pretty impossible to make compiler to not generate a branch > despite generating correct result without a branch.