bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: large integer truncation in regex module


From: Paul Eggert
Subject: Re: large integer truncation in regex module
Date: Sun, 27 May 2012 21:57:11 -0700
User-agent: Mozilla/5.0 (X11; Linux i686; rv:12.0) Gecko/20120430 Thunderbird/12.0.1

On 05/27/2012 05:55 PM, Steven M. Schweda wrote:
> it's not immediately obvious (to me) that the
> code for two shifts and an OR will necessarily be more efficient (in
> space or time) than the code for an assignment

Any decent compiler will generate exactly the same
code for both.  No shifts or OR will be done at
runtime -- they'll all be done at compile time.
For example:

#include <limits.h>                                                             
unsigned long int x;                                                            
unsigned long int y;                                                            
                                                                                
void f (void) {                                                                 
  unsigned long int a = 0x87fffffe;                                             
  unsigned long int b = 0x07fffffe;                                             
  if (ULONG_MAX >> 31 >> 31 == 3)                                               
    x = a << 31 << 1 | b;                                                       
  else {                                                                        
    x = a;                                                                      
    y = b;                                                                      
  }                                                                             
}                                                                               
void g (void) {                                                                 
#if ULONG_MAX >> 31 >> 31 == 3                                                  
  x = 0x87fffffe07fffffe;                                                       
#else                                                                           
  x = 0x87fffffe;                                                               
  y = 0x07fffffe;                                                               
#endif                                                                          
}

This generates exactly the same machine code for 'f' as for 'g'
on both x86-64 and x86 (with GCC 4.7.0 -O2).

> It's not absolutely clear that "Compaq C V6.4-005 on OpenVMS VAX
> V7.3" qualifies as "modern"

It's a little long in the tooth, as it was released over
ten years ago.  But you can try it out on the above code;
by compiling with optimization enabled, and
looking at the resulting assembly language code.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]