Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if 1
- // try this one first
- #define BCH_POLY 0b001110010101
- #else
- // other possibility
- #define BCH_POLY 0b010100111001
- #endif
- // we use the top 12 bits since it's more convenient for left-shifting
- #if 1
- unsigned omap3_mlc_bch_shl1( unsigned x )
- {
- if( x >> 31 )
- return ( x << 1 ) ^ ( BCH_POLY << 20 );
- else
- return ( x << 1 );
- }
- #else
- // maybe more efficient version?
- unsigned omap3_mlc_bch_shl1( unsigned x )
- {
- return ( x << 1 ) ^ ( ( BCH_POLY << 20 ) & -( x >> 31 ) );
- }
- #endif
- unsigned omap3_mlc_bch_word( uint16_t w )
- {
- // put 20-bit data word (incl padding) into top 20 bits
- unsigned x = w << 16;
- // shift left and reduce to 12 bits (again in top 12 bits)
- for( int i = 0; i < 20; ++i )
- x = omap3_mlc_bch_shl1( x );
- return w << 16 | x >> 20;
- }
- void omap3_mlc_bch( unsigned encoded[260], uint16_t const data[256] )
- {
- unsigned checksum = 0;
- for( int i = 0; i < 256; ++i ) {
- checksum += data[i];
- encoded[i] = omap3_mlc_bch_word( data[i] );
- }
- encoded[256] = omap3_mlc_bch_word( checksum & 0xffff );
- encoded[257] = 0;
- encoded[258] = 0;
- encoded[259] = 0;
- }
- void omap3_mlc_bch_verify( unsigned const encoded[260] )
- {
- for( int i = 0; i < 160; ++i ) {
- unsigned x = encoded[i];
- for( int i = 0; i < 32; ++i )
- x = omap3_mlc_bch_shl1( x );
- if( x != 0 )
- printf( "encoded[%3d] = 0x%08x verification failed (0x%03x != 0)\n", i, encoded[i], x >> 20 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment