Where is the adjustment calculation for the target value in the source code?

Can someone tell me the source code and explain how the average is calculated for the difficulty adjustment that takes place every 2016?