
; Copyright (c) 1993 Niklas Rjemo
; All rights reserved.

; Permission to use, copy, modify, and distribute this software and its
; documentation for any purpose, without fee, and without written agreement is
; hereby granted, provided that the above copyright notice and the following
; two paragraphs appear in all copies of this software.

; IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR
; DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
; OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR
; HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

; THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES,
; INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
; AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
; ON AN "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATION TO
; PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.


a1  rn  0
a2  rn  1
a3  rn  2 
a4  rn  3
v1  rn  4
v2  rn  5
v3  rn  6
v4  rn  7
v5  rn  8
v6  rn  9
sl  rn 10
fp  rn 11
ip  rn 12
sp  rn 13
lr  rn 14
pc  rn 15

;  bitreverse(int *dst,int *src,int size)   size is in bytes


        area |C$$Code|,CODE,READONLY

constants
        dcd  0x00ff00ff
        dcd  0x0f0f0f0f
        dcd  0x33333333
        dcd  0x55555555

        export bitreverseW
bitreverseW
        mov   ip,sp
        stmfd sp!,{v1-v4,fp,ip,lr,pc}
        sub   fp,ip,#4

        adr   a4,constants
        ldmia a4,{v1-v4}
        movs  a3,a3
        b     testW
loopW
        ldr   a4,[a2],#4              ; 2_abcdefghijklmnopqrstuvwxyzABCDEF 
        and   ip,v1,a4,ror #24        ; 2_00000000qrstuvwx00000000abcdefgh
        and   a4,v1,a4,ror #16        ; 2_00000000yzABCDEF00000000ijklmnop
        orr   a4,ip,a4,lsl #8         ; 2_yzABCDEFqrstuvwxijklmnopabcdefgh
        and   ip,v2,a4,lsr #4         ; 2_0000yzAB0000qrst0000ijkl0000abcd
        and   a4,v2,a4                ; 2_0000CDEF0000uvwx0000mnop0000efgh
        orr   a4,ip,a4,lsl #4         ; 2_CDEFyzABuvwxqrstmnopijklefghabcd
        and   ip,v3,a4,lsr #2         ; 2_00CD00yz00uv00qa40mn00ij00ef00ab
        and   a4,v3,a4                ; 2_00EF00AB00wx00st00op00kl00gh00cd
        orr   a4,ip,a4,lsl #2         ; 2_EFCDAByzwxuvstqropmnklijghefcdab
        and   ip,v4,a4,lsr #1         ; 2_0E0C0A0y0w0u0s0q0o0m0k0i0g0e0c0a
        and   a4,v4,a4                ; 2_0F0D0B0z0x0v0t0a4p0n0l0j0h0f0d0b
        orr   a4,ip,a4,lsl #1         ; 2_FEDCBAzyxwvutsrqponmlkjihgfedcba
        str   a4,[a1],#4              ; 2_FEDCBAzyxwvutsrqponmlkjihgfedcba
        subs  a3,a3,#4
testW
        bgt   loopW
        ldmea fp,{v1-v4,fp,sp,pc}^

        export bitreverseB
bitreverseB
        movs  a3,a3
        b     testB
loopB
        ldrb   a4,[a2],#1              ; 2_abcdefgh 
        orr     a4,a4,a4,lsl #8         ; a4 := 2_abcdefghabcdefgh
        and     ip,a4,#2_101010100000   ; ip :=     2_e0g0a0c00000
        and     a4,a4,#2_10101010000    ; a4 :=      2_f0h0b0d0000
        orr     a4,a4,ip,lsr #2         ; a4 :=      2_fehgbadc000
        and     ip,a4,#2_11001100000    ; ip :=      2_fe00ba00000
        and     a4,a4,#2_110011000      ; a4 :=        2_hg00dc000
        orr     a4,a4,ip,lsr #4         ; a4 :=        2_hgfedcba0
        mov     a4,a4,lsr #1            ; a4 :=         2_hgfedcba
        strb  a4,[a1],#1              ; 2_2_hgfedcba
        subs  a3,a3,#1
testB
        bgt   loopB
        movs  pc,lr
