QuickMul Practical FFT-based Integer Multiplication
- 格式:pdf
- 大小:94.68 KB
- 文档页数:4
QuickMul:PracticalFFT-basedIntegerMultiplicationCheeYapandChenLiDepartmentofComputerScienceCourantInstitute,NewYorkUniversityemail:{yap,chenli}@cs.nyu.edu
October6,2000
1IntroductionTheuseofarithmeticpackagesforarbitrarilylargeintegersisgrowinginmanyareasofapplication,beyondtheirtraditionalapplicationswhichismainlyincomputeralgebra.Forinstance,theBigIntegerclasslibrarythatisconsideredastandardpartofthepopularJavaprogramminglanguage.Onerecentareaofapplicationisinrobustgeometricalgorithms(e.g.,[4,2]).Thecriticalalgorithminalltheseapplicationsistheintegermultiplicationalgorithm.ThefastestknownalgorithmhereisduetoSch¨onhageandStrassen(1971)[5],achievingthetimeboundT(N)=O(NlogNloglogN)(1)
formultiplyingtwoN-bitintegers.Inthisnote,weareinterestedinexploringintegermultiplicational-gorithmswhich,liketheSch¨onhage-Strassenalgorithm,arebasedontheFastFourierTransform(FFT).Thesealgorithmsmaynotachievetherecordbound(1),buthasorderT(N)=O(NlogO(1)N)withsmallimplicitconstants.ThehopeisthatthesmallimplicitconstantsmaymakesuchalgorithmsmoreefficientforpracticalvaluesofN.TheoriginalSch¨onhage-Strassenalgorithmisrelativelycomplex;asimplifiedalgorithmisgivenin[8,chap.1]withtimeboundO(Nlog1+ǫN)foranyǫ.Suchsimplifiedalgorithmsarequiteeasytoimplement(asthispaperwillshow).Becausesuchsimplificationsarenotwell-known,manyimportantbignumberpackagescontinuetoavoidFFT-multiplicationsalgorithms.TwonotableexceptionsamongthefreelyavailablepackagesareDavidBailey’sMPFUNpackage(writteninFortran)[1]andBrunoHaible’sCLN(writteninC++)[3].Forinstance,onlyrecently(August2000)didthewidelyusedGnugmppackageimplementFFT-multiplication.
2TheOne-PrimeFFTMultiplicationThepresentpaperimplementsanFFT-multiplicationalgorithmdescribedin[7].ThebasicideaistoperformtheFFTintheringZM={0,1,...,M−1}ofnumbersmoduloM.HereMisspeciallychosenprimenumber.Itwassuggestedin[7]thatsuchanalgorithmshouldhave“small”constants,butinputnumbersmustbelessthansomefinitebutverylargelimit.Thisfinitelimitationplussmallconstantsiswhatwemean1by“practical”inthetitle.Typically,smallconstantsimplythatthealgorithmissimple.Inparticular,someoptimizationstepsintheSch¨onhage-Strassenalgorithmwillbediscarded.Wespecifychoosetwo“gamerules”helptofurtherensuresmallconstants:(1)assumea32-bitmachineand(2)insistthatthemultiplicationalgorithmisnon-recursive.Rule(1)meansthatwewantMatmost32-bitssothatarithmeticinZMcanbeperformedinO(1)time.Rule(2)doesnotimplythatwedonotuserecursionatall–indeed,theFFTalgorithmwhichwewilluseisinherentlyrecursive.However,therecursioninFFTalgorithmsisrelativelysimpleanddonotincurlargeoverhead.So,whatdoesrule(2)reallyexclude?AsimplifiedFFT-basedmultiplicationalgorithmhasthefollowingbasicsteps.AssumewearegiventwoN-bitintegersUandV,andwewanttocomputetheirproduceW=UV.
I.PreparationBreakupUandVintotwoK-vectorsVin(ZM)K(forsuitablenaturalnumbersKandM).
II.DFTComputationComputetheirdiscreteFouriertransforms,DFT(V)∈(ZM)K.III.Component-wiseProductComputethecomponent-wiseproductofDFT(V).Thisresultsinavectorin(ZM)KwhichwemaydenoteasDFT(
W)toobtainWistheconvolutionofV.
V.Re-assemblyItisrelativelystraightforwardtore-assembleW=U·Vfrom
U=(0,0,...,0,UK−1,UK−2,...,U0).Similarly,letLetusestimatethelargestvalueofNwhichcanbeachievedunderrules(1)and(2).Supposewecomputethe2K-vectorWistheconvolutionofV,itiseasytoseethateachWiisthesumof≤Knumbers,eachwith≤Lbits.ThusWihas≤2L+lg(K)bits.[Note:lgislogarithmtobase2.]Soweneed
2L+lg(K)≤lgM=30.9....SinceLandlg(K)areintegers,wehavehave2L+lg(K)≤30.ClearlyN=KLismaximizedbymakingKaslargeaspossible.LetNmaxbethismaximumvalue.SettingL=1,wehavelg(K)≤(lgM)−2orK≤M/4=503,316,480.SinceKisapowerof2,weobtainlg(K)=⌊lg(503,316,480)⌋=28orNmax=228.Sothismethodworksforintegersupto256megabitsor32MBlong(counting“1024”ratherthan“1000”asonekilo).Ingeneral,giventwonumberseachwithN≤Nmaxbits,wechoosepositiveK,LsoastominimizeK(andhencemaximizeL)andsubjectto
KL≥N,2L+lg(K)≤lgM.ThefollowingtablegivestheupperboundonNforanyL=1,2,...,8.Llg(K)814716618520422324226128
Forinstance,forN=1,000,000,wechooseL=6andK=218=262,144.Howfastcanwemultiplytwosuchnumbers?Well,FFTon2K-vectorstakes1.5(2K)lg(2K)(properlyimplemented)arithmeticoperations.Thisgives3·218·19orabout15millionfloatingpointoperations(flops).Countingafactoroffperflops,wehaveabout15fsecondsonamegaflopmachine.Thisfactorfcanbeestimatedforvariousplatforms.Forinstance,iff=20,thisis300seconds.
3ImplementationofQuickMulWehaveimplementedouralgorithminC++,inarelativelystraightforwardimplementation.Thiscodeandexperimentalresultscanbeobtainedfromourwebsitehttp://cs.nyu.edu/exact/.ThefollowingisatablecomparingouralgorithmQMUL(for“quickMul”)tothelatestGnu’sgmp(version3.1),BrunoHaible’sCLN(asincorporatedintoLiDIA).Forhistorical2reasons,wealsoincludeacomparisonwiththeoriginalGnu’sbigInteger.Theplatformforthesetestsisa“SunUltraSPARC-IIi”440MHzCPUmachinewith512MBmainmemory.