From owner-freebsd-arm@FreeBSD.ORG Tue May 5 07:04:21 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 3E985106566B for ; Tue, 5 May 2009 07:04:21 +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 E845C8FC08 for ; Tue, 5 May 2009 07:04:20 +0000 (UTC) (envelope-from venkiece2005@gmail.com) Received: by yw-out-2324.google.com with SMTP id 9so2693246ywe.13 for ; Tue, 05 May 2009 00:04:20 -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=4A2omUMQ3FYJ14ULsEDvkx44+rTDYKpvfr0zPs1zFL0=; b=uczXDVOrI4ZKbTVo9b37DhSDlOhgxbig9XHhbzYFR+VkNagdo3AahUEFTPhzg2K+QI tYbuf8MRCfubA8R5jSBcQQT2xRFGzhqWDGndXIvbyildawUJg/BdCKV/ccq/sC9QIbJm VD0wUQhPcgZno0qtVOaP1lCDnBWTUHG9hgmMM= 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=GZDPk7uuQfJTjwyZLaExhXbf6L1uGmUIzBEETEXEnjEEhXGThjpeyFxxvA7WQDraWQ glDWDUSlOQ5/PeawZIXKziZwh9yTt1I8ZMhvjNWNaMH/3waL804TZt1vOenseZRKcw7f 8ihfVe5lvus7tvRNRWQ2vZduHh9Ur2DOrWddA= MIME-Version: 1.0 Received: by 10.151.149.6 with SMTP id b6mr13763509ybo.47.1241507060202; Tue, 05 May 2009 00:04:20 -0700 (PDT) In-Reply-To: <200905041259.n44CxtV2077931@casselton.net> References: <515c64960905032352j25edfbcajb3d146fa769b97bb@mail.gmail.com> <200905041259.n44CxtV2077931@casselton.net> Date: Tue, 5 May 2009 12:34:20 +0530 Message-ID: <6d53329e0905050004k27d957bve15d37113b5fedbb@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 07:04:21 -0000 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 > >