Skip to content

Commit

Permalink
Merge branch 'feature/Remove_final_division_from_icscore' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Agbar committed Oct 18, 2017
2 parents 5318f8c + 4259862 commit 4b19171
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 60 deletions.
9 changes: 5 additions & 4 deletions libEnigma/include/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ typedef uint8_t scoreLength_t;

STATIC_ASSERT ( ( scoreLength_t ) -1 > 0 , "scoreLength_t should be unsigned, so next assert can be correct." );
STATIC_ASSERT ( 1 << ( sizeof( scoreLength_t ) * 8 ) > CT , "scoreLength_t is to narrow." );
STATIC_ASSERT ( UINT16_MAX > CT * CT, "uint16_t is to narrow for current CT value. Use ie. uint32_t." );

typedef struct _enigma_score_function_t{
int (*triscore) ( const Key* const restrict key, scoreLength_t length );
int (* biscore) ( const Key* const restrict key, scoreLength_t length );
double (* icscore) ( const Key* const restrict key, scoreLength_t length );
int (*uniscore) ( const Key* const restrict key, scoreLength_t length );
int (*triscore) ( const Key* const restrict key, scoreLength_t length );
int (* biscore) ( const Key* const restrict key, scoreLength_t length );
uint16_t (* icscore) ( const Key* const restrict key, scoreLength_t length );
int (*uniscore) ( const Key* const restrict key, scoreLength_t length );
} enigma_score_function_t;

// Flags are used to allow combining.
Expand Down
8 changes: 3 additions & 5 deletions libEnigma/include/score_inlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@ int ComputeUniscoreFromDecodedMsg( union ScoringDecodedMessage* msg, scoreLength
__attribute__ ((optimize("unroll-loops")))
__attribute__ ((optimize("unroll-loops,sched-stalled-insns=0,sched-stalled-insns-dep=16")))
inline
double ComputeIcscoreFromDecodedMsg( union ScoringDecodedMessage* msg, scoreLength_t len ){
uint16_t ComputeIcscoreFromDecodedMsg( union ScoringDecodedMessage* msg, scoreLength_t len ){
uint8_t f[32] = {0};
int i;
for( i = 0; i < len; i++ ) {
f[msg->plain[i]]++;
}

STATIC_ASSERT ( UINT16_MAX > CT * CT, "uint16_t is to narrow for current CT value. Use ie. uint32_t." );

uint16_t sum = 0;
for( i = 0; i < 26; i++ ) {
sum += f[i] * ( f[i] - 1 );
}
double ret = ( double )sum / ( len * ( len - 1 ) );
return ret;

return sum;
}

#endif
5 changes: 2 additions & 3 deletions libEnigma/include/x86/cipherAvx2_inlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ v32qi DecodeBiteBackwardCommonAvx2( v32qi bite, v32qi rRingOffset, const Key* c
__attribute__ ((optimize("unroll-loops")))
__attribute__ ((optimize("unroll-loops,sched-stalled-insns=0,sched-stalled-insns-dep=16")))
inline
double ComputeIcscoreFromDecodedMsgAvx2( union ScoringDecodedMessage* msg, scoreLength_t len ){
uint16_t ComputeIcscoreFromDecodedMsgAvx2( union ScoringDecodedMessage* msg, scoreLength_t len ){
ALIGNED_32( uint8_t f[32] ) = {0};
int i;
for( i = 0; i < len; i++ ) {
Expand Down Expand Up @@ -80,9 +80,8 @@ double ComputeIcscoreFromDecodedMsgAvx2( union ScoringDecodedMessage* msg, score
v16hi high = __builtin_ia32_psadbw256( ( v32qi ) ( foo ), zero );
v16hi low = __builtin_ia32_psadbw256( ( v32qi ) ( bar ), zero );

STATIC_ASSERT ( UINT16_MAX > CT * CT, "uint16_t is to narrow for current CT value. Use ie. uint32_t." );
uint16_t sum = 256 *( high[0] + high[4] + high[8] + high[12] ) + low[0] + low[4] + low[8] + low[12];
return ( double )sum / ( len * ( len - 1 ) );
return sum;
}


Expand Down
7 changes: 3 additions & 4 deletions libEnigma/include/x86/cipherSsse3_inlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void DecodeScoredMessagePartSsse3( const Key* const restrict key, int len, union
__attribute__ ((optimize("unroll-loops")))
__attribute__ ((optimize("unroll-loops,sched-stalled-insns=0,sched-stalled-insns-dep=16")))
inline
double ComputeIcscoreFromDecodedMsgSsse3( union ScoringDecodedMessage* msg, scoreLength_t len ){
uint16_t ComputeIcscoreFromDecodedMsgSsse3( union ScoringDecodedMessage* msg, scoreLength_t len ){
uint8_t ALIGNED_32( f[32] ) = {0};
int i;
for( i = 0; i < len; i++ ) {
Expand Down Expand Up @@ -135,10 +135,9 @@ double ComputeIcscoreFromDecodedMsgSsse3( union ScoringDecodedMessage* msg, scor
v8hi high = ( v8hi ) __builtin_ia32_psadbw128( ( v16qi ) foo, zero );
v8hi low = ( v8hi ) __builtin_ia32_psadbw128( ( v16qi ) bar, zero );

STATIC_ASSERT ( UINT16_MAX > CT * CT, "uint16_t is to narrow for current CT value. Use ie. uint32_t." );
uint16_t sum = ( high[0] + high[4] )* 256 + low[0] + low[4];
double ret = ( double )sum / ( len * ( len - 1 ) );
return ret;

return sum;
}

inline
Expand Down
4 changes: 2 additions & 2 deletions libEnigma/src/hillclimb.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ void hillclimb( const Key *from, const Key *to, const Key *ckey_res, const Key *
void OptimizeIcscore( text_t var[26], Key* const ckey, int len, const enigma_score_function_t* const sf ){
int i, z, x;
enum Action_t action = NONE;
double bestic = sf->icscore( ckey, len );
double ic;
uint16_t bestic = sf->icscore( ckey, len );
uint16_t ic;
for( i = 0; i < 26; i++ ) {
int k;
for( k = i + 1; k < 26; k++ ) {
Expand Down
2 changes: 1 addition & 1 deletion libEnigma/src/hillclimb2.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void hillclimb2( const Key *from, const Key *to, const Key *ckey_res, const Key
Fill0To25(var);
int pass;
int bestscore, jbestscore, a, globalscore;
double bestic, ic;
uint16_t bestic, ic;
int firstloop = 1;

enigma_score_function_t sf;
Expand Down
16 changes: 7 additions & 9 deletions libEnigma/src/scoreBasic.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#include "config/types.h"

// default scores
static double icscoreBasic( const Key* const restrict key, scoreLength_t length );
static int uniscoreBasic( const Key* const restrict key, scoreLength_t length );
static int biscoreBasic( const Key* const restrict key, scoreLength_t length );
static int triscoreBasic( const Key* const restrict key, scoreLength_t length );
static uint16_t icscoreBasic( const Key* const restrict key, scoreLength_t length );
static int uniscoreBasic( const Key* const restrict key, scoreLength_t length );
static int biscoreBasic( const Key* const restrict key, scoreLength_t length );
static int triscoreBasic( const Key* const restrict key, scoreLength_t length );

enigma_score_function_t enigmaScoreBasic = { triscoreBasic, biscoreBasic, icscoreBasic, uniscoreBasic };

Expand All @@ -23,10 +23,9 @@ union ScoringDecodedMessage decodedMsgPartBasic;
* opti scores
************************/
__attribute__ ((optimize("sched-stalled-insns=0,sched-stalled-insns-dep=16,unroll-loops")))
static double icscoreBasic( const Key* const restrict key, scoreLength_t len )
static uint16_t icscoreBasic( const Key* const restrict key, scoreLength_t len )
{
int f[26] = {0};
int S0, S1, S2, S3;
text_t c1;
int i;

Expand Down Expand Up @@ -102,7 +101,7 @@ static double icscoreBasic( const Key* const restrict key, scoreLength_t len )
f[c1]++;
}


uint16_t S0, S1, S2, S3;
S0 = S1 = S2 = S3 = 0;
for (i = 0; i < 23; i += 4) {
S0 += f[i]*(f[i]-1);
Expand All @@ -113,8 +112,7 @@ static double icscoreBasic( const Key* const restrict key, scoreLength_t len )
S0 += f[24]*(f[24]-1);
S1 += f[25]*(f[25]-1);

return (double)((S0+S1) + (S2+S3)) / (len*(len-1));

return (S0+S1) + (S2+S3);
}

static int uniscoreBasic( const Key* key, scoreLength_t len )
Expand Down
14 changes: 7 additions & 7 deletions libEnigma/src/scoreNoInterleave.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include "score.h"

// default scores
static int uniscoreNoInterleave( const Key* const restrict key, scoreLength_t len );
static int biscoreNoInterleave( const Key* const restrict key, scoreLength_t len );
static int triscoreNoInterleave( const Key* const restrict key, scoreLength_t len );
static double icscoreNoInterleave( const Key* const restrict key, scoreLength_t len );
static int uniscoreNoInterleave( const Key* const restrict key, scoreLength_t len );
static int biscoreNoInterleave( const Key* const restrict key, scoreLength_t len );
static int triscoreNoInterleave( const Key* const restrict key, scoreLength_t len );
static uint16_t icscoreNoInterleave( const Key* const restrict key, scoreLength_t len );

union ScoringDecodedMessage decodedMsgPartNoInterleave;

Expand Down Expand Up @@ -63,7 +63,7 @@ int triscoreNoInterleave( const Key* const restrict key, scoreLength_t len ) {
}

__attribute__ ((optimize("sched-stalled-insns=0,sched-stalled-insns-dep=16,unroll-loops")))
static double icscoreNoInterleave( const Key* const restrict key, scoreLength_t len ) {
static uint16_t icscoreNoInterleave( const Key* const restrict key, scoreLength_t len ) {
if (len < 2) {
return 0;
}
Expand All @@ -76,7 +76,7 @@ static double icscoreNoInterleave( const Key* const restrict key, scoreLength_t
f[decodedMsgPartNoInterleave.plain[i]]++;
}

int S0, S1, S2, S3;
uint16_t S0, S1, S2, S3;
S0 = S1 = S2 = S3 = 0;
for( i = 0; i < 26 - 3; i += 4 ) {
S0 += f[i] *( f[i] - 1 );
Expand All @@ -87,7 +87,7 @@ static double icscoreNoInterleave( const Key* const restrict key, scoreLength_t
S0 += f[24] * ( f[24] - 1 );
S1 += f[25] * ( f[25] - 1 );

return ( double )( ( S0 + S1 ) + ( S2 + S3 ) ) / ( len * ( len - 1 ) );
return ( S0 + S1 ) + ( S2 + S3 );
}

__attribute__ ((optimize("sched-stalled-insns=0,sched-stalled-insns-dep=16,unroll-loops")))
Expand Down
15 changes: 7 additions & 8 deletions libEnigma/src/scoreSimple.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
#include "cipher.h"


double icscoreSimple(const Key* const restrict key, scoreLength_t len);
int uniscoreSimple(const Key* const restrict key, scoreLength_t len);
int biscoreSimple(const Key* const restrict key, scoreLength_t len);
int triscoreSimple(const Key* const restrict key, scoreLength_t len);
uint16_t icscoreSimple( const Key* const restrict key, scoreLength_t len );
int uniscoreSimple( const Key* const restrict key, scoreLength_t len );
int biscoreSimple( const Key* const restrict key, scoreLength_t len );
int triscoreSimple( const Key* const restrict key, scoreLength_t len );

enigma_score_function_t enigmaScoreSimple = { triscoreSimple, biscoreSimple, icscoreSimple, uniscoreSimple};

PURE_FUNCTION
double icscoreSimple(const Key* const restrict key, scoreLength_t len)
uint16_t icscoreSimple( const Key* const restrict key, scoreLength_t len )
{
int f[26] = {0};
double S = 0;
int f[26] = {0};
int i;
int c;

Expand All @@ -30,9 +29,9 @@ double icscoreSimple(const Key* const restrict key, scoreLength_t len)
f[c]++;
}

uint16_t S = 0;
for (i = 0; i < 26; i++)
S += f[i]*(f[i]-1);
S /= len*(len-1);

return S;
}
Expand Down
2 changes: 1 addition & 1 deletion libEnigma/src/x86/cipherAvx2.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ v32qi DecodeBiteBackwardCommonAvx2( v32qi bite, v32qi rRingOffset, const Key* c
inline extern
v32qi PermuteV32qi( const PermutationMap_t* map, v32qi vec );
inline extern
double ComputeIcscoreFromDecodedMsgAvx2( union ScoringDecodedMessage* msg, scoreLength_t len );
uint16_t ComputeIcscoreFromDecodedMsgAvx2( union ScoringDecodedMessage* msg, scoreLength_t len );

enigma_cipher_function_t enigma_cipher_DecoderLookupAvx2 = { prepare_decoder_lookup_M_H3_avx2, prepare_decoder_lookup_ALL_avx2 };

Expand Down
2 changes: 1 addition & 1 deletion libEnigma/src/x86/cipherSsse3.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ v16qi PermuteV16qi(const PermutationMap_t* map, v16qi vec );
inline extern
void DecodeScoredMessagePartSsse3( const Key* const restrict key, int len, union ScoringDecodedMessage* output );
inline extern
double ComputeIcscoreFromDecodedMsgSsse3( union ScoringDecodedMessage* msg, scoreLength_t len );
uint16_t ComputeIcscoreFromDecodedMsgSsse3( union ScoringDecodedMessage* msg, scoreLength_t len );
inline extern
int ComputeBiscoreFromDecodedMsgSse2( union ScoringDecodedMessage* msg, scoreLength_t len );

Expand Down
10 changes: 5 additions & 5 deletions libEnigma/src/x86/scoreAvx.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
#include "score_inlines.h"

// SSSE3 scores
static double icscoreAvx( const Key* const restrict key, scoreLength_t len );
static int uniscoreAvx( const Key* const restrict key, scoreLength_t len );
static int biscoreAvx( const Key* const restrict key, scoreLength_t len );
static int triscoreAvx( const Key* const restrict key, scoreLength_t len );
static uint16_t icscoreAvx( const Key* const restrict key, scoreLength_t len );
static int uniscoreAvx( const Key* const restrict key, scoreLength_t len );
static int biscoreAvx( const Key* const restrict key, scoreLength_t len );
static int triscoreAvx( const Key* const restrict key, scoreLength_t len );

enigma_score_function_t enigmaScoreAvx = { triscoreAvx, biscoreAvx , icscoreAvx, uniscoreAvx } ;

union ScoringDecodedMessage decodedMsgPartAvx;

__attribute__ ((flatten))
__attribute__ ((optimize("unroll-loops")))
static double icscoreAvx( const Key* const restrict key, scoreLength_t len ) {
static uint16_t icscoreAvx( const Key* const restrict key, scoreLength_t len ) {
DecodeScoredMessagePartSsse3( key, len, &decodedMsgPartAvx );
return ComputeIcscoreFromDecodedMsgSsse3( &decodedMsgPartAvx, len );
}
Expand Down
10 changes: 5 additions & 5 deletions libEnigma/src/x86/scoreAvx2.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#include "x86/cipherSsse3_inlines.h"

// SSSE3 scores
static double icscoreAvx2( const Key* const restrict key, scoreLength_t len );
static int uniscoreAvx2( const Key* const restrict key, scoreLength_t len );
static int biscoreAvx2( const Key* const restrict key, scoreLength_t len );
static int triscoreAvx2( const Key* const restrict key, scoreLength_t len );
static uint16_t icscoreAvx2( const Key* const restrict key, scoreLength_t len );
static int uniscoreAvx2( const Key* const restrict key, scoreLength_t len );
static int biscoreAvx2( const Key* const restrict key, scoreLength_t len );
static int triscoreAvx2( const Key* const restrict key, scoreLength_t len );

enigma_score_function_t enigmaScoreAvx2 = { triscoreAvx2, biscoreAvx2 , icscoreAvx2, uniscoreAvx2 } ;

Expand Down Expand Up @@ -77,7 +77,7 @@ static void DecodeScoredMessagePartAvx2( const Key* const restrict key, int len,

__attribute__ ((flatten))
__attribute__ ((optimize("unroll-loops")))
static double icscoreAvx2( const Key* const restrict key, scoreLength_t len ) {
static uint16_t icscoreAvx2( const Key* const restrict key, scoreLength_t len ) {
DecodeScoredMessagePartAvx2( key, len, &decodedMsgPartAvx2 );
return ComputeIcscoreFromDecodedMsgAvx2( &decodedMsgPartAvx2, len );
}
Expand Down
10 changes: 5 additions & 5 deletions libEnigma/src/x86/scoreSsse3.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include "score_inlines.h"

// SSSE3 scores
static double icscoreSsse3( const Key* const restrict key, scoreLength_t len );
static int uniscoreSsse3( const Key* const restrict key, scoreLength_t len );
static int biscoreSsse3( const Key* const restrict key, scoreLength_t len );
static int triscoreSsse3( const Key* const restrict key, scoreLength_t len );
static uint16_t icscoreSsse3( const Key* const restrict key, scoreLength_t len );
static int uniscoreSsse3( const Key* const restrict key, scoreLength_t len );
static int biscoreSsse3( const Key* const restrict key, scoreLength_t len );
static int triscoreSsse3( const Key* const restrict key, scoreLength_t len );

inline extern
int ComputeTriscoreFromDecodedMsgSse2( union ScoringDecodedMessage* msg, scoreLength_t len );
Expand All @@ -32,7 +32,7 @@ union ScoringDecodedMessage decodedMsgPartSsse3;

__attribute__ ((flatten))
__attribute__ ((optimize("unroll-loops")))
static double icscoreSsse3( const Key* const restrict key, scoreLength_t len ) {
static uint16_t icscoreSsse3( const Key* const restrict key, scoreLength_t len ) {
DecodeScoredMessagePartSsse3( key, len, &decodedMsgPartSsse3 );
return ComputeIcscoreFromDecodedMsgSsse3( &decodedMsgPartSsse3, len );
}
Expand Down

0 comments on commit 4b19171

Please sign in to comment.