From owner-freebsd-arm@FreeBSD.ORG Tue May 5 10:20:35 2009 Return-Path: Delivered-To: freebsd-arm@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id F3EB91065677 for ; Tue, 5 May 2009 10:20:34 +0000 (UTC) (envelope-from venkiece2005@gmail.com) Received: from yw-out-2324.google.com (yw-out-2324.google.com [74.125.46.31]) by mx1.freebsd.org (Postfix) with ESMTP id A24158FC0A for ; Tue, 5 May 2009 10:20:34 +0000 (UTC) (envelope-from venkiece2005@gmail.com) Received: by yw-out-2324.google.com with SMTP id 9so2737538ywe.13 for ; Tue, 05 May 2009 03:20:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=jLmX1Ut4P9DY8eheuYxwCxuUuNfciehRI1F7NXR8P/4=; b=x+GPa8HE81AzQ/SqCcOJ99PKZ+00MY9c5Vr1a5Wze3sCjF9m/x9qqh0UGJyrT+oKHf xAO8UjpLni7T4Q0S4R6HdRAAiEVoxpoF+qu+9aKk92pnBDUkkCx4YBWKilDDNg0nadK2 VTq0GpyjoPEWvEPQGWVgaz2u0G6cSvokzmZg4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=VIZOAbgHxMjTlR83y37a0fJTQZZpZosfFUAKeLaRGQOpH5a4rrcrFnXFsiqVSwScWK S1EBdXkadAwjOGo/R2Yl71IQJ0XGa9rqs1jNhL+mJzUnYWvJW9yRf8X+A+EcQof/Z/XM ykIx6ZsOKZpUzeSJ9DqHHWvUNuIJPUohsgTkU= MIME-Version: 1.0 Received: by 10.150.139.15 with SMTP id m15mr14051576ybd.22.1241518833887; Tue, 05 May 2009 03:20:33 -0700 (PDT) In-Reply-To: <6d53329e0905050004k27d957bve15d37113b5fedbb@mail.gmail.com> References: <515c64960905032352j25edfbcajb3d146fa769b97bb@mail.gmail.com> <200905041259.n44CxtV2077931@casselton.net> <6d53329e0905050004k27d957bve15d37113b5fedbb@mail.gmail.com> Date: Tue, 5 May 2009 15:50:33 +0530 Message-ID: <6d53329e0905050320q5c7480e9h4bcb629222fce16@mail.gmail.com> From: venki kaps To: Mark Tinguely Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: channa.kad@gmail.com, freebsd-arm@freebsd.org Subject: Re: strncmp issue X-BeenThere: freebsd-arm@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Porting FreeBSD to the StrongARM Processor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 May 2009 10:20:35 -0000 Conditional compares are difficult but compilers are very good at: if (len == 0) return 0; cmp r2, 0 moveq r0, #0 moveq pc, lr while (r0 = *s1++, r1 = *s2++, ip = r0 + len; ip == r0 && r0 >= 1 && r0 == r1); add ip, r0, r2 loop ldrb r0, [s1],#1 ldrb r1, [s2],#1 cmp ip, r0 cmp r0,#1 cmpcs r0,r1 beq loop subroutine here Regards, Venkappa On Tue, May 5, 2009 at 12:34 PM, venki kaps wrote: > Hi, > > I have tested strncmp("abcdefg", "abcdefh", 6) without beq 2f. > It returns zero not -1. > > I have checked with conditional assembler but not normal assembler. > The beq 2f is required for normal assembler. > Right/Wrong? > > However also checked with strncmp.c implementation: > > int strnncmp(const char *s1, const char *s2, size_t n) { > if (n == 0) > return (0); > do { > if (*s1 != *s2++) > return (*(const unsigned char *)s1 - > *(const unsigned char *)--s2); > if (*s1++ == 0) > break; > } while (--n != 0); > return (0); > } > the equivalent assembly code: > > cmp r2, #0 > mov ip, r0 > beq 2f > 1: > ldrb r0, [ip, #0] > ldrb r3, [r1], #1 > add ip, ip, #1 > cmp r0, r3 > bne 3f > cmp r0, #0 > beq 2f > subs r2, r2, #1 > bne 1b > 2: > mov r0, #0 > mov pc, lr > 3: > ldrb r3, [r1, #-1] > rsb r0, r3, r0 /* operand2 - operand1 */ > mov pc, lr > > The results are same. > > what could you suggest? > > Thanks & Regards, > Venkappa > > On Mon, May 4, 2009 at 6:29 PM, Mark Tinguely wrote: > >> >> Hi, the propose code by both of you two: >> >> /* if (len == 0) return 0 */ >> cmp r2, #0 >> moveq r0, #0 >> moveq pc, lr >> >> /* ip == last src address to compare */ >> add ip, r0, r2 >> 1: >> ldrb r2, [r0], #1 >> ldrb r3, [r1], #1 >> cmp ip, r0 >> cmp r2, #1 >> cmpcs r2, r3 >> beq 1b >> sub r0, r2, r3 >> >> That was one of my failed attempts. I to was hoping to not add the branch >> to cut down in cycles. A person has to test every possible call to >> strncmp. >> This will fail on a positive string length less than strlen length of the >> input strings: >> >> strncmp("abcdefg", "abcdefh", 6) >> >> Will return (-1) str1 < str2 at 6 characters which is wrong. >> >> >> /* if (len == 0) return 0 */ >> cmp r2, #0 >> moveq r0, #0 >> moveq pc, lr >> >> /* ip == last src address to compare */ >> add ip, r0, r2 >> 1: >> ldrb r2, [r0], #1 >> ldrb r3, [r1], #1 >> cmp ip, r0 >> beq 2f <- stops in the case where strlen(s1) > len >> cmp r2, #1 <- stops thea NULL case, we can't just >> change >> the comparison because below we loop on >> an equality and can end up in a big loop >> >> cmpcs r2, r3 <- compare characters. >> beq 1b >> 2: >> sub r0, r2, r3 >> >> >