Berkeley CSUA MOTD:Entry 20990
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/25 [General] UID:1000 Activity:popular
5/25    

2001/4/16-17 [Computer/SW/Compilers] UID:20990 Activity:high
4/16    In a C statement like "if (foo > bar)" where foo is a int8 and
        bar is in32, how does the compiler translate that?  Does it turn
        it into a int32 to int32 comparison?  Or does it chop off bar and
        turn it into int8 to int8 comparison? If it does turn foo into an
        int32, where does it get the memory?  Compiler assign it a new
        register?  If so, is there a possibility that you run out of registers
        if you have lot of comparisons of different sizes?  Thanks.
        \_ 1. How a person writes a program has no bearing on how it will
           look like in the procedure stack. If a function is a leaf
           function and uses less registers than is actually available then
           there's no need for a new procedure stack. Where does it get the
           memory? The compiler figures this out.
           2. Yes, you can run out of registers. What's the solution?
           It's called register allocation.  There exists a myriad of
           algorithms to deal with this issue. Take 164 and you'll learn
           all about them. Not everything in a function fits in the register
              ints means "store in register and allow the automatic sign
              extension to happen". don't let the above comment make you
              think that int8<>int32 comparisons are somehow less efficient.
           file.
           \_ Uh, 164 with Savage Sue barely touched on register allocation.
        \_ C does type promotion.  In this case, foo will end up being a
           32-bit value that is the same, numerically, as the original
           8-bit value.
        \_ It creates a temporary int32 for the comparison.  The temporary
           value is normally stored in a register, but it will end up in
           memory instead if you run out of registers.
           \_ note that this doesn't mean shit in a modern architecture.
              your registers are 32 bits wide at least already, so that
              additional 24 bits are already wasted. type promotion of
              ints is actually a noop in a modern architecture: it's already
              stored as a sign extended 32 bit value. don't let the above
              comment make you think that int8<>int32 comparisons are somehow
              less efficient.
              \_ They are less efficient.  ALU operations don't automatically
                 sign extend 8-bit values in a register.  You need to do that
                 separately. For example, on a Sun:
                 foo.c
                 -----
                 int foo(char c, int i) { return c + i; }
                 int bar(int i1, int i2) { return i1 + i2; }
                 when you compile this with the -S option the two functions
                 are almost the same except:
                         stb     %o0, [%fp-17]
                         ldub    [%fp-17], %o0
                         sll     %o0, 24, %o1
                         sra     %o1, 24, %o0
                         ld      [%fp+72], %o1
                 used to perform the sign extension
                 \_ can you turn on -O  and rerun this, please?
                    \_ Similar results, though much shorter.  For both Sun
                       cc and gcc, foo produces a sll and sra by 24 but
                       not for bar. So yes, C promotion does incur overhead.
                       foo:
                       sll %o0, 24, %o0
                       sra %o0, 24, %o0
                       retl
                       add %o0, %o1, %o0
                       bar:
                       retl
                       add %o0, %o1, %o0
                       \_ i based my initial assertion on the fact that
                          many architectures provide a load byte instruction
                          which does sign extension on load to register.
2025/05/25 [General] UID:1000 Activity:popular
5/25    

You may also be interested in these entries...
2014/1/14-2/5 [Computer/SW/Languages/C_Cplusplus] UID:54763 Activity:nil
1/14    Why is NULL defined to be "0" in C++ instead of "((void *) 0)" like in
        C?  I have some overloaded functtions where one takes an integer
        parameter and the other a pointer parameter.  When I call it with
        "NULL", the compiler matches it with the integer version instead of
        the pointer version which is a problem.  Other funny effect is that
        sizeof(NULL) is different from sizeof(myPtr).  Thanks.
	...
2011/3/7-4/20 [Computer/SW/Languages/C_Cplusplus] UID:54056 Activity:nil
3/7     I have a C question.  I have the following source code in two identical
        files t.c and t.cpp:
                #include <stdlib.h>
                int main(int argc, char *argv[]) {
                  const char * const * p1;
                  const char * * p2;
	...
2010/1/22-30 [Computer/HW/Laptop, Computer/SW/OS/OsX] UID:53655 Activity:high
1/22    looking to buy a new development laptop
        needs ssdrive, >6 hr possible batt life, and runs linux reasonably
        Anyone have a recommendation? Thx.
        \_ thinkpad t23 w ssdrive and battery inplace of drive bay
        \_ Ever wondered what RICHARD STALLMAN uses for a laptop?  Well,
           wonder no more!
	...
2009/8/31-9/9 [Computer/SW/Compilers] UID:53312 Activity:nil
8/31    I'm trying to learn ActionScript, like a step by step tutorial.
        The site at http://www.actionscript.org/resources/categories/Tutorials/Flash/Beginner
        isn't well organized. It doesn't explain how to get started with
        an editor, compiler, IDE. And should I even learn AS2 when you can
        learn AS3? Is Adobe Flash CS4 >>> CS3 or just CS4 > CS3?
	...
2009/2/28-3/11 [Computer/SW/Compilers] UID:52661 Activity:nil
2/28    I'm looking for a recommendation of a compiler/IDE to use to
        develop C/C++ code under Linux. In school, we used jove/gcc and
        I still use emacs/vi and gcc to this day. However, it is really
        lacking. Under Windows I tried Visual Studio and there were some
        really nice things about it, although it was so overwhelming that
        after 6 months of occasional use I still didn't really know what I
	...
2008/6/9-12 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Security] UID:50194 Activity:nil
6/8     CSUA code guru please help. I need to see my random number
        generator with a good seed (I just need random 18 bit
        identifiers). The usual time(NULL) is OK, except my program
        might be invoked faster than once a second, and seeding using
        time() produced the same result. I tried clock() but it seems
        to return 0. My program needs to be run in Linux/DOS (Watcom
	...
2008/5/2-8 [Computer/SW/Compilers] UID:49874 Activity:low
5/2     How do I get the L1/L2 cache size and cache line size on my machine?
        Can I find this stuff out at compile time somehow?
        \_ You aren't planning on running your code on any other processors?
        \_ May I ask what it is you want to achieve ultimately? If you don't
           know your architecture and want to find out dynamically, there are
           tools that can peek/poke to give you definitive answers, plus you get
	...
2008/4/2-6 [Computer/SW/Languages/C_Cplusplus] UID:49645 Activity:moderate
4/2     Is there an interpreted version of C or C++ that can be used for
        educational purposes? It doesn't have to be full-featured or
        strictly adhere to the standards, but it's painful for students
        to change a variable in a for loop and then wait for a compile
        to see how it changes the result. Something really lightweight
        would encourage them to play around a lot more and learn more in
	...
2007/11/30-12/6 [Computer/SW/Compilers, Computer/HW/CPU] UID:48719 Activity:moderate
11/29   From the CSUA minutes:
        - Next Gen Console
        -- If we have $1800 in our accounts, should we buy a console:
           4 votes passes.
        -- Console voting: 2 votes each, neither passes
           * 360 = 600, more games
	...
2007/11/27-30 [Computer/SW/Languages/C_Cplusplus, Computer/SW/OS/Solaris] UID:48701 Activity:high
11/27   I'm using select to do a nonblocking check to see if a single socket
        has anything to read off it.  Problem is, I can have up to 12228
        file descriptors, and Linux fd_set only supports up to 4096.  Any idea
        what I can do about this?  (Or a better solution?) -jrleek
        \- 1. who are you
           2. i am busy this week and you didnt mention language
	...
2006/11/10-12 [Computer/SW/Compilers] UID:45316 Activity:nil
11/10   Is there anyway to get C/C++ compilers to automatically compile
        different code for different processors?  I'd like to be able to
        say something like:
          #if defined X86 ...
          #elif defined SPARC ...
          #else ...
	...