From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id ; Thu, 30 Aug 2001 17:18:57 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id ; Thu, 30 Aug 2001 17:18:51 -0400 Received: from chaos.analogic.com ([204.178.40.224]:58240 "EHLO chaos.analogic.com") by vger.kernel.org with ESMTP id ; Thu, 30 Aug 2001 17:18:09 -0400 Date: Thu, 30 Aug 2001 17:17:50 -0400 (EDT) From: "Richard B. Johnson" Reply-To: root@chaos.analogic.com To: Herbert Rosmanith cc: linux-kernel@vger.kernel.org, ptb@it.uc3m.es Subject: Re: [IDEA+RFC] Possible solution for min()/max() war In-Reply-To: <200108302044.f7UKi7c20040@wildsau.idv-edu.uni-linz.ac.at> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 30 Aug 2001, Herbert Rosmanith wrote: > > > if sizeof(typeof(a)) != sizeof(typeof(b)) > > BUG() // sizes differ > > this is not neccessarily a problem. should work with char/short char/int > short/int comparison. > > only problem seems to be signed/unsigned int comparison. > > > const (typeof(a)) _a = ~(typeof(a))0 > > const (typeof(b)) _b = ~(typeof(b))0 > > if _a < 0 && _b > 0 || _a > 0 && b < 0 > > BUG() // one signed, the other unsigned > > standard_max(a,b) > > if sizeof(typeof(a))==sizeof(int) && sizeof(typeof(b))==sizeof(int) && > ( _a < 0 && _b > 0 || _a > 0 && b < 0 ) > BUG() // signed unsigned int compare > The problem really can't be solved with macros. Here is a little script that you can run, which shows that some versions of gcc don't even perform macro-expansion correctly. SNIP------- #!/bin/bash cat >/tmp/xxx.c < #undef MIN #define MIN(a, b) ((unsigned int)(a) < (unsigned int)(b) ? (a) : (b)) int main(void); int main() { int i; unsigned int j; i = j = 0; printf("%08x\n", MIN(i, j)); return 0; } EOF gcc -Wall -Wsign-compare -c -o /dev/null /tmp/xxx.c rm -f /tmp/xxx.c gcc --version SNIP------ Here's a "good" execution: Script started on Thu Aug 30 17:00:10 2001 # sh -v xxx.sh #!/bin/bash cat >/tmp/xxx.c </tmp/xxx.c <