Announcement

Collapse
No announcement yet.

Numeric equate expressions pre-calculated?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Numeric equate expressions pre-calculated?

    The following is from the PBCC Help:
    Numeric equates which are derived from an equation are pre-calculated by PowerBASIC during the compilation process, to ensure that unnecessary calculations are eliminated from the executable code. If this optimization was not performed, PowerBASIC code would need to perform the same calculation every time the equate was used in the code.
    Is it true? Try compiling the following twice, once with only the 1st Print (the 2nd commented out), once with the 2nd Print (the 1st commented out), and each time look at the size of the exe. The 1st (longer) Print gives a larger executable, about 500 bytes larger. Apparently the “pre-calculated” in the Help refers to expressions in the equate not expressions in the code itself.
    Code:
    %a0  =  0
    %a1  =  1
    %a2  =  2
    %a3  =  3
    %a4  =  4
    %a5  =  5
    %a6  =  6
    %a7  =  7
    %a8  =  8
    %a9  =  9
    %a10 = 10
    %a11 = 11
    %a12 = 12
    %a13 = 13
    %a14 = 14
    %a15 = 15
    %a16 = 16
    %a17 = 17
    %a18 = 18
    %a19 = 19
    %a20 = 20
    
    %s = %a0*2+%a1*3+%a2*3+%a3*4+%a4*4+%a5*5+%a6*6+%a7*7+%a8*7+%a9*6+%a10*3+%a11*2+%a12*3+%a13*4+%a14*5+%a15*6+%a16*7+%a17*6+%a18*5+%a19*3+%a20*9
    
    Function PBMain
    ' print %a0*2+%a1*3+%a2*3+%a3*4+%a4*4+%a5*5+%a6*6+%a7*7+%a8*7+%a9*6+%a10*3+%a11*2+%a12*3+%a13*4+%a14*5+%a15*6+%a16*7+%a17*6+%a18*5+%a19*3+%a20*9
     Print %s
     WaitKey$
    End Function
    Last edited by Mark Hunter; 19 Jan 2016, 09:16 PM.
    Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

  • #2
    Interesting.

    At the other extreme, it's clear that some equate-math is done at compile time because you can use it with #IF.
    "Not my circus, not my monkeys."

    Comment


    • #3
      The first print statement is composed of literals and constants known at compile time. Perhaps this is pre-calculated as well. Maybe we could intice someone who programs in assembler could verify.
      Last edited by Rickey Richardson; 19 Jan 2016, 09:50 PM. Reason: sorry. misread the message as having the same size.

      Comment


      • #4
        Numeric equates which are derived from an equation are pre-calculated by PowerBASIC during the compilation process, to ensure that unnecessary calculations are eliminated from the executable code. If this optimization was not performed, PowerBASIC code would need to perform the same calculation every time the equate was used in the code.
        The complete paragraph quote is as below. That quote does not apply to equations constructed of equates. It is of the equate value being the result of an equation.
        Numeric equates which are derived from an equation are pre-calculated by PowerBASIC during the compilation process, to ensure that unnecessary calculations are eliminated from the executable code. If this optimization was not performed, PowerBASIC code would need to perform the same calculation every time the equate was used in the code. Examples of numeric equates derived from expressions follows:

        %WHATEVER1 = 10
        %WHATEVER2 = (%WHATEVER1 * 3) + 1
        %DEBUG = -1&
        %RELEASE = NOT %DEBUG
        %DEMO = %RELEASE AND (NOT %DEBUG)
        I don't see how to read that any differently.
        Rod
        In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

        Comment


        • #5
          You also see the difference if you use just numbers in the expression. The exe for the 1st Print below is about 500 bytes larger than for the 2nd:
          Code:
          %t = 0*2+1*3+2*3+3*4+4*4+5*5+6*6+7*7+8*7+9*6+10*3+11*2+12*3+13*4+14*5+15*6+16*7+17*6+18*5+19*3+20*9
          
          Function PBMain
           Print 0*2+1*3+2*3+3*4+4*4+5*5+6*6+7*7+8*7+9*6+10*3+11*2+12*3+13*4+14*5+15*6+16*7+17*6+18*5+19*3+20*9
          ' Print %t
           WaitKey$
          End Function
          Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

          Comment


          • #6
            "Numeric equates which are derived ..."

            "%s = ..." is an equate derived from what is to the right of the equal sign.

            "PRINT ..." is a statement. What follows PRINT in your example just happens to have only equates instead of at least some variables.

            Help is precisely correct.

            Cheers,
            Dale

            Comment


            • #7
              In programming jargon everything is a “statement,” including Print ... .

              However unlike with Print the following does give the same size exe for each “n = ...
              Code:
              %t = 0*2+1*3+2*3+3*4+4*4+5*5+6*6+7*7+8*7+9*6+10*3+11*2+12*3+13*4+14*5+15*6+16*7+17*6+18*5+19*3+20*9
              
              Function PBMain
               Dim n As Long
               n = 0*2+1*3+2*3+3*4+4*4+5*5+6*6+7*7+8*7+9*6+10*3+11*2+12*3+13*4+14*5+15*6+16*7+17*6+18*5+19*3+20*9
              ' n = %t
               WaitKey$
              End Function
              Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

              Comment


              • #8
                The 1st (longer) Print gives a larger executable, about 500 bytes larger.
                Of course it is. The first PRINT statement has to calculate something, the second doesn't.

                As Dale Yarker says, help is correct because this 'precalculation' statement only applies to equate definition.

                Or maybe you should look at what the compiler has to do .. the stuff you don't see, which is why you licensed a high level language compiler..

                Code:
                Function PBMain
                ' print %a0*2+%a1*3+%a2*3+%a3*4+%a4*4+%a5*5+%a6*6+%a7*7+%a8*7+%a9*6+%a10*3+%a11*2+%a12*3+%a13*4+%a14*5+%a15*6+%a16*7+%a17*6+%a18*5+%a19*3+%a20*9
                Creates a temp variable to hold the result of the expression to the right of PRINT. Calculates the value for that variable by following the operator precedence rules for each pair of operands, peforming an addition or multiplication for each pair. Formats that temp variable. Sends to screen.

                Code:
                 Print %s
                Creates a temp variable to hold the result of the expression to the right of PRINT.
                Calculates the value of that variable, which this time doesn't take quite so much work. Formats that temp variable. Sends to screen.

                While some code optimizers will find this kind of thing, the only one I have personally seen do this is the IBM mainframe COBOL compiler. In 2001, prices started at $40,000 up front + $9,000/year maintenance.

                It's been a while since I purchased a "new" license for a PowerBASIC compiler (there have always been deals for 'upgrades') but I'm pretty sure I didn't pay quite that much.

                MCM
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Originally posted by Michael Mattias View Post
                  The first PRINT statement has to calculate something, the second doesn’t.
                  We are referring to the code in my first post. At compile time the first print statement doesn't have to calculate anything, the compiler could do it – but apparently doesn’t.

                  The price of the IBM mainframe is irrelevant.

                  Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

                  Comment


                  • #10
                    >the compiler could do it – but apparently doesn’t.

                    The PB compiler "could" do overflow checking. It doesn't.

                    The PB compiler "could" support a true boolean data type. It doesn't.

                    The PB compiler "could" have native statements for both OLE or ODBC database access. It doesn't.

                    The PowerBASIC compiler "could" have MIN() and MAX() support an array argument. It doesn't.

                    The bottom line is, your PowerBASIC compiler works as documented. Deal with it.

                    MCM
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      > The PB compiler "could" support a true boolean data type. It doesn't.

                      (.)tfFlag AS SBIT *1

                      No?
                      "Not my circus, not my monkeys."

                      Comment


                      • #12
                        MCM’s list is a mixture of should and shouldn’ts. Overflow checking would slow down the executable. It’s a good thing the compiler doesn’t do overflow checking. On the other hand the compiler could and should have a Boolean type, at least in the sense of a synonym for Long, because it would make the source code clearer. (In my programs I use Macro Boolean = Long, but that doesn’t make Boolean highlighted or auto-capitalized.)

                        The documentation’s use of the word “pre-compute” is less than clear. Regardless of the documentation, it is reasonable to expect that a good compiler do simple arithmetic when it can instead of expecting you to do it.

                        The compiler advertises speed and leanness. Yet in some cases the compiler doesn’t do simple arithmetic on whole number constants, and the failure to do it results in a longer executable than necessary. We can’t say a slower executable because the only evidence of the bloat we have is when using Print.
                        Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

                        Comment


                        • #13
                          > The PB compiler "could" support a true boolean data type. It doesn't.

                          (.)tfFlag AS SBIT *1
                          No?
                          No.

                          Boolean has three possible values: TRUE, FALSE and NULL (or INDETERMINATE)
                          Code:
                          [LET] Boolvar = {TRUE|FALSE}
                          or 
                          SET BoolVar [TO|=] {TRUE|FALSE}
                          PB bit has only two possible values: zero and one.

                          MCM
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            https://en.wikipedia.org/wiki/Boolean_data_type
                            "Not my circus, not my monkeys."

                            Comment


                            • #15
                              Mark, I looked at the compiled exe and using PRINT (actually TXT.PRINT in my case) it does the calc at runtime (and in floating point to boot even tho it's integer math's). But using it in other situations like IF...THEN or as you noted assigning it to a LONG, it is pre-calculated. Only PRINT causes the runtime calculation, as far as I've seen.

                              Comment


                              • #16
                                Thanks, John, that’s good news.
                                Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

                                Comment


                                • #17
                                  Originally posted by Mark Hunter View Post
                                  We are referring to the code in my first post. At compile time the first print statement doesn't have to calculate anything, the compiler could do it – but apparently doesn’t.

                                  The price of the IBM mainframe is irrelevant.

                                  This would seem to indicate that pre-compile directives are treated differently
                                  than code blocks. Imagine that.
                                  The world is strange and wonderful.*
                                  I reserve the right to be horrifically wrong.
                                  Please maintain a safe following distance.
                                  *wonderful sold separately.

                                  Comment


                                  • #18
                                    >MCM’s list is a mixture of should and shouldn’ts.

                                    No, it isn't. It's a list of "coulds," provided without editorial comment on the desirability of any "could."

                                    An editorial comment would be something like responding to ..
                                    Overflow checking would slow down the executable. It’s a good thing the compiler doesn’t do overflow checking
                                    .. with:
                                    Of course it will slow down the executable, because there is no such thing as a free lunch. But when the need to detect overflow exceeds the need for maximum speed, the ability to detect overflow is a Good Thing.

                                    Many years ago when I worked for SDI (no, not the Star Wars SDI) I visited a distributor in Green Bay WI. They had purchased a new computer system (Burroughs B1900/CMS) but continued to do things the same way they had been doing them in the 1960's.. and had some of the same ongoing problems they had had for years.

                                    The relevance? That was the origin of my frequent comment that someone "has purchased the right to [expletive] up at six megahertz." [Really fast then].

                                    Without overflow checking, your software can get the wrong answer faster than mine gets the right answer. I won't call that a Good Thing.

                                    HINT:
                                    Code:
                                    #DEBUG OVERFLOW {ON|OFF}
                                    Default is off.

                                    MCM
                                    Michael Mattias
                                    Tal Systems (retired)
                                    Port Washington WI USA
                                    [email protected]
                                    http://www.talsystems.com

                                    Comment


                                    • #19
                                      The Creature That Would Not Die
                                      Starring Hugh Jackman, Naomi Watts and Michael Mattias as the creature.

                                      Coming to a theater near you.
                                      Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

                                      Comment


                                      • #20
                                        > The PB compiler "could" support a true boolean data type. It doesn't.
                                        (...)
                                        > No.
                                        > Boolean has three possible values: TRUE, FALSE and NULL

                                        Boolean data type: https://en.wikipedia.org/wiki/Boolean_data_type

                                        Pro Tip to Mark; that works better.
                                        Last edited by Eric Pearson; 21 Jan 2016, 09:35 AM.
                                        "Not my circus, not my monkeys."

                                        Comment

                                        Working...
                                        X