From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27195 invoked by alias); 19 Feb 2008 18:49:27 -0000 Received: (qmail 27182 invoked by uid 22791); 19 Feb 2008 18:49:26 -0000 X-Spam-Check-By: sourceware.org Received: from mail.artimi.com (HELO mail.artimi.com) (194.72.81.2) by sourceware.org (qpsmtpd/0.31) with ESMTP; Tue, 19 Feb 2008 18:49:07 +0000 Received: from mail.artimi.com ([192.168.1.3] RDNS failed) by mail.artimi.com with Microsoft SMTPSVC(6.0.3790.3959); Tue, 19 Feb 2008 18:49:04 +0000 Received: from rainbow ([192.168.8.46]) by mail.artimi.com with Microsoft SMTPSVC(6.0.3790.3959); Tue, 19 Feb 2008 18:49:04 +0000 From: "Dave Korn" To: "'David Daney'" Cc: References: <5800c1cc0802162157g3ac31acas4ae95585b9b2e263@mail.gmail.com><5800c1cc0802170641g68ab2e5fte724cd076412333@mail.gmail.com><20080217152625.GA4810@caradoc.them.org><47BB03ED.5060708@baymicrosystems.com><007f01c87324$00f45a90$2e08a8c0@CAM.ARTIMI.COM> <18363.7810.91075.314867@gargle.gargle.HOWL> <008001c87325$6db805e0$2e08a8c0@CAM.ARTIMI.COM> <47BB2168.7090600@avtrex.com> Subject: RE: (len % 1) != 0 Date: Tue, 19 Feb 2008 19:08:00 -0000 Message-ID: <008101c87328$19bde2e0$2e08a8c0@CAM.ARTIMI.COM> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <47BB2168.7090600@avtrex.com> Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2008-02/txt/msg00138.txt.bz2 On 19 February 2008 18:35, David Daney wrote: > Dave Korn wrote: >> On 19 February 2008 18:23, Paul Koning wrote: >> >>>>>>>> "Dave" == Dave Korn writes: >>> Dave> On 19 February 2008 16:30, Sheng-Liang Song wrote: >> or >>> >> >>> >> (len & 1) != 0 <=> (len % 2) != 0 >>> >> >>> >>> Dave> That would have the advantage of not requiring a divide Dave> >>> operation :) >>> >>> It shouldn't matter -- the optimizer will do the right thing, as I >>> recall. >> >> NB len == signed int. >> >> Yes, it can simplify it to a bunch of shifts and sign extends and masks >> without using an explicit divide, but it's not as good as a simple AND >> operation. (Maybe VRP in 4.x could handle that by knowing that the result >> of strlen has to be >= 0, but 3.x series won't do it). >> > > ?? Not to be pedantic, but on 3.4.3 for mipsel-linux I get: > > $ cat j.c > int f1 (int a) > { > return (a % 2) != 0; > } > $ mipsel-linux-gcc -c -O3 j.c > $ mipsel-linux-objdump -d j.o > > j.o: file format elf32-tradlittlemips > > Disassembly of section .text: > > 00000000 : > 0: 03e00008 jr ra > 4: 30820001 andi v0,a0,0x1 Ah, it's the !=0 that allows the compiler to go that last step of the way, I was just testing int bar (int len) { return len % 2; } int baz (int len) { return len & 1; } Hmm, 3.4.4 on x86 still doesn't optimise it quite as well: /artimi/boards $ gcc -xc -S -oo.s -O2 - int bar (int len) { return len % 2; } int baz (int len) { return len & 1; } int bat (int len) { return (len % 2) != 0; } int quux (int len) { return (len & 1) != 0; } /artimi/boards $ cat o.s .file "" .text .globl _bar .def _bar; .scl 2; .type 32; .endef _bar: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax popl %ebp movl %eax, %edx shrl $31, %edx leal (%eax,%edx), %edx andl $-2, %edx subl %edx, %eax ret .p2align 4,,15 .globl _baz .def _baz; .scl 2; .type 32; .endef _baz: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax popl %ebp andl $1, %eax ret .p2align 4,,15 .globl _bat .def _bat; .scl 2; .type 32; .endef _bat: pushl %ebp xorl %eax, %eax movl %esp, %ebp testb $1, 8(%ebp) popl %ebp setne %al ret .p2align 4,,15 .globl _quux .def _quux; .scl 2; .type 32; .endef _quux: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax popl %ebp andl $1, %eax ret /artimi/boards/Kitsman/Test Boards/A200_TPCB002 $ cheers, DaveK -- Can't think of a witty .sigline today....