Cooler_

mysoft ninja

Aug 12th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.73 KB | None | 0 0
  1. // by MySoft   irc freenode #asm
  2. // *** Constants For ASM (as we normally do for MMX/SSE) ***
  3. u32 iRGBMask = 0b01110001100011100111000110001110 //RGB Component Mask
  4. u32 iSHMask  = 0b00000100001000010000010000100000 //Shield Component Mask
  5. u32 iCRGMask = 0b10000000000100001000000000010000 //Carry Red/Green Mask
  6. u32 iCBMask  = 0b00000010000000000000001000000000 //Carry Blue Mask
  7.  
  8. // ======== Code in C ========
  9. // 'Average with round to source (6.3ms Nintendo DS -O fast)
  10. void Alpha50_C( u32* pTGT, u32* PSRC, int iPixelCount ) {
  11.  //                   RRRGGGBBrrrgggbbRRRGGGBBrrrgggbb
  12.     #define _RGB1_  0b01110001100011100111000110001110u
  13.     #define _RGB1x_ 0b11100011000111001110001100011100u
  14.     #define _SHLD_  0b00000100001000010000010000100000u
  15.     #define _cRG1_  0b10000000000100001000000000010000u
  16.     #define _cB1_   0b00000010000000000000001000000000u
  17.     #define _RGB2_  0b00011100111000110001110011100011u
  18.     #define _RGB2x_ 0b00111001110001100011100111000110u
  19.     #define _cRG2_  0b01000010000000000100001000000000u
  20.     #define _cB2_   0b00000000000010000000000000001000u
  21.  
  22.     u32 s1,s2,tg;
  23.     int CNT;
  24.     for ( CNT=0 ; CNT<(iPixelCount>>2) ; CNT++ ) {
  25.       if ( pTGT[CNT] != pSRC[CNT] ) {
  26.         // *** Mask 1 ***
  27.         s1 = (pTGT[CNT] >> 1) & _RGB1_;
  28.         s2 = (pSRC[CNT] >> 1) & _RGB1_;
  29.         s1 = (s1+s2) & _RGB1x_;
  30.         s1 = (s1 >> 1) | _SHLD_;
  31.         s2 = s1-s2; //(carry check)
  32.         tg = ((s1 &_RGB1_)+((s2 & _cRG1_) >> 3)+((s2 & _cB1_) >> 2)) << 1;
  33.         // *** Mask 2 ***
  34.         s1 = pTGT[CNT] & _RGB2_;
  35.         s2 = pSRC[CNT] & _RGB2_;
  36.         s1 = ((s1+s2) & _RGB2x_) | _SHLD_;
  37.         s2 = s1-(s2 shl 1)        
  38.         pTGT[CNT] = tg | (((s1 & _RGB2x_) +
  39.         (((s2 & _cRG2_) >> 3)+((s2 & _cB2_) >> 2))) >> 1)
  40.      }
  41.    }
  42. }
  43.  
  44. // ========= Code o Optimized in Nintendo DS ASM ========
  45. // 'Average with bonus towards source (~4.4ms (~5.3ms without unrolling))
  46. void Alpha50_ASM ( u32* pTGT, u32* PSRC, int iPixelCount ) {
  47.  
  48.      asm { 'Variable Loading Block
  49.        ldr r0, $pTGT            'Target Pointer
  50.         ldr r1, $pSRC            'Source Pointer
  51.        ldr r2, $iPixelCount     'How Many pixels to process?
  52.         ldr r3,  $iCBMask        'Carry Mask for Blue
  53.        ldr r10, $iRGBMask       'Mask 'R0B0G0R0B0G0' / '0G0R0B0G0R0B'
  54.         ldr r11, $iSHMask        'Shield Mask (component shield)
  55.        ldr r12, $iCRGMask       'Carry Mask for Red/Green      
  56.       }
  57.       asm { 'Alpha50% with source boost
  58.        mov r2, r2, asr #3       'We're processing 8! pixels each time
  59.        str r14, [r13,#-4]!      'Save r14(link) on stack
  60.         'str r13, 9f              'save r13 (stack) on data inside code...
  61.         mov r14, r3              'Now that r14 is safe use it as 'carry' mask
  62.        sub r0, #4
  63.        sub r1, #4
  64.        0:                   'processing pixels
  65.         ldmia r0 !, {r3,r8}      'loading 8 pixels targer and position ahead
  66.        ldmia r1 !, {r4,r9}      'loading 4 pixels source and position ahead
  67.         cmp r3, r4               'Are src/tgt pixels equal?
  68.        beq 1f                   'yes? skip processing
  69.         ' --- Mask1 ---
  70.        and r5, r10, r3, lsr #1  'mask RGB and shift right (tgt) 'R5:0RRR000BB000GGG00RRR000BB000GGG0
  71.        and r6, r10, r4, lsr #1  'mask RGB and shift right (src) 'R6:0RRR000BB000GGG00RRR000BB000GGG0
  72.        add r5, r6               'add both halved values         'R5:RRRR00BBB00GGGG0RRRR00BBB00GGGG0
  73.        and r5, r10, lsl #1      'only Mask RGB bits             'R5:RRR000BB000GGG00RRR000BB000GGG00
  74.        orr r5, r11, r5, lsr #1  'Shield components (overflow)   'R5:0RRR010BB010GGG10RRR010BB010GGG0
  75.        sub r6, r5, r6           'temp = tgt-src  (carry check)  'R6:c???00c??00c???0c???00c??00c???0
  76.        and r5, r10              'only Mask RGB bits             'R5:0RRR000BB000GGG00RRR000BB000GGG0
  77.        and r7, r6, r12          'keep carry of RG's             'R8:c0000000000c0000c0000000000c0000
  78.         and r6, r14              'keep carry of B's              'R6:000000c000000000000000c000000000
  79.        add r7, r5, r7, lsr #3   'sum tgt+carry (RG's)           'R7:0RRr000BB000GGg00RRr000BB000GGg0
  80.         add r7, r6, lsr #2       'sum carry (B's)                'R7:0RRr000Bb000GGg00RRr000Bb000GGg0
  81.         mov r7, r7, lsl #1       'adjust sum to correct place    'R7:RRr000Bb000GGg00RRr000Bb000GGg00
  82.         ' --- Mask2 ---
  83.        and r5, r3, r10, ror #7  'mask2 RGB (tgt)                'R5:000GGG00RRR000BB000GGG00RRR000BB
  84.        and r6, r4, r10, ror #7  'mask2 RGB (src)                'R6:000GGG00RRR000BB000GGG00RRR000BB
  85.        add r5, r6               'add values (no 32bit overflow) 'R5:00GGGG0RRRR00BBB00GGGG0RRRR00BBB
  86.        and r5, r10, ror #6      'only Mask2 bits (lsl 1)        'R5:00GGG00RRR000BB000GGG00RRR000BB0
  87.        orr r5, r11              'AVG and Shield (overflow)      'R5:00GGG10RRR100BB100GGG10RRR100BB0
  88.        sub r6, r5, r6, lsl #1   'temp = tgt-src  (carry check)  'R6:cc???0c???0cc??0cc???0c???0cc??0
  89.        and r5, r10, ror #6      'only Mask2 component bits      'R5:00GGG00RRR000BB000GGG00RRR000BB0
  90.        and r3, r6, r12, ror #6  'keep carry of RG's             'R8:0c0000c0000000000c0000c000000000
  91.         and r6, r14, ror #6      'keep carry of B's              'R6:000000000000c000000000000000c000
  92.         add r5, r3, lsr #3       'add tgt+carry (RG's)           'R5:00GGg00RRr000BB000GGg00RRr000BB0
  93.         add r5, r6, lsr #2       'add tgt+carry (B's)            'R5:00GGg00RRr000Bb000GGg00RRr000Bb0
  94.         orr r7, r5, lsr #1       'store mask on correct place    'R7:RRrGGgBbRRrGGgBbRRrGGgBbRRrGGgBb
  95.         str r7, [r0,#-8]         'store first 4 pixels
  96.         1:                   'now unrolling the loop...
  97.        cmp r8, r9               'Are src/tgt pixels equal?
  98.         beq 1f                   'yes? skip processing
  99.        ' --- Mask1 ---
  100.         and r5, r10, r8, lsr #1  'mask RGB and shift right (tgt) 'R5:0RRR000BB000GGG00RRR000BB000GGG0
  101.         and r6, r10, r9, lsr #1  'mask RGB and shift right (src) 'R6:0RRR000BB000GGG00RRR000BB000GGG0
  102.         add r5, r6               'add both halved values         'R5:RRRR00BBB00GGGG0RRRR00BBB00GGGG0
  103.         and r5, r10, lsl #1      'only Mask RGB bits             'R5:RRR000BB000GGG00RRR000BB000GGG00
  104.         orr r5, r11, r5, lsr #1  'Shield components (overflow)   'R5:0RRR010BB010GGG10RRR010BB010GGG0
  105.         sub r6, r5, r6           'temp = tgt-src  (carry check)  'R6:c???00c??00c???0c???00c??00c???0
  106.         and r5, r10              'only Mask RGB bits             'R5:0RRR000BB000GGG00RRR000BB000GGG0
  107.         and r7, r6, r12          'keep carry of RG's             'R8:c0000000000c0000c0000000000c0000
  108.        and r6, r14              'keep carry of B's              'R6:000000c000000000000000c000000000
  109.         add r7, r5, r7, lsr #3   'sum tgt+carry (RG's)           'R7:0RRr000BB000GGg00RRr000BB000GGg0
  110.         add r7, r6, lsr #2       'sum carry (B's)                'R7:0RRr000Bb000GGg00RRr000Bb000GGg0
  111.         mov r7, r7, lsl #1       'adjust sum to correct place    'R7:RRr000Bb000GGg00RRr000Bb000GGg00
  112.         ' --- Mask2 ---
  113.        and r5, r8, r10, ror #7  'mask2 RGB (tgt)                'R5:000GGG00RRR000BB000GGG00RRR000BB
  114.        and r6, r9, r10, ror #7  'mask2 RGB (src)                'R6:000GGG00RRR000BB000GGG00RRR000BB
  115.        add r5, r6               'add values (no 32bit overflow) 'R5:00GGGG0RRRR00BBB00GGGG0RRRR00BBB
  116.        and r5, r10, ror #6      'only Mask2 bits (lsl 1)        'R5:00GGG00RRR000BB000GGG00RRR000BB0
  117.        orr r5, r11              'AVG and Shield (overflow)      'R5:00GGG10RRR100BB100GGG10RRR100BB0
  118.        sub r6, r5, r6, lsl #1   'temp = tgt-src  (carry check)  'R6:cc???0c???0cc??0cc???0c???0cc??0
  119.        and r5, r10, ror #6      'only Mask2 component bits      'R5:00GGG00RRR000BB000GGG00RRR000BB0
  120.        and r3, r6, r12, ror #6  'keep carry of RG's             'R8:0c0000c0000000000c0000c000000000
  121.         and r6, r14, ror #6      'keep carry of B's              'R6:000000000000c000000000000000c000
  122.         add r5, r3, lsr #3       'add tgt+carry (RG's)           'R5:00GGg00RRr000BB000GGg00RRr000BB0
  123.         add r5, r6, lsr #2       'add tgt+carry (B's)            'R5:00GGg00RRr000Bb000GGg00RRr000Bb0
  124.         orr r7, r5, lsr #1       'store mask on correct place    'R7:RRrGGgBbRRrGGgBbRRrGGgBbRRrGGgBb
  125.         str r7, [r0,#-4]         'store next 4 pixels
  126.         1:                   'done with pixels
  127.        subs r2, #1              '8 pixels done there are more?
  128.         bne 0b                   'yes? then go process them!
  129.        'b 1f                     'skip data in code
  130.        '9: .word 0           ' here goes r13 upon return
  131.        '1: ldr r13, 9b           'restore r13 (stack)
  132.        ldmia r13 !, {r14}       'restore saved r13
  133.         ' --- DONE! ---
  134.      }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment