Thread: packing two characters into an unsigned integer.

  1. #16
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Where did I use a call? #define is not a call.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    ASM is not going to be faster than simple bit shifting. If it is, it's minutely so:

    #define PACK(a,b) ((a<<8)&b)

    result = PAC( somevar, anothervar );

    There, now they're packed. With bit shifts and bitwise AND, you really aren't going to get much faster, especially once your compiler optimizes. There really is no point in using ASM for most applications. You're compiler will likely optimize it better than you can with ASM anyway.

    Hope is the first step on the road to disappointment.

  3. #18
    Registered User wavering's Avatar
    Join Date
    Dec 2001
    "ASM is not going to be faster than simple bit shifting. If it is, it's minutely so:"

    I can only answer for my compiler ( MSVC++ ) and having done careful timings I find that if I need to do TWO shifts then asm is almost twice as fast. I am writing a very time critical application - a program that runs for days at a time. The innermost loop boils down to about 50 lines of executed asm code in the disassembler so even saving a couple of lines makes a measurable effect.

    In most applications whether the program responds in a hundreth of a second or a tenth does not make much difference but if you are going round a loop hundreds of billions of time it is critical.

    Here is the code from the disassembler:

    402:      dummy >>= 13;   
    0040279D   mov         dx,word ptr [ebp-8]
    004027A1   shr         dx,0Dh
    004027A5   mov         word ptr [ebp-8],dx
    403:      dummy <<= 3;             
    004027A9   mov         ax,word ptr [ebp-8]
    004027AD   shl         ax,3
    004027B1   mov         word ptr [ebp-8],ax
    Here is hand coded asm:
    _asm{                       //Find SECOND variable on "RHS" Bits 4 to 6
      mov  ax,dummy
      shl  ax,3                 ;Shift 3 "bits" to extreme left
      shr  ax,13                ;Shift 13 "bits" to extreme right
      mov  dummy,ax
    The above is NOT from my supercritical loop and could be further optimised. To take another very simple example:


    My compiler does

    mov ax, liner
    add ax,1
    mov liner,ax

    I do

    add liner,1

    In other words even in the most trivial example the compiler takes three lines to do what can be done in hand coded asm in one line. It is not three times faster but this simple substitituion speeded my program by about 5%. I suspect that it puts the variable into the register because this is not(?) acceptable to older processors but I am the only person likely to run this program so it is OK for me. Plus who is still using 8086 processors?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 01:13 PM
Website Security Test