Thread: Why only boolean? Why not quadlean?

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

    Why only boolean? Why not quadlean?

    No, I'm not crazy!!

    This thread (if statements in c++ compared to java) got me thinking about boolean conditions. I know what boolean logic is, and I'm very comfortable with boolean logic. But I wonder if the higher level languages are selling themselves short?

    He's my thought process.

    I work on mainframes daily. I code a LOT in assembler. In assembler, as some of you must know, you work with condition codes set by individual instructions. On the mainframe, there are two bits used to hold the condition code of condition-code-setting instructions. With these 2 bits you get 4 combinations ("quad") of conditions to test for. Now, not all instructions set all possible conditions, but a lot do, and in assembler, you get to take the branch you need after you've tested for the condition code you desire.

    Why is it that this concept of 4 conditions codes (aka 4 possible code paths) has been dumbed down into a true or false in higher level languages?

    In assembler to compare two integers, you just compare them. Then, you branch if equal, less than, greater than, or not equal. In C, for example, you compare against EQ, LT, GT, or NE. If I want to test EQ, LT and GT, then that's two compares I have to code. In asm, it would be one compare and then two possible branches. Simpler.

    Or, with the above scenario, is the compiler smart enough to see the multiple compares and generate the code that does a single assembler compare and then branch accordingly?
    Mainframe assembler programmer by trade. C coder when I can.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dino View Post
    Or, with the above scenario, is the compiler smart enough to see the multiple compares and generate the code that does a single assembler compare and then branch accordingly?
    Yes. This sort of optimization is typically done midway through code optimization. The condition flags can be represented as boolean temporaries, and the compiler is aware that more than one temporary can be set by a single comparison. The redundant comparisons can then be eliminated during data flow analysis.

    It's actually a fairly easy optimization to make.

    And of course, the proof:

    Code:
    void a();
    void b();
    void c();
    
    void foo( int x )
    {
        if( x < 0 ) a();
        else if( x == 0 ) b();
        else c();
    }
    Compiled with gcc -O3 -S:

    Code:
    	.file	"foo.c"
    	.text
    	.p2align 4,,15
    .globl foo
    	.type	foo, @function
    foo:
    	pushl	%ebp
    	movl	%esp, %ebp
    	cmpl	$0, 8(%ebp)
    	jl	.L9
    	je	.L10
    	popl	%ebp
    	jmp	c
    	.p2align 4,,7
    .L10:
    	popl	%ebp
    	.p2align 4,,6
    	jmp	b
    	.p2align 4,,7
    .L9:
    	popl	%ebp
    	jmp	a
    	.size	foo, .-foo
    	.ident	"GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
    	.section	.note.GNU-stack,"",@progbits
    (The code is somewhat odd-looking because of the compiler's tail-call elimination, but you can see that the comparison only happens once)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Dino View Post
    Or, with the above scenario, is the compiler smart enough to see the multiple compares and generate the code that does a single assembler compare and then branch accordingly?
    Brewbuck proved this. But...

    Quote Originally Posted by Dino View Post
    In assembler to compare two integers, you just compare them. Then, you branch if equal, less than, greater than, or not equal. In C, for example, you compare against EQ, LT, GT, or NE. If I want to test EQ, LT and GT, then that's two compares I have to code. In asm, it would be one compare and then two possible branches. Simpler.
    With higher level languages, comes higher-level expression

    The way boolean logic is expressed in higher-level languages more closely mimics the usual problem domains; i.e you more often than not want to test for one relationship between two values and then operate on whether the test returns true or false.

    Other language constructs can then be used if you need a one-stop comparison. That is, it's fairly easy to emulate assembly on this particular matter. A simple function can return all possible relationships in the form of a integer, for instance.

    In this sense, high-level programming languages aren't selling themselves short by not implementing a similar construct to assembly. Naturally they are if their compilers do not optimize, but that aside and purely at the syntactic level such a construct could easily be deemed unnecessary since it could be implemented by the user, using the regular comparison operators.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You mean like this?

    Code:
    enum Qualean
    {
        True,
        False,
        Maybe,
        MaybeNot
    };
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Or maybe even:

    Code:
    
    enum Quadlean
    {
          False, 
          True,
          Indeterminate,
          Undecided
    };

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    OK, very nice. Thanks brew for the example, and Mario and cp too.

    And actually, this would be quite fun too:
    Code:
    enum Qualean
    {
        True,
        False,
        IfYouAreLucky,
        OnOddThursdays,
        Surprise,
    };
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The idea however would be something like this:

    Code:
    enum Qualean
    {
        INVALID,
        GT,
        EQ,
        LT
    };
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Reminds me of a Magic 8 Ball... a ton of different, cryptic answers that ultimately only makes you ponder if it's saying 'Yes' or 'No.' Hence why computers have always chosen to reduce everything down to George Boole's flawless logic.
    Sent from my iPadŽ

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Dino View Post
    OK, very nice. Thanks brew for the example, and Mario and cp too.

    And actually, this would be quite fun too:
    Code:
    enum Qualean
    {
        True,
        False,
        IfYouAreLucky,
        OnOddThursdays,
        Surprise,
    };
    This is more fun -

    Code:
    enum Qualean {
       Blonde,
       Brunnette,
       Redhead,
       Bald
       }
    Last edited by abachler; 11-21-2009 at 02:41 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Grr... not enough states to accomodate those of us with black hair.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by laserlight View Post
    Grr... not enough states to accomodate those of us with black hair.
    You need Quintilean state logic for that and I couldnt think of a one word state name, but there's always room in my heart for a black haired beauty

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by abachler
    I couldnt think of a one word state name
    Yes, blackhead has a very different meaning from redhead!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    Yes, blackhead has a very different meaning from redhead!
    LOL!!! Maybe you could try the German version "Schwarzkopf" which also means "black head", but maybe it doesn't have the same connotation in German as it does in English?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Awe, this would be my favorite enum list
    Code:
    enum Qualean {
       Redhead,
       Redhead,
       Redhead,
       Redhead
       }
    Mainframe assembler programmer by trade. C coder when I can.

  15. #15
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Dino View Post
    Awe, this would be my favorite enum list
    Code:
    enum Qualean {
       Redhead,
       Redhead,
       Redhead,
       Redhead
       }
    Variety is the Spice of life, no way are 4 redheads going to get along with each other unless you find 4 twin sisters

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. Casting boolean as string
    By doofusboy in forum C Programming
    Replies: 11
    Last Post: 11-10-2005, 12:24 PM
  3. Replies: 4
    Last Post: 04-02-2004, 07:30 PM
  4. Working with boolean...
    By CompiledMonkey in forum C Programming
    Replies: 4
    Last Post: 11-03-2003, 10:39 AM
  5. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM