···11+/*22+ * Copyright 2015 Advanced Micro Devices, Inc.33+ *44+ * Permission is hereby granted, free of charge, to any person obtaining a55+ * copy of this software and associated documentation files (the "Software"),66+ * to deal in the Software without restriction, including without limitation77+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,88+ * and/or sell copies of the Software, and to permit persons to whom the99+ * Software is furnished to do so, subject to the following conditions:1010+ *1111+ * The above copyright notice and this permission notice shall be included in1212+ * all copies or substantial portions of the Software.1313+ *1414+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1515+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1616+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL1717+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR1818+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,1919+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR2020+ * OTHER DEALINGS IN THE SOFTWARE.2121+ *2222+ */2323+#include <asm/div64.h>2424+2525+#define SHIFT_AMOUNT 16 /* We multiply all original integers with 2^SHIFT_AMOUNT to get the fInt representation */2626+2727+#define PRECISION 5 /* Change this value to change the number of decimal places in the final output - 5 is a good default */2828+2929+#define SHIFTED_2 (2 << SHIFT_AMOUNT)3030+#define MAX (1 << (SHIFT_AMOUNT - 1)) - 1 /* 32767 - Might change in the future */3131+3232+/* -------------------------------------------------------------------------------3333+ * NEW TYPE - fINT3434+ * -------------------------------------------------------------------------------3535+ * A variable of type fInt can be accessed in 3 ways using the dot (.) operator3636+ * fInt A;3737+ * A.full => The full number as it is. Generally not easy to read3838+ * A.partial.real => Only the integer portion3939+ * A.partial.decimal => Only the fractional portion4040+ */4141+typedef union _fInt {4242+ int full;4343+ struct _partial {4444+ unsigned int decimal: SHIFT_AMOUNT; /*Needs to always be unsigned*/4545+ int real: 32 - SHIFT_AMOUNT;4646+ } partial;4747+} fInt;4848+4949+/* -------------------------------------------------------------------------------5050+ * Function Declarations5151+ * -------------------------------------------------------------------------------5252+ */5353+fInt ConvertToFraction(int); /* Use this to convert an INT to a FINT */5454+fInt Convert_ULONG_ToFraction(uint32_t); /* Use this to convert an uint32_t to a FINT */5555+fInt GetScaledFraction(int, int); /* Use this to convert an INT to a FINT after scaling it by a factor */5656+int ConvertBackToInteger(fInt); /* Convert a FINT back to an INT that is scaled by 1000 (i.e. last 3 digits are the decimal digits) */5757+5858+fInt fNegate(fInt); /* Returns -1 * input fInt value */5959+fInt fAdd (fInt, fInt); /* Returns the sum of two fInt numbers */6060+fInt fSubtract (fInt A, fInt B); /* Returns A-B - Sometimes easier than Adding negative numbers */6161+fInt fMultiply (fInt, fInt); /* Returns the product of two fInt numbers */6262+fInt fDivide (fInt A, fInt B); /* Returns A/B */6363+fInt fGetSquare(fInt); /* Returns the square of a fInt number */6464+fInt fSqrt(fInt); /* Returns the Square Root of a fInt number */6565+6666+int uAbs(int); /* Returns the Absolute value of the Int */6767+fInt fAbs(fInt); /* Returns the Absolute value of the fInt */6868+int uPow(int base, int exponent); /* Returns base^exponent an INT */6969+7070+void SolveQuadracticEqn(fInt, fInt, fInt, fInt[]); /* Returns the 2 roots via the array */7171+bool Equal(fInt, fInt); /* Returns true if two fInts are equal to each other */7272+bool GreaterThan(fInt A, fInt B); /* Returns true if A > B */7373+7474+fInt fExponential(fInt exponent); /* Can be used to calculate e^exponent */7575+fInt fNaturalLog(fInt value); /* Can be used to calculate ln(value) */7676+7777+/* Fuse decoding functions7878+ * -------------------------------------------------------------------------------------7979+ */8080+fInt fDecodeLinearFuse(uint32_t fuse_value, fInt f_min, fInt f_range, uint32_t bitlength);8181+fInt fDecodeLogisticFuse(uint32_t fuse_value, fInt f_average, fInt f_range, uint32_t bitlength);8282+fInt fDecodeLeakageID (uint32_t leakageID_fuse, fInt ln_max_div_min, fInt f_min, uint32_t bitlength);8383+8484+/* Internal Support Functions - Use these ONLY for testing or adding to internal functions8585+ * -------------------------------------------------------------------------------------8686+ * Some of the following functions take two INTs as their input - This is unsafe for a variety of reasons.8787+ */8888+fInt Add (int, int); /* Add two INTs and return Sum as FINT */8989+fInt Multiply (int, int); /* Multiply two INTs and return Product as FINT */9090+fInt Divide (int, int); /* You get the idea... */9191+fInt fNegate(fInt);9292+9393+int uGetScaledDecimal (fInt); /* Internal function */9494+int GetReal (fInt A); /* Internal function */9595+9696+/* Future Additions and Incomplete Functions9797+ * -------------------------------------------------------------------------------------9898+ */9999+int GetRoundedValue(fInt); /* Incomplete function - Useful only when Precision is lacking */100100+ /* Let us say we have 2.126 but can only handle 2 decimal points. We could */101101+ /* either chop of 6 and keep 2.12 or use this function to get 2.13, which is more accurate */102102+103103+/* -------------------------------------------------------------------------------------104104+ * TROUBLESHOOTING INFORMATION105105+ * -------------------------------------------------------------------------------------106106+ * 1) ConvertToFraction - InputOutOfRangeException: Only accepts numbers smaller than MAX (default: 32767)107107+ * 2) fAdd - OutputOutOfRangeException: Output bigger than MAX (default: 32767)108108+ * 3) fMultiply - OutputOutOfRangeException:109109+ * 4) fGetSquare - OutputOutOfRangeException:110110+ * 5) fDivide - DivideByZeroException111111+ * 6) fSqrt - NegativeSquareRootException: Input cannot be a negative number112112+ */113113+114114+/* -------------------------------------------------------------------------------------115115+ * START OF CODE116116+ * -------------------------------------------------------------------------------------117117+ */118118+fInt fExponential(fInt exponent) /*Can be used to calculate e^exponent*/119119+{120120+ uint32_t i;121121+ bool bNegated = false;122122+123123+ fInt fPositiveOne = ConvertToFraction(1);124124+ fInt fZERO = ConvertToFraction(0);125125+126126+ fInt lower_bound = Divide(78, 10000);127127+ fInt solution = fPositiveOne; /*Starting off with baseline of 1 */128128+ fInt error_term;129129+130130+ uint32_t k_array[11] = {55452, 27726, 13863, 6931, 4055, 2231, 1178, 606, 308, 155, 78};131131+ uint32_t expk_array[11] = {2560000, 160000, 40000, 20000, 15000, 12500, 11250, 10625, 10313, 10156, 10078};132132+133133+ if (GreaterThan(fZERO, exponent)) {134134+ exponent = fNegate(exponent);135135+ bNegated = true;136136+ }137137+138138+ while (GreaterThan(exponent, lower_bound)) {139139+ for (i = 0; i < 11; i++) {140140+ if (GreaterThan(exponent, GetScaledFraction(k_array[i], 10000))) {141141+ exponent = fSubtract(exponent, GetScaledFraction(k_array[i], 10000));142142+ solution = fMultiply(solution, GetScaledFraction(expk_array[i], 10000));143143+ }144144+ }145145+ }146146+147147+ error_term = fAdd(fPositiveOne, exponent);148148+149149+ solution = fMultiply(solution, error_term);150150+151151+ if (bNegated)152152+ solution = fDivide(fPositiveOne, solution);153153+154154+ return solution;155155+}156156+157157+fInt fNaturalLog(fInt value)158158+{159159+ uint32_t i;160160+ fInt upper_bound = Divide(8, 1000);161161+ fInt fNegativeOne = ConvertToFraction(-1);162162+ fInt solution = ConvertToFraction(0); /*Starting off with baseline of 0 */163163+ fInt error_term;164164+165165+ uint32_t k_array[10] = {160000, 40000, 20000, 15000, 12500, 11250, 10625, 10313, 10156, 10078};166166+ uint32_t logk_array[10] = {27726, 13863, 6931, 4055, 2231, 1178, 606, 308, 155, 78};167167+168168+ while (GreaterThan(fAdd(value, fNegativeOne), upper_bound)) {169169+ for (i = 0; i < 10; i++) {170170+ if (GreaterThan(value, GetScaledFraction(k_array[i], 10000))) {171171+ value = fDivide(value, GetScaledFraction(k_array[i], 10000));172172+ solution = fAdd(solution, GetScaledFraction(logk_array[i], 10000));173173+ }174174+ }175175+ }176176+177177+ error_term = fAdd(fNegativeOne, value);178178+179179+ return (fAdd(solution, error_term));180180+}181181+182182+fInt fDecodeLinearFuse(uint32_t fuse_value, fInt f_min, fInt f_range, uint32_t bitlength)183183+{184184+ fInt f_fuse_value = Convert_ULONG_ToFraction(fuse_value);185185+ fInt f_bit_max_value = Convert_ULONG_ToFraction((uPow(2, bitlength)) - 1);186186+187187+ fInt f_decoded_value;188188+189189+ f_decoded_value = fDivide(f_fuse_value, f_bit_max_value);190190+ f_decoded_value = fMultiply(f_decoded_value, f_range);191191+ f_decoded_value = fAdd(f_decoded_value, f_min);192192+193193+ return f_decoded_value;194194+}195195+196196+197197+fInt fDecodeLogisticFuse(uint32_t fuse_value, fInt f_average, fInt f_range, uint32_t bitlength)198198+{199199+ fInt f_fuse_value = Convert_ULONG_ToFraction(fuse_value);200200+ fInt f_bit_max_value = Convert_ULONG_ToFraction((uPow(2, bitlength)) - 1);201201+202202+ fInt f_CONSTANT_NEG13 = ConvertToFraction(-13);203203+ fInt f_CONSTANT1 = ConvertToFraction(1);204204+205205+ fInt f_decoded_value;206206+207207+ f_decoded_value = fSubtract(fDivide(f_bit_max_value, f_fuse_value), f_CONSTANT1);208208+ f_decoded_value = fNaturalLog(f_decoded_value);209209+ f_decoded_value = fMultiply(f_decoded_value, fDivide(f_range, f_CONSTANT_NEG13));210210+ f_decoded_value = fAdd(f_decoded_value, f_average);211211+212212+ return f_decoded_value;213213+}214214+215215+fInt fDecodeLeakageID (uint32_t leakageID_fuse, fInt ln_max_div_min, fInt f_min, uint32_t bitlength)216216+{217217+ fInt fLeakage;218218+ fInt f_bit_max_value = Convert_ULONG_ToFraction((uPow(2, bitlength)) - 1);219219+220220+ fLeakage = fMultiply(ln_max_div_min, Convert_ULONG_ToFraction(leakageID_fuse));221221+ fLeakage = fDivide(fLeakage, f_bit_max_value);222222+ fLeakage = fExponential(fLeakage);223223+ fLeakage = fMultiply(fLeakage, f_min);224224+225225+ return fLeakage;226226+}227227+228228+fInt ConvertToFraction(int X) /*Add all range checking here. Is it possible to make fInt a private declaration? */229229+{230230+ fInt temp;231231+232232+ if (X <= MAX)233233+ temp.full = (X << SHIFT_AMOUNT);234234+ else235235+ temp.full = 0;236236+237237+ return temp;238238+}239239+240240+fInt fNegate(fInt X)241241+{242242+ fInt CONSTANT_NEGONE = ConvertToFraction(-1);243243+ return (fMultiply(X, CONSTANT_NEGONE));244244+}245245+246246+fInt Convert_ULONG_ToFraction(uint32_t X)247247+{248248+ fInt temp;249249+250250+ if (X <= MAX)251251+ temp.full = (X << SHIFT_AMOUNT);252252+ else253253+ temp.full = 0;254254+255255+ return temp;256256+}257257+258258+fInt GetScaledFraction(int X, int factor)259259+{260260+ int times_shifted, factor_shifted;261261+ bool bNEGATED;262262+ fInt fValue;263263+264264+ times_shifted = 0;265265+ factor_shifted = 0;266266+ bNEGATED = false;267267+268268+ if (X < 0) {269269+ X = -1*X;270270+ bNEGATED = true;271271+ }272272+273273+ if (factor < 0) {274274+ factor = -1*factor;275275+276276+ bNEGATED = !bNEGATED; /*If bNEGATED = true due to X < 0, this will cover the case of negative cancelling negative */277277+ }278278+279279+ if ((X > MAX) || factor > MAX) {280280+ if ((X/factor) <= MAX) {281281+ while (X > MAX) {282282+ X = X >> 1;283283+ times_shifted++;284284+ }285285+286286+ while (factor > MAX) {287287+ factor = factor >> 1;288288+ factor_shifted++;289289+ }290290+ } else {291291+ fValue.full = 0;292292+ return fValue;293293+ }294294+ }295295+296296+ if (factor == 1)297297+ return (ConvertToFraction(X));298298+299299+ fValue = fDivide(ConvertToFraction(X * uPow(-1, bNEGATED)), ConvertToFraction(factor));300300+301301+ fValue.full = fValue.full << times_shifted;302302+ fValue.full = fValue.full >> factor_shifted;303303+304304+ return fValue;305305+}306306+307307+/* Addition using two fInts */308308+fInt fAdd (fInt X, fInt Y)309309+{310310+ fInt Sum;311311+312312+ Sum.full = X.full + Y.full;313313+314314+ return Sum;315315+}316316+317317+/* Addition using two fInts */318318+fInt fSubtract (fInt X, fInt Y)319319+{320320+ fInt Difference;321321+322322+ Difference.full = X.full - Y.full;323323+324324+ return Difference;325325+}326326+327327+bool Equal(fInt A, fInt B)328328+{329329+ if (A.full == B.full)330330+ return true;331331+ else332332+ return false;333333+}334334+335335+bool GreaterThan(fInt A, fInt B)336336+{337337+ if (A.full > B.full)338338+ return true;339339+ else340340+ return false;341341+}342342+343343+fInt fMultiply (fInt X, fInt Y) /* Uses 64-bit integers (int64_t) */344344+{345345+ fInt Product;346346+ int64_t tempProduct;347347+ bool X_LessThanOne, Y_LessThanOne;348348+349349+ X_LessThanOne = (X.partial.real == 0 && X.partial.decimal != 0 && X.full >= 0);350350+ Y_LessThanOne = (Y.partial.real == 0 && Y.partial.decimal != 0 && Y.full >= 0);351351+352352+ /*The following is for a very specific common case: Non-zero number with ONLY fractional portion*/353353+ /* TEMPORARILY DISABLED - CAN BE USED TO IMPROVE PRECISION354354+355355+ if (X_LessThanOne && Y_LessThanOne) {356356+ Product.full = X.full * Y.full;357357+ return Product358358+ }*/359359+360360+ tempProduct = ((int64_t)X.full) * ((int64_t)Y.full); /*Q(16,16)*Q(16,16) = Q(32, 32) - Might become a negative number! */361361+ tempProduct = tempProduct >> 16; /*Remove lagging 16 bits - Will lose some precision from decimal; */362362+ Product.full = (int)tempProduct; /*The int64_t will lose the leading 16 bits that were part of the integer portion */363363+364364+ return Product;365365+}366366+367367+fInt fDivide (fInt X, fInt Y)368368+{369369+ fInt fZERO, fQuotient;370370+ int64_t longlongX, longlongY;371371+372372+ fZERO = ConvertToFraction(0);373373+374374+ if (Equal(Y, fZERO))375375+ return fZERO;376376+377377+ longlongX = (int64_t)X.full;378378+ longlongY = (int64_t)Y.full;379379+380380+ longlongX = longlongX << 16; /*Q(16,16) -> Q(32,32) */381381+382382+ do_div(longlongX, longlongY); /*Q(32,32) divided by Q(16,16) = Q(16,16) Back to original format */383383+384384+ fQuotient.full = (int)longlongX;385385+ return fQuotient;386386+}387387+388388+int ConvertBackToInteger (fInt A) /*THIS is the function that will be used to check with the Golden settings table*/389389+{390390+ fInt fullNumber, scaledDecimal, scaledReal;391391+392392+ scaledReal.full = GetReal(A) * uPow(10, PRECISION-1); /* DOUBLE CHECK THISSSS!!! */393393+394394+ scaledDecimal.full = uGetScaledDecimal(A);395395+396396+ fullNumber = fAdd(scaledDecimal,scaledReal);397397+398398+ return fullNumber.full;399399+}400400+401401+fInt fGetSquare(fInt A)402402+{403403+ return fMultiply(A,A);404404+}405405+406406+/* x_new = x_old - (x_old^2 - C) / (2 * x_old) */407407+fInt fSqrt(fInt num)408408+{409409+ fInt F_divide_Fprime, Fprime;410410+ fInt test;411411+ fInt twoShifted;412412+ int seed, counter, error;413413+ fInt x_new, x_old, C, y;414414+415415+ fInt fZERO = ConvertToFraction(0);416416+ /* (0 > num) is the same as (num < 0), i.e., num is negative */417417+ if (GreaterThan(fZERO, num) || Equal(fZERO, num))418418+ return fZERO;419419+420420+ C = num;421421+422422+ if (num.partial.real > 3000)423423+ seed = 60;424424+ else if (num.partial.real > 1000)425425+ seed = 30;426426+ else if (num.partial.real > 100)427427+ seed = 10;428428+ else429429+ seed = 2;430430+431431+ counter = 0;432432+433433+ if (Equal(num, fZERO)) /*Square Root of Zero is zero */434434+ return fZERO;435435+436436+ twoShifted = ConvertToFraction(2);437437+ x_new = ConvertToFraction(seed);438438+439439+ do {440440+ counter++;441441+442442+ x_old.full = x_new.full;443443+444444+ test = fGetSquare(x_old); /*1.75*1.75 is reverting back to 1 when shifted down */445445+ y = fSubtract(test, C); /*y = f(x) = x^2 - C; */446446+447447+ Fprime = fMultiply(twoShifted, x_old);448448+ F_divide_Fprime = fDivide(y, Fprime);449449+450450+ x_new = fSubtract(x_old, F_divide_Fprime);451451+452452+ error = ConvertBackToInteger(x_new) - ConvertBackToInteger(x_old);453453+454454+ if (counter > 20) /*20 is already way too many iterations. If we dont have an answer by then, we never will*/455455+ return x_new;456456+457457+ } while (uAbs(error) > 0);458458+459459+ return (x_new);460460+}461461+462462+void SolveQuadracticEqn(fInt A, fInt B, fInt C, fInt Roots[])463463+{464464+ fInt* pRoots = &Roots[0];465465+ fInt temp, root_first, root_second;466466+ fInt f_CONSTANT10, f_CONSTANT100;467467+468468+ f_CONSTANT100 = ConvertToFraction(100);469469+ f_CONSTANT10 = ConvertToFraction(10);470470+471471+ while(GreaterThan(A, f_CONSTANT100) || GreaterThan(B, f_CONSTANT100) || GreaterThan(C, f_CONSTANT100)) {472472+ A = fDivide(A, f_CONSTANT10);473473+ B = fDivide(B, f_CONSTANT10);474474+ C = fDivide(C, f_CONSTANT10);475475+ }476476+477477+ temp = fMultiply(ConvertToFraction(4), A); /* root = 4*A */478478+ temp = fMultiply(temp, C); /* root = 4*A*C */479479+ temp = fSubtract(fGetSquare(B), temp); /* root = b^2 - 4AC */480480+ temp = fSqrt(temp); /*root = Sqrt (b^2 - 4AC); */481481+482482+ root_first = fSubtract(fNegate(B), temp); /* b - Sqrt(b^2 - 4AC) */483483+ root_second = fAdd(fNegate(B), temp); /* b + Sqrt(b^2 - 4AC) */484484+485485+ root_first = fDivide(root_first, ConvertToFraction(2)); /* [b +- Sqrt(b^2 - 4AC)]/[2] */486486+ root_first = fDivide(root_first, A); /*[b +- Sqrt(b^2 - 4AC)]/[2*A] */487487+488488+ root_second = fDivide(root_second, ConvertToFraction(2)); /* [b +- Sqrt(b^2 - 4AC)]/[2] */489489+ root_second = fDivide(root_second, A); /*[b +- Sqrt(b^2 - 4AC)]/[2*A] */490490+491491+ *(pRoots + 0) = root_first;492492+ *(pRoots + 1) = root_second;493493+}494494+495495+/* -----------------------------------------------------------------------------496496+ * SUPPORT FUNCTIONS497497+ * -----------------------------------------------------------------------------498498+ */499499+500500+/* Addition using two normal ints - Temporary - Use only for testing purposes?. */501501+fInt Add (int X, int Y)502502+{503503+ fInt A, B, Sum;504504+505505+ A.full = (X << SHIFT_AMOUNT);506506+ B.full = (Y << SHIFT_AMOUNT);507507+508508+ Sum.full = A.full + B.full;509509+510510+ return Sum;511511+}512512+513513+/* Conversion Functions */514514+int GetReal (fInt A)515515+{516516+ return (A.full >> SHIFT_AMOUNT);517517+}518518+519519+/* Temporarily Disabled */520520+int GetRoundedValue(fInt A) /*For now, round the 3rd decimal place */521521+{522522+ /* ROUNDING TEMPORARLY DISABLED523523+ int temp = A.full;524524+525525+ int decimal_cutoff, decimal_mask = 0x000001FF;526526+527527+ decimal_cutoff = temp & decimal_mask;528528+529529+530530+ if (decimal_cutoff > 0x147) {531531+ temp += 673;532532+ }*/533533+534534+ return ConvertBackToInteger(A)/10000; /*Temporary - in case this was used somewhere else */535535+}536536+537537+fInt Multiply (int X, int Y)538538+{539539+ fInt A, B, Product;540540+541541+ A.full = X << SHIFT_AMOUNT;542542+ B.full = Y << SHIFT_AMOUNT;543543+544544+ Product = fMultiply(A, B);545545+546546+ return Product;547547+}548548+fInt Divide (int X, int Y)549549+{550550+ fInt A, B, Quotient;551551+552552+ A.full = X << SHIFT_AMOUNT;553553+ B.full = Y << SHIFT_AMOUNT;554554+555555+ Quotient = fDivide(A, B);556556+557557+ return Quotient;558558+}559559+560560+int uGetScaledDecimal (fInt A) /*Converts the fractional portion to whole integers - Costly function */561561+{562562+ int dec[PRECISION];563563+ int i, scaledDecimal = 0, tmp = A.partial.decimal;564564+565565+ for (i = 0; i < PRECISION; i++) {566566+ dec[i] = tmp / (1 << SHIFT_AMOUNT);567567+568568+ tmp = tmp - ((1 << SHIFT_AMOUNT)*dec[i]);569569+570570+ tmp *= 10;571571+572572+ scaledDecimal = scaledDecimal + dec[i]*uPow(10, PRECISION - 1 -i);573573+ }574574+575575+ return scaledDecimal;576576+}577577+578578+int uPow(int base, int power)579579+{580580+ if (power == 0)581581+ return 1;582582+ else583583+ return (base)*uPow(base, power - 1);584584+}585585+586586+fInt fAbs(fInt A)587587+{588588+ if (A.partial.real < 0)589589+ return (fMultiply(A, ConvertToFraction(-1)));590590+ else591591+ return A;592592+}593593+594594+int uAbs(int X)595595+{596596+ if (X < 0)597597+ return (X * -1);598598+ else599599+ return X;600600+}601601+602602+fInt fRoundUpByStepSize(fInt A, fInt fStepSize, bool error_term)603603+{604604+ fInt solution;605605+606606+ solution = fDivide(A, fStepSize);607607+ solution.partial.decimal = 0; /*All fractional digits changes to 0 */608608+609609+ if (error_term)610610+ solution.partial.real += 1; /*Error term of 1 added */611611+612612+ solution = fMultiply(solution, fStepSize);613613+ solution = fAdd(solution, fStepSize);614614+615615+ return solution;616616+}617617+