top of page

Stage 3 Optimization: Hot Function(closer look part2)

The main function I will be working on is processBuffer


static void processBuffer(struct NESSIEstruct * const structpointer)


This function looks to be doing 2 main things to cipher the hash inside the struct(block of characters we wish to hash).

1. << and >> Bitshifting


AND


2. ^ the XOR (Magical Bitwise Operator)


I will briefly explain each one before moving forward because I did not understand how these two concepts worked or how they altered the code before I began working on this project.

Perhaps it might help to inform someone who also does not understand.

If this is common knowledge to you please feel free to skip over.


*********** Bitshifting << and >> operators ***********


What they do:

Shift the value of a bit to the left(<<) or right(>>)


Example:

Lets say we have a for loop

for (int a = 0; a < 10; a++ )

{

var = 10 << a;

printf("%d \n", var);

}

It will print out

10

20

40

80

160

320

640

1280

2560

5120

Why:

//128 64 32 16 8 4 2 1

// 0 0 0 0 0 0 0 0

If we want to write the number 10 into bit value it would be

//8 4 2 0

1 0 1 0 this is 10

Now the for loop STARTS at 0, which means we shift the bits ZERO times to the LEFT

The next iteration is at 1, which means we shift the bits ONE value over, so

//8 4 2 0 //16 8 4 2 1

1 0 1 0 (10 before bitshift) 1 0 1 0 0 (20 after bitshift)


Notice how all the bits shifted one value to the left. The next iteration in the for loop will be 2, so the bits will be shifted 2 positions to the left from their original position of 10.


And when the operator is pointing the opposite direction to the right >>, than the bits shift to the right.


How to implement:

$baseValue $operator $BitsShifted

1 << 5

therefore, 1 has 5 bits shifted and becomes 32.


NOTE:

A great way to double the value of any number is too simply shift its bit 1 over. And that goes for division as well.


*********** ^ the XOR (Magical Bitwise Operator) ***********


What it does:

This takes 2 number and flips the bits of the first and second number which are opposite of one another, or are both 1, in the case of both being zero's nothing is changed.


Example:

I found this one a bit more confusing but once you understand it becomes very simple.

3 ^ 5, will create 6.

8 ^ 11, will create 3.

8 ^ 15, will create 7.


Why:

Lets take a look at the first 3 ^ 5 example.

//128 64 32 16 8 4 2 1

3 0 1 1

5 1 0 1

Remember we are comparing everything to the first number 3's column bits, and switching those, the 5's columns are only used to compare FROM.

Number 3's first number is 1, and number 5's first number is 1, so number 3's bit is flipped to zero.

Number 3's second number is 1 however, number 5's second number is 0, so number 3's bit is left alone.

Number 3's third number is a 0, but number 5's third number is a 1, they are opposite and because it is a 1 number 3's bit value is flipped to 1.

The result gives us 6

//4 2 1

1 1 0


======================================================================================================================================================

**** THE REASON WHY I AM FOCUSING ON THE ABOVE 2 OPERATIONS IS BECAUSE THEY ARE THE MAIN OPERATIONS INSIDE OF THE processBuffer FUNCTION.

======================================================================================================================================================


5 views0 comments

Recent Posts

See All

Closing Thoughts

For my final blog post I would like to discuss what I have learned and plan to utilize in the future from this course. So although I was not able to successfully improve my package to operate function

Stage 3 Optimization(COMPUTER ARCHITECTURE ENDIANESS)

Seeing as how the compiler flags did not provide any optimization I will on to my next attempt which is converting big endian to small endian. The aarch64 architecture uses the little endian byte orde

Stage 3 Optimization(Compiler Flags)

My first attempt to optimize the project will be to work with the compiler flag options. By default the compiler is set to compile in this manner "gcc -E -g -o2" The -E option represents preprocesses

bottom of page