the power function of the SGI STL is used to calculate the nth exponent of a number, such as the n power n for x.= -( -Binary10100) 1 0 1 0 0 -=2^4+2^2x^ -= x^ ((2^4) + (2^2)) = x^ (2^4) * x ^ (2^2) Part2 Part1template<class_TP,class_integer,class_monoidoperation>_tp __power (_tp __x, _integer __n, _monoidoperation __opr) {if(__n = =0) returnidentity_element (__OPR); Else { while((__n &1) ==0) {//__n >>=1;//This while loop is part1 to find out__x = __opr (__x, __x);//}_TP __result= __x;//the value of the current x is Part1__n >>=1;//Here's a _result record of part1, so here n just move right one. while(__n! =0) {//__x = __opr (__x, __x);//ask for Part2 (if more part is also calculated in this loop, the instructions here are only for 20 powers) if((__n &1) !=0)//__result = __opr (__result, __x);//result = result * Part2 record only if a bit with a value of 1 is encountered (result = Part1 at this time)//because only a bit is 1 to find the next part__n >>=1;//n = n/2; } return__result; }}Template<typename t>structMultiplies {typedef T first_argument_type; typedef T SECOND_ARGUMENT_TYPE; typedef T RESULT_TYPE; __host__ __device__ Toperator()(ConstT &LHS,ConstT &RHS)Const{returnLHS *RHS;} }; //End Multiplies
A personal understanding of the power functions of SGI STL