All times are UTC-06:00




Post new topic  Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Aug 22, 2006 4:50 am 
Offline

Joined: Fri Feb 17, 2006 12:31 pm
Posts: 10
A few days ago i implemented CAST-256 in C and was unsatisfied by GCC's usage of the PowerPC isa's uncommon instructions.
Code:
Type 1: I = ((Kmi + D) <<< Kri)
O = ((S1[Ia] ^ S2[Ib]) - S3[Ic]) + S4[Id]
This excerpt from rfc2612 describes the first of the three F functions used in CAST-256 in C I used the following macro to implement it:
Code:
#define F1(D, R, M) \
( \
I = ( (M) + (D) ), \
I = rol( (R), I ), \
( ( ( SBox1[I >> 24] ^ SBox2[(I >> 16) & 0xFF] ) - SBox3[(I >> 8) & 0xFF] ) + SBox4[I & 0xFF] ) \
)
No matter what i tried gcc always used at least 3 instructions to calculate the index of an sbox if more than a simple shift right or masking is required for the case ( I >> 16 ) & 0xFF gcc used srwi, andi, and slwi. This are three dependend operations where as a single rlwinm $1, $4, 18, 22, 29 would do the job.

Is anyone here interessed in the source then it works?

Current version F1 in Assembler:
Code:
Dst0, Dst1, Dst2, Dst3, Src
.macro Split_Word_Mul_4
rlwinm $0, $4, 10, 22, 29
rlwinm $1, $4, 18, 22, 29
rlwinm $2, $4, 26, 22, 29
rlwinm $3, $4, 2, 22, 29
.endmacro

; Dst0 & Idx0, .. , Dst3 & Idx3
.macro Load_SBoxes
lwzx $0, SBox1, $0
lwzx $1, SBox2, $1
lwzx $2, SBox3, $2
lwzx $3, SBox4, $3
.endmacro

; Dst0, Dst1, Dst2, Dst3, Src
.macro SBoxes
Split_Word_Mul_4 $0, $1, $2, $3, $4
Load_SBoxes $0, $1, $2, $3
.endmacro

.macro F1
add Tmp3, Mask, Data
rotlw Tmp3, Tmp3, Rota

SBoxes Tmp0, Tmp1, Tmp2, Tmp3, Tmp3


xor Tmp0, Tmp0, Tmp1
sub Tmp0, Tmp0, Tmp2
add Tmp0, Tmp0, Tmp3
.endmacro
P.S.: I didn't reformat the code sections their are just copied and pasted for Xcode.


Top
   
PostPosted: Tue Aug 22, 2006 9:16 am 
Offline
Site Admin

Joined: Fri Sep 24, 2004 1:39 am
Posts: 1589
Location: Austin, TX
Quote:
A few days ago i implemented CAST-256 in C and was unsatisfied by GCC's usage of the PowerPC isa's uncommon instructions.
You're right GCC generally doesn't translate anything into rlwinm :(

Nice code. I'm sure it will come in handy for someone :)

_________________
Matt Sealey


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 2 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 23 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:  
PowerDeveloper.org: Copyright © 2004-2012, Genesi USA, Inc. The Power Architecture and Power.org wordmarks and the Power and Power.org logos and related marks are trademarks and service marks licensed by Power.org.
All other names and trademarks used are property of their respective owners. Privacy Policy
Powered by phpBB® Forum Software © phpBB Group