It is currently Sat Mar 06, 2021 2:38 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: linasm library
PostPosted: Sun Jun 22, 2014 9:19 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3574
Location: New Zealand
Here's an interesting asm library.
http://linasm.sourceforge.net/index.php

I almost felt like posting kcc animated gifs when I looked at it not just for the functions and structs
but the way it's written, which for us is shamelessly cut and paste. :mrgreen:
For linux x64 but some suitable routines for windows x64

short sample of Bitscan Reverse 64 and 32

Code:
;/*                                                                        Math.h
;################################################################################
;# Encoding: UTF-8                                                  Tab size: 4 #
;#                                                                              #
;#                             FAST MATH FUNCTIONS                              #
;#                                                                              #
;# License: LGPLv3+                               Copyleft (?) 2014, Jack Black #
;################################################################################

!Macro   BYTE_SWAP   result, value, scale
!{
!      MOV      result, value
!If scale = 1
!      ROL      ax, 8
!Else If scale > 1
!      BSWAP   result
!End If
!}
!ByteSwap8:   BYTE_SWAP   al, dil, 0
!ByteSwap16:   BYTE_SWAP   ax, di, 1
!ByteSwap32:   BYTE_SWAP   eax, edi, 2
!ByteSwap64:   BYTE_SWAP   rax, rdi, 3

! Macro   BIT_REVERSE      result, value, temp1, temp2, scale
! {
! ;---[Internal variables]-------------------
! If scale = 0
! const11   = 0x55                        ; const #1 For first stage
! const12   = 0x33                        ; const #1 For second stage
! const13   = 0x0F                        ; const #1 For third stage
! const21   = 0xAA                        ; const #2 For first stage
! const22   = 0xCC                        ; const #2 For second stage
! const23   = 0xF0                        ; const #2 For third stage
! Else If scale = 1
! const11   = 0x5555                     ; const #1 For first stage
! const12   = 0x3333                     ; const #1 For second stage
! const13   = 0x0F0F                     ; const #1 For third stage
! const21   = 0xAAAA                     ; const #2 For first stage
! const22   = 0xCCCC                     ; const #2 For second stage
! const23   = 0xF0F0                     ; const #2 For third stage
! Else If scale = 2
! const11   = 0x55555555                  ; const #1 For first stage
! const12   = 0x33333333                  ; const #1 For second stage
! const13   = 0x0F0F0F0F                  ; const #1 For third stage
! const21   = 0xAAAAAAAA                  ; const #2 For first stage
! const22   = 0xCCCCCCCC                  ; const #2 For second stage
! const23   = 0xF0F0F0F0                  ; const #2 For third stage
! Else If scale = 3
! const11   = 0x5555555555555555            ; const #1 For first stage
! const12   = 0x3333333333333333            ; const #1 For second stage
! const13   = 0x0F0F0F0F0F0F0F0F            ; const #1 For third stage
! const21   = 0xAAAAAAAAAAAAAAAA            ; const #2 For first stage
! const22   = 0xCCCCCCCCCCCCCCCC            ; const #2 For second stage
! const23   = 0xF0F0F0F0F0F0F0F0            ; const #2 For third stage
! End If
! ;---[First stage]--------------------------
!       MOV      temp1, const11            ; temp1 = const11
!       MOV      temp2, const21            ; temp2 = const21
!       AND      temp1, value
!       AND      temp2, value
!       SHL      temp1, 1               ; temp1 = (value & temp1) << 1
!       SHR      temp2, 1               ; temp2 = (value & temp2) >> 1
!       MOV      value, temp1
!       OR      value, temp2            ; value = temp1 | temp2
! ;---[Second stage]-------------------------
!       MOV      temp1, const12            ; temp1 = const12
!       MOV      temp2, const22            ; temp2 = const22
!       AND      temp1, value
!       AND      temp2, value
!       SHL      temp1, 2               ; temp1 = (value & temp1) << 2
!       SHR      temp2, 2               ; temp2 = (value & temp2) >> 2
!       MOV      value, temp1
!       OR      value, temp2            ; value = temp1 | temp2
! ;---[Third stage]--------------------------
!       MOV      temp1, const13            ; temp1 = const13
!       MOV      temp2, const23            ; temp2 = const23
!       AND      temp1, value
!       AND      temp2, value
!       SHL      temp1, 4               ; temp1 = (value & temp1) << 4
!       SHR      temp2, 4               ; temp2 = (value & temp2) >> 4
!       MOV      value, temp1
!       OR      value, temp2            ; value = temp1 | temp2
! ;---[Swap bytes If required]---------------
!    BYTE_SWAP   result, value, scale      ; Return ByteSwap (value)
! }
! BitReverse8:     BIT_REVERSE      al, dil, dl, cl, 0
! BitReverse16:   BIT_REVERSE      ax, di, dx, cx, 1
! BitReverse32:   BIT_REVERSE      eax, edi, edx, ecx, 2
! BitReverse64:   BIT_REVERSE      rax, rdi, rdx, rcx, 3

Macro _BitReverse64(value)
   EnableASM
   MOV rdi, value
   !BIT_REVERSE      rax, rdi, rdx, rcx, 3
   MOV value, rax
   DisableASM
EndMacro

Macro _BitReverse32(value)
   EnableASM
   MOV edi, value
   !BIT_REVERSE      eax, edi, edx, ecx, 2
   MOV value, eax
   DisableASM
EndMacro   

Global a.i,b.l 
a.i = %1110000000000000000000000000000000000000000000000000000000000000
b.l = %11100000000000000000000000000000

ProcedureCDLL.i BitReverse64(val.i)
   _BitReverse64(val)
   ProcedureReturn val
EndProcedure

ProcedureCDLL.l BitReverse32(val.l)
   _BitReverse32(val)
   ProcedureReturn val
EndProcedure


;call procedure
a = BitReverse64(a)
Debug RSet(Bin(a,#PB_Quad),64,"0")
b = BitReverse32(b)
Debug RSet(Bin(b,#PB_Long ),32,"0")

;use the macro
_BitReverse64(a)
Debug RSet(Bin(a,#PB_Quad),64,"0")
_BitReverse32(b)
Debug RSet(Bin(b,#PB_Long),32,"0")



Top
 Profile  
Reply with quote  
 Post subject: Re: linasm library
PostPosted: Sat Aug 09, 2014 9:06 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2005 5:05 pm
Posts: 1769
Location: Norway
viewtopic.php?f=35&t=47524
:wink:


Top
 Profile  
Reply with quote  
 Post subject: Re: linasm library
PostPosted: Sun Aug 10, 2014 12:59 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3574
Location: New Zealand
Rescator wrote:
http://www.purebasic.fr/english/viewtopic.php?f=35&t=47524
:wink:


Perhaps I could have chosen another example from the library.
The intention was to highlight the libs utility rather than the example.


Top
 Profile  
Reply with quote  
 Post subject: Re: linasm library
PostPosted: Sun Aug 10, 2014 5:37 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2005 5:05 pm
Posts: 1769
Location: Norway
The example is still interesting (hence me referencing the other thread), in that thread the solution I post near the end is one of the fastest asm implementations like that.
And the example you showed is very similar which hopefully reflect the rest of the stuff as being equally well optimized.


Top
 Profile  
Reply with quote  
 Post subject: Re: linasm library
PostPosted: Sun Aug 10, 2014 10:22 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3574
Location: New Zealand
Yes I noticed the similarity reading through the thread. :D

I thought the library is a good example to learn from, it's architecture lends it self well
to integrating asm with PB


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye