Author Topic: (Optimization) Something about "Bit Set" and the good old "OR"  (Read 4490 times)

0 Members and 1 Guest are viewing this topic.

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 897
    • it-berater
We want to set one bit.

PB has a command for this:
BIT SET. Lets take a look, just on the result.

Code: [Select]
40287B B801000000             MOV EAX, DWORD 00000001
402880 8D9D5CFFFFFF           LEA EBX, DWORD PTR [EBP+FFFFFF5C]
402886 E8D82B0000             CALL L405463

405463 E813000000             CALL L40547B
405468 0803                       OR  BYTE PTR [EBX], AL
40546A C3                         RET NEAR

Doesn't look promissing. We just want to set ONE BIT. We don't want to make Subroutine calls.

Ok, lets try teh traditional BASIC-way.

Code: [Select]
LOCAL T16 AS LONG
T16= (T16 OR 1) 


Looks quite simple, we assume that it becomes what it becomes:

Code: [Select]
40287B B801000000             MOV EAX, DWORD 00000001
402880 09855CFFFFFF           OR  DWORD PTR [EBP+FFFFFF5C], EAX

Looks good!

Thinking a bit about the bit, we can simply use PB-Inline ASM:

Code: [Select]
LOCAL T16 AS LONG
!OR T16,1 


Code: [Select]
' We get this result:
402887 838D5CFFFFFF01         OR  DWORD PTR [EBP+FFFFFF5C], BYTE 01

This is it, we cant get better here!
« Last Edit: January 02, 2007, 09:42:04 AM by Theo Gottwald »

Offline Donald Darden

  • Sr. Member
  • ****
  • Posts: 364
Re: (Optimization) Something about "Bit Set" and the good old "OR"
« Reply #1 on: April 20, 2007, 10:32:53 PM »
Agreed, for bits within a given word.  However, you can use BIT statements to
span any array as well, so treating it as a pointer offset works best for that
purpose.

Often, the tradeoff to gain power and flexibility is at the expense of doing something simple in the simplest way.

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 897
    • it-berater
Re: (Optimization) Something about "Bit Set" and the good old "OR"
« Reply #2 on: May 21, 2007, 05:03:45 PM »
Agreed, also while looking in the Disassembly, my idea was that the more universal the instruction, the worse the optimization.
Anyway, there may be chances, where future compilers can be improved in optimization on such things.

On the other side thats a architectural question. If you do the Optimization as a "third pass", you may have lost important datatype Informations from the source code. Thats why it these sort of optimizations depends on how open the compiler architecture is.