delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1998/03/17/08:02:21

From: loth AT gec DOT net (Burton Radons)
To: djgpp-workers AT delorie DOT com
Subject: libc replacement (memcmp)
Date: Tue, 17 Mar 1998 12:53:21 GMT
Message-ID: <350f6b73.34099775@mail.gec.net>
MIME-Version: 1.0

memcmp used to compare using char-by-char, so I replaced it with
int-by-int -- quick optimizations, but it hadn't been done in three
years, so... bcmp follows this message.  

350% speed increase in memcmp at peak (12 bytes and up), never slower,
reacts the same as before.

- Burton Radons, loth AT gec DOT net
Vancouver Island, British Columbia, Canada

*** src/new/memcmp.c	Tue Mar 17 00:41:56 1998
--- src/libc/ansi/string/memcmp.c	Tue Nov 29 04:41:36 1994
***************
*** 1,41 ****
! /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
  #include <string.h>
  
  int
  memcmp(const void *s1, const void *s2, size_t n)
  {
!    if (n != 0)
!    {
!       const unsigned char *p1 = s1;
!       const unsigned char *p2 = s2;
! 
!       while (n > 4) {
!          if (*((int *) p1) != *((int *) p2)) {
!             if (*((short *) p1) != *((short *) p2)) {
!                if (*p1 != *p2)
!                   return (*p1 - *p2);
! 
!                return (*++p1 - *++p2);
!             }
! 
!             p1 += 2;
!             p2 += 2;
! 
!             if (*p1 != *p2)
!                return (*p1 - *p2);
! 
!             return (*++p1 - *++p2);
!          }
! 
!          p1 += 4;
!          p2 += 4;
!          n -= 4;
!       }
! 
!       for (; n; n --)
!          if (*p1++ != *p2++)
!             return *--p1 - *--p2;
!    }
! 
!    return 0;
  }
--- 1,17 ----
! /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
  #include <string.h>
  
  int
  memcmp(const void *s1, const void *s2, size_t n)
  {
!   if (n != 0)
!   {
!     const unsigned char *p1 = s1, *p2 = s2;
! 
!     do {
!       if (*p1++ != *p2++)
! 	return (*--p1 - *--p2);
!     } while (--n != 0);
!   }
!   return 0;
  }

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019