??? 06/10/07 17:09 Modified: 06/10/07 17:11 Read: times |
#140510 - OK Then 39 Bytes... Responding to: ???'s previous message |
I want to take back the two bytes lost to the new 2 letter key words rule!! So here is a new version that clocks in at 39 bytes.
UC TestFlip(UC c) { //UC r,i=8;WH(i--){r>>=1;r|=c&128;c*=2;}RT r; //c=c/2&85|c*2&170;c=c/4&51|c*4&204;RT c>>4|c<<4; UC r,i=8;WH(i--){r*=2;r|=c&1;c/=2;}RT r; } In considering how we are looking at the C language source code I was using the Keil C compiler to run and test this code instead of some PC code compiler. One thing I noticed was how well these obtuse and compact lines of C code collapse to optimized machine language. In some cases one could even use the assembler to figure out how the C code actually works!! Cooper's 47 byte source code compiles to this efficient assembler code: 78: UC TestFlip(UC c) 79: { 80: c=c/2&85|c*2&170;c=c/4&51|c*4&204;RT c>>4|c<<4; C:0x006E EF MOV A,R7 C:0x006F C3 CLR C C:0x0070 13 RRC A C:0x0071 5455 ANL A,#0x55 C:0x0073 FE MOV R6,A C:0x0074 EF MOV A,R7 C:0x0075 25E0 ADD A,ACC(0xE0) C:0x0077 54AA ANL A,#0xAA C:0x0079 4E ORL A,R6 C:0x007A FF MOV R7,A C:0x007B 13 RRC A C:0x007C 13 RRC A C:0x007D 5433 ANL A,#0x33 C:0x007F FE MOV R6,A C:0x0080 EF MOV A,R7 C:0x0081 25E0 ADD A,ACC(0xE0) C:0x0083 25E0 ADD A,ACC(0xE0) C:0x0085 54CC ANL A,#TL2(0xCC) C:0x0087 4E ORL A,R6 C:0x0088 FF MOV R7,A C:0x0089 C4 SWAP A C:0x008A 54F0 ANL A,#B(0xF0) C:0x008C FE MOV R6,A C:0x008D EF MOV A,R7 C:0x008E C4 SWAP A C:0x008F 540F ANL A,#0x0F C:0x0091 4E ORL A,R6 C:0x0092 FF MOV R7,A 81: } C:0x0093 22 RET My most recent 39 byte source code collapses to this even smaller assembler code. Cooper's still would run faster for a time critical application because his is straight line code while on the other hand mine runs almost all of the assembler eight times in a loop. 78: UC TestFlip(UC c) 79: { 80: UC r,i=8;WH(i--){r*=2;r|=c&1;c/=2;}RT r; C:0x006E 7D08 MOV R5,#0x08 C:0x0070 AC05 MOV R4,0x05 C:0x0072 1D DEC R5 C:0x0073 EC MOV A,R4 C:0x0074 600F JZ C:0085 C:0x0076 EE MOV A,R6 C:0x0077 25E0 ADD A,ACC(0xE0) C:0x0079 FE MOV R6,A C:0x007A EF MOV A,R7 C:0x007B 5401 ANL A,#0x01 C:0x007D 4206 ORL 0x06,A C:0x007F EF MOV A,R7 C:0x0080 C3 CLR C C:0x0081 13 RRC A C:0x0082 FF MOV R7,A C:0x0083 80EB SJMP C:0070 C:0x0085 AF06 MOV R7,0x06 81: } C:0x0087 22 RET Note that both above compilations were done with the Keil C51 version 7.01 with the Optimization level set at #8. Michael Karas |