Joachim B Haga <[EMAIL PROTECTED]> wrote on 9 Dec 2007 1:23:
> It looks like it could be significantly faster in the general case, but
> possibly not if most of the compared times differ in the tv_sec field.
> 
> A non-macro variant could be something like
>... 

Good idea. You may help current compilers/processors with an explicit branch 
prediction. But 
your code is not bad, because it creates the same output ;-)

#include <sys/cdefs.h>
#include <sys/time.h>

int
  timeval_before(struct timeval *t1, struct timeval *t2)
  {
      return (t1->tv_sec != t2->tv_sec ?
              t1->tv_sec  < t2->tv_sec :
              t1->tv_usec < t2->tv_usec);
  }

int
  timeval_before2(struct timeval *t1, struct timeval *t2)
  {
        if ( __predict_true(t1->tv_sec != t2->tv_sec))
              return (t1->tv_sec  < t2->tv_sec);
        else
              return (t1->tv_usec < t2->tv_usec);
  }

#gcc -O2 -march=pentium4 -Wall -fomit-frame-pointer -c -S tst.c
#cat tst.s
        .file   "tst.c"
        .text
.globl timeval_before
        .type   timeval_before, @function
timeval_before:
        movl    4(%esp), %edx
        movl    8(%esp), %ecx
        movl    (%ecx), %eax
        cmpl    %eax, (%edx)
        je      .L2
        setl    %al
        movzbl  %al, %eax
        ret
.L2:
        movl    4(%edx), %eax
        cmpl    4(%ecx), %eax
        setl    %al
        movzbl  %al, %eax
        ret
        .size   timeval_before, .-timeval_before
.globl timeval_before2
        .type   timeval_before2, @function
timeval_before2:
        movl    4(%esp), %edx
        movl    8(%esp), %ecx
        movl    (%ecx), %eax
        cmpl    %eax, (%edx)
        je      .L8
        setl    %al
        movzbl  %al, %eax
        ret
.L8:
        movl    4(%edx), %eax
        cmpl    4(%ecx), %eax
        setl    %al
        movzbl  %al, %eax
        ret
        .size   timeval_before2, .-timeval_before2
        .ident  "GCC: (GNU) 4.2.1 20070719  [FreeBSD]"

The prediction macros in cdefs.h are defined as
#define __predict_true(exp)     __builtin_expect((exp), 1)
#define __predict_false(exp)    __builtin_expect((exp), 0)

-- 
Frank Behrens, Osterwieck, Germany
PGP-key 0x5B7C47ED on public servers available.


-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Polipo-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/polipo-users

Reply via email to