isl_aff.c 265 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096
  1. /*
  2. * Copyright 2011 INRIA Saclay
  3. * Copyright 2011 Sven Verdoolaege
  4. * Copyright 2012-2014 Ecole Normale Superieure
  5. * Copyright 2014 INRIA Rocquencourt
  6. * Copyright 2016 Sven Verdoolaege
  7. * Copyright 2018,2020 Cerebras Systems
  8. * Copyright 2021 Sven Verdoolaege
  9. *
  10. * Use of this software is governed by the MIT license
  11. *
  12. * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  13. * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  14. * 91893 Orsay, France
  15. * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
  16. * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
  17. * B.P. 105 - 78153 Le Chesnay, France
  18. * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
  19. */
  20. #include <isl_ctx_private.h>
  21. #include <isl_map_private.h>
  22. #include <isl_union_map_private.h>
  23. #include <isl_aff_private.h>
  24. #include <isl_space_private.h>
  25. #include <isl_local_space_private.h>
  26. #include <isl_vec_private.h>
  27. #include <isl_mat_private.h>
  28. #include <isl_id_private.h>
  29. #include <isl/constraint.h>
  30. #include <isl_seq.h>
  31. #include <isl/set.h>
  32. #include <isl_val_private.h>
  33. #include <isl_point_private.h>
  34. #include <isl_config.h>
  35. #undef EL_BASE
  36. #define EL_BASE aff
  37. #include <isl_list_templ.c>
  38. #include <isl_list_read_templ.c>
  39. #undef EL_BASE
  40. #define EL_BASE pw_aff
  41. #include <isl_list_templ.c>
  42. #include <isl_list_read_templ.c>
  43. #undef EL_BASE
  44. #define EL_BASE pw_multi_aff
  45. #include <isl_list_templ.c>
  46. #include <isl_list_read_templ.c>
  47. #undef EL_BASE
  48. #define EL_BASE union_pw_aff
  49. #include <isl_list_templ.c>
  50. #include <isl_list_read_templ.c>
  51. #undef EL_BASE
  52. #define EL_BASE union_pw_multi_aff
  53. #include <isl_list_templ.c>
  54. __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
  55. __isl_take isl_vec *v)
  56. {
  57. isl_aff *aff;
  58. if (!ls || !v)
  59. goto error;
  60. aff = isl_calloc_type(v->ctx, struct isl_aff);
  61. if (!aff)
  62. goto error;
  63. aff->ref = 1;
  64. aff->ls = ls;
  65. aff->v = v;
  66. return aff;
  67. error:
  68. isl_local_space_free(ls);
  69. isl_vec_free(v);
  70. return NULL;
  71. }
  72. __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
  73. {
  74. isl_ctx *ctx;
  75. isl_vec *v;
  76. isl_size total;
  77. if (!ls)
  78. return NULL;
  79. ctx = isl_local_space_get_ctx(ls);
  80. if (!isl_local_space_divs_known(ls))
  81. isl_die(ctx, isl_error_invalid, "local space has unknown divs",
  82. goto error);
  83. if (!isl_local_space_is_set(ls))
  84. isl_die(ctx, isl_error_invalid,
  85. "domain of affine expression should be a set",
  86. goto error);
  87. total = isl_local_space_dim(ls, isl_dim_all);
  88. if (total < 0)
  89. goto error;
  90. v = isl_vec_alloc(ctx, 1 + 1 + total);
  91. return isl_aff_alloc_vec(ls, v);
  92. error:
  93. isl_local_space_free(ls);
  94. return NULL;
  95. }
  96. __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
  97. {
  98. if (!aff)
  99. return NULL;
  100. aff->ref++;
  101. return aff;
  102. }
  103. __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
  104. {
  105. if (!aff)
  106. return NULL;
  107. return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
  108. isl_vec_copy(aff->v));
  109. }
  110. __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
  111. {
  112. if (!aff)
  113. return NULL;
  114. if (aff->ref == 1)
  115. return aff;
  116. aff->ref--;
  117. return isl_aff_dup(aff);
  118. }
  119. __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
  120. {
  121. isl_aff *aff;
  122. aff = isl_aff_alloc(ls);
  123. if (!aff)
  124. return NULL;
  125. isl_int_set_si(aff->v->el[0], 1);
  126. isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
  127. return aff;
  128. }
  129. /* Return an affine expression that is equal to zero on domain space "space".
  130. */
  131. __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
  132. {
  133. return isl_aff_zero_on_domain(isl_local_space_from_space(space));
  134. }
  135. /* This function performs the same operation as isl_aff_zero_on_domain_space,
  136. * but is considered as a function on an isl_space when exported.
  137. */
  138. __isl_give isl_aff *isl_space_zero_aff_on_domain(__isl_take isl_space *space)
  139. {
  140. return isl_aff_zero_on_domain_space(space);
  141. }
  142. /* Return a piecewise affine expression defined on the specified domain
  143. * that is equal to zero.
  144. */
  145. __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
  146. {
  147. return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
  148. }
  149. /* Change "aff" into a NaN.
  150. *
  151. * Note that this function gets called from isl_aff_nan_on_domain,
  152. * so "aff" may not have been initialized yet.
  153. */
  154. static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
  155. {
  156. aff = isl_aff_cow(aff);
  157. if (!aff)
  158. return NULL;
  159. aff->v = isl_vec_clr(aff->v);
  160. if (!aff->v)
  161. return isl_aff_free(aff);
  162. return aff;
  163. }
  164. /* Return an affine expression defined on the specified domain
  165. * that represents NaN.
  166. */
  167. __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
  168. {
  169. isl_aff *aff;
  170. aff = isl_aff_alloc(ls);
  171. return isl_aff_set_nan(aff);
  172. }
  173. /* Return an affine expression defined on the specified domain space
  174. * that represents NaN.
  175. */
  176. __isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
  177. {
  178. return isl_aff_nan_on_domain(isl_local_space_from_space(space));
  179. }
  180. /* Return a piecewise affine expression defined on the specified domain space
  181. * that represents NaN.
  182. */
  183. __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
  184. __isl_take isl_space *space)
  185. {
  186. return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
  187. }
  188. /* Return a piecewise affine expression defined on the specified domain
  189. * that represents NaN.
  190. */
  191. __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
  192. {
  193. return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
  194. }
  195. /* Return an affine expression that is equal to "val" on
  196. * domain local space "ls".
  197. */
  198. __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
  199. __isl_take isl_val *val)
  200. {
  201. isl_aff *aff;
  202. if (!ls || !val)
  203. goto error;
  204. if (!isl_val_is_rat(val))
  205. isl_die(isl_val_get_ctx(val), isl_error_invalid,
  206. "expecting rational value", goto error);
  207. aff = isl_aff_alloc(isl_local_space_copy(ls));
  208. if (!aff)
  209. goto error;
  210. isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
  211. isl_int_set(aff->v->el[1], val->n);
  212. isl_int_set(aff->v->el[0], val->d);
  213. isl_local_space_free(ls);
  214. isl_val_free(val);
  215. return aff;
  216. error:
  217. isl_local_space_free(ls);
  218. isl_val_free(val);
  219. return NULL;
  220. }
  221. /* Return an affine expression that is equal to "val" on domain space "space".
  222. */
  223. __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
  224. __isl_take isl_val *val)
  225. {
  226. return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
  227. }
  228. /* Return an affine expression that is equal to the specified dimension
  229. * in "ls".
  230. */
  231. __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
  232. enum isl_dim_type type, unsigned pos)
  233. {
  234. isl_space *space;
  235. isl_aff *aff;
  236. if (!ls)
  237. return NULL;
  238. space = isl_local_space_get_space(ls);
  239. if (!space)
  240. goto error;
  241. if (isl_space_is_map(space))
  242. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  243. "expecting (parameter) set space", goto error);
  244. if (isl_local_space_check_range(ls, type, pos, 1) < 0)
  245. goto error;
  246. isl_space_free(space);
  247. aff = isl_aff_alloc(ls);
  248. if (!aff)
  249. return NULL;
  250. pos += isl_local_space_offset(aff->ls, type);
  251. isl_int_set_si(aff->v->el[0], 1);
  252. isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
  253. isl_int_set_si(aff->v->el[1 + pos], 1);
  254. return aff;
  255. error:
  256. isl_local_space_free(ls);
  257. isl_space_free(space);
  258. return NULL;
  259. }
  260. /* Return a piecewise affine expression that is equal to
  261. * the specified dimension in "ls".
  262. */
  263. __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
  264. enum isl_dim_type type, unsigned pos)
  265. {
  266. return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
  267. }
  268. /* Return an affine expression that is equal to the parameter
  269. * in the domain space "space" with identifier "id".
  270. */
  271. __isl_give isl_aff *isl_aff_param_on_domain_space_id(
  272. __isl_take isl_space *space, __isl_take isl_id *id)
  273. {
  274. int pos;
  275. isl_local_space *ls;
  276. if (!space || !id)
  277. goto error;
  278. pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
  279. if (pos < 0)
  280. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  281. "parameter not found in space", goto error);
  282. isl_id_free(id);
  283. ls = isl_local_space_from_space(space);
  284. return isl_aff_var_on_domain(ls, isl_dim_param, pos);
  285. error:
  286. isl_space_free(space);
  287. isl_id_free(id);
  288. return NULL;
  289. }
  290. /* This function performs the same operation as
  291. * isl_aff_param_on_domain_space_id,
  292. * but is considered as a function on an isl_space when exported.
  293. */
  294. __isl_give isl_aff *isl_space_param_aff_on_domain_id(
  295. __isl_take isl_space *space, __isl_take isl_id *id)
  296. {
  297. return isl_aff_param_on_domain_space_id(space, id);
  298. }
  299. __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
  300. {
  301. if (!aff)
  302. return NULL;
  303. if (--aff->ref > 0)
  304. return NULL;
  305. isl_local_space_free(aff->ls);
  306. isl_vec_free(aff->v);
  307. free(aff);
  308. return NULL;
  309. }
  310. isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
  311. {
  312. return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
  313. }
  314. /* Return a hash value that digests "aff".
  315. */
  316. uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
  317. {
  318. uint32_t hash, ls_hash, v_hash;
  319. if (!aff)
  320. return 0;
  321. hash = isl_hash_init();
  322. ls_hash = isl_local_space_get_hash(aff->ls);
  323. isl_hash_hash(hash, ls_hash);
  324. v_hash = isl_vec_get_hash(aff->v);
  325. isl_hash_hash(hash, v_hash);
  326. return hash;
  327. }
  328. /* Return the domain local space of "aff".
  329. */
  330. static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
  331. __isl_keep isl_aff *aff)
  332. {
  333. return aff ? aff->ls : NULL;
  334. }
  335. /* Return the number of variables of the given type in the domain of "aff".
  336. */
  337. isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
  338. {
  339. isl_local_space *ls;
  340. ls = isl_aff_peek_domain_local_space(aff);
  341. return isl_local_space_dim(ls, type);
  342. }
  343. /* Externally, an isl_aff has a map space, but internally, the
  344. * ls field corresponds to the domain of that space.
  345. */
  346. isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
  347. {
  348. if (!aff)
  349. return isl_size_error;
  350. if (type == isl_dim_out)
  351. return 1;
  352. if (type == isl_dim_in)
  353. type = isl_dim_set;
  354. return isl_aff_domain_dim(aff, type);
  355. }
  356. /* Return the offset of the first coefficient of type "type" in
  357. * the domain of "aff".
  358. */
  359. isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
  360. {
  361. isl_local_space *ls;
  362. ls = isl_aff_peek_domain_local_space(aff);
  363. return isl_local_space_offset(ls, type);
  364. }
  365. /* Return the position of the dimension of the given type and name
  366. * in "aff".
  367. * Return -1 if no such dimension can be found.
  368. */
  369. int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
  370. const char *name)
  371. {
  372. if (!aff)
  373. return -1;
  374. if (type == isl_dim_out)
  375. return -1;
  376. if (type == isl_dim_in)
  377. type = isl_dim_set;
  378. return isl_local_space_find_dim_by_name(aff->ls, type, name);
  379. }
  380. /* Return the domain space of "aff".
  381. */
  382. static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
  383. {
  384. return aff ? isl_local_space_peek_space(aff->ls) : NULL;
  385. }
  386. __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
  387. {
  388. return isl_space_copy(isl_aff_peek_domain_space(aff));
  389. }
  390. __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
  391. {
  392. isl_space *space;
  393. if (!aff)
  394. return NULL;
  395. space = isl_local_space_get_space(aff->ls);
  396. space = isl_space_from_domain(space);
  397. space = isl_space_add_dims(space, isl_dim_out, 1);
  398. return space;
  399. }
  400. /* Return a copy of the domain space of "aff".
  401. */
  402. __isl_give isl_local_space *isl_aff_get_domain_local_space(
  403. __isl_keep isl_aff *aff)
  404. {
  405. return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
  406. }
  407. __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
  408. {
  409. isl_local_space *ls;
  410. if (!aff)
  411. return NULL;
  412. ls = isl_local_space_copy(aff->ls);
  413. ls = isl_local_space_from_domain(ls);
  414. ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
  415. return ls;
  416. }
  417. /* Return the local space of the domain of "aff".
  418. * This may be either a copy or the local space itself
  419. * if there is only one reference to "aff".
  420. * This allows the local space to be modified inplace
  421. * if both the expression and its local space have only a single reference.
  422. * The caller is not allowed to modify "aff" between this call and
  423. * a subsequent call to isl_aff_restore_domain_local_space.
  424. * The only exception is that isl_aff_free can be called instead.
  425. */
  426. __isl_give isl_local_space *isl_aff_take_domain_local_space(
  427. __isl_keep isl_aff *aff)
  428. {
  429. isl_local_space *ls;
  430. if (!aff)
  431. return NULL;
  432. if (aff->ref != 1)
  433. return isl_aff_get_domain_local_space(aff);
  434. ls = aff->ls;
  435. aff->ls = NULL;
  436. return ls;
  437. }
  438. /* Set the local space of the domain of "aff" to "ls",
  439. * where the local space of "aff" may be missing
  440. * due to a preceding call to isl_aff_take_domain_local_space.
  441. * However, in this case, "aff" only has a single reference and
  442. * then the call to isl_aff_cow has no effect.
  443. */
  444. __isl_give isl_aff *isl_aff_restore_domain_local_space(
  445. __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
  446. {
  447. if (!aff || !ls)
  448. goto error;
  449. if (aff->ls == ls) {
  450. isl_local_space_free(ls);
  451. return aff;
  452. }
  453. aff = isl_aff_cow(aff);
  454. if (!aff)
  455. goto error;
  456. isl_local_space_free(aff->ls);
  457. aff->ls = ls;
  458. return aff;
  459. error:
  460. isl_aff_free(aff);
  461. isl_local_space_free(ls);
  462. return NULL;
  463. }
  464. /* Externally, an isl_aff has a map space, but internally, the
  465. * ls field corresponds to the domain of that space.
  466. */
  467. const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
  468. enum isl_dim_type type, unsigned pos)
  469. {
  470. if (!aff)
  471. return NULL;
  472. if (type == isl_dim_out)
  473. return NULL;
  474. if (type == isl_dim_in)
  475. type = isl_dim_set;
  476. return isl_local_space_get_dim_name(aff->ls, type, pos);
  477. }
  478. __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
  479. __isl_take isl_space *space)
  480. {
  481. aff = isl_aff_cow(aff);
  482. if (!aff || !space)
  483. goto error;
  484. aff->ls = isl_local_space_reset_space(aff->ls, space);
  485. if (!aff->ls)
  486. return isl_aff_free(aff);
  487. return aff;
  488. error:
  489. isl_aff_free(aff);
  490. isl_space_free(space);
  491. return NULL;
  492. }
  493. /* Reset the space of "aff". This function is called from isl_pw_templ.c
  494. * and doesn't know if the space of an element object is represented
  495. * directly or through its domain. It therefore passes along both.
  496. */
  497. __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
  498. __isl_take isl_space *space, __isl_take isl_space *domain)
  499. {
  500. isl_space_free(space);
  501. return isl_aff_reset_domain_space(aff, domain);
  502. }
  503. /* Reorder the coefficients of the affine expression based
  504. * on the given reordering.
  505. * The reordering r is assumed to have been extended with the local
  506. * variables.
  507. */
  508. static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
  509. __isl_take isl_reordering *r, int n_div)
  510. {
  511. isl_space *space;
  512. isl_vec *res;
  513. isl_size dim;
  514. int i;
  515. if (!vec || !r)
  516. goto error;
  517. space = isl_reordering_peek_space(r);
  518. dim = isl_space_dim(space, isl_dim_all);
  519. if (dim < 0)
  520. goto error;
  521. res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
  522. if (!res)
  523. goto error;
  524. isl_seq_cpy(res->el, vec->el, 2);
  525. isl_seq_clr(res->el + 2, res->size - 2);
  526. for (i = 0; i < r->len; ++i)
  527. isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
  528. isl_reordering_free(r);
  529. isl_vec_free(vec);
  530. return res;
  531. error:
  532. isl_vec_free(vec);
  533. isl_reordering_free(r);
  534. return NULL;
  535. }
  536. /* Reorder the dimensions of the domain of "aff" according
  537. * to the given reordering.
  538. */
  539. __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
  540. __isl_take isl_reordering *r)
  541. {
  542. aff = isl_aff_cow(aff);
  543. if (!aff)
  544. goto error;
  545. r = isl_reordering_extend(r, aff->ls->div->n_row);
  546. aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
  547. aff->ls->div->n_row);
  548. aff->ls = isl_local_space_realign(aff->ls, r);
  549. if (!aff->v || !aff->ls)
  550. return isl_aff_free(aff);
  551. return aff;
  552. error:
  553. isl_aff_free(aff);
  554. isl_reordering_free(r);
  555. return NULL;
  556. }
  557. __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
  558. __isl_take isl_space *model)
  559. {
  560. isl_bool equal_params;
  561. if (!aff || !model)
  562. goto error;
  563. equal_params = isl_space_has_equal_params(aff->ls->dim, model);
  564. if (equal_params < 0)
  565. goto error;
  566. if (!equal_params) {
  567. isl_reordering *exp;
  568. exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
  569. exp = isl_reordering_extend_space(exp,
  570. isl_aff_get_domain_space(aff));
  571. aff = isl_aff_realign_domain(aff, exp);
  572. }
  573. isl_space_free(model);
  574. return aff;
  575. error:
  576. isl_space_free(model);
  577. isl_aff_free(aff);
  578. return NULL;
  579. }
  580. #undef TYPE
  581. #define TYPE isl_aff
  582. #include "isl_unbind_params_templ.c"
  583. /* Is "aff" obviously equal to zero?
  584. *
  585. * If the denominator is zero, then "aff" is not equal to zero.
  586. */
  587. isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
  588. {
  589. int pos;
  590. if (!aff)
  591. return isl_bool_error;
  592. if (isl_int_is_zero(aff->v->el[0]))
  593. return isl_bool_false;
  594. pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
  595. return isl_bool_ok(pos < 0);
  596. }
  597. /* Does "aff" represent NaN?
  598. */
  599. isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
  600. {
  601. if (!aff)
  602. return isl_bool_error;
  603. return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
  604. }
  605. /* Are "aff1" and "aff2" obviously equal?
  606. *
  607. * NaN is not equal to anything, not even to another NaN.
  608. */
  609. isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
  610. __isl_keep isl_aff *aff2)
  611. {
  612. isl_bool equal;
  613. if (!aff1 || !aff2)
  614. return isl_bool_error;
  615. if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
  616. return isl_bool_false;
  617. equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
  618. if (equal < 0 || !equal)
  619. return equal;
  620. return isl_vec_is_equal(aff1->v, aff2->v);
  621. }
  622. /* Return the common denominator of "aff" in "v".
  623. *
  624. * We cannot return anything meaningful in case of a NaN.
  625. */
  626. isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
  627. {
  628. if (!aff)
  629. return isl_stat_error;
  630. if (isl_aff_is_nan(aff))
  631. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  632. "cannot get denominator of NaN", return isl_stat_error);
  633. isl_int_set(*v, aff->v->el[0]);
  634. return isl_stat_ok;
  635. }
  636. /* Return the common denominator of "aff".
  637. */
  638. __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
  639. {
  640. isl_ctx *ctx;
  641. if (!aff)
  642. return NULL;
  643. ctx = isl_aff_get_ctx(aff);
  644. if (isl_aff_is_nan(aff))
  645. return isl_val_nan(ctx);
  646. return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
  647. }
  648. /* Return the constant term of "aff".
  649. */
  650. __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
  651. {
  652. isl_ctx *ctx;
  653. isl_val *v;
  654. if (!aff)
  655. return NULL;
  656. ctx = isl_aff_get_ctx(aff);
  657. if (isl_aff_is_nan(aff))
  658. return isl_val_nan(ctx);
  659. v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
  660. return isl_val_normalize(v);
  661. }
  662. /* Return the coefficient of the variable of type "type" at position "pos"
  663. * of "aff".
  664. */
  665. __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
  666. enum isl_dim_type type, int pos)
  667. {
  668. isl_ctx *ctx;
  669. isl_val *v;
  670. if (!aff)
  671. return NULL;
  672. ctx = isl_aff_get_ctx(aff);
  673. if (type == isl_dim_out)
  674. isl_die(ctx, isl_error_invalid,
  675. "output/set dimension does not have a coefficient",
  676. return NULL);
  677. if (type == isl_dim_in)
  678. type = isl_dim_set;
  679. if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
  680. return NULL;
  681. if (isl_aff_is_nan(aff))
  682. return isl_val_nan(ctx);
  683. pos += isl_local_space_offset(aff->ls, type);
  684. v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
  685. return isl_val_normalize(v);
  686. }
  687. /* Return the sign of the coefficient of the variable of type "type"
  688. * at position "pos" of "aff".
  689. */
  690. int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
  691. int pos)
  692. {
  693. isl_ctx *ctx;
  694. if (!aff)
  695. return 0;
  696. ctx = isl_aff_get_ctx(aff);
  697. if (type == isl_dim_out)
  698. isl_die(ctx, isl_error_invalid,
  699. "output/set dimension does not have a coefficient",
  700. return 0);
  701. if (type == isl_dim_in)
  702. type = isl_dim_set;
  703. if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
  704. return 0;
  705. pos += isl_local_space_offset(aff->ls, type);
  706. return isl_int_sgn(aff->v->el[1 + pos]);
  707. }
  708. /* Replace the numerator of the constant term of "aff" by "v".
  709. *
  710. * A NaN is unaffected by this operation.
  711. */
  712. __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
  713. {
  714. if (!aff)
  715. return NULL;
  716. if (isl_aff_is_nan(aff))
  717. return aff;
  718. aff = isl_aff_cow(aff);
  719. if (!aff)
  720. return NULL;
  721. aff->v = isl_vec_cow(aff->v);
  722. if (!aff->v)
  723. return isl_aff_free(aff);
  724. isl_int_set(aff->v->el[1], v);
  725. return aff;
  726. }
  727. /* Replace the constant term of "aff" by "v".
  728. *
  729. * A NaN is unaffected by this operation.
  730. */
  731. __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
  732. __isl_take isl_val *v)
  733. {
  734. if (!aff || !v)
  735. goto error;
  736. if (isl_aff_is_nan(aff)) {
  737. isl_val_free(v);
  738. return aff;
  739. }
  740. if (!isl_val_is_rat(v))
  741. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  742. "expecting rational value", goto error);
  743. if (isl_int_eq(aff->v->el[1], v->n) &&
  744. isl_int_eq(aff->v->el[0], v->d)) {
  745. isl_val_free(v);
  746. return aff;
  747. }
  748. aff = isl_aff_cow(aff);
  749. if (!aff)
  750. goto error;
  751. aff->v = isl_vec_cow(aff->v);
  752. if (!aff->v)
  753. goto error;
  754. if (isl_int_eq(aff->v->el[0], v->d)) {
  755. isl_int_set(aff->v->el[1], v->n);
  756. } else if (isl_int_is_one(v->d)) {
  757. isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
  758. } else {
  759. isl_seq_scale(aff->v->el + 1,
  760. aff->v->el + 1, v->d, aff->v->size - 1);
  761. isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
  762. isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
  763. aff->v = isl_vec_normalize(aff->v);
  764. if (!aff->v)
  765. goto error;
  766. }
  767. isl_val_free(v);
  768. return aff;
  769. error:
  770. isl_aff_free(aff);
  771. isl_val_free(v);
  772. return NULL;
  773. }
  774. /* Add "v" to the constant term of "aff".
  775. *
  776. * A NaN is unaffected by this operation.
  777. */
  778. __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
  779. {
  780. if (isl_int_is_zero(v))
  781. return aff;
  782. if (!aff)
  783. return NULL;
  784. if (isl_aff_is_nan(aff))
  785. return aff;
  786. aff = isl_aff_cow(aff);
  787. if (!aff)
  788. return NULL;
  789. aff->v = isl_vec_cow(aff->v);
  790. if (!aff->v)
  791. return isl_aff_free(aff);
  792. isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
  793. return aff;
  794. }
  795. /* Add "v" to the constant term of "aff",
  796. * in case "aff" is a rational expression.
  797. */
  798. static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
  799. __isl_take isl_val *v)
  800. {
  801. aff = isl_aff_cow(aff);
  802. if (!aff)
  803. goto error;
  804. aff->v = isl_vec_cow(aff->v);
  805. if (!aff->v)
  806. goto error;
  807. if (isl_int_is_one(v->d)) {
  808. isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
  809. } else if (isl_int_eq(aff->v->el[0], v->d)) {
  810. isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
  811. aff->v = isl_vec_normalize(aff->v);
  812. if (!aff->v)
  813. goto error;
  814. } else {
  815. isl_seq_scale(aff->v->el + 1,
  816. aff->v->el + 1, v->d, aff->v->size - 1);
  817. isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
  818. isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
  819. aff->v = isl_vec_normalize(aff->v);
  820. if (!aff->v)
  821. goto error;
  822. }
  823. isl_val_free(v);
  824. return aff;
  825. error:
  826. isl_aff_free(aff);
  827. isl_val_free(v);
  828. return NULL;
  829. }
  830. /* Return the first argument and free the second.
  831. */
  832. static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
  833. __isl_take isl_val *v)
  834. {
  835. isl_val_free(v);
  836. return aff;
  837. }
  838. /* Replace the first argument by NaN and free the second argument.
  839. */
  840. static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
  841. __isl_take isl_val *v)
  842. {
  843. isl_val_free(v);
  844. return isl_aff_set_nan(aff);
  845. }
  846. /* Add "v" to the constant term of "aff".
  847. *
  848. * A NaN is unaffected by this operation.
  849. * Conversely, adding a NaN turns "aff" into a NaN.
  850. */
  851. __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
  852. __isl_take isl_val *v)
  853. {
  854. isl_bool is_nan, is_zero, is_rat;
  855. is_nan = isl_aff_is_nan(aff);
  856. is_zero = isl_val_is_zero(v);
  857. if (is_nan < 0 || is_zero < 0)
  858. goto error;
  859. if (is_nan || is_zero)
  860. return pick_free(aff, v);
  861. is_nan = isl_val_is_nan(v);
  862. is_rat = isl_val_is_rat(v);
  863. if (is_nan < 0 || is_rat < 0)
  864. goto error;
  865. if (is_nan)
  866. return set_nan_free_val(aff, v);
  867. if (!is_rat)
  868. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  869. "expecting rational value or NaN", goto error);
  870. return isl_aff_add_rat_constant_val(aff, v);
  871. error:
  872. isl_aff_free(aff);
  873. isl_val_free(v);
  874. return NULL;
  875. }
  876. __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
  877. {
  878. isl_int t;
  879. isl_int_init(t);
  880. isl_int_set_si(t, v);
  881. aff = isl_aff_add_constant(aff, t);
  882. isl_int_clear(t);
  883. return aff;
  884. }
  885. /* Add "v" to the numerator of the constant term of "aff".
  886. *
  887. * A NaN is unaffected by this operation.
  888. */
  889. __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
  890. {
  891. if (isl_int_is_zero(v))
  892. return aff;
  893. if (!aff)
  894. return NULL;
  895. if (isl_aff_is_nan(aff))
  896. return aff;
  897. aff = isl_aff_cow(aff);
  898. if (!aff)
  899. return NULL;
  900. aff->v = isl_vec_cow(aff->v);
  901. if (!aff->v)
  902. return isl_aff_free(aff);
  903. isl_int_add(aff->v->el[1], aff->v->el[1], v);
  904. return aff;
  905. }
  906. /* Add "v" to the numerator of the constant term of "aff".
  907. *
  908. * A NaN is unaffected by this operation.
  909. */
  910. __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
  911. {
  912. isl_int t;
  913. if (v == 0)
  914. return aff;
  915. isl_int_init(t);
  916. isl_int_set_si(t, v);
  917. aff = isl_aff_add_constant_num(aff, t);
  918. isl_int_clear(t);
  919. return aff;
  920. }
  921. /* Replace the numerator of the constant term of "aff" by "v".
  922. *
  923. * A NaN is unaffected by this operation.
  924. */
  925. __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
  926. {
  927. if (!aff)
  928. return NULL;
  929. if (isl_aff_is_nan(aff))
  930. return aff;
  931. aff = isl_aff_cow(aff);
  932. if (!aff)
  933. return NULL;
  934. aff->v = isl_vec_cow(aff->v);
  935. if (!aff->v)
  936. return isl_aff_free(aff);
  937. isl_int_set_si(aff->v->el[1], v);
  938. return aff;
  939. }
  940. /* Replace the numerator of the coefficient of the variable of type "type"
  941. * at position "pos" of "aff" by "v".
  942. *
  943. * A NaN is unaffected by this operation.
  944. */
  945. __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
  946. enum isl_dim_type type, int pos, isl_int v)
  947. {
  948. if (!aff)
  949. return NULL;
  950. if (type == isl_dim_out)
  951. isl_die(aff->v->ctx, isl_error_invalid,
  952. "output/set dimension does not have a coefficient",
  953. return isl_aff_free(aff));
  954. if (type == isl_dim_in)
  955. type = isl_dim_set;
  956. if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
  957. return isl_aff_free(aff);
  958. if (isl_aff_is_nan(aff))
  959. return aff;
  960. aff = isl_aff_cow(aff);
  961. if (!aff)
  962. return NULL;
  963. aff->v = isl_vec_cow(aff->v);
  964. if (!aff->v)
  965. return isl_aff_free(aff);
  966. pos += isl_local_space_offset(aff->ls, type);
  967. isl_int_set(aff->v->el[1 + pos], v);
  968. return aff;
  969. }
  970. /* Replace the numerator of the coefficient of the variable of type "type"
  971. * at position "pos" of "aff" by "v".
  972. *
  973. * A NaN is unaffected by this operation.
  974. */
  975. __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
  976. enum isl_dim_type type, int pos, int v)
  977. {
  978. if (!aff)
  979. return NULL;
  980. if (type == isl_dim_out)
  981. isl_die(aff->v->ctx, isl_error_invalid,
  982. "output/set dimension does not have a coefficient",
  983. return isl_aff_free(aff));
  984. if (type == isl_dim_in)
  985. type = isl_dim_set;
  986. if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
  987. return isl_aff_free(aff);
  988. if (isl_aff_is_nan(aff))
  989. return aff;
  990. pos += isl_local_space_offset(aff->ls, type);
  991. if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
  992. return aff;
  993. aff = isl_aff_cow(aff);
  994. if (!aff)
  995. return NULL;
  996. aff->v = isl_vec_cow(aff->v);
  997. if (!aff->v)
  998. return isl_aff_free(aff);
  999. isl_int_set_si(aff->v->el[1 + pos], v);
  1000. return aff;
  1001. }
  1002. /* Replace the coefficient of the variable of type "type" at position "pos"
  1003. * of "aff" by "v".
  1004. *
  1005. * A NaN is unaffected by this operation.
  1006. */
  1007. __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
  1008. enum isl_dim_type type, int pos, __isl_take isl_val *v)
  1009. {
  1010. if (!aff || !v)
  1011. goto error;
  1012. if (type == isl_dim_out)
  1013. isl_die(aff->v->ctx, isl_error_invalid,
  1014. "output/set dimension does not have a coefficient",
  1015. goto error);
  1016. if (type == isl_dim_in)
  1017. type = isl_dim_set;
  1018. if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
  1019. return isl_aff_free(aff);
  1020. if (isl_aff_is_nan(aff)) {
  1021. isl_val_free(v);
  1022. return aff;
  1023. }
  1024. if (!isl_val_is_rat(v))
  1025. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  1026. "expecting rational value", goto error);
  1027. pos += isl_local_space_offset(aff->ls, type);
  1028. if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
  1029. isl_int_eq(aff->v->el[0], v->d)) {
  1030. isl_val_free(v);
  1031. return aff;
  1032. }
  1033. aff = isl_aff_cow(aff);
  1034. if (!aff)
  1035. goto error;
  1036. aff->v = isl_vec_cow(aff->v);
  1037. if (!aff->v)
  1038. goto error;
  1039. if (isl_int_eq(aff->v->el[0], v->d)) {
  1040. isl_int_set(aff->v->el[1 + pos], v->n);
  1041. } else if (isl_int_is_one(v->d)) {
  1042. isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
  1043. } else {
  1044. isl_seq_scale(aff->v->el + 1,
  1045. aff->v->el + 1, v->d, aff->v->size - 1);
  1046. isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
  1047. isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
  1048. aff->v = isl_vec_normalize(aff->v);
  1049. if (!aff->v)
  1050. goto error;
  1051. }
  1052. isl_val_free(v);
  1053. return aff;
  1054. error:
  1055. isl_aff_free(aff);
  1056. isl_val_free(v);
  1057. return NULL;
  1058. }
  1059. /* Add "v" to the coefficient of the variable of type "type"
  1060. * at position "pos" of "aff".
  1061. *
  1062. * A NaN is unaffected by this operation.
  1063. */
  1064. __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
  1065. enum isl_dim_type type, int pos, isl_int v)
  1066. {
  1067. if (!aff)
  1068. return NULL;
  1069. if (type == isl_dim_out)
  1070. isl_die(aff->v->ctx, isl_error_invalid,
  1071. "output/set dimension does not have a coefficient",
  1072. return isl_aff_free(aff));
  1073. if (type == isl_dim_in)
  1074. type = isl_dim_set;
  1075. if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
  1076. return isl_aff_free(aff);
  1077. if (isl_aff_is_nan(aff))
  1078. return aff;
  1079. aff = isl_aff_cow(aff);
  1080. if (!aff)
  1081. return NULL;
  1082. aff->v = isl_vec_cow(aff->v);
  1083. if (!aff->v)
  1084. return isl_aff_free(aff);
  1085. pos += isl_local_space_offset(aff->ls, type);
  1086. isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
  1087. return aff;
  1088. }
  1089. /* Add "v" to the coefficient of the variable of type "type"
  1090. * at position "pos" of "aff".
  1091. *
  1092. * A NaN is unaffected by this operation.
  1093. */
  1094. __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
  1095. enum isl_dim_type type, int pos, __isl_take isl_val *v)
  1096. {
  1097. if (!aff || !v)
  1098. goto error;
  1099. if (isl_val_is_zero(v)) {
  1100. isl_val_free(v);
  1101. return aff;
  1102. }
  1103. if (type == isl_dim_out)
  1104. isl_die(aff->v->ctx, isl_error_invalid,
  1105. "output/set dimension does not have a coefficient",
  1106. goto error);
  1107. if (type == isl_dim_in)
  1108. type = isl_dim_set;
  1109. if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
  1110. goto error;
  1111. if (isl_aff_is_nan(aff)) {
  1112. isl_val_free(v);
  1113. return aff;
  1114. }
  1115. if (!isl_val_is_rat(v))
  1116. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  1117. "expecting rational value", goto error);
  1118. aff = isl_aff_cow(aff);
  1119. if (!aff)
  1120. goto error;
  1121. aff->v = isl_vec_cow(aff->v);
  1122. if (!aff->v)
  1123. goto error;
  1124. pos += isl_local_space_offset(aff->ls, type);
  1125. if (isl_int_is_one(v->d)) {
  1126. isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
  1127. } else if (isl_int_eq(aff->v->el[0], v->d)) {
  1128. isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
  1129. aff->v = isl_vec_normalize(aff->v);
  1130. if (!aff->v)
  1131. goto error;
  1132. } else {
  1133. isl_seq_scale(aff->v->el + 1,
  1134. aff->v->el + 1, v->d, aff->v->size - 1);
  1135. isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
  1136. isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
  1137. aff->v = isl_vec_normalize(aff->v);
  1138. if (!aff->v)
  1139. goto error;
  1140. }
  1141. isl_val_free(v);
  1142. return aff;
  1143. error:
  1144. isl_aff_free(aff);
  1145. isl_val_free(v);
  1146. return NULL;
  1147. }
  1148. __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
  1149. enum isl_dim_type type, int pos, int v)
  1150. {
  1151. isl_int t;
  1152. isl_int_init(t);
  1153. isl_int_set_si(t, v);
  1154. aff = isl_aff_add_coefficient(aff, type, pos, t);
  1155. isl_int_clear(t);
  1156. return aff;
  1157. }
  1158. __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
  1159. {
  1160. if (!aff)
  1161. return NULL;
  1162. return isl_local_space_get_div(aff->ls, pos);
  1163. }
  1164. /* Return the negation of "aff".
  1165. *
  1166. * As a special case, -NaN = NaN.
  1167. */
  1168. __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
  1169. {
  1170. if (!aff)
  1171. return NULL;
  1172. if (isl_aff_is_nan(aff))
  1173. return aff;
  1174. aff = isl_aff_cow(aff);
  1175. if (!aff)
  1176. return NULL;
  1177. aff->v = isl_vec_cow(aff->v);
  1178. if (!aff->v)
  1179. return isl_aff_free(aff);
  1180. isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
  1181. return aff;
  1182. }
  1183. /* Remove divs from the local space that do not appear in the affine
  1184. * expression.
  1185. * We currently only remove divs at the end.
  1186. * Some intermediate divs may also not appear directly in the affine
  1187. * expression, but we would also need to check that no other divs are
  1188. * defined in terms of them.
  1189. */
  1190. __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
  1191. {
  1192. int pos;
  1193. isl_size off;
  1194. isl_size n;
  1195. n = isl_aff_domain_dim(aff, isl_dim_div);
  1196. off = isl_aff_domain_offset(aff, isl_dim_div);
  1197. if (n < 0 || off < 0)
  1198. return isl_aff_free(aff);
  1199. pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
  1200. if (pos == n)
  1201. return aff;
  1202. aff = isl_aff_cow(aff);
  1203. if (!aff)
  1204. return NULL;
  1205. aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
  1206. aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
  1207. if (!aff->ls || !aff->v)
  1208. return isl_aff_free(aff);
  1209. return aff;
  1210. }
  1211. /* Look for any divs in the aff->ls with a denominator equal to one
  1212. * and plug them into the affine expression and any subsequent divs
  1213. * that may reference the div.
  1214. */
  1215. static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
  1216. {
  1217. int i;
  1218. isl_size n;
  1219. int len;
  1220. isl_int v;
  1221. isl_vec *vec;
  1222. isl_local_space *ls;
  1223. isl_size off;
  1224. n = isl_aff_domain_dim(aff, isl_dim_div);
  1225. off = isl_aff_domain_offset(aff, isl_dim_div);
  1226. if (n < 0 || off < 0)
  1227. return isl_aff_free(aff);
  1228. len = aff->v->size;
  1229. for (i = 0; i < n; ++i) {
  1230. if (!isl_int_is_one(aff->ls->div->row[i][0]))
  1231. continue;
  1232. ls = isl_local_space_copy(aff->ls);
  1233. ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
  1234. aff->ls->div->row[i], len, i + 1, n - (i + 1));
  1235. vec = isl_vec_copy(aff->v);
  1236. vec = isl_vec_cow(vec);
  1237. if (!ls || !vec)
  1238. goto error;
  1239. isl_int_init(v);
  1240. isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
  1241. len, len, v);
  1242. isl_int_clear(v);
  1243. isl_vec_free(aff->v);
  1244. aff->v = vec;
  1245. isl_local_space_free(aff->ls);
  1246. aff->ls = ls;
  1247. }
  1248. return aff;
  1249. error:
  1250. isl_vec_free(vec);
  1251. isl_local_space_free(ls);
  1252. return isl_aff_free(aff);
  1253. }
  1254. /* Look for any divs j that appear with a unit coefficient inside
  1255. * the definitions of other divs i and plug them into the definitions
  1256. * of the divs i.
  1257. *
  1258. * In particular, an expression of the form
  1259. *
  1260. * floor((f(..) + floor(g(..)/n))/m)
  1261. *
  1262. * is simplified to
  1263. *
  1264. * floor((n * f(..) + g(..))/(n * m))
  1265. *
  1266. * This simplification is correct because we can move the expression
  1267. * f(..) into the inner floor in the original expression to obtain
  1268. *
  1269. * floor(floor((n * f(..) + g(..))/n)/m)
  1270. *
  1271. * from which we can derive the simplified expression.
  1272. */
  1273. static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
  1274. {
  1275. int i, j;
  1276. isl_size n;
  1277. isl_size off;
  1278. n = isl_aff_domain_dim(aff, isl_dim_div);
  1279. off = isl_aff_domain_offset(aff, isl_dim_div);
  1280. if (n < 0 || off < 0)
  1281. return isl_aff_free(aff);
  1282. for (i = 1; i < n; ++i) {
  1283. for (j = 0; j < i; ++j) {
  1284. if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
  1285. continue;
  1286. aff->ls = isl_local_space_substitute_seq(aff->ls,
  1287. isl_dim_div, j, aff->ls->div->row[j],
  1288. aff->v->size, i, 1);
  1289. if (!aff->ls)
  1290. return isl_aff_free(aff);
  1291. }
  1292. }
  1293. return aff;
  1294. }
  1295. /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
  1296. *
  1297. * Even though this function is only called on isl_affs with a single
  1298. * reference, we are careful to only change aff->v and aff->ls together.
  1299. */
  1300. static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
  1301. {
  1302. isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
  1303. isl_local_space *ls;
  1304. isl_vec *v;
  1305. if (off < 0)
  1306. return isl_aff_free(aff);
  1307. ls = isl_local_space_copy(aff->ls);
  1308. ls = isl_local_space_swap_div(ls, a, b);
  1309. v = isl_vec_copy(aff->v);
  1310. v = isl_vec_cow(v);
  1311. if (!ls || !v)
  1312. goto error;
  1313. isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
  1314. isl_vec_free(aff->v);
  1315. aff->v = v;
  1316. isl_local_space_free(aff->ls);
  1317. aff->ls = ls;
  1318. return aff;
  1319. error:
  1320. isl_vec_free(v);
  1321. isl_local_space_free(ls);
  1322. return isl_aff_free(aff);
  1323. }
  1324. /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
  1325. *
  1326. * We currently do not actually remove div "b", but simply add its
  1327. * coefficient to that of "a" and then zero it out.
  1328. */
  1329. static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
  1330. {
  1331. isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
  1332. if (off < 0)
  1333. return isl_aff_free(aff);
  1334. if (isl_int_is_zero(aff->v->el[1 + off + b]))
  1335. return aff;
  1336. aff->v = isl_vec_cow(aff->v);
  1337. if (!aff->v)
  1338. return isl_aff_free(aff);
  1339. isl_int_add(aff->v->el[1 + off + a],
  1340. aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
  1341. isl_int_set_si(aff->v->el[1 + off + b], 0);
  1342. return aff;
  1343. }
  1344. /* Sort the divs in the local space of "aff" according to
  1345. * the comparison function "cmp_row" in isl_local_space.c,
  1346. * combining the coefficients of identical divs.
  1347. *
  1348. * Reordering divs does not change the semantics of "aff",
  1349. * so there is no need to call isl_aff_cow.
  1350. * Moreover, this function is currently only called on isl_affs
  1351. * with a single reference.
  1352. */
  1353. static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
  1354. {
  1355. isl_size n;
  1356. int i, j;
  1357. n = isl_aff_dim(aff, isl_dim_div);
  1358. if (n < 0)
  1359. return isl_aff_free(aff);
  1360. for (i = 1; i < n; ++i) {
  1361. for (j = i - 1; j >= 0; --j) {
  1362. int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
  1363. if (cmp < 0)
  1364. break;
  1365. if (cmp == 0)
  1366. aff = merge_divs(aff, j, j + 1);
  1367. else
  1368. aff = swap_div(aff, j, j + 1);
  1369. if (!aff)
  1370. return NULL;
  1371. }
  1372. }
  1373. return aff;
  1374. }
  1375. /* Normalize the representation of "aff".
  1376. *
  1377. * This function should only be called on "new" isl_affs, i.e.,
  1378. * with only a single reference. We therefore do not need to
  1379. * worry about affecting other instances.
  1380. */
  1381. __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
  1382. {
  1383. if (!aff)
  1384. return NULL;
  1385. aff->v = isl_vec_normalize(aff->v);
  1386. if (!aff->v)
  1387. return isl_aff_free(aff);
  1388. aff = plug_in_integral_divs(aff);
  1389. aff = plug_in_unit_divs(aff);
  1390. aff = sort_divs(aff);
  1391. aff = isl_aff_remove_unused_divs(aff);
  1392. return aff;
  1393. }
  1394. /* Given f, return floor(f).
  1395. * If f is an integer expression, then just return f.
  1396. * If f is a constant, then return the constant floor(f).
  1397. * Otherwise, if f = g/m, write g = q m + r,
  1398. * create a new div d = [r/m] and return the expression q + d.
  1399. * The coefficients in r are taken to lie between -m/2 and m/2.
  1400. *
  1401. * reduce_div_coefficients performs the same normalization.
  1402. *
  1403. * As a special case, floor(NaN) = NaN.
  1404. */
  1405. __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
  1406. {
  1407. int i;
  1408. int size;
  1409. isl_ctx *ctx;
  1410. isl_vec *div;
  1411. if (!aff)
  1412. return NULL;
  1413. if (isl_aff_is_nan(aff))
  1414. return aff;
  1415. if (isl_int_is_one(aff->v->el[0]))
  1416. return aff;
  1417. aff = isl_aff_cow(aff);
  1418. if (!aff)
  1419. return NULL;
  1420. aff->v = isl_vec_cow(aff->v);
  1421. if (!aff->v)
  1422. return isl_aff_free(aff);
  1423. if (isl_aff_is_cst(aff)) {
  1424. isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
  1425. isl_int_set_si(aff->v->el[0], 1);
  1426. return aff;
  1427. }
  1428. div = isl_vec_copy(aff->v);
  1429. div = isl_vec_cow(div);
  1430. if (!div)
  1431. return isl_aff_free(aff);
  1432. ctx = isl_aff_get_ctx(aff);
  1433. isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
  1434. for (i = 1; i < aff->v->size; ++i) {
  1435. isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
  1436. isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
  1437. if (isl_int_gt(div->el[i], aff->v->el[0])) {
  1438. isl_int_sub(div->el[i], div->el[i], div->el[0]);
  1439. isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
  1440. }
  1441. }
  1442. aff->ls = isl_local_space_add_div(aff->ls, div);
  1443. if (!aff->ls)
  1444. return isl_aff_free(aff);
  1445. size = aff->v->size;
  1446. aff->v = isl_vec_extend(aff->v, size + 1);
  1447. if (!aff->v)
  1448. return isl_aff_free(aff);
  1449. isl_int_set_si(aff->v->el[0], 1);
  1450. isl_int_set_si(aff->v->el[size], 1);
  1451. aff = isl_aff_normalize(aff);
  1452. return aff;
  1453. }
  1454. /* Compute
  1455. *
  1456. * aff mod m = aff - m * floor(aff/m)
  1457. *
  1458. * with m an integer value.
  1459. */
  1460. __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
  1461. __isl_take isl_val *m)
  1462. {
  1463. isl_aff *res;
  1464. if (!aff || !m)
  1465. goto error;
  1466. if (!isl_val_is_int(m))
  1467. isl_die(isl_val_get_ctx(m), isl_error_invalid,
  1468. "expecting integer modulo", goto error);
  1469. res = isl_aff_copy(aff);
  1470. aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
  1471. aff = isl_aff_floor(aff);
  1472. aff = isl_aff_scale_val(aff, m);
  1473. res = isl_aff_sub(res, aff);
  1474. return res;
  1475. error:
  1476. isl_aff_free(aff);
  1477. isl_val_free(m);
  1478. return NULL;
  1479. }
  1480. /* Compute
  1481. *
  1482. * pwaff mod m = pwaff - m * floor(pwaff/m)
  1483. */
  1484. __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
  1485. {
  1486. isl_pw_aff *res;
  1487. res = isl_pw_aff_copy(pwaff);
  1488. pwaff = isl_pw_aff_scale_down(pwaff, m);
  1489. pwaff = isl_pw_aff_floor(pwaff);
  1490. pwaff = isl_pw_aff_scale(pwaff, m);
  1491. res = isl_pw_aff_sub(res, pwaff);
  1492. return res;
  1493. }
  1494. /* Compute
  1495. *
  1496. * pa mod m = pa - m * floor(pa/m)
  1497. *
  1498. * with m an integer value.
  1499. */
  1500. __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
  1501. __isl_take isl_val *m)
  1502. {
  1503. if (!pa || !m)
  1504. goto error;
  1505. if (!isl_val_is_int(m))
  1506. isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
  1507. "expecting integer modulo", goto error);
  1508. pa = isl_pw_aff_mod(pa, m->n);
  1509. isl_val_free(m);
  1510. return pa;
  1511. error:
  1512. isl_pw_aff_free(pa);
  1513. isl_val_free(m);
  1514. return NULL;
  1515. }
  1516. /* Given f, return ceil(f).
  1517. * If f is an integer expression, then just return f.
  1518. * Otherwise, let f be the expression
  1519. *
  1520. * e/m
  1521. *
  1522. * then return
  1523. *
  1524. * floor((e + m - 1)/m)
  1525. *
  1526. * As a special case, ceil(NaN) = NaN.
  1527. */
  1528. __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
  1529. {
  1530. if (!aff)
  1531. return NULL;
  1532. if (isl_aff_is_nan(aff))
  1533. return aff;
  1534. if (isl_int_is_one(aff->v->el[0]))
  1535. return aff;
  1536. aff = isl_aff_cow(aff);
  1537. if (!aff)
  1538. return NULL;
  1539. aff->v = isl_vec_cow(aff->v);
  1540. if (!aff->v)
  1541. return isl_aff_free(aff);
  1542. isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
  1543. isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
  1544. aff = isl_aff_floor(aff);
  1545. return aff;
  1546. }
  1547. /* Apply the expansion computed by isl_merge_divs.
  1548. * The expansion itself is given by "exp" while the resulting
  1549. * list of divs is given by "div".
  1550. */
  1551. __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
  1552. __isl_take isl_mat *div, int *exp)
  1553. {
  1554. isl_size old_n_div;
  1555. isl_size new_n_div;
  1556. isl_size offset;
  1557. aff = isl_aff_cow(aff);
  1558. offset = isl_aff_domain_offset(aff, isl_dim_div);
  1559. old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
  1560. new_n_div = isl_mat_rows(div);
  1561. if (offset < 0 || old_n_div < 0 || new_n_div < 0)
  1562. goto error;
  1563. aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
  1564. aff->ls = isl_local_space_replace_divs(aff->ls, div);
  1565. if (!aff->v || !aff->ls)
  1566. return isl_aff_free(aff);
  1567. return aff;
  1568. error:
  1569. isl_aff_free(aff);
  1570. isl_mat_free(div);
  1571. return NULL;
  1572. }
  1573. /* Add two affine expressions that live in the same local space.
  1574. */
  1575. static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
  1576. __isl_take isl_aff *aff2)
  1577. {
  1578. isl_int gcd, f;
  1579. aff1 = isl_aff_cow(aff1);
  1580. if (!aff1 || !aff2)
  1581. goto error;
  1582. aff1->v = isl_vec_cow(aff1->v);
  1583. if (!aff1->v)
  1584. goto error;
  1585. isl_int_init(gcd);
  1586. isl_int_init(f);
  1587. isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
  1588. isl_int_divexact(f, aff2->v->el[0], gcd);
  1589. isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
  1590. isl_int_divexact(f, aff1->v->el[0], gcd);
  1591. isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
  1592. isl_int_divexact(f, aff2->v->el[0], gcd);
  1593. isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
  1594. isl_int_clear(f);
  1595. isl_int_clear(gcd);
  1596. isl_aff_free(aff2);
  1597. aff1 = isl_aff_normalize(aff1);
  1598. return aff1;
  1599. error:
  1600. isl_aff_free(aff1);
  1601. isl_aff_free(aff2);
  1602. return NULL;
  1603. }
  1604. /* Replace one of the arguments by a NaN and free the other one.
  1605. */
  1606. static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
  1607. __isl_take isl_aff *aff2)
  1608. {
  1609. isl_aff_free(aff2);
  1610. return isl_aff_set_nan(aff1);
  1611. }
  1612. /* Return the sum of "aff1" and "aff2".
  1613. *
  1614. * If either of the two is NaN, then the result is NaN.
  1615. */
  1616. __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
  1617. __isl_take isl_aff *aff2)
  1618. {
  1619. isl_ctx *ctx;
  1620. int *exp1 = NULL;
  1621. int *exp2 = NULL;
  1622. isl_mat *div;
  1623. isl_size n_div1, n_div2;
  1624. if (!aff1 || !aff2)
  1625. goto error;
  1626. ctx = isl_aff_get_ctx(aff1);
  1627. if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
  1628. isl_die(ctx, isl_error_invalid,
  1629. "spaces don't match", goto error);
  1630. if (isl_aff_is_nan(aff1)) {
  1631. isl_aff_free(aff2);
  1632. return aff1;
  1633. }
  1634. if (isl_aff_is_nan(aff2)) {
  1635. isl_aff_free(aff1);
  1636. return aff2;
  1637. }
  1638. n_div1 = isl_aff_dim(aff1, isl_dim_div);
  1639. n_div2 = isl_aff_dim(aff2, isl_dim_div);
  1640. if (n_div1 < 0 || n_div2 < 0)
  1641. goto error;
  1642. if (n_div1 == 0 && n_div2 == 0)
  1643. return add_expanded(aff1, aff2);
  1644. exp1 = isl_alloc_array(ctx, int, n_div1);
  1645. exp2 = isl_alloc_array(ctx, int, n_div2);
  1646. if ((n_div1 && !exp1) || (n_div2 && !exp2))
  1647. goto error;
  1648. div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
  1649. aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
  1650. aff2 = isl_aff_expand_divs(aff2, div, exp2);
  1651. free(exp1);
  1652. free(exp2);
  1653. return add_expanded(aff1, aff2);
  1654. error:
  1655. free(exp1);
  1656. free(exp2);
  1657. isl_aff_free(aff1);
  1658. isl_aff_free(aff2);
  1659. return NULL;
  1660. }
  1661. __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
  1662. __isl_take isl_aff *aff2)
  1663. {
  1664. return isl_aff_add(aff1, isl_aff_neg(aff2));
  1665. }
  1666. /* Return the result of scaling "aff" by a factor of "f".
  1667. *
  1668. * As a special case, f * NaN = NaN.
  1669. */
  1670. __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
  1671. {
  1672. isl_int gcd;
  1673. if (!aff)
  1674. return NULL;
  1675. if (isl_aff_is_nan(aff))
  1676. return aff;
  1677. if (isl_int_is_one(f))
  1678. return aff;
  1679. aff = isl_aff_cow(aff);
  1680. if (!aff)
  1681. return NULL;
  1682. aff->v = isl_vec_cow(aff->v);
  1683. if (!aff->v)
  1684. return isl_aff_free(aff);
  1685. if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
  1686. isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
  1687. return aff;
  1688. }
  1689. isl_int_init(gcd);
  1690. isl_int_gcd(gcd, aff->v->el[0], f);
  1691. isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
  1692. isl_int_divexact(gcd, f, gcd);
  1693. isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
  1694. isl_int_clear(gcd);
  1695. return aff;
  1696. }
  1697. /* Multiple "aff" by "v".
  1698. */
  1699. __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
  1700. __isl_take isl_val *v)
  1701. {
  1702. if (!aff || !v)
  1703. goto error;
  1704. if (isl_val_is_one(v)) {
  1705. isl_val_free(v);
  1706. return aff;
  1707. }
  1708. if (!isl_val_is_rat(v))
  1709. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  1710. "expecting rational factor", goto error);
  1711. aff = isl_aff_scale(aff, v->n);
  1712. aff = isl_aff_scale_down(aff, v->d);
  1713. isl_val_free(v);
  1714. return aff;
  1715. error:
  1716. isl_aff_free(aff);
  1717. isl_val_free(v);
  1718. return NULL;
  1719. }
  1720. /* Return the result of scaling "aff" down by a factor of "f".
  1721. *
  1722. * As a special case, NaN/f = NaN.
  1723. */
  1724. __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
  1725. {
  1726. isl_int gcd;
  1727. if (!aff)
  1728. return NULL;
  1729. if (isl_aff_is_nan(aff))
  1730. return aff;
  1731. if (isl_int_is_one(f))
  1732. return aff;
  1733. aff = isl_aff_cow(aff);
  1734. if (!aff)
  1735. return NULL;
  1736. if (isl_int_is_zero(f))
  1737. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  1738. "cannot scale down by zero", return isl_aff_free(aff));
  1739. aff->v = isl_vec_cow(aff->v);
  1740. if (!aff->v)
  1741. return isl_aff_free(aff);
  1742. isl_int_init(gcd);
  1743. isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
  1744. isl_int_gcd(gcd, gcd, f);
  1745. isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
  1746. isl_int_divexact(gcd, f, gcd);
  1747. isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
  1748. isl_int_clear(gcd);
  1749. return aff;
  1750. }
  1751. /* Divide "aff" by "v".
  1752. */
  1753. __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
  1754. __isl_take isl_val *v)
  1755. {
  1756. if (!aff || !v)
  1757. goto error;
  1758. if (isl_val_is_one(v)) {
  1759. isl_val_free(v);
  1760. return aff;
  1761. }
  1762. if (!isl_val_is_rat(v))
  1763. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  1764. "expecting rational factor", goto error);
  1765. if (!isl_val_is_pos(v))
  1766. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  1767. "factor needs to be positive", goto error);
  1768. aff = isl_aff_scale(aff, v->d);
  1769. aff = isl_aff_scale_down(aff, v->n);
  1770. isl_val_free(v);
  1771. return aff;
  1772. error:
  1773. isl_aff_free(aff);
  1774. isl_val_free(v);
  1775. return NULL;
  1776. }
  1777. __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
  1778. {
  1779. isl_int v;
  1780. if (f == 1)
  1781. return aff;
  1782. isl_int_init(v);
  1783. isl_int_set_ui(v, f);
  1784. aff = isl_aff_scale_down(aff, v);
  1785. isl_int_clear(v);
  1786. return aff;
  1787. }
  1788. __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
  1789. enum isl_dim_type type, unsigned pos, const char *s)
  1790. {
  1791. aff = isl_aff_cow(aff);
  1792. if (!aff)
  1793. return NULL;
  1794. if (type == isl_dim_out)
  1795. isl_die(aff->v->ctx, isl_error_invalid,
  1796. "cannot set name of output/set dimension",
  1797. return isl_aff_free(aff));
  1798. if (type == isl_dim_in)
  1799. type = isl_dim_set;
  1800. aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
  1801. if (!aff->ls)
  1802. return isl_aff_free(aff);
  1803. return aff;
  1804. }
  1805. __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
  1806. enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
  1807. {
  1808. aff = isl_aff_cow(aff);
  1809. if (!aff)
  1810. goto error;
  1811. if (type == isl_dim_out)
  1812. isl_die(aff->v->ctx, isl_error_invalid,
  1813. "cannot set name of output/set dimension",
  1814. goto error);
  1815. if (type == isl_dim_in)
  1816. type = isl_dim_set;
  1817. aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
  1818. if (!aff->ls)
  1819. return isl_aff_free(aff);
  1820. return aff;
  1821. error:
  1822. isl_id_free(id);
  1823. isl_aff_free(aff);
  1824. return NULL;
  1825. }
  1826. /* Replace the identifier of the input tuple of "aff" by "id".
  1827. * type is currently required to be equal to isl_dim_in
  1828. */
  1829. __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
  1830. enum isl_dim_type type, __isl_take isl_id *id)
  1831. {
  1832. aff = isl_aff_cow(aff);
  1833. if (!aff)
  1834. goto error;
  1835. if (type != isl_dim_in)
  1836. isl_die(aff->v->ctx, isl_error_invalid,
  1837. "cannot only set id of input tuple", goto error);
  1838. aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
  1839. if (!aff->ls)
  1840. return isl_aff_free(aff);
  1841. return aff;
  1842. error:
  1843. isl_id_free(id);
  1844. isl_aff_free(aff);
  1845. return NULL;
  1846. }
  1847. /* Exploit the equalities in "eq" to simplify the affine expression
  1848. * and the expressions of the integer divisions in the local space.
  1849. * The integer divisions in this local space are assumed to appear
  1850. * as regular dimensions in "eq".
  1851. */
  1852. static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
  1853. __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
  1854. {
  1855. int i, j;
  1856. unsigned o_div;
  1857. unsigned n_div;
  1858. if (!eq)
  1859. goto error;
  1860. if (eq->n_eq == 0) {
  1861. isl_basic_set_free(eq);
  1862. return aff;
  1863. }
  1864. aff = isl_aff_cow(aff);
  1865. if (!aff)
  1866. goto error;
  1867. aff->ls = isl_local_space_substitute_equalities(aff->ls,
  1868. isl_basic_set_copy(eq));
  1869. aff->v = isl_vec_cow(aff->v);
  1870. if (!aff->ls || !aff->v)
  1871. goto error;
  1872. o_div = isl_basic_set_offset(eq, isl_dim_div);
  1873. n_div = eq->n_div;
  1874. for (i = 0; i < eq->n_eq; ++i) {
  1875. j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
  1876. if (j < 0 || j == 0 || j >= o_div)
  1877. continue;
  1878. isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
  1879. &aff->v->el[0]);
  1880. }
  1881. isl_basic_set_free(eq);
  1882. aff = isl_aff_normalize(aff);
  1883. return aff;
  1884. error:
  1885. isl_basic_set_free(eq);
  1886. isl_aff_free(aff);
  1887. return NULL;
  1888. }
  1889. /* Exploit the equalities in "eq" to simplify the affine expression
  1890. * and the expressions of the integer divisions in the local space.
  1891. */
  1892. __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
  1893. __isl_take isl_basic_set *eq)
  1894. {
  1895. isl_size n_div;
  1896. n_div = isl_aff_domain_dim(aff, isl_dim_div);
  1897. if (n_div < 0)
  1898. goto error;
  1899. if (n_div > 0)
  1900. eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
  1901. return isl_aff_substitute_equalities_lifted(aff, eq);
  1902. error:
  1903. isl_basic_set_free(eq);
  1904. isl_aff_free(aff);
  1905. return NULL;
  1906. }
  1907. /* Look for equalities among the variables shared by context and aff
  1908. * and the integer divisions of aff, if any.
  1909. * The equalities are then used to eliminate coefficients and/or integer
  1910. * divisions from aff.
  1911. */
  1912. __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
  1913. __isl_take isl_set *context)
  1914. {
  1915. isl_local_space *ls;
  1916. isl_basic_set *hull;
  1917. ls = isl_aff_get_domain_local_space(aff);
  1918. context = isl_local_space_lift_set(ls, context);
  1919. hull = isl_set_affine_hull(context);
  1920. return isl_aff_substitute_equalities_lifted(aff, hull);
  1921. }
  1922. __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
  1923. __isl_take isl_set *context)
  1924. {
  1925. isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
  1926. dom_context = isl_set_intersect_params(dom_context, context);
  1927. return isl_aff_gist(aff, dom_context);
  1928. }
  1929. /* Return a basic set containing those elements in the space
  1930. * of aff where it is positive. "rational" should not be set.
  1931. *
  1932. * If "aff" is NaN, then it is not positive.
  1933. */
  1934. static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
  1935. int rational, void *user)
  1936. {
  1937. isl_constraint *ineq;
  1938. isl_basic_set *bset;
  1939. isl_val *c;
  1940. if (!aff)
  1941. return NULL;
  1942. if (isl_aff_is_nan(aff)) {
  1943. isl_space *space = isl_aff_get_domain_space(aff);
  1944. isl_aff_free(aff);
  1945. return isl_basic_set_empty(space);
  1946. }
  1947. if (rational)
  1948. isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
  1949. "rational sets not supported", goto error);
  1950. ineq = isl_inequality_from_aff(aff);
  1951. c = isl_constraint_get_constant_val(ineq);
  1952. c = isl_val_sub_ui(c, 1);
  1953. ineq = isl_constraint_set_constant_val(ineq, c);
  1954. bset = isl_basic_set_from_constraint(ineq);
  1955. bset = isl_basic_set_simplify(bset);
  1956. return bset;
  1957. error:
  1958. isl_aff_free(aff);
  1959. return NULL;
  1960. }
  1961. /* Return a basic set containing those elements in the space
  1962. * of aff where it is non-negative.
  1963. * If "rational" is set, then return a rational basic set.
  1964. *
  1965. * If "aff" is NaN, then it is not non-negative (it's not negative either).
  1966. */
  1967. static __isl_give isl_basic_set *aff_nonneg_basic_set(
  1968. __isl_take isl_aff *aff, int rational, void *user)
  1969. {
  1970. isl_constraint *ineq;
  1971. isl_basic_set *bset;
  1972. if (!aff)
  1973. return NULL;
  1974. if (isl_aff_is_nan(aff)) {
  1975. isl_space *space = isl_aff_get_domain_space(aff);
  1976. isl_aff_free(aff);
  1977. return isl_basic_set_empty(space);
  1978. }
  1979. ineq = isl_inequality_from_aff(aff);
  1980. bset = isl_basic_set_from_constraint(ineq);
  1981. if (rational)
  1982. bset = isl_basic_set_set_rational(bset);
  1983. bset = isl_basic_set_simplify(bset);
  1984. return bset;
  1985. }
  1986. /* Return a basic set containing those elements in the space
  1987. * of aff where it is non-negative.
  1988. */
  1989. __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
  1990. {
  1991. return aff_nonneg_basic_set(aff, 0, NULL);
  1992. }
  1993. /* Return a basic set containing those elements in the domain space
  1994. * of "aff" where it is positive.
  1995. */
  1996. __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
  1997. {
  1998. aff = isl_aff_add_constant_num_si(aff, -1);
  1999. return isl_aff_nonneg_basic_set(aff);
  2000. }
  2001. /* Return a basic set containing those elements in the domain space
  2002. * of aff where it is negative.
  2003. */
  2004. __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
  2005. {
  2006. aff = isl_aff_neg(aff);
  2007. return isl_aff_pos_basic_set(aff);
  2008. }
  2009. /* Return a basic set containing those elements in the space
  2010. * of aff where it is zero.
  2011. * If "rational" is set, then return a rational basic set.
  2012. *
  2013. * If "aff" is NaN, then it is not zero.
  2014. */
  2015. static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
  2016. int rational, void *user)
  2017. {
  2018. isl_constraint *ineq;
  2019. isl_basic_set *bset;
  2020. if (!aff)
  2021. return NULL;
  2022. if (isl_aff_is_nan(aff)) {
  2023. isl_space *space = isl_aff_get_domain_space(aff);
  2024. isl_aff_free(aff);
  2025. return isl_basic_set_empty(space);
  2026. }
  2027. ineq = isl_equality_from_aff(aff);
  2028. bset = isl_basic_set_from_constraint(ineq);
  2029. if (rational)
  2030. bset = isl_basic_set_set_rational(bset);
  2031. bset = isl_basic_set_simplify(bset);
  2032. return bset;
  2033. }
  2034. /* Return a basic set containing those elements in the space
  2035. * of aff where it is zero.
  2036. */
  2037. __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
  2038. {
  2039. return aff_zero_basic_set(aff, 0, NULL);
  2040. }
  2041. /* Return a basic set containing those elements in the shared space
  2042. * of aff1 and aff2 where aff1 is greater than or equal to aff2.
  2043. */
  2044. __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
  2045. __isl_take isl_aff *aff2)
  2046. {
  2047. aff1 = isl_aff_sub(aff1, aff2);
  2048. return isl_aff_nonneg_basic_set(aff1);
  2049. }
  2050. /* Return a basic set containing those elements in the shared domain space
  2051. * of "aff1" and "aff2" where "aff1" is greater than "aff2".
  2052. */
  2053. __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
  2054. __isl_take isl_aff *aff2)
  2055. {
  2056. aff1 = isl_aff_sub(aff1, aff2);
  2057. return isl_aff_pos_basic_set(aff1);
  2058. }
  2059. /* Return a set containing those elements in the shared space
  2060. * of aff1 and aff2 where aff1 is greater than or equal to aff2.
  2061. */
  2062. __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
  2063. __isl_take isl_aff *aff2)
  2064. {
  2065. return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
  2066. }
  2067. /* Return a set containing those elements in the shared domain space
  2068. * of aff1 and aff2 where aff1 is greater than aff2.
  2069. *
  2070. * If either of the two inputs is NaN, then the result is empty,
  2071. * as comparisons with NaN always return false.
  2072. */
  2073. __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
  2074. __isl_take isl_aff *aff2)
  2075. {
  2076. return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
  2077. }
  2078. /* Return a basic set containing those elements in the shared space
  2079. * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
  2080. */
  2081. __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
  2082. __isl_take isl_aff *aff2)
  2083. {
  2084. return isl_aff_ge_basic_set(aff2, aff1);
  2085. }
  2086. /* Return a basic set containing those elements in the shared domain space
  2087. * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
  2088. */
  2089. __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
  2090. __isl_take isl_aff *aff2)
  2091. {
  2092. return isl_aff_gt_basic_set(aff2, aff1);
  2093. }
  2094. /* Return a set containing those elements in the shared space
  2095. * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
  2096. */
  2097. __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
  2098. __isl_take isl_aff *aff2)
  2099. {
  2100. return isl_aff_ge_set(aff2, aff1);
  2101. }
  2102. /* Return a set containing those elements in the shared domain space
  2103. * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
  2104. */
  2105. __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
  2106. __isl_take isl_aff *aff2)
  2107. {
  2108. return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
  2109. }
  2110. /* Return a basic set containing those elements in the shared space
  2111. * of aff1 and aff2 where aff1 and aff2 are equal.
  2112. */
  2113. __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
  2114. __isl_take isl_aff *aff2)
  2115. {
  2116. aff1 = isl_aff_sub(aff1, aff2);
  2117. return isl_aff_zero_basic_set(aff1);
  2118. }
  2119. /* Return a set containing those elements in the shared space
  2120. * of aff1 and aff2 where aff1 and aff2 are equal.
  2121. */
  2122. __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
  2123. __isl_take isl_aff *aff2)
  2124. {
  2125. return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
  2126. }
  2127. /* Return a set containing those elements in the shared domain space
  2128. * of aff1 and aff2 where aff1 and aff2 are not equal.
  2129. *
  2130. * If either of the two inputs is NaN, then the result is empty,
  2131. * as comparisons with NaN always return false.
  2132. */
  2133. __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
  2134. __isl_take isl_aff *aff2)
  2135. {
  2136. isl_set *set_lt, *set_gt;
  2137. set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
  2138. isl_aff_copy(aff2));
  2139. set_gt = isl_aff_gt_set(aff1, aff2);
  2140. return isl_set_union_disjoint(set_lt, set_gt);
  2141. }
  2142. __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
  2143. __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
  2144. {
  2145. aff1 = isl_aff_add(aff1, aff2);
  2146. aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
  2147. return aff1;
  2148. }
  2149. isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
  2150. {
  2151. if (!aff)
  2152. return isl_bool_error;
  2153. return isl_bool_false;
  2154. }
  2155. #undef TYPE
  2156. #define TYPE isl_aff
  2157. static
  2158. #include "check_type_range_templ.c"
  2159. /* Check whether the given affine expression has non-zero coefficient
  2160. * for any dimension in the given range or if any of these dimensions
  2161. * appear with non-zero coefficients in any of the integer divisions
  2162. * involved in the affine expression.
  2163. */
  2164. isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
  2165. enum isl_dim_type type, unsigned first, unsigned n)
  2166. {
  2167. int i;
  2168. int *active = NULL;
  2169. isl_bool involves = isl_bool_false;
  2170. if (!aff)
  2171. return isl_bool_error;
  2172. if (n == 0)
  2173. return isl_bool_false;
  2174. if (isl_aff_check_range(aff, type, first, n) < 0)
  2175. return isl_bool_error;
  2176. active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
  2177. if (!active)
  2178. goto error;
  2179. first += isl_local_space_offset(aff->ls, type) - 1;
  2180. for (i = 0; i < n; ++i)
  2181. if (active[first + i]) {
  2182. involves = isl_bool_true;
  2183. break;
  2184. }
  2185. free(active);
  2186. return involves;
  2187. error:
  2188. free(active);
  2189. return isl_bool_error;
  2190. }
  2191. /* Does "aff" involve any local variables, i.e., integer divisions?
  2192. */
  2193. isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
  2194. {
  2195. isl_size n;
  2196. n = isl_aff_dim(aff, isl_dim_div);
  2197. if (n < 0)
  2198. return isl_bool_error;
  2199. return isl_bool_ok(n > 0);
  2200. }
  2201. __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
  2202. enum isl_dim_type type, unsigned first, unsigned n)
  2203. {
  2204. isl_ctx *ctx;
  2205. if (!aff)
  2206. return NULL;
  2207. if (type == isl_dim_out)
  2208. isl_die(aff->v->ctx, isl_error_invalid,
  2209. "cannot drop output/set dimension",
  2210. return isl_aff_free(aff));
  2211. if (type == isl_dim_in)
  2212. type = isl_dim_set;
  2213. if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
  2214. return aff;
  2215. ctx = isl_aff_get_ctx(aff);
  2216. if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
  2217. return isl_aff_free(aff);
  2218. aff = isl_aff_cow(aff);
  2219. if (!aff)
  2220. return NULL;
  2221. aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
  2222. if (!aff->ls)
  2223. return isl_aff_free(aff);
  2224. first += 1 + isl_local_space_offset(aff->ls, type);
  2225. aff->v = isl_vec_drop_els(aff->v, first, n);
  2226. if (!aff->v)
  2227. return isl_aff_free(aff);
  2228. return aff;
  2229. }
  2230. /* Is the domain of "aff" a product?
  2231. */
  2232. static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
  2233. {
  2234. return isl_space_is_product(isl_aff_peek_domain_space(aff));
  2235. }
  2236. #undef TYPE
  2237. #define TYPE isl_aff
  2238. #include <isl_domain_factor_templ.c>
  2239. /* Project the domain of the affine expression onto its parameter space.
  2240. * The affine expression may not involve any of the domain dimensions.
  2241. */
  2242. __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
  2243. {
  2244. isl_space *space;
  2245. isl_size n;
  2246. n = isl_aff_dim(aff, isl_dim_in);
  2247. if (n < 0)
  2248. return isl_aff_free(aff);
  2249. aff = isl_aff_drop_domain(aff, 0, n);
  2250. space = isl_aff_get_domain_space(aff);
  2251. space = isl_space_params(space);
  2252. aff = isl_aff_reset_domain_space(aff, space);
  2253. return aff;
  2254. }
  2255. /* Convert an affine expression defined over a parameter domain
  2256. * into one that is defined over a zero-dimensional set.
  2257. */
  2258. __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
  2259. {
  2260. isl_local_space *ls;
  2261. ls = isl_aff_take_domain_local_space(aff);
  2262. ls = isl_local_space_set_from_params(ls);
  2263. aff = isl_aff_restore_domain_local_space(aff, ls);
  2264. return aff;
  2265. }
  2266. __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
  2267. enum isl_dim_type type, unsigned first, unsigned n)
  2268. {
  2269. isl_ctx *ctx;
  2270. if (!aff)
  2271. return NULL;
  2272. if (type == isl_dim_out)
  2273. isl_die(aff->v->ctx, isl_error_invalid,
  2274. "cannot insert output/set dimensions",
  2275. return isl_aff_free(aff));
  2276. if (type == isl_dim_in)
  2277. type = isl_dim_set;
  2278. if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
  2279. return aff;
  2280. ctx = isl_aff_get_ctx(aff);
  2281. if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
  2282. return isl_aff_free(aff);
  2283. aff = isl_aff_cow(aff);
  2284. if (!aff)
  2285. return NULL;
  2286. aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
  2287. if (!aff->ls)
  2288. return isl_aff_free(aff);
  2289. first += 1 + isl_local_space_offset(aff->ls, type);
  2290. aff->v = isl_vec_insert_zero_els(aff->v, first, n);
  2291. if (!aff->v)
  2292. return isl_aff_free(aff);
  2293. return aff;
  2294. }
  2295. __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
  2296. enum isl_dim_type type, unsigned n)
  2297. {
  2298. isl_size pos;
  2299. pos = isl_aff_dim(aff, type);
  2300. if (pos < 0)
  2301. return isl_aff_free(aff);
  2302. return isl_aff_insert_dims(aff, type, pos, n);
  2303. }
  2304. /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
  2305. * to dimensions of "dst_type" at "dst_pos".
  2306. *
  2307. * We only support moving input dimensions to parameters and vice versa.
  2308. */
  2309. __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
  2310. enum isl_dim_type dst_type, unsigned dst_pos,
  2311. enum isl_dim_type src_type, unsigned src_pos, unsigned n)
  2312. {
  2313. unsigned g_dst_pos;
  2314. unsigned g_src_pos;
  2315. isl_size src_off, dst_off;
  2316. if (!aff)
  2317. return NULL;
  2318. if (n == 0 &&
  2319. !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
  2320. !isl_local_space_is_named_or_nested(aff->ls, dst_type))
  2321. return aff;
  2322. if (dst_type == isl_dim_out || src_type == isl_dim_out)
  2323. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  2324. "cannot move output/set dimension",
  2325. return isl_aff_free(aff));
  2326. if (dst_type == isl_dim_div || src_type == isl_dim_div)
  2327. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  2328. "cannot move divs", return isl_aff_free(aff));
  2329. if (dst_type == isl_dim_in)
  2330. dst_type = isl_dim_set;
  2331. if (src_type == isl_dim_in)
  2332. src_type = isl_dim_set;
  2333. if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
  2334. return isl_aff_free(aff);
  2335. if (dst_type == src_type)
  2336. isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
  2337. "moving dims within the same type not supported",
  2338. return isl_aff_free(aff));
  2339. aff = isl_aff_cow(aff);
  2340. src_off = isl_aff_domain_offset(aff, src_type);
  2341. dst_off = isl_aff_domain_offset(aff, dst_type);
  2342. if (src_off < 0 || dst_off < 0)
  2343. return isl_aff_free(aff);
  2344. g_src_pos = 1 + src_off + src_pos;
  2345. g_dst_pos = 1 + dst_off + dst_pos;
  2346. if (dst_type > src_type)
  2347. g_dst_pos -= n;
  2348. aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
  2349. aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
  2350. src_type, src_pos, n);
  2351. if (!aff->v || !aff->ls)
  2352. return isl_aff_free(aff);
  2353. aff = sort_divs(aff);
  2354. return aff;
  2355. }
  2356. /* Return a zero isl_aff in the given space.
  2357. *
  2358. * This is a helper function for isl_pw_*_as_* that ensures a uniform
  2359. * interface over all piecewise types.
  2360. */
  2361. static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
  2362. {
  2363. isl_local_space *ls;
  2364. ls = isl_local_space_from_space(isl_space_domain(space));
  2365. return isl_aff_zero_on_domain(ls);
  2366. }
  2367. #define isl_aff_involves_nan isl_aff_is_nan
  2368. #undef PW
  2369. #define PW isl_pw_aff
  2370. #undef BASE
  2371. #define BASE aff
  2372. #undef EL_IS_ZERO
  2373. #define EL_IS_ZERO is_empty
  2374. #undef ZERO
  2375. #define ZERO empty
  2376. #undef IS_ZERO
  2377. #define IS_ZERO is_empty
  2378. #undef FIELD
  2379. #define FIELD aff
  2380. #undef DEFAULT_IS_ZERO
  2381. #define DEFAULT_IS_ZERO 0
  2382. #include <isl_pw_templ.c>
  2383. #include <isl_pw_add_constant_val_templ.c>
  2384. #include <isl_pw_bind_domain_templ.c>
  2385. #include <isl_pw_eval.c>
  2386. #include <isl_pw_hash.c>
  2387. #include <isl_pw_insert_dims_templ.c>
  2388. #include <isl_pw_insert_domain_templ.c>
  2389. #include <isl_pw_move_dims_templ.c>
  2390. #include <isl_pw_neg_templ.c>
  2391. #include <isl_pw_pullback_templ.c>
  2392. #include <isl_pw_sub_templ.c>
  2393. #include <isl_pw_union_opt.c>
  2394. #undef BASE
  2395. #define BASE pw_aff
  2396. #include <isl_union_single.c>
  2397. #include <isl_union_neg.c>
  2398. #undef BASE
  2399. #define BASE aff
  2400. #include <isl_union_pw_templ.c>
  2401. /* Compute a piecewise quasi-affine expression with a domain that
  2402. * is the union of those of pwaff1 and pwaff2 and such that on each
  2403. * cell, the quasi-affine expression is the maximum of those of pwaff1
  2404. * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
  2405. * cell, then the associated expression is the defined one.
  2406. */
  2407. __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
  2408. __isl_take isl_pw_aff *pwaff2)
  2409. {
  2410. isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
  2411. return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
  2412. }
  2413. /* Compute a piecewise quasi-affine expression with a domain that
  2414. * is the union of those of pwaff1 and pwaff2 and such that on each
  2415. * cell, the quasi-affine expression is the minimum of those of pwaff1
  2416. * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
  2417. * cell, then the associated expression is the defined one.
  2418. */
  2419. __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
  2420. __isl_take isl_pw_aff *pwaff2)
  2421. {
  2422. isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
  2423. return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
  2424. }
  2425. __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
  2426. __isl_take isl_pw_aff *pwaff2, int max)
  2427. {
  2428. if (max)
  2429. return isl_pw_aff_union_max(pwaff1, pwaff2);
  2430. else
  2431. return isl_pw_aff_union_min(pwaff1, pwaff2);
  2432. }
  2433. /* Is the domain of "pa" a product?
  2434. */
  2435. static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
  2436. {
  2437. return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
  2438. }
  2439. #undef TYPE
  2440. #define TYPE isl_pw_aff
  2441. #include <isl_domain_factor_templ.c>
  2442. /* Return a set containing those elements in the domain
  2443. * of "pwaff" where it satisfies "fn" (if complement is 0) or
  2444. * does not satisfy "fn" (if complement is 1).
  2445. *
  2446. * The pieces with a NaN never belong to the result since
  2447. * NaN does not satisfy any property.
  2448. */
  2449. static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
  2450. __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
  2451. void *user),
  2452. int complement, void *user)
  2453. {
  2454. int i;
  2455. isl_set *set;
  2456. if (!pwaff)
  2457. return NULL;
  2458. set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
  2459. for (i = 0; i < pwaff->n; ++i) {
  2460. isl_basic_set *bset;
  2461. isl_set *set_i, *locus;
  2462. isl_bool rational;
  2463. if (isl_aff_is_nan(pwaff->p[i].aff))
  2464. continue;
  2465. rational = isl_set_has_rational(pwaff->p[i].set);
  2466. bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
  2467. locus = isl_set_from_basic_set(bset);
  2468. set_i = isl_set_copy(pwaff->p[i].set);
  2469. if (complement)
  2470. set_i = isl_set_subtract(set_i, locus);
  2471. else
  2472. set_i = isl_set_intersect(set_i, locus);
  2473. set = isl_set_union_disjoint(set, set_i);
  2474. }
  2475. isl_pw_aff_free(pwaff);
  2476. return set;
  2477. }
  2478. /* Return a set containing those elements in the domain
  2479. * of "pa" where it is positive.
  2480. */
  2481. __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
  2482. {
  2483. return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
  2484. }
  2485. /* Return a set containing those elements in the domain
  2486. * of pwaff where it is non-negative.
  2487. */
  2488. __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
  2489. {
  2490. return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
  2491. }
  2492. /* Return a set containing those elements in the domain
  2493. * of pwaff where it is zero.
  2494. */
  2495. __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
  2496. {
  2497. return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
  2498. }
  2499. /* Return a set containing those elements in the domain
  2500. * of pwaff where it is not zero.
  2501. */
  2502. __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
  2503. {
  2504. return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
  2505. }
  2506. /* Bind the affine function "aff" to the parameter "id",
  2507. * returning the elements in the domain where the affine expression
  2508. * is equal to the parameter.
  2509. */
  2510. __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
  2511. __isl_take isl_id *id)
  2512. {
  2513. isl_space *space;
  2514. isl_aff *aff_id;
  2515. space = isl_aff_get_domain_space(aff);
  2516. space = isl_space_add_param_id(space, isl_id_copy(id));
  2517. aff = isl_aff_align_params(aff, isl_space_copy(space));
  2518. aff_id = isl_aff_param_on_domain_space_id(space, id);
  2519. return isl_aff_eq_basic_set(aff, aff_id);
  2520. }
  2521. /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
  2522. * "rational" should not be set.
  2523. */
  2524. static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
  2525. int rational, void *user)
  2526. {
  2527. isl_id *id = user;
  2528. if (!aff)
  2529. return NULL;
  2530. if (rational)
  2531. isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
  2532. "rational binding not supported", goto error);
  2533. return isl_aff_bind_id(aff, isl_id_copy(id));
  2534. error:
  2535. isl_aff_free(aff);
  2536. return NULL;
  2537. }
  2538. /* Bind the piecewise affine function "pa" to the parameter "id",
  2539. * returning the elements in the domain where the expression
  2540. * is equal to the parameter.
  2541. */
  2542. __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
  2543. __isl_take isl_id *id)
  2544. {
  2545. isl_set *bound;
  2546. bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
  2547. isl_id_free(id);
  2548. return bound;
  2549. }
  2550. /* Return a set containing those elements in the shared domain
  2551. * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
  2552. *
  2553. * We compute the difference on the shared domain and then construct
  2554. * the set of values where this difference is non-negative.
  2555. * If strict is set, we first subtract 1 from the difference.
  2556. * If equal is set, we only return the elements where pwaff1 and pwaff2
  2557. * are equal.
  2558. */
  2559. static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
  2560. __isl_take isl_pw_aff *pwaff2, int strict, int equal)
  2561. {
  2562. isl_set *set1, *set2;
  2563. set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
  2564. set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
  2565. set1 = isl_set_intersect(set1, set2);
  2566. pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
  2567. pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
  2568. pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
  2569. if (strict) {
  2570. isl_space *space = isl_set_get_space(set1);
  2571. isl_aff *aff;
  2572. aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
  2573. aff = isl_aff_add_constant_si(aff, -1);
  2574. pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
  2575. } else
  2576. isl_set_free(set1);
  2577. if (equal)
  2578. return isl_pw_aff_zero_set(pwaff1);
  2579. return isl_pw_aff_nonneg_set(pwaff1);
  2580. }
  2581. /* Return a set containing those elements in the shared domain
  2582. * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
  2583. */
  2584. __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
  2585. __isl_take isl_pw_aff *pwaff2)
  2586. {
  2587. isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
  2588. return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
  2589. }
  2590. /* Return a set containing those elements in the shared domain
  2591. * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
  2592. */
  2593. __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
  2594. __isl_take isl_pw_aff *pwaff2)
  2595. {
  2596. isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
  2597. return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
  2598. }
  2599. /* Return a set containing those elements in the shared domain
  2600. * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
  2601. */
  2602. __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
  2603. __isl_take isl_pw_aff *pwaff2)
  2604. {
  2605. isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
  2606. return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
  2607. }
  2608. __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
  2609. __isl_take isl_pw_aff *pwaff2)
  2610. {
  2611. return isl_pw_aff_ge_set(pwaff2, pwaff1);
  2612. }
  2613. __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
  2614. __isl_take isl_pw_aff *pwaff2)
  2615. {
  2616. return isl_pw_aff_gt_set(pwaff2, pwaff1);
  2617. }
  2618. /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
  2619. * where the function values are ordered in the same way as "order",
  2620. * which returns a set in the shared domain of its two arguments.
  2621. *
  2622. * Let "pa1" and "pa2" be defined on domains A and B respectively.
  2623. * We first pull back the two functions such that they are defined on
  2624. * the domain [A -> B]. Then we apply "order", resulting in a set
  2625. * in the space [A -> B]. Finally, we unwrap this set to obtain
  2626. * a map in the space A -> B.
  2627. */
  2628. static __isl_give isl_map *isl_pw_aff_order_map(
  2629. __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
  2630. __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
  2631. __isl_take isl_pw_aff *pa2))
  2632. {
  2633. isl_space *space1, *space2;
  2634. isl_multi_aff *ma;
  2635. isl_set *set;
  2636. isl_pw_aff_align_params_bin(&pa1, &pa2);
  2637. space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
  2638. space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
  2639. space1 = isl_space_map_from_domain_and_range(space1, space2);
  2640. ma = isl_multi_aff_domain_map(isl_space_copy(space1));
  2641. pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
  2642. ma = isl_multi_aff_range_map(space1);
  2643. pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
  2644. set = order(pa1, pa2);
  2645. return isl_set_unwrap(set);
  2646. }
  2647. /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
  2648. * where the function values are equal.
  2649. */
  2650. __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
  2651. __isl_take isl_pw_aff *pa2)
  2652. {
  2653. return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
  2654. }
  2655. /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
  2656. * where the function value of "pa1" is less than or equal to
  2657. * the function value of "pa2".
  2658. */
  2659. __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
  2660. __isl_take isl_pw_aff *pa2)
  2661. {
  2662. return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
  2663. }
  2664. /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
  2665. * where the function value of "pa1" is less than the function value of "pa2".
  2666. */
  2667. __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
  2668. __isl_take isl_pw_aff *pa2)
  2669. {
  2670. return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
  2671. }
  2672. /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
  2673. * where the function value of "pa1" is greater than or equal to
  2674. * the function value of "pa2".
  2675. */
  2676. __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
  2677. __isl_take isl_pw_aff *pa2)
  2678. {
  2679. return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
  2680. }
  2681. /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
  2682. * where the function value of "pa1" is greater than the function value
  2683. * of "pa2".
  2684. */
  2685. __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
  2686. __isl_take isl_pw_aff *pa2)
  2687. {
  2688. return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
  2689. }
  2690. /* Return a set containing those elements in the shared domain
  2691. * of the elements of list1 and list2 where each element in list1
  2692. * has the relation specified by "fn" with each element in list2.
  2693. */
  2694. static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
  2695. __isl_take isl_pw_aff_list *list2,
  2696. __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
  2697. __isl_take isl_pw_aff *pwaff2))
  2698. {
  2699. int i, j;
  2700. isl_ctx *ctx;
  2701. isl_set *set;
  2702. if (!list1 || !list2)
  2703. goto error;
  2704. ctx = isl_pw_aff_list_get_ctx(list1);
  2705. if (list1->n < 1 || list2->n < 1)
  2706. isl_die(ctx, isl_error_invalid,
  2707. "list should contain at least one element", goto error);
  2708. set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
  2709. for (i = 0; i < list1->n; ++i)
  2710. for (j = 0; j < list2->n; ++j) {
  2711. isl_set *set_ij;
  2712. set_ij = fn(isl_pw_aff_copy(list1->p[i]),
  2713. isl_pw_aff_copy(list2->p[j]));
  2714. set = isl_set_intersect(set, set_ij);
  2715. }
  2716. isl_pw_aff_list_free(list1);
  2717. isl_pw_aff_list_free(list2);
  2718. return set;
  2719. error:
  2720. isl_pw_aff_list_free(list1);
  2721. isl_pw_aff_list_free(list2);
  2722. return NULL;
  2723. }
  2724. /* Return a set containing those elements in the shared domain
  2725. * of the elements of list1 and list2 where each element in list1
  2726. * is equal to each element in list2.
  2727. */
  2728. __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
  2729. __isl_take isl_pw_aff_list *list2)
  2730. {
  2731. return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
  2732. }
  2733. __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
  2734. __isl_take isl_pw_aff_list *list2)
  2735. {
  2736. return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
  2737. }
  2738. /* Return a set containing those elements in the shared domain
  2739. * of the elements of list1 and list2 where each element in list1
  2740. * is less than or equal to each element in list2.
  2741. */
  2742. __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
  2743. __isl_take isl_pw_aff_list *list2)
  2744. {
  2745. return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
  2746. }
  2747. __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
  2748. __isl_take isl_pw_aff_list *list2)
  2749. {
  2750. return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
  2751. }
  2752. __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
  2753. __isl_take isl_pw_aff_list *list2)
  2754. {
  2755. return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
  2756. }
  2757. __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
  2758. __isl_take isl_pw_aff_list *list2)
  2759. {
  2760. return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
  2761. }
  2762. /* Return a set containing those elements in the shared domain
  2763. * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
  2764. */
  2765. __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
  2766. __isl_take isl_pw_aff *pwaff2)
  2767. {
  2768. isl_set *set_lt, *set_gt;
  2769. isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
  2770. set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
  2771. isl_pw_aff_copy(pwaff2));
  2772. set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
  2773. return isl_set_union_disjoint(set_lt, set_gt);
  2774. }
  2775. __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
  2776. isl_int v)
  2777. {
  2778. int i;
  2779. if (isl_int_is_one(v))
  2780. return pwaff;
  2781. if (!isl_int_is_pos(v))
  2782. isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
  2783. "factor needs to be positive",
  2784. return isl_pw_aff_free(pwaff));
  2785. pwaff = isl_pw_aff_cow(pwaff);
  2786. if (!pwaff)
  2787. return NULL;
  2788. if (pwaff->n == 0)
  2789. return pwaff;
  2790. for (i = 0; i < pwaff->n; ++i) {
  2791. pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
  2792. if (!pwaff->p[i].aff)
  2793. return isl_pw_aff_free(pwaff);
  2794. }
  2795. return pwaff;
  2796. }
  2797. __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
  2798. {
  2799. int i;
  2800. pwaff = isl_pw_aff_cow(pwaff);
  2801. if (!pwaff)
  2802. return NULL;
  2803. if (pwaff->n == 0)
  2804. return pwaff;
  2805. for (i = 0; i < pwaff->n; ++i) {
  2806. pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
  2807. if (!pwaff->p[i].aff)
  2808. return isl_pw_aff_free(pwaff);
  2809. }
  2810. return pwaff;
  2811. }
  2812. __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
  2813. {
  2814. int i;
  2815. pwaff = isl_pw_aff_cow(pwaff);
  2816. if (!pwaff)
  2817. return NULL;
  2818. if (pwaff->n == 0)
  2819. return pwaff;
  2820. for (i = 0; i < pwaff->n; ++i) {
  2821. pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
  2822. if (!pwaff->p[i].aff)
  2823. return isl_pw_aff_free(pwaff);
  2824. }
  2825. return pwaff;
  2826. }
  2827. /* Assuming that "cond1" and "cond2" are disjoint,
  2828. * return an affine expression that is equal to pwaff1 on cond1
  2829. * and to pwaff2 on cond2.
  2830. */
  2831. static __isl_give isl_pw_aff *isl_pw_aff_select(
  2832. __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
  2833. __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
  2834. {
  2835. pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
  2836. pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
  2837. return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
  2838. }
  2839. /* Return an affine expression that is equal to pwaff_true for elements
  2840. * where "cond" is non-zero and to pwaff_false for elements where "cond"
  2841. * is zero.
  2842. * That is, return cond ? pwaff_true : pwaff_false;
  2843. *
  2844. * If "cond" involves and NaN, then we conservatively return a NaN
  2845. * on its entire domain. In principle, we could consider the pieces
  2846. * where it is NaN separately from those where it is not.
  2847. *
  2848. * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
  2849. * then only use the domain of "cond" to restrict the domain.
  2850. */
  2851. __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
  2852. __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
  2853. {
  2854. isl_set *cond_true, *cond_false;
  2855. isl_bool equal;
  2856. if (!cond)
  2857. goto error;
  2858. if (isl_pw_aff_involves_nan(cond)) {
  2859. isl_space *space = isl_pw_aff_get_domain_space(cond);
  2860. isl_local_space *ls = isl_local_space_from_space(space);
  2861. isl_pw_aff_free(cond);
  2862. isl_pw_aff_free(pwaff_true);
  2863. isl_pw_aff_free(pwaff_false);
  2864. return isl_pw_aff_nan_on_domain(ls);
  2865. }
  2866. pwaff_true = isl_pw_aff_align_params(pwaff_true,
  2867. isl_pw_aff_get_space(pwaff_false));
  2868. pwaff_false = isl_pw_aff_align_params(pwaff_false,
  2869. isl_pw_aff_get_space(pwaff_true));
  2870. equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
  2871. if (equal < 0)
  2872. goto error;
  2873. if (equal) {
  2874. isl_set *dom;
  2875. dom = isl_set_coalesce(isl_pw_aff_domain(cond));
  2876. isl_pw_aff_free(pwaff_false);
  2877. return isl_pw_aff_intersect_domain(pwaff_true, dom);
  2878. }
  2879. cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
  2880. cond_false = isl_pw_aff_zero_set(cond);
  2881. return isl_pw_aff_select(cond_true, pwaff_true,
  2882. cond_false, pwaff_false);
  2883. error:
  2884. isl_pw_aff_free(cond);
  2885. isl_pw_aff_free(pwaff_true);
  2886. isl_pw_aff_free(pwaff_false);
  2887. return NULL;
  2888. }
  2889. isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
  2890. {
  2891. int pos;
  2892. if (!aff)
  2893. return isl_bool_error;
  2894. pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
  2895. return isl_bool_ok(pos == -1);
  2896. }
  2897. /* Check whether pwaff is a piecewise constant.
  2898. */
  2899. isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
  2900. {
  2901. int i;
  2902. if (!pwaff)
  2903. return isl_bool_error;
  2904. for (i = 0; i < pwaff->n; ++i) {
  2905. isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
  2906. if (is_cst < 0 || !is_cst)
  2907. return is_cst;
  2908. }
  2909. return isl_bool_true;
  2910. }
  2911. /* Return the product of "aff1" and "aff2".
  2912. *
  2913. * If either of the two is NaN, then the result is NaN.
  2914. *
  2915. * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
  2916. */
  2917. __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
  2918. __isl_take isl_aff *aff2)
  2919. {
  2920. if (!aff1 || !aff2)
  2921. goto error;
  2922. if (isl_aff_is_nan(aff1)) {
  2923. isl_aff_free(aff2);
  2924. return aff1;
  2925. }
  2926. if (isl_aff_is_nan(aff2)) {
  2927. isl_aff_free(aff1);
  2928. return aff2;
  2929. }
  2930. if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
  2931. return isl_aff_mul(aff2, aff1);
  2932. if (!isl_aff_is_cst(aff2))
  2933. isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
  2934. "at least one affine expression should be constant",
  2935. goto error);
  2936. aff1 = isl_aff_cow(aff1);
  2937. if (!aff1 || !aff2)
  2938. goto error;
  2939. aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
  2940. aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
  2941. isl_aff_free(aff2);
  2942. return aff1;
  2943. error:
  2944. isl_aff_free(aff1);
  2945. isl_aff_free(aff2);
  2946. return NULL;
  2947. }
  2948. /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
  2949. *
  2950. * If either of the two is NaN, then the result is NaN.
  2951. * A division by zero also results in NaN.
  2952. */
  2953. __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
  2954. __isl_take isl_aff *aff2)
  2955. {
  2956. isl_bool is_cst, is_zero;
  2957. int neg;
  2958. if (!aff1 || !aff2)
  2959. goto error;
  2960. if (isl_aff_is_nan(aff1)) {
  2961. isl_aff_free(aff2);
  2962. return aff1;
  2963. }
  2964. if (isl_aff_is_nan(aff2)) {
  2965. isl_aff_free(aff1);
  2966. return aff2;
  2967. }
  2968. is_cst = isl_aff_is_cst(aff2);
  2969. if (is_cst < 0)
  2970. goto error;
  2971. if (!is_cst)
  2972. isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
  2973. "second argument should be a constant", goto error);
  2974. is_zero = isl_aff_plain_is_zero(aff2);
  2975. if (is_zero < 0)
  2976. goto error;
  2977. if (is_zero)
  2978. return set_nan_free(aff1, aff2);
  2979. neg = isl_int_is_neg(aff2->v->el[1]);
  2980. if (neg) {
  2981. isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
  2982. isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
  2983. }
  2984. aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
  2985. aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
  2986. if (neg) {
  2987. isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
  2988. isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
  2989. }
  2990. isl_aff_free(aff2);
  2991. return aff1;
  2992. error:
  2993. isl_aff_free(aff1);
  2994. isl_aff_free(aff2);
  2995. return NULL;
  2996. }
  2997. __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
  2998. __isl_take isl_pw_aff *pwaff2)
  2999. {
  3000. isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
  3001. return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
  3002. }
  3003. __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
  3004. __isl_take isl_pw_aff *pwaff2)
  3005. {
  3006. return isl_pw_aff_union_add_(pwaff1, pwaff2);
  3007. }
  3008. __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
  3009. __isl_take isl_pw_aff *pwaff2)
  3010. {
  3011. isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
  3012. return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
  3013. }
  3014. /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
  3015. */
  3016. __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
  3017. __isl_take isl_pw_aff *pa2)
  3018. {
  3019. int is_cst;
  3020. is_cst = isl_pw_aff_is_cst(pa2);
  3021. if (is_cst < 0)
  3022. goto error;
  3023. if (!is_cst)
  3024. isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
  3025. "second argument should be a piecewise constant",
  3026. goto error);
  3027. isl_pw_aff_align_params_bin(&pa1, &pa2);
  3028. return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
  3029. error:
  3030. isl_pw_aff_free(pa1);
  3031. isl_pw_aff_free(pa2);
  3032. return NULL;
  3033. }
  3034. /* Compute the quotient of the integer division of "pa1" by "pa2"
  3035. * with rounding towards zero.
  3036. * "pa2" is assumed to be a piecewise constant.
  3037. *
  3038. * In particular, return
  3039. *
  3040. * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
  3041. *
  3042. */
  3043. __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
  3044. __isl_take isl_pw_aff *pa2)
  3045. {
  3046. int is_cst;
  3047. isl_set *cond;
  3048. isl_pw_aff *f, *c;
  3049. is_cst = isl_pw_aff_is_cst(pa2);
  3050. if (is_cst < 0)
  3051. goto error;
  3052. if (!is_cst)
  3053. isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
  3054. "second argument should be a piecewise constant",
  3055. goto error);
  3056. pa1 = isl_pw_aff_div(pa1, pa2);
  3057. cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
  3058. f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
  3059. c = isl_pw_aff_ceil(pa1);
  3060. return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
  3061. error:
  3062. isl_pw_aff_free(pa1);
  3063. isl_pw_aff_free(pa2);
  3064. return NULL;
  3065. }
  3066. /* Compute the remainder of the integer division of "pa1" by "pa2"
  3067. * with rounding towards zero.
  3068. * "pa2" is assumed to be a piecewise constant.
  3069. *
  3070. * In particular, return
  3071. *
  3072. * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
  3073. *
  3074. */
  3075. __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
  3076. __isl_take isl_pw_aff *pa2)
  3077. {
  3078. int is_cst;
  3079. isl_pw_aff *res;
  3080. is_cst = isl_pw_aff_is_cst(pa2);
  3081. if (is_cst < 0)
  3082. goto error;
  3083. if (!is_cst)
  3084. isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
  3085. "second argument should be a piecewise constant",
  3086. goto error);
  3087. res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
  3088. res = isl_pw_aff_mul(pa2, res);
  3089. res = isl_pw_aff_sub(pa1, res);
  3090. return res;
  3091. error:
  3092. isl_pw_aff_free(pa1);
  3093. isl_pw_aff_free(pa2);
  3094. return NULL;
  3095. }
  3096. /* Does either of "pa1" or "pa2" involve any NaN2?
  3097. */
  3098. static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
  3099. __isl_keep isl_pw_aff *pa2)
  3100. {
  3101. isl_bool has_nan;
  3102. has_nan = isl_pw_aff_involves_nan(pa1);
  3103. if (has_nan < 0 || has_nan)
  3104. return has_nan;
  3105. return isl_pw_aff_involves_nan(pa2);
  3106. }
  3107. /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
  3108. * by a NaN on their shared domain.
  3109. *
  3110. * In principle, the result could be refined to only being NaN
  3111. * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
  3112. */
  3113. static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
  3114. __isl_take isl_pw_aff *pa2)
  3115. {
  3116. isl_local_space *ls;
  3117. isl_set *dom;
  3118. isl_pw_aff *pa;
  3119. dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
  3120. ls = isl_local_space_from_space(isl_set_get_space(dom));
  3121. pa = isl_pw_aff_nan_on_domain(ls);
  3122. pa = isl_pw_aff_intersect_domain(pa, dom);
  3123. return pa;
  3124. }
  3125. static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
  3126. __isl_take isl_pw_aff *pwaff2)
  3127. {
  3128. isl_set *le;
  3129. isl_set *dom;
  3130. dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
  3131. isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
  3132. le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
  3133. isl_pw_aff_copy(pwaff2));
  3134. dom = isl_set_subtract(dom, isl_set_copy(le));
  3135. return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
  3136. }
  3137. static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
  3138. __isl_take isl_pw_aff *pwaff2)
  3139. {
  3140. isl_set *ge;
  3141. isl_set *dom;
  3142. dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
  3143. isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
  3144. ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
  3145. isl_pw_aff_copy(pwaff2));
  3146. dom = isl_set_subtract(dom, isl_set_copy(ge));
  3147. return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
  3148. }
  3149. /* Return an expression for the minimum (if "max" is not set) or
  3150. * the maximum (if "max" is set) of "pa1" and "pa2".
  3151. * If either expression involves any NaN, then return a NaN
  3152. * on the shared domain as result.
  3153. */
  3154. static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
  3155. __isl_take isl_pw_aff *pa2, int max)
  3156. {
  3157. isl_bool has_nan;
  3158. has_nan = either_involves_nan(pa1, pa2);
  3159. if (has_nan < 0)
  3160. pa1 = isl_pw_aff_free(pa1);
  3161. else if (has_nan)
  3162. return replace_by_nan(pa1, pa2);
  3163. isl_pw_aff_align_params_bin(&pa1, &pa2);
  3164. if (max)
  3165. return pw_aff_max(pa1, pa2);
  3166. else
  3167. return pw_aff_min(pa1, pa2);
  3168. }
  3169. /* Return an expression for the minimum of "pwaff1" and "pwaff2".
  3170. */
  3171. __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
  3172. __isl_take isl_pw_aff *pwaff2)
  3173. {
  3174. return pw_aff_min_max(pwaff1, pwaff2, 0);
  3175. }
  3176. /* Return an expression for the maximum of "pwaff1" and "pwaff2".
  3177. */
  3178. __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
  3179. __isl_take isl_pw_aff *pwaff2)
  3180. {
  3181. return pw_aff_min_max(pwaff1, pwaff2, 1);
  3182. }
  3183. static __isl_give isl_pw_aff *pw_aff_list_reduce(
  3184. __isl_take isl_pw_aff_list *list,
  3185. __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
  3186. __isl_take isl_pw_aff *pwaff2))
  3187. {
  3188. int i;
  3189. isl_ctx *ctx;
  3190. isl_pw_aff *res;
  3191. if (!list)
  3192. return NULL;
  3193. ctx = isl_pw_aff_list_get_ctx(list);
  3194. if (list->n < 1)
  3195. isl_die(ctx, isl_error_invalid,
  3196. "list should contain at least one element", goto error);
  3197. res = isl_pw_aff_copy(list->p[0]);
  3198. for (i = 1; i < list->n; ++i)
  3199. res = fn(res, isl_pw_aff_copy(list->p[i]));
  3200. isl_pw_aff_list_free(list);
  3201. return res;
  3202. error:
  3203. isl_pw_aff_list_free(list);
  3204. return NULL;
  3205. }
  3206. /* Return an isl_pw_aff that maps each element in the intersection of the
  3207. * domains of the elements of list to the minimal corresponding affine
  3208. * expression.
  3209. */
  3210. __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
  3211. {
  3212. return pw_aff_list_reduce(list, &isl_pw_aff_min);
  3213. }
  3214. /* Return an isl_pw_aff that maps each element in the intersection of the
  3215. * domains of the elements of list to the maximal corresponding affine
  3216. * expression.
  3217. */
  3218. __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
  3219. {
  3220. return pw_aff_list_reduce(list, &isl_pw_aff_max);
  3221. }
  3222. /* Mark the domains of "pwaff" as rational.
  3223. */
  3224. __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
  3225. {
  3226. int i;
  3227. pwaff = isl_pw_aff_cow(pwaff);
  3228. if (!pwaff)
  3229. return NULL;
  3230. if (pwaff->n == 0)
  3231. return pwaff;
  3232. for (i = 0; i < pwaff->n; ++i) {
  3233. pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
  3234. if (!pwaff->p[i].set)
  3235. return isl_pw_aff_free(pwaff);
  3236. }
  3237. return pwaff;
  3238. }
  3239. /* Mark the domains of the elements of "list" as rational.
  3240. */
  3241. __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
  3242. __isl_take isl_pw_aff_list *list)
  3243. {
  3244. int i, n;
  3245. if (!list)
  3246. return NULL;
  3247. if (list->n == 0)
  3248. return list;
  3249. n = list->n;
  3250. for (i = 0; i < n; ++i) {
  3251. isl_pw_aff *pa;
  3252. pa = isl_pw_aff_list_get_pw_aff(list, i);
  3253. pa = isl_pw_aff_set_rational(pa);
  3254. list = isl_pw_aff_list_set_pw_aff(list, i, pa);
  3255. }
  3256. return list;
  3257. }
  3258. /* Do the parameters of "aff" match those of "space"?
  3259. */
  3260. isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
  3261. __isl_keep isl_space *space)
  3262. {
  3263. isl_space *aff_space;
  3264. isl_bool match;
  3265. if (!aff || !space)
  3266. return isl_bool_error;
  3267. aff_space = isl_aff_get_domain_space(aff);
  3268. match = isl_space_has_equal_params(space, aff_space);
  3269. isl_space_free(aff_space);
  3270. return match;
  3271. }
  3272. /* Check that the domain space of "aff" matches "space".
  3273. */
  3274. isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
  3275. __isl_keep isl_space *space)
  3276. {
  3277. isl_space *aff_space;
  3278. isl_bool match;
  3279. if (!aff || !space)
  3280. return isl_stat_error;
  3281. aff_space = isl_aff_get_domain_space(aff);
  3282. match = isl_space_has_equal_params(space, aff_space);
  3283. if (match < 0)
  3284. goto error;
  3285. if (!match)
  3286. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  3287. "parameters don't match", goto error);
  3288. match = isl_space_tuple_is_equal(space, isl_dim_in,
  3289. aff_space, isl_dim_set);
  3290. if (match < 0)
  3291. goto error;
  3292. if (!match)
  3293. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  3294. "domains don't match", goto error);
  3295. isl_space_free(aff_space);
  3296. return isl_stat_ok;
  3297. error:
  3298. isl_space_free(aff_space);
  3299. return isl_stat_error;
  3300. }
  3301. /* Return the shared (universe) domain of the elements of "ma".
  3302. *
  3303. * Since an isl_multi_aff (and an isl_aff) is always total,
  3304. * the domain is always the universe set in its domain space.
  3305. * This is a helper function for use in the generic isl_multi_*_bind.
  3306. */
  3307. static __isl_give isl_basic_set *isl_multi_aff_domain(
  3308. __isl_take isl_multi_aff *ma)
  3309. {
  3310. isl_space *space;
  3311. space = isl_multi_aff_get_space(ma);
  3312. isl_multi_aff_free(ma);
  3313. return isl_basic_set_universe(isl_space_domain(space));
  3314. }
  3315. #undef BASE
  3316. #define BASE aff
  3317. #include <isl_multi_no_explicit_domain.c>
  3318. #include <isl_multi_templ.c>
  3319. #include <isl_multi_add_constant_templ.c>
  3320. #include <isl_multi_apply_set.c>
  3321. #include <isl_multi_arith_templ.c>
  3322. #include <isl_multi_bind_domain_templ.c>
  3323. #include <isl_multi_cmp.c>
  3324. #include <isl_multi_dim_id_templ.c>
  3325. #include <isl_multi_dims.c>
  3326. #include <isl_multi_floor.c>
  3327. #include <isl_multi_from_base_templ.c>
  3328. #include <isl_multi_identity_templ.c>
  3329. #include <isl_multi_insert_domain_templ.c>
  3330. #include <isl_multi_locals_templ.c>
  3331. #include <isl_multi_move_dims_templ.c>
  3332. #include <isl_multi_nan_templ.c>
  3333. #include <isl_multi_product_templ.c>
  3334. #include <isl_multi_splice_templ.c>
  3335. #include <isl_multi_tuple_id_templ.c>
  3336. #include <isl_multi_unbind_params_templ.c>
  3337. #include <isl_multi_zero_templ.c>
  3338. #undef DOMBASE
  3339. #define DOMBASE set
  3340. #include <isl_multi_gist.c>
  3341. #undef DOMBASE
  3342. #define DOMBASE basic_set
  3343. #include <isl_multi_bind_templ.c>
  3344. /* Construct an isl_multi_aff living in "space" that corresponds
  3345. * to the affine transformation matrix "mat".
  3346. */
  3347. __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
  3348. __isl_take isl_space *space, __isl_take isl_mat *mat)
  3349. {
  3350. isl_ctx *ctx;
  3351. isl_local_space *ls = NULL;
  3352. isl_multi_aff *ma = NULL;
  3353. isl_size n_row, n_col, n_out, total;
  3354. int i;
  3355. if (!space || !mat)
  3356. goto error;
  3357. ctx = isl_mat_get_ctx(mat);
  3358. n_row = isl_mat_rows(mat);
  3359. n_col = isl_mat_cols(mat);
  3360. n_out = isl_space_dim(space, isl_dim_out);
  3361. total = isl_space_dim(space, isl_dim_all);
  3362. if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
  3363. goto error;
  3364. if (n_row < 1)
  3365. isl_die(ctx, isl_error_invalid,
  3366. "insufficient number of rows", goto error);
  3367. if (n_col < 1)
  3368. isl_die(ctx, isl_error_invalid,
  3369. "insufficient number of columns", goto error);
  3370. if (1 + n_out != n_row || 2 + total != n_row + n_col)
  3371. isl_die(ctx, isl_error_invalid,
  3372. "dimension mismatch", goto error);
  3373. ma = isl_multi_aff_zero(isl_space_copy(space));
  3374. space = isl_space_domain(space);
  3375. ls = isl_local_space_from_space(isl_space_copy(space));
  3376. for (i = 0; i < n_row - 1; ++i) {
  3377. isl_vec *v;
  3378. isl_aff *aff;
  3379. v = isl_vec_alloc(ctx, 1 + n_col);
  3380. if (!v)
  3381. goto error;
  3382. isl_int_set(v->el[0], mat->row[0][0]);
  3383. isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
  3384. v = isl_vec_normalize(v);
  3385. aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
  3386. ma = isl_multi_aff_set_aff(ma, i, aff);
  3387. }
  3388. isl_space_free(space);
  3389. isl_local_space_free(ls);
  3390. isl_mat_free(mat);
  3391. return ma;
  3392. error:
  3393. isl_space_free(space);
  3394. isl_local_space_free(ls);
  3395. isl_mat_free(mat);
  3396. isl_multi_aff_free(ma);
  3397. return NULL;
  3398. }
  3399. /* Return the constant terms of the affine expressions of "ma".
  3400. */
  3401. __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
  3402. __isl_keep isl_multi_aff *ma)
  3403. {
  3404. int i;
  3405. isl_size n;
  3406. isl_space *space;
  3407. isl_multi_val *mv;
  3408. n = isl_multi_aff_size(ma);
  3409. if (n < 0)
  3410. return NULL;
  3411. space = isl_space_range(isl_multi_aff_get_space(ma));
  3412. space = isl_space_drop_all_params(space);
  3413. mv = isl_multi_val_zero(space);
  3414. for (i = 0; i < n; ++i) {
  3415. isl_aff *aff;
  3416. isl_val *val;
  3417. aff = isl_multi_aff_get_at(ma, i);
  3418. val = isl_aff_get_constant_val(aff);
  3419. isl_aff_free(aff);
  3420. mv = isl_multi_val_set_at(mv, i, val);
  3421. }
  3422. return mv;
  3423. }
  3424. /* Remove any internal structure of the domain of "ma".
  3425. * If there is any such internal structure in the input,
  3426. * then the name of the corresponding space is also removed.
  3427. */
  3428. __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
  3429. __isl_take isl_multi_aff *ma)
  3430. {
  3431. isl_space *space;
  3432. if (!ma)
  3433. return NULL;
  3434. if (!ma->space->nested[0])
  3435. return ma;
  3436. space = isl_multi_aff_get_space(ma);
  3437. space = isl_space_flatten_domain(space);
  3438. ma = isl_multi_aff_reset_space(ma, space);
  3439. return ma;
  3440. }
  3441. /* Given a map space, return an isl_multi_aff that maps a wrapped copy
  3442. * of the space to its domain.
  3443. */
  3444. __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
  3445. {
  3446. int i;
  3447. isl_size n_in;
  3448. isl_local_space *ls;
  3449. isl_multi_aff *ma;
  3450. if (!space)
  3451. return NULL;
  3452. if (!isl_space_is_map(space))
  3453. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  3454. "not a map space", goto error);
  3455. n_in = isl_space_dim(space, isl_dim_in);
  3456. if (n_in < 0)
  3457. goto error;
  3458. space = isl_space_domain_map(space);
  3459. ma = isl_multi_aff_alloc(isl_space_copy(space));
  3460. if (n_in == 0) {
  3461. isl_space_free(space);
  3462. return ma;
  3463. }
  3464. space = isl_space_domain(space);
  3465. ls = isl_local_space_from_space(space);
  3466. for (i = 0; i < n_in; ++i) {
  3467. isl_aff *aff;
  3468. aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
  3469. isl_dim_set, i);
  3470. ma = isl_multi_aff_set_aff(ma, i, aff);
  3471. }
  3472. isl_local_space_free(ls);
  3473. return ma;
  3474. error:
  3475. isl_space_free(space);
  3476. return NULL;
  3477. }
  3478. /* This function performs the same operation as isl_multi_aff_domain_map,
  3479. * but is considered as a function on an isl_space when exported.
  3480. */
  3481. __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
  3482. __isl_take isl_space *space)
  3483. {
  3484. return isl_multi_aff_domain_map(space);
  3485. }
  3486. /* Given a map space, return an isl_multi_aff that maps a wrapped copy
  3487. * of the space to its range.
  3488. */
  3489. __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
  3490. {
  3491. int i;
  3492. isl_size n_in, n_out;
  3493. isl_local_space *ls;
  3494. isl_multi_aff *ma;
  3495. if (!space)
  3496. return NULL;
  3497. if (!isl_space_is_map(space))
  3498. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  3499. "not a map space", goto error);
  3500. n_in = isl_space_dim(space, isl_dim_in);
  3501. n_out = isl_space_dim(space, isl_dim_out);
  3502. if (n_in < 0 || n_out < 0)
  3503. goto error;
  3504. space = isl_space_range_map(space);
  3505. ma = isl_multi_aff_alloc(isl_space_copy(space));
  3506. if (n_out == 0) {
  3507. isl_space_free(space);
  3508. return ma;
  3509. }
  3510. space = isl_space_domain(space);
  3511. ls = isl_local_space_from_space(space);
  3512. for (i = 0; i < n_out; ++i) {
  3513. isl_aff *aff;
  3514. aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
  3515. isl_dim_set, n_in + i);
  3516. ma = isl_multi_aff_set_aff(ma, i, aff);
  3517. }
  3518. isl_local_space_free(ls);
  3519. return ma;
  3520. error:
  3521. isl_space_free(space);
  3522. return NULL;
  3523. }
  3524. /* This function performs the same operation as isl_multi_aff_range_map,
  3525. * but is considered as a function on an isl_space when exported.
  3526. */
  3527. __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
  3528. __isl_take isl_space *space)
  3529. {
  3530. return isl_multi_aff_range_map(space);
  3531. }
  3532. /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
  3533. * of the space to its domain.
  3534. */
  3535. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
  3536. __isl_take isl_space *space)
  3537. {
  3538. return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
  3539. }
  3540. /* This function performs the same operation as isl_pw_multi_aff_domain_map,
  3541. * but is considered as a function on an isl_space when exported.
  3542. */
  3543. __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
  3544. __isl_take isl_space *space)
  3545. {
  3546. return isl_pw_multi_aff_domain_map(space);
  3547. }
  3548. /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
  3549. * of the space to its range.
  3550. */
  3551. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
  3552. __isl_take isl_space *space)
  3553. {
  3554. return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
  3555. }
  3556. /* This function performs the same operation as isl_pw_multi_aff_range_map,
  3557. * but is considered as a function on an isl_space when exported.
  3558. */
  3559. __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
  3560. __isl_take isl_space *space)
  3561. {
  3562. return isl_pw_multi_aff_range_map(space);
  3563. }
  3564. /* Given the space of a set and a range of set dimensions,
  3565. * construct an isl_multi_aff that projects out those dimensions.
  3566. */
  3567. __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
  3568. __isl_take isl_space *space, enum isl_dim_type type,
  3569. unsigned first, unsigned n)
  3570. {
  3571. int i;
  3572. isl_size dim;
  3573. isl_local_space *ls;
  3574. isl_multi_aff *ma;
  3575. if (!space)
  3576. return NULL;
  3577. if (!isl_space_is_set(space))
  3578. isl_die(isl_space_get_ctx(space), isl_error_unsupported,
  3579. "expecting set space", goto error);
  3580. if (type != isl_dim_set)
  3581. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  3582. "only set dimensions can be projected out", goto error);
  3583. if (isl_space_check_range(space, type, first, n) < 0)
  3584. goto error;
  3585. dim = isl_space_dim(space, isl_dim_set);
  3586. if (dim < 0)
  3587. goto error;
  3588. space = isl_space_from_domain(space);
  3589. space = isl_space_add_dims(space, isl_dim_out, dim - n);
  3590. if (dim == n)
  3591. return isl_multi_aff_alloc(space);
  3592. ma = isl_multi_aff_alloc(isl_space_copy(space));
  3593. space = isl_space_domain(space);
  3594. ls = isl_local_space_from_space(space);
  3595. for (i = 0; i < first; ++i) {
  3596. isl_aff *aff;
  3597. aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
  3598. isl_dim_set, i);
  3599. ma = isl_multi_aff_set_aff(ma, i, aff);
  3600. }
  3601. for (i = 0; i < dim - (first + n); ++i) {
  3602. isl_aff *aff;
  3603. aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
  3604. isl_dim_set, first + n + i);
  3605. ma = isl_multi_aff_set_aff(ma, first + i, aff);
  3606. }
  3607. isl_local_space_free(ls);
  3608. return ma;
  3609. error:
  3610. isl_space_free(space);
  3611. return NULL;
  3612. }
  3613. /* Given the space of a set and a range of set dimensions,
  3614. * construct an isl_pw_multi_aff that projects out those dimensions.
  3615. */
  3616. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
  3617. __isl_take isl_space *space, enum isl_dim_type type,
  3618. unsigned first, unsigned n)
  3619. {
  3620. isl_multi_aff *ma;
  3621. ma = isl_multi_aff_project_out_map(space, type, first, n);
  3622. return isl_pw_multi_aff_from_multi_aff(ma);
  3623. }
  3624. /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
  3625. * but is considered as a function on an isl_multi_aff when exported.
  3626. */
  3627. __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
  3628. __isl_take isl_multi_aff *ma)
  3629. {
  3630. return isl_pw_multi_aff_from_multi_aff(ma);
  3631. }
  3632. /* Create a piecewise multi-affine expression in the given space that maps each
  3633. * input dimension to the corresponding output dimension.
  3634. */
  3635. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
  3636. __isl_take isl_space *space)
  3637. {
  3638. return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
  3639. }
  3640. /* Create a piecewise multi expression that maps elements in the given space
  3641. * to themselves.
  3642. */
  3643. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
  3644. __isl_take isl_space *space)
  3645. {
  3646. isl_multi_aff *ma;
  3647. ma = isl_multi_aff_identity_on_domain_space(space);
  3648. return isl_pw_multi_aff_from_multi_aff(ma);
  3649. }
  3650. /* This function performs the same operation as
  3651. * isl_pw_multi_aff_identity_on_domain_space,
  3652. * but is considered as a function on an isl_space when exported.
  3653. */
  3654. __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
  3655. __isl_take isl_space *space)
  3656. {
  3657. return isl_pw_multi_aff_identity_on_domain_space(space);
  3658. }
  3659. /* Exploit the equalities in "eq" to simplify the affine expressions.
  3660. */
  3661. static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
  3662. __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
  3663. {
  3664. int i;
  3665. maff = isl_multi_aff_cow(maff);
  3666. if (!maff || !eq)
  3667. goto error;
  3668. for (i = 0; i < maff->n; ++i) {
  3669. maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
  3670. isl_basic_set_copy(eq));
  3671. if (!maff->u.p[i])
  3672. goto error;
  3673. }
  3674. isl_basic_set_free(eq);
  3675. return maff;
  3676. error:
  3677. isl_basic_set_free(eq);
  3678. isl_multi_aff_free(maff);
  3679. return NULL;
  3680. }
  3681. __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
  3682. isl_int f)
  3683. {
  3684. int i;
  3685. maff = isl_multi_aff_cow(maff);
  3686. if (!maff)
  3687. return NULL;
  3688. for (i = 0; i < maff->n; ++i) {
  3689. maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
  3690. if (!maff->u.p[i])
  3691. return isl_multi_aff_free(maff);
  3692. }
  3693. return maff;
  3694. }
  3695. __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
  3696. __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
  3697. {
  3698. maff1 = isl_multi_aff_add(maff1, maff2);
  3699. maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
  3700. return maff1;
  3701. }
  3702. isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
  3703. {
  3704. if (!maff)
  3705. return isl_bool_error;
  3706. return isl_bool_false;
  3707. }
  3708. /* Return the set of domain elements where "ma1" is lexicographically
  3709. * smaller than or equal to "ma2".
  3710. */
  3711. __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
  3712. __isl_take isl_multi_aff *ma2)
  3713. {
  3714. return isl_multi_aff_lex_ge_set(ma2, ma1);
  3715. }
  3716. /* Return the set of domain elements where "ma1" is lexicographically
  3717. * smaller than "ma2".
  3718. */
  3719. __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
  3720. __isl_take isl_multi_aff *ma2)
  3721. {
  3722. return isl_multi_aff_lex_gt_set(ma2, ma1);
  3723. }
  3724. /* Return the set of domain elements where "ma1" is lexicographically
  3725. * greater than to "ma2". If "equal" is set, then include the domain
  3726. * elements where they are equal.
  3727. * Do this for the case where there are no entries.
  3728. * In this case, "ma1" cannot be greater than "ma2",
  3729. * but it is (greater than or) equal to "ma2".
  3730. */
  3731. static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
  3732. __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
  3733. {
  3734. isl_space *space;
  3735. space = isl_multi_aff_get_domain_space(ma1);
  3736. isl_multi_aff_free(ma1);
  3737. isl_multi_aff_free(ma2);
  3738. if (equal)
  3739. return isl_set_universe(space);
  3740. else
  3741. return isl_set_empty(space);
  3742. }
  3743. /* Return the set where entry "i" of "ma1" and "ma2"
  3744. * satisfy the relation prescribed by "cmp".
  3745. */
  3746. static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
  3747. __isl_keep isl_multi_aff *ma2, int i,
  3748. __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
  3749. __isl_take isl_aff *aff2))
  3750. {
  3751. isl_aff *aff1, *aff2;
  3752. aff1 = isl_multi_aff_get_at(ma1, i);
  3753. aff2 = isl_multi_aff_get_at(ma2, i);
  3754. return cmp(aff1, aff2);
  3755. }
  3756. /* Return the set of domain elements where "ma1" is lexicographically
  3757. * greater than to "ma2". If "equal" is set, then include the domain
  3758. * elements where they are equal.
  3759. *
  3760. * In particular, for all but the final entry,
  3761. * include the set of elements where this entry is strictly greater in "ma1"
  3762. * and all previous entries are equal.
  3763. * The final entry is also allowed to be equal in the two functions
  3764. * if "equal" is set.
  3765. *
  3766. * The case where there are no entries is handled separately.
  3767. */
  3768. static __isl_give isl_set *isl_multi_aff_lex_gte_set(
  3769. __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
  3770. {
  3771. int i;
  3772. isl_size n;
  3773. isl_space *space;
  3774. isl_set *res;
  3775. isl_set *equal_set;
  3776. isl_set *gte;
  3777. if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
  3778. goto error;
  3779. n = isl_multi_aff_size(ma1);
  3780. if (n < 0)
  3781. goto error;
  3782. if (n == 0)
  3783. return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
  3784. space = isl_multi_aff_get_domain_space(ma1);
  3785. res = isl_set_empty(isl_space_copy(space));
  3786. equal_set = isl_set_universe(space);
  3787. for (i = 0; i + 1 < n; ++i) {
  3788. isl_bool empty;
  3789. isl_set *gt, *eq;
  3790. gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
  3791. gt = isl_set_intersect(gt, isl_set_copy(equal_set));
  3792. res = isl_set_union(res, gt);
  3793. eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
  3794. equal_set = isl_set_intersect(equal_set, eq);
  3795. empty = isl_set_is_empty(equal_set);
  3796. if (empty >= 0 && empty)
  3797. break;
  3798. }
  3799. if (equal)
  3800. gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
  3801. else
  3802. gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
  3803. isl_multi_aff_free(ma1);
  3804. isl_multi_aff_free(ma2);
  3805. gte = isl_set_intersect(gte, equal_set);
  3806. return isl_set_union(res, gte);
  3807. error:
  3808. isl_multi_aff_free(ma1);
  3809. isl_multi_aff_free(ma2);
  3810. return NULL;
  3811. }
  3812. /* Return the set of domain elements where "ma1" is lexicographically
  3813. * greater than or equal to "ma2".
  3814. */
  3815. __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
  3816. __isl_take isl_multi_aff *ma2)
  3817. {
  3818. return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
  3819. }
  3820. /* Return the set of domain elements where "ma1" is lexicographically
  3821. * greater than "ma2".
  3822. */
  3823. __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
  3824. __isl_take isl_multi_aff *ma2)
  3825. {
  3826. return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
  3827. }
  3828. #define isl_multi_aff_zero_in_space isl_multi_aff_zero
  3829. #undef PW
  3830. #define PW isl_pw_multi_aff
  3831. #undef BASE
  3832. #define BASE multi_aff
  3833. #undef EL_IS_ZERO
  3834. #define EL_IS_ZERO is_empty
  3835. #undef ZERO
  3836. #define ZERO empty
  3837. #undef IS_ZERO
  3838. #define IS_ZERO is_empty
  3839. #undef FIELD
  3840. #define FIELD maff
  3841. #undef DEFAULT_IS_ZERO
  3842. #define DEFAULT_IS_ZERO 0
  3843. #include <isl_pw_templ.c>
  3844. #include <isl_pw_add_constant_multi_val_templ.c>
  3845. #include <isl_pw_add_constant_val_templ.c>
  3846. #include <isl_pw_bind_domain_templ.c>
  3847. #include <isl_pw_insert_dims_templ.c>
  3848. #include <isl_pw_insert_domain_templ.c>
  3849. #include <isl_pw_locals_templ.c>
  3850. #include <isl_pw_move_dims_templ.c>
  3851. #include <isl_pw_neg_templ.c>
  3852. #include <isl_pw_pullback_templ.c>
  3853. #include <isl_pw_range_tuple_id_templ.c>
  3854. #include <isl_pw_union_opt.c>
  3855. #undef BASE
  3856. #define BASE pw_multi_aff
  3857. #include <isl_union_multi.c>
  3858. #include "isl_union_locals_templ.c"
  3859. #include <isl_union_neg.c>
  3860. #undef BASE
  3861. #define BASE multi_aff
  3862. #include <isl_union_pw_templ.c>
  3863. /* Generic function for extracting a factor from a product "pma".
  3864. * "check_space" checks that the space is that of the right kind of product.
  3865. * "space_factor" extracts the factor from the space.
  3866. * "multi_aff_factor" extracts the factor from the constituent functions.
  3867. */
  3868. static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
  3869. __isl_take isl_pw_multi_aff *pma,
  3870. isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
  3871. __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
  3872. __isl_give isl_multi_aff *(*multi_aff_factor)(
  3873. __isl_take isl_multi_aff *ma))
  3874. {
  3875. int i;
  3876. isl_space *space;
  3877. if (check_space(pma) < 0)
  3878. return isl_pw_multi_aff_free(pma);
  3879. space = isl_pw_multi_aff_take_space(pma);
  3880. space = space_factor(space);
  3881. for (i = 0; pma && i < pma->n; ++i) {
  3882. isl_multi_aff *ma;
  3883. ma = isl_pw_multi_aff_take_base_at(pma, i);
  3884. ma = multi_aff_factor(ma);
  3885. pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
  3886. }
  3887. pma = isl_pw_multi_aff_restore_space(pma, space);
  3888. return pma;
  3889. }
  3890. /* Is the range of "pma" a wrapped relation?
  3891. */
  3892. static isl_bool isl_pw_multi_aff_range_is_wrapping(
  3893. __isl_keep isl_pw_multi_aff *pma)
  3894. {
  3895. return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
  3896. }
  3897. /* Check that the range of "pma" is a product.
  3898. */
  3899. static isl_stat pw_multi_aff_check_range_product(
  3900. __isl_keep isl_pw_multi_aff *pma)
  3901. {
  3902. isl_bool wraps;
  3903. wraps = isl_pw_multi_aff_range_is_wrapping(pma);
  3904. if (wraps < 0)
  3905. return isl_stat_error;
  3906. if (!wraps)
  3907. isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
  3908. "range is not a product", return isl_stat_error);
  3909. return isl_stat_ok;
  3910. }
  3911. /* Given a function A -> [B -> C], extract the function A -> B.
  3912. */
  3913. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
  3914. __isl_take isl_pw_multi_aff *pma)
  3915. {
  3916. return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
  3917. &isl_space_range_factor_domain,
  3918. &isl_multi_aff_range_factor_domain);
  3919. }
  3920. /* Given a function A -> [B -> C], extract the function A -> C.
  3921. */
  3922. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
  3923. __isl_take isl_pw_multi_aff *pma)
  3924. {
  3925. return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
  3926. &isl_space_range_factor_range,
  3927. &isl_multi_aff_range_factor_range);
  3928. }
  3929. /* Given two piecewise multi affine expressions, return a piecewise
  3930. * multi-affine expression defined on the union of the definition domains
  3931. * of the inputs that is equal to the lexicographic maximum of the two
  3932. * inputs on each cell. If only one of the two inputs is defined on
  3933. * a given cell, then it is considered to be the maximum.
  3934. */
  3935. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
  3936. __isl_take isl_pw_multi_aff *pma1,
  3937. __isl_take isl_pw_multi_aff *pma2)
  3938. {
  3939. isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
  3940. return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
  3941. &isl_multi_aff_lex_ge_set);
  3942. }
  3943. /* Given two piecewise multi affine expressions, return a piecewise
  3944. * multi-affine expression defined on the union of the definition domains
  3945. * of the inputs that is equal to the lexicographic minimum of the two
  3946. * inputs on each cell. If only one of the two inputs is defined on
  3947. * a given cell, then it is considered to be the minimum.
  3948. */
  3949. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
  3950. __isl_take isl_pw_multi_aff *pma1,
  3951. __isl_take isl_pw_multi_aff *pma2)
  3952. {
  3953. isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
  3954. return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
  3955. &isl_multi_aff_lex_le_set);
  3956. }
  3957. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
  3958. __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
  3959. {
  3960. isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
  3961. return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
  3962. &isl_multi_aff_add);
  3963. }
  3964. /* Subtract "pma2" from "pma1" and return the result.
  3965. */
  3966. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
  3967. __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
  3968. {
  3969. isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
  3970. return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
  3971. &isl_multi_aff_sub);
  3972. }
  3973. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
  3974. __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
  3975. {
  3976. return isl_pw_multi_aff_union_add_(pma1, pma2);
  3977. }
  3978. /* Compute the sum of "upa1" and "upa2" on the union of their domains,
  3979. * with the actual sum on the shared domain and
  3980. * the defined expression on the symmetric difference of the domains.
  3981. */
  3982. __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
  3983. __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
  3984. {
  3985. return isl_union_pw_aff_union_add_(upa1, upa2);
  3986. }
  3987. /* Compute the sum of "upma1" and "upma2" on the union of their domains,
  3988. * with the actual sum on the shared domain and
  3989. * the defined expression on the symmetric difference of the domains.
  3990. */
  3991. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
  3992. __isl_take isl_union_pw_multi_aff *upma1,
  3993. __isl_take isl_union_pw_multi_aff *upma2)
  3994. {
  3995. return isl_union_pw_multi_aff_union_add_(upma1, upma2);
  3996. }
  3997. /* Given two piecewise multi-affine expressions A -> B and C -> D,
  3998. * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
  3999. */
  4000. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
  4001. __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
  4002. {
  4003. int i, j, n;
  4004. isl_space *space;
  4005. isl_pw_multi_aff *res;
  4006. if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
  4007. goto error;
  4008. n = pma1->n * pma2->n;
  4009. space = isl_space_product(isl_space_copy(pma1->dim),
  4010. isl_space_copy(pma2->dim));
  4011. res = isl_pw_multi_aff_alloc_size(space, n);
  4012. for (i = 0; i < pma1->n; ++i) {
  4013. for (j = 0; j < pma2->n; ++j) {
  4014. isl_set *domain;
  4015. isl_multi_aff *ma;
  4016. domain = isl_set_product(isl_set_copy(pma1->p[i].set),
  4017. isl_set_copy(pma2->p[j].set));
  4018. ma = isl_multi_aff_product(
  4019. isl_multi_aff_copy(pma1->p[i].maff),
  4020. isl_multi_aff_copy(pma2->p[j].maff));
  4021. res = isl_pw_multi_aff_add_piece(res, domain, ma);
  4022. }
  4023. }
  4024. isl_pw_multi_aff_free(pma1);
  4025. isl_pw_multi_aff_free(pma2);
  4026. return res;
  4027. error:
  4028. isl_pw_multi_aff_free(pma1);
  4029. isl_pw_multi_aff_free(pma2);
  4030. return NULL;
  4031. }
  4032. /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
  4033. * denominator "denom".
  4034. * "denom" is allowed to be negative, in which case the actual denominator
  4035. * is -denom and the expressions are added instead.
  4036. */
  4037. static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
  4038. __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
  4039. {
  4040. int i, first;
  4041. int sign;
  4042. isl_int d;
  4043. first = isl_seq_first_non_zero(c, n);
  4044. if (first == -1)
  4045. return aff;
  4046. sign = isl_int_sgn(denom);
  4047. isl_int_init(d);
  4048. isl_int_abs(d, denom);
  4049. for (i = first; i < n; ++i) {
  4050. isl_aff *aff_i;
  4051. if (isl_int_is_zero(c[i]))
  4052. continue;
  4053. aff_i = isl_multi_aff_get_aff(ma, i);
  4054. aff_i = isl_aff_scale(aff_i, c[i]);
  4055. aff_i = isl_aff_scale_down(aff_i, d);
  4056. if (sign >= 0)
  4057. aff = isl_aff_sub(aff, aff_i);
  4058. else
  4059. aff = isl_aff_add(aff, aff_i);
  4060. }
  4061. isl_int_clear(d);
  4062. return aff;
  4063. }
  4064. /* Extract an affine expression that expresses the output dimension "pos"
  4065. * of "bmap" in terms of the parameters and input dimensions from
  4066. * equality "eq".
  4067. * Note that this expression may involve integer divisions defined
  4068. * in terms of parameters and input dimensions.
  4069. * The equality may also involve references to earlier (but not later)
  4070. * output dimensions. These are replaced by the corresponding elements
  4071. * in "ma".
  4072. *
  4073. * If the equality is of the form
  4074. *
  4075. * f(i) + h(j) + a x + g(i) = 0,
  4076. *
  4077. * with f(i) a linear combinations of the parameters and input dimensions,
  4078. * g(i) a linear combination of integer divisions defined in terms of the same
  4079. * and h(j) a linear combinations of earlier output dimensions,
  4080. * then the affine expression is
  4081. *
  4082. * (-f(i) - g(i))/a - h(j)/a
  4083. *
  4084. * If the equality is of the form
  4085. *
  4086. * f(i) + h(j) - a x + g(i) = 0,
  4087. *
  4088. * then the affine expression is
  4089. *
  4090. * (f(i) + g(i))/a - h(j)/(-a)
  4091. *
  4092. *
  4093. * If "div" refers to an integer division (i.e., it is smaller than
  4094. * the number of integer divisions), then the equality constraint
  4095. * does involve an integer division (the one at position "div") that
  4096. * is defined in terms of output dimensions. However, this integer
  4097. * division can be eliminated by exploiting a pair of constraints
  4098. * x >= l and x <= l + n, with n smaller than the coefficient of "div"
  4099. * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
  4100. * -l + x >= 0.
  4101. * In particular, let
  4102. *
  4103. * x = e(i) + m floor(...)
  4104. *
  4105. * with e(i) the expression derived above and floor(...) the integer
  4106. * division involving output dimensions.
  4107. * From
  4108. *
  4109. * l <= x <= l + n,
  4110. *
  4111. * we have
  4112. *
  4113. * 0 <= x - l <= n
  4114. *
  4115. * This means
  4116. *
  4117. * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
  4118. * = (e(i) - l) mod m
  4119. *
  4120. * Therefore,
  4121. *
  4122. * x - l = (e(i) - l) mod m
  4123. *
  4124. * or
  4125. *
  4126. * x = ((e(i) - l) mod m) + l
  4127. *
  4128. * The variable "shift" below contains the expression -l, which may
  4129. * also involve a linear combination of earlier output dimensions.
  4130. */
  4131. static __isl_give isl_aff *extract_aff_from_equality(
  4132. __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
  4133. __isl_keep isl_multi_aff *ma)
  4134. {
  4135. unsigned o_out;
  4136. isl_size n_div, n_out;
  4137. isl_ctx *ctx;
  4138. isl_local_space *ls;
  4139. isl_aff *aff, *shift;
  4140. isl_val *mod;
  4141. ctx = isl_basic_map_get_ctx(bmap);
  4142. ls = isl_basic_map_get_local_space(bmap);
  4143. ls = isl_local_space_domain(ls);
  4144. aff = isl_aff_alloc(isl_local_space_copy(ls));
  4145. if (!aff)
  4146. goto error;
  4147. o_out = isl_basic_map_offset(bmap, isl_dim_out);
  4148. n_out = isl_basic_map_dim(bmap, isl_dim_out);
  4149. n_div = isl_basic_map_dim(bmap, isl_dim_div);
  4150. if (n_out < 0 || n_div < 0)
  4151. goto error;
  4152. if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
  4153. isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
  4154. isl_seq_cpy(aff->v->el + 1 + o_out,
  4155. bmap->eq[eq] + o_out + n_out, n_div);
  4156. } else {
  4157. isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
  4158. isl_seq_neg(aff->v->el + 1 + o_out,
  4159. bmap->eq[eq] + o_out + n_out, n_div);
  4160. }
  4161. if (div < n_div)
  4162. isl_int_set_si(aff->v->el[1 + o_out + div], 0);
  4163. isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
  4164. aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
  4165. bmap->eq[eq][o_out + pos]);
  4166. if (div < n_div) {
  4167. shift = isl_aff_alloc(isl_local_space_copy(ls));
  4168. if (!shift)
  4169. goto error;
  4170. isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
  4171. isl_seq_cpy(shift->v->el + 1 + o_out,
  4172. bmap->ineq[ineq] + o_out + n_out, n_div);
  4173. isl_int_set_si(shift->v->el[0], 1);
  4174. shift = subtract_initial(shift, ma, pos,
  4175. bmap->ineq[ineq] + o_out, ctx->negone);
  4176. aff = isl_aff_add(aff, isl_aff_copy(shift));
  4177. mod = isl_val_int_from_isl_int(ctx,
  4178. bmap->eq[eq][o_out + n_out + div]);
  4179. mod = isl_val_abs(mod);
  4180. aff = isl_aff_mod_val(aff, mod);
  4181. aff = isl_aff_sub(aff, shift);
  4182. }
  4183. isl_local_space_free(ls);
  4184. return aff;
  4185. error:
  4186. isl_local_space_free(ls);
  4187. isl_aff_free(aff);
  4188. return NULL;
  4189. }
  4190. /* Given a basic map with output dimensions defined
  4191. * in terms of the parameters input dimensions and earlier
  4192. * output dimensions using an equality (and possibly a pair on inequalities),
  4193. * extract an isl_aff that expresses output dimension "pos" in terms
  4194. * of the parameters and input dimensions.
  4195. * Note that this expression may involve integer divisions defined
  4196. * in terms of parameters and input dimensions.
  4197. * "ma" contains the expressions corresponding to earlier output dimensions.
  4198. *
  4199. * This function shares some similarities with
  4200. * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
  4201. */
  4202. static __isl_give isl_aff *extract_isl_aff_from_basic_map(
  4203. __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
  4204. {
  4205. int eq, div, ineq;
  4206. isl_aff *aff;
  4207. if (!bmap)
  4208. return NULL;
  4209. eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
  4210. if (eq >= bmap->n_eq)
  4211. isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
  4212. "unable to find suitable equality", return NULL);
  4213. aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
  4214. aff = isl_aff_remove_unused_divs(aff);
  4215. return aff;
  4216. }
  4217. /* Given a basic map where each output dimension is defined
  4218. * in terms of the parameters and input dimensions using an equality,
  4219. * extract an isl_multi_aff that expresses the output dimensions in terms
  4220. * of the parameters and input dimensions.
  4221. */
  4222. static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
  4223. __isl_take isl_basic_map *bmap)
  4224. {
  4225. int i;
  4226. isl_size n_out;
  4227. isl_multi_aff *ma;
  4228. if (!bmap)
  4229. return NULL;
  4230. ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
  4231. n_out = isl_basic_map_dim(bmap, isl_dim_out);
  4232. if (n_out < 0)
  4233. ma = isl_multi_aff_free(ma);
  4234. for (i = 0; i < n_out; ++i) {
  4235. isl_aff *aff;
  4236. aff = extract_isl_aff_from_basic_map(bmap, i, ma);
  4237. ma = isl_multi_aff_set_aff(ma, i, aff);
  4238. }
  4239. isl_basic_map_free(bmap);
  4240. return ma;
  4241. }
  4242. /* Given a basic set where each set dimension is defined
  4243. * in terms of the parameters using an equality,
  4244. * extract an isl_multi_aff that expresses the set dimensions in terms
  4245. * of the parameters.
  4246. */
  4247. __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
  4248. __isl_take isl_basic_set *bset)
  4249. {
  4250. return extract_isl_multi_aff_from_basic_map(bset);
  4251. }
  4252. /* Create an isl_pw_multi_aff that is equivalent to
  4253. * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
  4254. * The given basic map is such that each output dimension is defined
  4255. * in terms of the parameters and input dimensions using an equality.
  4256. *
  4257. * Since some applications expect the result of isl_pw_multi_aff_from_map
  4258. * to only contain integer affine expressions, we compute the floor
  4259. * of the expression before returning.
  4260. *
  4261. * Remove all constraints involving local variables without
  4262. * an explicit representation (resulting in the removal of those
  4263. * local variables) prior to the actual extraction to ensure
  4264. * that the local spaces in which the resulting affine expressions
  4265. * are created do not contain any unknown local variables.
  4266. * Removing such constraints is safe because constraints involving
  4267. * unknown local variables are not used to determine whether
  4268. * a basic map is obviously single-valued.
  4269. */
  4270. static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
  4271. __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
  4272. {
  4273. isl_multi_aff *ma;
  4274. bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
  4275. ma = extract_isl_multi_aff_from_basic_map(bmap);
  4276. ma = isl_multi_aff_floor(ma);
  4277. return isl_pw_multi_aff_alloc(domain, ma);
  4278. }
  4279. /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
  4280. * This obviously only works if the input "map" is single-valued.
  4281. * If so, we compute the lexicographic minimum of the image in the form
  4282. * of an isl_pw_multi_aff. Since the image is unique, it is equal
  4283. * to its lexicographic minimum.
  4284. * If the input is not single-valued, we produce an error.
  4285. */
  4286. static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
  4287. __isl_take isl_map *map)
  4288. {
  4289. int i;
  4290. int sv;
  4291. isl_pw_multi_aff *pma;
  4292. sv = isl_map_is_single_valued(map);
  4293. if (sv < 0)
  4294. goto error;
  4295. if (!sv)
  4296. isl_die(isl_map_get_ctx(map), isl_error_invalid,
  4297. "map is not single-valued", goto error);
  4298. map = isl_map_make_disjoint(map);
  4299. if (!map)
  4300. return NULL;
  4301. pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
  4302. for (i = 0; i < map->n; ++i) {
  4303. isl_pw_multi_aff *pma_i;
  4304. isl_basic_map *bmap;
  4305. bmap = isl_basic_map_copy(map->p[i]);
  4306. pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
  4307. pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
  4308. }
  4309. isl_map_free(map);
  4310. return pma;
  4311. error:
  4312. isl_map_free(map);
  4313. return NULL;
  4314. }
  4315. /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
  4316. * taking into account that the output dimension at position "d"
  4317. * can be represented as
  4318. *
  4319. * x = floor((e(...) + c1) / m)
  4320. *
  4321. * given that constraint "i" is of the form
  4322. *
  4323. * e(...) + c1 - m x >= 0
  4324. *
  4325. *
  4326. * Let "map" be of the form
  4327. *
  4328. * A -> B
  4329. *
  4330. * We construct a mapping
  4331. *
  4332. * A -> [A -> x = floor(...)]
  4333. *
  4334. * apply that to the map, obtaining
  4335. *
  4336. * [A -> x = floor(...)] -> B
  4337. *
  4338. * and equate dimension "d" to x.
  4339. * We then compute a isl_pw_multi_aff representation of the resulting map
  4340. * and plug in the mapping above.
  4341. */
  4342. static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
  4343. __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
  4344. {
  4345. isl_ctx *ctx;
  4346. isl_space *space = NULL;
  4347. isl_local_space *ls;
  4348. isl_multi_aff *ma;
  4349. isl_aff *aff;
  4350. isl_vec *v;
  4351. isl_map *insert;
  4352. int offset;
  4353. isl_size n;
  4354. isl_size n_in;
  4355. isl_pw_multi_aff *pma;
  4356. isl_bool is_set;
  4357. is_set = isl_map_is_set(map);
  4358. if (is_set < 0)
  4359. goto error;
  4360. offset = isl_basic_map_offset(hull, isl_dim_out);
  4361. ctx = isl_map_get_ctx(map);
  4362. space = isl_space_domain(isl_map_get_space(map));
  4363. n_in = isl_space_dim(space, isl_dim_set);
  4364. n = isl_space_dim(space, isl_dim_all);
  4365. if (n_in < 0 || n < 0)
  4366. goto error;
  4367. v = isl_vec_alloc(ctx, 1 + 1 + n);
  4368. if (v) {
  4369. isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
  4370. isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
  4371. }
  4372. isl_basic_map_free(hull);
  4373. ls = isl_local_space_from_space(isl_space_copy(space));
  4374. aff = isl_aff_alloc_vec(ls, v);
  4375. aff = isl_aff_floor(aff);
  4376. if (is_set) {
  4377. isl_space_free(space);
  4378. ma = isl_multi_aff_from_aff(aff);
  4379. } else {
  4380. ma = isl_multi_aff_identity(isl_space_map_from_set(space));
  4381. ma = isl_multi_aff_range_product(ma,
  4382. isl_multi_aff_from_aff(aff));
  4383. }
  4384. insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
  4385. map = isl_map_apply_domain(map, insert);
  4386. map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
  4387. pma = isl_pw_multi_aff_from_map(map);
  4388. pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
  4389. return pma;
  4390. error:
  4391. isl_space_free(space);
  4392. isl_map_free(map);
  4393. isl_basic_map_free(hull);
  4394. return NULL;
  4395. }
  4396. /* Is constraint "c" of the form
  4397. *
  4398. * e(...) + c1 - m x >= 0
  4399. *
  4400. * or
  4401. *
  4402. * -e(...) + c2 + m x >= 0
  4403. *
  4404. * where m > 1 and e only depends on parameters and input dimensions?
  4405. *
  4406. * "offset" is the offset of the output dimensions
  4407. * "pos" is the position of output dimension x.
  4408. */
  4409. static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
  4410. {
  4411. if (isl_int_is_zero(c[offset + d]))
  4412. return 0;
  4413. if (isl_int_is_one(c[offset + d]))
  4414. return 0;
  4415. if (isl_int_is_negone(c[offset + d]))
  4416. return 0;
  4417. if (isl_seq_first_non_zero(c + offset, d) != -1)
  4418. return 0;
  4419. if (isl_seq_first_non_zero(c + offset + d + 1,
  4420. total - (offset + d + 1)) != -1)
  4421. return 0;
  4422. return 1;
  4423. }
  4424. /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
  4425. *
  4426. * As a special case, we first check if there is any pair of constraints,
  4427. * shared by all the basic maps in "map" that force a given dimension
  4428. * to be equal to the floor of some affine combination of the input dimensions.
  4429. *
  4430. * In particular, if we can find two constraints
  4431. *
  4432. * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
  4433. *
  4434. * and
  4435. *
  4436. * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
  4437. *
  4438. * where m > 1 and e only depends on parameters and input dimensions,
  4439. * and such that
  4440. *
  4441. * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
  4442. *
  4443. * then we know that we can take
  4444. *
  4445. * x = floor((e(...) + c1) / m)
  4446. *
  4447. * without having to perform any computation.
  4448. *
  4449. * Note that we know that
  4450. *
  4451. * c1 + c2 >= 1
  4452. *
  4453. * If c1 + c2 were 0, then we would have detected an equality during
  4454. * simplification. If c1 + c2 were negative, then we would have detected
  4455. * a contradiction.
  4456. */
  4457. static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
  4458. __isl_take isl_map *map)
  4459. {
  4460. int d;
  4461. isl_size dim;
  4462. int i, j, n;
  4463. int offset;
  4464. isl_size total;
  4465. isl_int sum;
  4466. isl_basic_map *hull;
  4467. hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
  4468. dim = isl_map_dim(map, isl_dim_out);
  4469. total = isl_basic_map_dim(hull, isl_dim_all);
  4470. if (dim < 0 || total < 0)
  4471. goto error;
  4472. isl_int_init(sum);
  4473. offset = isl_basic_map_offset(hull, isl_dim_out);
  4474. n = hull->n_ineq;
  4475. for (d = 0; d < dim; ++d) {
  4476. for (i = 0; i < n; ++i) {
  4477. if (!is_potential_div_constraint(hull->ineq[i],
  4478. offset, d, 1 + total))
  4479. continue;
  4480. for (j = i + 1; j < n; ++j) {
  4481. if (!isl_seq_is_neg(hull->ineq[i] + 1,
  4482. hull->ineq[j] + 1, total))
  4483. continue;
  4484. isl_int_add(sum, hull->ineq[i][0],
  4485. hull->ineq[j][0]);
  4486. if (isl_int_abs_lt(sum,
  4487. hull->ineq[i][offset + d]))
  4488. break;
  4489. }
  4490. if (j >= n)
  4491. continue;
  4492. isl_int_clear(sum);
  4493. if (isl_int_is_pos(hull->ineq[j][offset + d]))
  4494. j = i;
  4495. return pw_multi_aff_from_map_div(map, hull, d, j);
  4496. }
  4497. }
  4498. isl_int_clear(sum);
  4499. isl_basic_map_free(hull);
  4500. return pw_multi_aff_from_map_base(map);
  4501. error:
  4502. isl_map_free(map);
  4503. isl_basic_map_free(hull);
  4504. return NULL;
  4505. }
  4506. /* Given an affine expression
  4507. *
  4508. * [A -> B] -> f(A,B)
  4509. *
  4510. * construct an isl_multi_aff
  4511. *
  4512. * [A -> B] -> B'
  4513. *
  4514. * such that dimension "d" in B' is set to "aff" and the remaining
  4515. * dimensions are set equal to the corresponding dimensions in B.
  4516. * "n_in" is the dimension of the space A.
  4517. * "n_out" is the dimension of the space B.
  4518. *
  4519. * If "is_set" is set, then the affine expression is of the form
  4520. *
  4521. * [B] -> f(B)
  4522. *
  4523. * and we construct an isl_multi_aff
  4524. *
  4525. * B -> B'
  4526. */
  4527. static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
  4528. unsigned n_in, unsigned n_out, int is_set)
  4529. {
  4530. int i;
  4531. isl_multi_aff *ma;
  4532. isl_space *space, *space2;
  4533. isl_local_space *ls;
  4534. space = isl_aff_get_domain_space(aff);
  4535. ls = isl_local_space_from_space(isl_space_copy(space));
  4536. space2 = isl_space_copy(space);
  4537. if (!is_set)
  4538. space2 = isl_space_range(isl_space_unwrap(space2));
  4539. space = isl_space_map_from_domain_and_range(space, space2);
  4540. ma = isl_multi_aff_alloc(space);
  4541. ma = isl_multi_aff_set_aff(ma, d, aff);
  4542. for (i = 0; i < n_out; ++i) {
  4543. if (i == d)
  4544. continue;
  4545. aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
  4546. isl_dim_set, n_in + i);
  4547. ma = isl_multi_aff_set_aff(ma, i, aff);
  4548. }
  4549. isl_local_space_free(ls);
  4550. return ma;
  4551. }
  4552. /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
  4553. * taking into account that the dimension at position "d" can be written as
  4554. *
  4555. * x = m a + f(..) (1)
  4556. *
  4557. * where m is equal to "gcd".
  4558. * "i" is the index of the equality in "hull" that defines f(..).
  4559. * In particular, the equality is of the form
  4560. *
  4561. * f(..) - x + m g(existentials) = 0
  4562. *
  4563. * or
  4564. *
  4565. * -f(..) + x + m g(existentials) = 0
  4566. *
  4567. * We basically plug (1) into "map", resulting in a map with "a"
  4568. * in the range instead of "x". The corresponding isl_pw_multi_aff
  4569. * defining "a" is then plugged back into (1) to obtain a definition for "x".
  4570. *
  4571. * Specifically, given the input map
  4572. *
  4573. * A -> B
  4574. *
  4575. * We first wrap it into a set
  4576. *
  4577. * [A -> B]
  4578. *
  4579. * and define (1) on top of the corresponding space, resulting in "aff".
  4580. * We use this to create an isl_multi_aff that maps the output position "d"
  4581. * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
  4582. * We plug this into the wrapped map, unwrap the result and compute the
  4583. * corresponding isl_pw_multi_aff.
  4584. * The result is an expression
  4585. *
  4586. * A -> T(A)
  4587. *
  4588. * We adjust that to
  4589. *
  4590. * A -> [A -> T(A)]
  4591. *
  4592. * so that we can plug that into "aff", after extending the latter to
  4593. * a mapping
  4594. *
  4595. * [A -> B] -> B'
  4596. *
  4597. *
  4598. * If "map" is actually a set, then there is no "A" space, meaning
  4599. * that we do not need to perform any wrapping, and that the result
  4600. * of the recursive call is of the form
  4601. *
  4602. * [T]
  4603. *
  4604. * which is plugged into a mapping of the form
  4605. *
  4606. * B -> B'
  4607. */
  4608. static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
  4609. __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
  4610. isl_int gcd)
  4611. {
  4612. isl_set *set;
  4613. isl_space *space;
  4614. isl_local_space *ls;
  4615. isl_aff *aff;
  4616. isl_multi_aff *ma;
  4617. isl_pw_multi_aff *pma, *id;
  4618. isl_size n_in;
  4619. unsigned o_out;
  4620. isl_size n_out;
  4621. isl_bool is_set;
  4622. is_set = isl_map_is_set(map);
  4623. if (is_set < 0)
  4624. goto error;
  4625. n_in = isl_basic_map_dim(hull, isl_dim_in);
  4626. n_out = isl_basic_map_dim(hull, isl_dim_out);
  4627. if (n_in < 0 || n_out < 0)
  4628. goto error;
  4629. o_out = isl_basic_map_offset(hull, isl_dim_out);
  4630. if (is_set)
  4631. set = map;
  4632. else
  4633. set = isl_map_wrap(map);
  4634. space = isl_space_map_from_set(isl_set_get_space(set));
  4635. ma = isl_multi_aff_identity(space);
  4636. ls = isl_local_space_from_space(isl_set_get_space(set));
  4637. aff = isl_aff_alloc(ls);
  4638. if (aff) {
  4639. isl_int_set_si(aff->v->el[0], 1);
  4640. if (isl_int_is_one(hull->eq[i][o_out + d]))
  4641. isl_seq_neg(aff->v->el + 1, hull->eq[i],
  4642. aff->v->size - 1);
  4643. else
  4644. isl_seq_cpy(aff->v->el + 1, hull->eq[i],
  4645. aff->v->size - 1);
  4646. isl_int_set(aff->v->el[1 + o_out + d], gcd);
  4647. }
  4648. ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
  4649. set = isl_set_preimage_multi_aff(set, ma);
  4650. ma = range_map(aff, d, n_in, n_out, is_set);
  4651. if (is_set)
  4652. map = set;
  4653. else
  4654. map = isl_set_unwrap(set);
  4655. pma = isl_pw_multi_aff_from_map(map);
  4656. if (!is_set) {
  4657. space = isl_pw_multi_aff_get_domain_space(pma);
  4658. space = isl_space_map_from_set(space);
  4659. id = isl_pw_multi_aff_identity(space);
  4660. pma = isl_pw_multi_aff_range_product(id, pma);
  4661. }
  4662. id = isl_pw_multi_aff_from_multi_aff(ma);
  4663. pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
  4664. isl_basic_map_free(hull);
  4665. return pma;
  4666. error:
  4667. isl_map_free(map);
  4668. isl_basic_map_free(hull);
  4669. return NULL;
  4670. }
  4671. /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
  4672. * "hull" contains the equalities valid for "map".
  4673. *
  4674. * Check if any of the output dimensions is "strided".
  4675. * That is, we check if it can be written as
  4676. *
  4677. * x = m a + f(..)
  4678. *
  4679. * with m greater than 1, a some combination of existentially quantified
  4680. * variables and f an expression in the parameters and input dimensions.
  4681. * If so, we remove the stride in pw_multi_aff_from_map_stride.
  4682. *
  4683. * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
  4684. * special case.
  4685. */
  4686. static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
  4687. __isl_take isl_map *map, __isl_take isl_basic_map *hull)
  4688. {
  4689. int i, j;
  4690. isl_size n_out;
  4691. unsigned o_out;
  4692. isl_size n_div;
  4693. unsigned o_div;
  4694. isl_int gcd;
  4695. n_div = isl_basic_map_dim(hull, isl_dim_div);
  4696. n_out = isl_basic_map_dim(hull, isl_dim_out);
  4697. if (n_div < 0 || n_out < 0)
  4698. goto error;
  4699. if (n_div == 0) {
  4700. isl_basic_map_free(hull);
  4701. return pw_multi_aff_from_map_check_div(map);
  4702. }
  4703. isl_int_init(gcd);
  4704. o_div = isl_basic_map_offset(hull, isl_dim_div);
  4705. o_out = isl_basic_map_offset(hull, isl_dim_out);
  4706. for (i = 0; i < n_out; ++i) {
  4707. for (j = 0; j < hull->n_eq; ++j) {
  4708. isl_int *eq = hull->eq[j];
  4709. isl_pw_multi_aff *res;
  4710. if (!isl_int_is_one(eq[o_out + i]) &&
  4711. !isl_int_is_negone(eq[o_out + i]))
  4712. continue;
  4713. if (isl_seq_first_non_zero(eq + o_out, i) != -1)
  4714. continue;
  4715. if (isl_seq_first_non_zero(eq + o_out + i + 1,
  4716. n_out - (i + 1)) != -1)
  4717. continue;
  4718. isl_seq_gcd(eq + o_div, n_div, &gcd);
  4719. if (isl_int_is_zero(gcd))
  4720. continue;
  4721. if (isl_int_is_one(gcd))
  4722. continue;
  4723. res = pw_multi_aff_from_map_stride(map, hull,
  4724. i, j, gcd);
  4725. isl_int_clear(gcd);
  4726. return res;
  4727. }
  4728. }
  4729. isl_int_clear(gcd);
  4730. isl_basic_map_free(hull);
  4731. return pw_multi_aff_from_map_check_div(map);
  4732. error:
  4733. isl_map_free(map);
  4734. isl_basic_map_free(hull);
  4735. return NULL;
  4736. }
  4737. /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
  4738. *
  4739. * As a special case, we first check if all output dimensions are uniquely
  4740. * defined in terms of the parameters and input dimensions over the entire
  4741. * domain. If so, we extract the desired isl_pw_multi_aff directly
  4742. * from the affine hull of "map" and its domain.
  4743. *
  4744. * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
  4745. * special cases.
  4746. */
  4747. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
  4748. {
  4749. isl_bool sv;
  4750. isl_size n;
  4751. isl_basic_map *hull;
  4752. n = isl_map_n_basic_map(map);
  4753. if (n < 0)
  4754. goto error;
  4755. if (n == 1) {
  4756. hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
  4757. hull = isl_basic_map_plain_affine_hull(hull);
  4758. sv = isl_basic_map_plain_is_single_valued(hull);
  4759. if (sv >= 0 && sv)
  4760. return plain_pw_multi_aff_from_map(isl_map_domain(map),
  4761. hull);
  4762. isl_basic_map_free(hull);
  4763. }
  4764. map = isl_map_detect_equalities(map);
  4765. hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
  4766. sv = isl_basic_map_plain_is_single_valued(hull);
  4767. if (sv >= 0 && sv)
  4768. return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
  4769. if (sv >= 0)
  4770. return pw_multi_aff_from_map_check_strides(map, hull);
  4771. isl_basic_map_free(hull);
  4772. error:
  4773. isl_map_free(map);
  4774. return NULL;
  4775. }
  4776. /* This function performs the same operation as isl_pw_multi_aff_from_map,
  4777. * but is considered as a function on an isl_map when exported.
  4778. */
  4779. __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
  4780. {
  4781. return isl_pw_multi_aff_from_map(map);
  4782. }
  4783. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
  4784. {
  4785. return isl_pw_multi_aff_from_map(set);
  4786. }
  4787. /* This function performs the same operation as isl_pw_multi_aff_from_set,
  4788. * but is considered as a function on an isl_set when exported.
  4789. */
  4790. __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
  4791. {
  4792. return isl_pw_multi_aff_from_set(set);
  4793. }
  4794. /* Convert "map" into an isl_pw_multi_aff (if possible) and
  4795. * add it to *user.
  4796. */
  4797. static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
  4798. {
  4799. isl_union_pw_multi_aff **upma = user;
  4800. isl_pw_multi_aff *pma;
  4801. pma = isl_pw_multi_aff_from_map(map);
  4802. *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
  4803. return *upma ? isl_stat_ok : isl_stat_error;
  4804. }
  4805. /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
  4806. * domain.
  4807. */
  4808. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
  4809. __isl_take isl_aff *aff)
  4810. {
  4811. isl_multi_aff *ma;
  4812. isl_pw_multi_aff *pma;
  4813. ma = isl_multi_aff_from_aff(aff);
  4814. pma = isl_pw_multi_aff_from_multi_aff(ma);
  4815. return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
  4816. }
  4817. /* Try and create an isl_union_pw_multi_aff that is equivalent
  4818. * to the given isl_union_map.
  4819. * The isl_union_map is required to be single-valued in each space.
  4820. * Otherwise, an error is produced.
  4821. */
  4822. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
  4823. __isl_take isl_union_map *umap)
  4824. {
  4825. isl_space *space;
  4826. isl_union_pw_multi_aff *upma;
  4827. space = isl_union_map_get_space(umap);
  4828. upma = isl_union_pw_multi_aff_empty(space);
  4829. if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
  4830. upma = isl_union_pw_multi_aff_free(upma);
  4831. isl_union_map_free(umap);
  4832. return upma;
  4833. }
  4834. /* This function performs the same operation as
  4835. * isl_union_pw_multi_aff_from_union_map,
  4836. * but is considered as a function on an isl_union_map when exported.
  4837. */
  4838. __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
  4839. __isl_take isl_union_map *umap)
  4840. {
  4841. return isl_union_pw_multi_aff_from_union_map(umap);
  4842. }
  4843. /* Try and create an isl_union_pw_multi_aff that is equivalent
  4844. * to the given isl_union_set.
  4845. * The isl_union_set is required to be a singleton in each space.
  4846. * Otherwise, an error is produced.
  4847. */
  4848. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
  4849. __isl_take isl_union_set *uset)
  4850. {
  4851. return isl_union_pw_multi_aff_from_union_map(uset);
  4852. }
  4853. /* Return the piecewise affine expression "set ? 1 : 0".
  4854. */
  4855. __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
  4856. {
  4857. isl_pw_aff *pa;
  4858. isl_space *space = isl_set_get_space(set);
  4859. isl_local_space *ls = isl_local_space_from_space(space);
  4860. isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
  4861. isl_aff *one = isl_aff_zero_on_domain(ls);
  4862. one = isl_aff_add_constant_si(one, 1);
  4863. pa = isl_pw_aff_alloc(isl_set_copy(set), one);
  4864. set = isl_set_complement(set);
  4865. pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
  4866. return pa;
  4867. }
  4868. /* Plug in "subs" for dimension "type", "pos" of "aff".
  4869. *
  4870. * Let i be the dimension to replace and let "subs" be of the form
  4871. *
  4872. * f/d
  4873. *
  4874. * and "aff" of the form
  4875. *
  4876. * (a i + g)/m
  4877. *
  4878. * The result is
  4879. *
  4880. * (a f + d g')/(m d)
  4881. *
  4882. * where g' is the result of plugging in "subs" in each of the integer
  4883. * divisions in g.
  4884. */
  4885. __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
  4886. enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
  4887. {
  4888. isl_ctx *ctx;
  4889. isl_int v;
  4890. isl_size n_div;
  4891. aff = isl_aff_cow(aff);
  4892. if (!aff || !subs)
  4893. return isl_aff_free(aff);
  4894. ctx = isl_aff_get_ctx(aff);
  4895. if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
  4896. isl_die(ctx, isl_error_invalid,
  4897. "spaces don't match", return isl_aff_free(aff));
  4898. n_div = isl_aff_domain_dim(subs, isl_dim_div);
  4899. if (n_div < 0)
  4900. return isl_aff_free(aff);
  4901. if (n_div != 0)
  4902. isl_die(ctx, isl_error_unsupported,
  4903. "cannot handle divs yet", return isl_aff_free(aff));
  4904. aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
  4905. if (!aff->ls)
  4906. return isl_aff_free(aff);
  4907. aff->v = isl_vec_cow(aff->v);
  4908. if (!aff->v)
  4909. return isl_aff_free(aff);
  4910. pos += isl_local_space_offset(aff->ls, type);
  4911. isl_int_init(v);
  4912. isl_seq_substitute(aff->v->el, pos, subs->v->el,
  4913. aff->v->size, subs->v->size, v);
  4914. isl_int_clear(v);
  4915. return aff;
  4916. }
  4917. /* Plug in "subs" for dimension "type", "pos" in each of the affine
  4918. * expressions in "maff".
  4919. */
  4920. __isl_give isl_multi_aff *isl_multi_aff_substitute(
  4921. __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
  4922. __isl_keep isl_aff *subs)
  4923. {
  4924. int i;
  4925. maff = isl_multi_aff_cow(maff);
  4926. if (!maff || !subs)
  4927. return isl_multi_aff_free(maff);
  4928. if (type == isl_dim_in)
  4929. type = isl_dim_set;
  4930. for (i = 0; i < maff->n; ++i) {
  4931. maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
  4932. type, pos, subs);
  4933. if (!maff->u.p[i])
  4934. return isl_multi_aff_free(maff);
  4935. }
  4936. return maff;
  4937. }
  4938. /* Plug in "subs" for input dimension "pos" of "pma".
  4939. *
  4940. * pma is of the form
  4941. *
  4942. * A_i(v) -> M_i(v)
  4943. *
  4944. * while subs is of the form
  4945. *
  4946. * v' = B_j(v) -> S_j
  4947. *
  4948. * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
  4949. * has a contribution in the result, in particular
  4950. *
  4951. * C_ij(S_j) -> M_i(S_j)
  4952. *
  4953. * Note that plugging in S_j in C_ij may also result in an empty set
  4954. * and this contribution should simply be discarded.
  4955. */
  4956. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
  4957. __isl_take isl_pw_multi_aff *pma, unsigned pos,
  4958. __isl_keep isl_pw_aff *subs)
  4959. {
  4960. int i, j, n;
  4961. isl_pw_multi_aff *res;
  4962. if (!pma || !subs)
  4963. return isl_pw_multi_aff_free(pma);
  4964. n = pma->n * subs->n;
  4965. res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
  4966. for (i = 0; i < pma->n; ++i) {
  4967. for (j = 0; j < subs->n; ++j) {
  4968. isl_set *common;
  4969. isl_multi_aff *res_ij;
  4970. int empty;
  4971. common = isl_set_intersect(
  4972. isl_set_copy(pma->p[i].set),
  4973. isl_set_copy(subs->p[j].set));
  4974. common = isl_set_substitute(common,
  4975. pos, subs->p[j].aff);
  4976. empty = isl_set_plain_is_empty(common);
  4977. if (empty < 0 || empty) {
  4978. isl_set_free(common);
  4979. if (empty < 0)
  4980. goto error;
  4981. continue;
  4982. }
  4983. res_ij = isl_multi_aff_substitute(
  4984. isl_multi_aff_copy(pma->p[i].maff),
  4985. isl_dim_in, pos, subs->p[j].aff);
  4986. res = isl_pw_multi_aff_add_piece(res, common, res_ij);
  4987. }
  4988. }
  4989. isl_pw_multi_aff_free(pma);
  4990. return res;
  4991. error:
  4992. isl_pw_multi_aff_free(pma);
  4993. isl_pw_multi_aff_free(res);
  4994. return NULL;
  4995. }
  4996. /* Compute the preimage of a range of dimensions in the affine expression "src"
  4997. * under "ma" and put the result in "dst". The number of dimensions in "src"
  4998. * that precede the range is given by "n_before". The number of dimensions
  4999. * in the range is given by the number of output dimensions of "ma".
  5000. * The number of dimensions that follow the range is given by "n_after".
  5001. * If "has_denom" is set (to one),
  5002. * then "src" and "dst" have an extra initial denominator.
  5003. * "n_div_ma" is the number of existentials in "ma"
  5004. * "n_div_bset" is the number of existentials in "src"
  5005. * The resulting "dst" (which is assumed to have been allocated by
  5006. * the caller) contains coefficients for both sets of existentials,
  5007. * first those in "ma" and then those in "src".
  5008. * f, c1, c2 and g are temporary objects that have been initialized
  5009. * by the caller.
  5010. *
  5011. * Let src represent the expression
  5012. *
  5013. * (a(p) + f_u u + b v + f_w w + c(divs))/d
  5014. *
  5015. * and let ma represent the expressions
  5016. *
  5017. * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
  5018. *
  5019. * We start out with the following expression for dst:
  5020. *
  5021. * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
  5022. *
  5023. * with the multiplication factor f initially equal to 1
  5024. * and f \sum_i b_i v_i kept separately.
  5025. * For each x_i that we substitute, we multiply the numerator
  5026. * (and denominator) of dst by c_1 = m_i and add the numerator
  5027. * of the x_i expression multiplied by c_2 = f b_i,
  5028. * after removing the common factors of c_1 and c_2.
  5029. * The multiplication factor f also needs to be multiplied by c_1
  5030. * for the next x_j, j > i.
  5031. */
  5032. isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
  5033. __isl_keep isl_multi_aff *ma, int n_before, int n_after,
  5034. int n_div_ma, int n_div_bmap,
  5035. isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
  5036. {
  5037. int i;
  5038. isl_size n_param, n_in, n_out;
  5039. int o_dst, o_src;
  5040. n_param = isl_multi_aff_dim(ma, isl_dim_param);
  5041. n_in = isl_multi_aff_dim(ma, isl_dim_in);
  5042. n_out = isl_multi_aff_dim(ma, isl_dim_out);
  5043. if (n_param < 0 || n_in < 0 || n_out < 0)
  5044. return isl_stat_error;
  5045. isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
  5046. o_dst = o_src = has_denom + 1 + n_param + n_before;
  5047. isl_seq_clr(dst + o_dst, n_in);
  5048. o_dst += n_in;
  5049. o_src += n_out;
  5050. isl_seq_cpy(dst + o_dst, src + o_src, n_after);
  5051. o_dst += n_after;
  5052. o_src += n_after;
  5053. isl_seq_clr(dst + o_dst, n_div_ma);
  5054. o_dst += n_div_ma;
  5055. isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
  5056. isl_int_set_si(f, 1);
  5057. for (i = 0; i < n_out; ++i) {
  5058. int offset = has_denom + 1 + n_param + n_before + i;
  5059. if (isl_int_is_zero(src[offset]))
  5060. continue;
  5061. isl_int_set(c1, ma->u.p[i]->v->el[0]);
  5062. isl_int_mul(c2, f, src[offset]);
  5063. isl_int_gcd(g, c1, c2);
  5064. isl_int_divexact(c1, c1, g);
  5065. isl_int_divexact(c2, c2, g);
  5066. isl_int_mul(f, f, c1);
  5067. o_dst = has_denom;
  5068. o_src = 1;
  5069. isl_seq_combine(dst + o_dst, c1, dst + o_dst,
  5070. c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
  5071. o_dst += 1 + n_param;
  5072. o_src += 1 + n_param;
  5073. isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
  5074. o_dst += n_before;
  5075. isl_seq_combine(dst + o_dst, c1, dst + o_dst,
  5076. c2, ma->u.p[i]->v->el + o_src, n_in);
  5077. o_dst += n_in;
  5078. o_src += n_in;
  5079. isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
  5080. o_dst += n_after;
  5081. isl_seq_combine(dst + o_dst, c1, dst + o_dst,
  5082. c2, ma->u.p[i]->v->el + o_src, n_div_ma);
  5083. o_dst += n_div_ma;
  5084. o_src += n_div_ma;
  5085. isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
  5086. if (has_denom)
  5087. isl_int_mul(dst[0], dst[0], c1);
  5088. }
  5089. return isl_stat_ok;
  5090. }
  5091. /* Compute the pullback of "aff" by the function represented by "ma".
  5092. * In other words, plug in "ma" in "aff". The result is an affine expression
  5093. * defined over the domain space of "ma".
  5094. *
  5095. * If "aff" is represented by
  5096. *
  5097. * (a(p) + b x + c(divs))/d
  5098. *
  5099. * and ma is represented by
  5100. *
  5101. * x = D(p) + F(y) + G(divs')
  5102. *
  5103. * then the result is
  5104. *
  5105. * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
  5106. *
  5107. * The divs in the local space of the input are similarly adjusted
  5108. * through a call to isl_local_space_preimage_multi_aff.
  5109. */
  5110. __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
  5111. __isl_take isl_multi_aff *ma)
  5112. {
  5113. isl_aff *res = NULL;
  5114. isl_local_space *ls;
  5115. isl_size n_div_aff, n_div_ma;
  5116. isl_int f, c1, c2, g;
  5117. ma = isl_multi_aff_align_divs(ma);
  5118. if (!aff || !ma)
  5119. goto error;
  5120. n_div_aff = isl_aff_dim(aff, isl_dim_div);
  5121. n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
  5122. if (n_div_aff < 0 || n_div_ma < 0)
  5123. goto error;
  5124. ls = isl_aff_get_domain_local_space(aff);
  5125. ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
  5126. res = isl_aff_alloc(ls);
  5127. if (!res)
  5128. goto error;
  5129. isl_int_init(f);
  5130. isl_int_init(c1);
  5131. isl_int_init(c2);
  5132. isl_int_init(g);
  5133. if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
  5134. n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
  5135. res = isl_aff_free(res);
  5136. isl_int_clear(f);
  5137. isl_int_clear(c1);
  5138. isl_int_clear(c2);
  5139. isl_int_clear(g);
  5140. isl_aff_free(aff);
  5141. isl_multi_aff_free(ma);
  5142. res = isl_aff_normalize(res);
  5143. return res;
  5144. error:
  5145. isl_aff_free(aff);
  5146. isl_multi_aff_free(ma);
  5147. isl_aff_free(res);
  5148. return NULL;
  5149. }
  5150. /* Compute the pullback of "aff1" by the function represented by "aff2".
  5151. * In other words, plug in "aff2" in "aff1". The result is an affine expression
  5152. * defined over the domain space of "aff1".
  5153. *
  5154. * The domain of "aff1" should match the range of "aff2", which means
  5155. * that it should be single-dimensional.
  5156. */
  5157. __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
  5158. __isl_take isl_aff *aff2)
  5159. {
  5160. isl_multi_aff *ma;
  5161. ma = isl_multi_aff_from_aff(aff2);
  5162. return isl_aff_pullback_multi_aff(aff1, ma);
  5163. }
  5164. /* Compute the pullback of "ma1" by the function represented by "ma2".
  5165. * In other words, plug in "ma2" in "ma1".
  5166. */
  5167. __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
  5168. __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
  5169. {
  5170. int i;
  5171. isl_space *space = NULL;
  5172. isl_multi_aff_align_params_bin(&ma1, &ma2);
  5173. ma2 = isl_multi_aff_align_divs(ma2);
  5174. ma1 = isl_multi_aff_cow(ma1);
  5175. if (!ma1 || !ma2)
  5176. goto error;
  5177. space = isl_space_join(isl_multi_aff_get_space(ma2),
  5178. isl_multi_aff_get_space(ma1));
  5179. for (i = 0; i < ma1->n; ++i) {
  5180. ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
  5181. isl_multi_aff_copy(ma2));
  5182. if (!ma1->u.p[i])
  5183. goto error;
  5184. }
  5185. ma1 = isl_multi_aff_reset_space(ma1, space);
  5186. isl_multi_aff_free(ma2);
  5187. return ma1;
  5188. error:
  5189. isl_space_free(space);
  5190. isl_multi_aff_free(ma2);
  5191. isl_multi_aff_free(ma1);
  5192. return NULL;
  5193. }
  5194. /* Extend the local space of "dst" to include the divs
  5195. * in the local space of "src".
  5196. *
  5197. * If "src" does not have any divs or if the local spaces of "dst" and
  5198. * "src" are the same, then no extension is required.
  5199. */
  5200. __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
  5201. __isl_keep isl_aff *src)
  5202. {
  5203. isl_ctx *ctx;
  5204. isl_size src_n_div, dst_n_div;
  5205. int *exp1 = NULL;
  5206. int *exp2 = NULL;
  5207. isl_bool equal;
  5208. isl_mat *div;
  5209. if (!src || !dst)
  5210. return isl_aff_free(dst);
  5211. ctx = isl_aff_get_ctx(src);
  5212. equal = isl_local_space_has_equal_space(src->ls, dst->ls);
  5213. if (equal < 0)
  5214. return isl_aff_free(dst);
  5215. if (!equal)
  5216. isl_die(ctx, isl_error_invalid,
  5217. "spaces don't match", goto error);
  5218. src_n_div = isl_aff_domain_dim(src, isl_dim_div);
  5219. dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
  5220. if (src_n_div == 0)
  5221. return dst;
  5222. equal = isl_local_space_is_equal(src->ls, dst->ls);
  5223. if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
  5224. return isl_aff_free(dst);
  5225. if (equal)
  5226. return dst;
  5227. exp1 = isl_alloc_array(ctx, int, src_n_div);
  5228. exp2 = isl_alloc_array(ctx, int, dst_n_div);
  5229. if (!exp1 || (dst_n_div && !exp2))
  5230. goto error;
  5231. div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
  5232. dst = isl_aff_expand_divs(dst, div, exp2);
  5233. free(exp1);
  5234. free(exp2);
  5235. return dst;
  5236. error:
  5237. free(exp1);
  5238. free(exp2);
  5239. return isl_aff_free(dst);
  5240. }
  5241. /* Adjust the local spaces of the affine expressions in "maff"
  5242. * such that they all have the save divs.
  5243. */
  5244. __isl_give isl_multi_aff *isl_multi_aff_align_divs(
  5245. __isl_take isl_multi_aff *maff)
  5246. {
  5247. int i;
  5248. if (!maff)
  5249. return NULL;
  5250. if (maff->n == 0)
  5251. return maff;
  5252. maff = isl_multi_aff_cow(maff);
  5253. if (!maff)
  5254. return NULL;
  5255. for (i = 1; i < maff->n; ++i)
  5256. maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
  5257. for (i = 1; i < maff->n; ++i) {
  5258. maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
  5259. if (!maff->u.p[i])
  5260. return isl_multi_aff_free(maff);
  5261. }
  5262. return maff;
  5263. }
  5264. __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
  5265. {
  5266. aff = isl_aff_cow(aff);
  5267. if (!aff)
  5268. return NULL;
  5269. aff->ls = isl_local_space_lift(aff->ls);
  5270. if (!aff->ls)
  5271. return isl_aff_free(aff);
  5272. return aff;
  5273. }
  5274. /* Lift "maff" to a space with extra dimensions such that the result
  5275. * has no more existentially quantified variables.
  5276. * If "ls" is not NULL, then *ls is assigned the local space that lies
  5277. * at the basis of the lifting applied to "maff".
  5278. */
  5279. __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
  5280. __isl_give isl_local_space **ls)
  5281. {
  5282. int i;
  5283. isl_space *space;
  5284. isl_size n_div;
  5285. if (ls)
  5286. *ls = NULL;
  5287. if (!maff)
  5288. return NULL;
  5289. if (maff->n == 0) {
  5290. if (ls) {
  5291. isl_space *space = isl_multi_aff_get_domain_space(maff);
  5292. *ls = isl_local_space_from_space(space);
  5293. if (!*ls)
  5294. return isl_multi_aff_free(maff);
  5295. }
  5296. return maff;
  5297. }
  5298. maff = isl_multi_aff_cow(maff);
  5299. maff = isl_multi_aff_align_divs(maff);
  5300. if (!maff)
  5301. return NULL;
  5302. n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
  5303. if (n_div < 0)
  5304. return isl_multi_aff_free(maff);
  5305. space = isl_multi_aff_get_space(maff);
  5306. space = isl_space_lift(isl_space_domain(space), n_div);
  5307. space = isl_space_extend_domain_with_range(space,
  5308. isl_multi_aff_get_space(maff));
  5309. if (!space)
  5310. return isl_multi_aff_free(maff);
  5311. isl_space_free(maff->space);
  5312. maff->space = space;
  5313. if (ls) {
  5314. *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
  5315. if (!*ls)
  5316. return isl_multi_aff_free(maff);
  5317. }
  5318. for (i = 0; i < maff->n; ++i) {
  5319. maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
  5320. if (!maff->u.p[i])
  5321. goto error;
  5322. }
  5323. return maff;
  5324. error:
  5325. if (ls)
  5326. isl_local_space_free(*ls);
  5327. return isl_multi_aff_free(maff);
  5328. }
  5329. #undef TYPE
  5330. #define TYPE isl_pw_multi_aff
  5331. static
  5332. #include "check_type_range_templ.c"
  5333. /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
  5334. */
  5335. __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
  5336. __isl_keep isl_pw_multi_aff *pma, int pos)
  5337. {
  5338. int i;
  5339. isl_size n_out;
  5340. isl_space *space;
  5341. isl_pw_aff *pa;
  5342. if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
  5343. return NULL;
  5344. n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
  5345. if (n_out < 0)
  5346. return NULL;
  5347. space = isl_pw_multi_aff_get_space(pma);
  5348. space = isl_space_drop_dims(space, isl_dim_out,
  5349. pos + 1, n_out - pos - 1);
  5350. space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
  5351. pa = isl_pw_aff_alloc_size(space, pma->n);
  5352. for (i = 0; i < pma->n; ++i) {
  5353. isl_aff *aff;
  5354. aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
  5355. pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
  5356. }
  5357. return pa;
  5358. }
  5359. /* This is an alternative name for the function above.
  5360. */
  5361. __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
  5362. __isl_keep isl_pw_multi_aff *pma, int pos)
  5363. {
  5364. return isl_pw_multi_aff_get_at(pma, pos);
  5365. }
  5366. /* Return an isl_pw_multi_aff with the given "set" as domain and
  5367. * an unnamed zero-dimensional range.
  5368. */
  5369. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
  5370. __isl_take isl_set *set)
  5371. {
  5372. isl_multi_aff *ma;
  5373. isl_space *space;
  5374. space = isl_set_get_space(set);
  5375. space = isl_space_from_domain(space);
  5376. ma = isl_multi_aff_zero(space);
  5377. return isl_pw_multi_aff_alloc(set, ma);
  5378. }
  5379. /* Add an isl_pw_multi_aff with the given "set" as domain and
  5380. * an unnamed zero-dimensional range to *user.
  5381. */
  5382. static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
  5383. void *user)
  5384. {
  5385. isl_union_pw_multi_aff **upma = user;
  5386. isl_pw_multi_aff *pma;
  5387. pma = isl_pw_multi_aff_from_domain(set);
  5388. *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
  5389. return isl_stat_ok;
  5390. }
  5391. /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
  5392. * an unnamed zero-dimensional range.
  5393. */
  5394. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
  5395. __isl_take isl_union_set *uset)
  5396. {
  5397. isl_space *space;
  5398. isl_union_pw_multi_aff *upma;
  5399. if (!uset)
  5400. return NULL;
  5401. space = isl_union_set_get_space(uset);
  5402. upma = isl_union_pw_multi_aff_empty(space);
  5403. if (isl_union_set_foreach_set(uset,
  5404. &add_pw_multi_aff_from_domain, &upma) < 0)
  5405. goto error;
  5406. isl_union_set_free(uset);
  5407. return upma;
  5408. error:
  5409. isl_union_set_free(uset);
  5410. isl_union_pw_multi_aff_free(upma);
  5411. return NULL;
  5412. }
  5413. /* Local data for bin_entry and the callback "fn".
  5414. */
  5415. struct isl_union_pw_multi_aff_bin_data {
  5416. isl_union_pw_multi_aff *upma2;
  5417. isl_union_pw_multi_aff *res;
  5418. isl_pw_multi_aff *pma;
  5419. isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
  5420. };
  5421. /* Given an isl_pw_multi_aff from upma1, store it in data->pma
  5422. * and call data->fn for each isl_pw_multi_aff in data->upma2.
  5423. */
  5424. static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
  5425. {
  5426. struct isl_union_pw_multi_aff_bin_data *data = user;
  5427. isl_stat r;
  5428. data->pma = pma;
  5429. r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
  5430. data->fn, data);
  5431. isl_pw_multi_aff_free(pma);
  5432. return r;
  5433. }
  5434. /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
  5435. * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
  5436. * passed as user field) and the isl_pw_multi_aff from upma2 is available
  5437. * as *entry. The callback should adjust data->res if desired.
  5438. */
  5439. static __isl_give isl_union_pw_multi_aff *bin_op(
  5440. __isl_take isl_union_pw_multi_aff *upma1,
  5441. __isl_take isl_union_pw_multi_aff *upma2,
  5442. isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
  5443. {
  5444. isl_space *space;
  5445. struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
  5446. space = isl_union_pw_multi_aff_get_space(upma2);
  5447. upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
  5448. space = isl_union_pw_multi_aff_get_space(upma1);
  5449. upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
  5450. if (!upma1 || !upma2)
  5451. goto error;
  5452. data.upma2 = upma2;
  5453. data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
  5454. if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
  5455. &bin_entry, &data) < 0)
  5456. goto error;
  5457. isl_union_pw_multi_aff_free(upma1);
  5458. isl_union_pw_multi_aff_free(upma2);
  5459. return data.res;
  5460. error:
  5461. isl_union_pw_multi_aff_free(upma1);
  5462. isl_union_pw_multi_aff_free(upma2);
  5463. isl_union_pw_multi_aff_free(data.res);
  5464. return NULL;
  5465. }
  5466. /* Given two isl_pw_multi_affs A -> B and C -> D,
  5467. * construct an isl_pw_multi_aff (A * C) -> [B -> D].
  5468. */
  5469. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
  5470. __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
  5471. {
  5472. isl_space *space;
  5473. isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
  5474. space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
  5475. isl_pw_multi_aff_get_space(pma2));
  5476. return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
  5477. &isl_multi_aff_range_product);
  5478. }
  5479. /* Given two isl_pw_multi_affs A -> B and C -> D,
  5480. * construct an isl_pw_multi_aff (A * C) -> (B, D).
  5481. */
  5482. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
  5483. __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
  5484. {
  5485. isl_space *space;
  5486. isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
  5487. space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
  5488. isl_pw_multi_aff_get_space(pma2));
  5489. space = isl_space_flatten_range(space);
  5490. return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
  5491. &isl_multi_aff_flat_range_product);
  5492. }
  5493. /* If data->pma and "pma2" have the same domain space, then use "range_product"
  5494. * to compute some form of range product and add the result to data->res.
  5495. */
  5496. static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
  5497. __isl_give isl_pw_multi_aff *(*range_product)(
  5498. __isl_take isl_pw_multi_aff *pma1,
  5499. __isl_take isl_pw_multi_aff *pma2),
  5500. void *user)
  5501. {
  5502. struct isl_union_pw_multi_aff_bin_data *data = user;
  5503. isl_bool match;
  5504. isl_space *space1, *space2;
  5505. space1 = isl_pw_multi_aff_peek_space(data->pma);
  5506. space2 = isl_pw_multi_aff_peek_space(pma2);
  5507. match = isl_space_tuple_is_equal(space1, isl_dim_in,
  5508. space2, isl_dim_in);
  5509. if (match < 0 || !match) {
  5510. isl_pw_multi_aff_free(pma2);
  5511. return match < 0 ? isl_stat_error : isl_stat_ok;
  5512. }
  5513. pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
  5514. data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
  5515. return isl_stat_ok;
  5516. }
  5517. /* If data->pma and "pma2" have the same domain space, then compute
  5518. * their flat range product and add the result to data->res.
  5519. */
  5520. static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
  5521. void *user)
  5522. {
  5523. return gen_range_product_entry(pma2,
  5524. &isl_pw_multi_aff_flat_range_product, user);
  5525. }
  5526. /* Given two isl_union_pw_multi_affs A -> B and C -> D,
  5527. * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
  5528. */
  5529. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
  5530. __isl_take isl_union_pw_multi_aff *upma1,
  5531. __isl_take isl_union_pw_multi_aff *upma2)
  5532. {
  5533. return bin_op(upma1, upma2, &flat_range_product_entry);
  5534. }
  5535. /* If data->pma and "pma2" have the same domain space, then compute
  5536. * their range product and add the result to data->res.
  5537. */
  5538. static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
  5539. void *user)
  5540. {
  5541. return gen_range_product_entry(pma2,
  5542. &isl_pw_multi_aff_range_product, user);
  5543. }
  5544. /* Given two isl_union_pw_multi_affs A -> B and C -> D,
  5545. * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
  5546. */
  5547. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
  5548. __isl_take isl_union_pw_multi_aff *upma1,
  5549. __isl_take isl_union_pw_multi_aff *upma2)
  5550. {
  5551. return bin_op(upma1, upma2, &range_product_entry);
  5552. }
  5553. /* Replace the affine expressions at position "pos" in "pma" by "pa".
  5554. * The parameters are assumed to have been aligned.
  5555. *
  5556. * The implementation essentially performs an isl_pw_*_on_shared_domain,
  5557. * except that it works on two different isl_pw_* types.
  5558. */
  5559. static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
  5560. __isl_take isl_pw_multi_aff *pma, unsigned pos,
  5561. __isl_take isl_pw_aff *pa)
  5562. {
  5563. int i, j, n;
  5564. isl_pw_multi_aff *res = NULL;
  5565. if (!pma || !pa)
  5566. goto error;
  5567. if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
  5568. pa->dim, isl_dim_in))
  5569. isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
  5570. "domains don't match", goto error);
  5571. if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
  5572. goto error;
  5573. n = pma->n * pa->n;
  5574. res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
  5575. for (i = 0; i < pma->n; ++i) {
  5576. for (j = 0; j < pa->n; ++j) {
  5577. isl_set *common;
  5578. isl_multi_aff *res_ij;
  5579. int empty;
  5580. common = isl_set_intersect(isl_set_copy(pma->p[i].set),
  5581. isl_set_copy(pa->p[j].set));
  5582. empty = isl_set_plain_is_empty(common);
  5583. if (empty < 0 || empty) {
  5584. isl_set_free(common);
  5585. if (empty < 0)
  5586. goto error;
  5587. continue;
  5588. }
  5589. res_ij = isl_multi_aff_set_aff(
  5590. isl_multi_aff_copy(pma->p[i].maff), pos,
  5591. isl_aff_copy(pa->p[j].aff));
  5592. res_ij = isl_multi_aff_gist(res_ij,
  5593. isl_set_copy(common));
  5594. res = isl_pw_multi_aff_add_piece(res, common, res_ij);
  5595. }
  5596. }
  5597. isl_pw_multi_aff_free(pma);
  5598. isl_pw_aff_free(pa);
  5599. return res;
  5600. error:
  5601. isl_pw_multi_aff_free(pma);
  5602. isl_pw_aff_free(pa);
  5603. return isl_pw_multi_aff_free(res);
  5604. }
  5605. /* Replace the affine expressions at position "pos" in "pma" by "pa".
  5606. */
  5607. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
  5608. __isl_take isl_pw_multi_aff *pma, unsigned pos,
  5609. __isl_take isl_pw_aff *pa)
  5610. {
  5611. isl_bool equal_params;
  5612. if (!pma || !pa)
  5613. goto error;
  5614. equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
  5615. if (equal_params < 0)
  5616. goto error;
  5617. if (equal_params)
  5618. return pw_multi_aff_set_pw_aff(pma, pos, pa);
  5619. if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
  5620. isl_pw_aff_check_named_params(pa) < 0)
  5621. goto error;
  5622. pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
  5623. pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
  5624. return pw_multi_aff_set_pw_aff(pma, pos, pa);
  5625. error:
  5626. isl_pw_multi_aff_free(pma);
  5627. isl_pw_aff_free(pa);
  5628. return NULL;
  5629. }
  5630. /* Do the parameters of "pa" match those of "space"?
  5631. */
  5632. isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
  5633. __isl_keep isl_space *space)
  5634. {
  5635. isl_space *pa_space;
  5636. isl_bool match;
  5637. if (!pa || !space)
  5638. return isl_bool_error;
  5639. pa_space = isl_pw_aff_get_space(pa);
  5640. match = isl_space_has_equal_params(space, pa_space);
  5641. isl_space_free(pa_space);
  5642. return match;
  5643. }
  5644. /* Check that the domain space of "pa" matches "space".
  5645. */
  5646. isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
  5647. __isl_keep isl_space *space)
  5648. {
  5649. isl_space *pa_space;
  5650. isl_bool match;
  5651. if (!pa || !space)
  5652. return isl_stat_error;
  5653. pa_space = isl_pw_aff_get_space(pa);
  5654. match = isl_space_has_equal_params(space, pa_space);
  5655. if (match < 0)
  5656. goto error;
  5657. if (!match)
  5658. isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
  5659. "parameters don't match", goto error);
  5660. match = isl_space_tuple_is_equal(space, isl_dim_in,
  5661. pa_space, isl_dim_in);
  5662. if (match < 0)
  5663. goto error;
  5664. if (!match)
  5665. isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
  5666. "domains don't match", goto error);
  5667. isl_space_free(pa_space);
  5668. return isl_stat_ok;
  5669. error:
  5670. isl_space_free(pa_space);
  5671. return isl_stat_error;
  5672. }
  5673. #undef BASE
  5674. #define BASE pw_aff
  5675. #undef DOMBASE
  5676. #define DOMBASE set
  5677. #include <isl_multi_explicit_domain.c>
  5678. #include <isl_multi_pw_aff_explicit_domain.c>
  5679. #include <isl_multi_templ.c>
  5680. #include <isl_multi_add_constant_templ.c>
  5681. #include <isl_multi_apply_set.c>
  5682. #include <isl_multi_arith_templ.c>
  5683. #include <isl_multi_bind_templ.c>
  5684. #include <isl_multi_bind_domain_templ.c>
  5685. #include <isl_multi_coalesce.c>
  5686. #include <isl_multi_domain_templ.c>
  5687. #include <isl_multi_dim_id_templ.c>
  5688. #include <isl_multi_dims.c>
  5689. #include <isl_multi_from_base_templ.c>
  5690. #include <isl_multi_gist.c>
  5691. #include <isl_multi_hash.c>
  5692. #include <isl_multi_identity_templ.c>
  5693. #include <isl_multi_align_set.c>
  5694. #include <isl_multi_insert_domain_templ.c>
  5695. #include <isl_multi_intersect.c>
  5696. #include <isl_multi_min_max_templ.c>
  5697. #include <isl_multi_move_dims_templ.c>
  5698. #include <isl_multi_nan_templ.c>
  5699. #include <isl_multi_param_templ.c>
  5700. #include <isl_multi_product_templ.c>
  5701. #include <isl_multi_splice_templ.c>
  5702. #include <isl_multi_tuple_id_templ.c>
  5703. #include <isl_multi_union_add_templ.c>
  5704. #include <isl_multi_zero_templ.c>
  5705. #include <isl_multi_unbind_params_templ.c>
  5706. /* Is every element of "mpa" defined over a single universe domain?
  5707. */
  5708. isl_bool isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff *mpa)
  5709. {
  5710. return isl_multi_pw_aff_every(mpa, &isl_pw_aff_isa_aff);
  5711. }
  5712. /* Given that every element of "mpa" is defined over a single universe domain,
  5713. * return the corresponding base expressions.
  5714. */
  5715. __isl_give isl_multi_aff *isl_multi_pw_aff_as_multi_aff(
  5716. __isl_take isl_multi_pw_aff *mpa)
  5717. {
  5718. int i;
  5719. isl_size n;
  5720. isl_multi_aff *ma;
  5721. n = isl_multi_pw_aff_size(mpa);
  5722. if (n < 0)
  5723. mpa = isl_multi_pw_aff_free(mpa);
  5724. ma = isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa));
  5725. for (i = 0; i < n; ++i) {
  5726. isl_aff *aff;
  5727. aff = isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa, i));
  5728. ma = isl_multi_aff_set_aff(ma, i, aff);
  5729. }
  5730. isl_multi_pw_aff_free(mpa);
  5731. return ma;
  5732. }
  5733. /* If "mpa" has an explicit domain, then intersect the domain of "map"
  5734. * with this explicit domain.
  5735. */
  5736. __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
  5737. __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
  5738. {
  5739. isl_set *dom;
  5740. if (!isl_multi_pw_aff_has_explicit_domain(mpa))
  5741. return map;
  5742. dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
  5743. map = isl_map_intersect_domain(map, dom);
  5744. return map;
  5745. }
  5746. /* Are all elements of "mpa" piecewise constants?
  5747. */
  5748. isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
  5749. {
  5750. return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
  5751. }
  5752. /* Does "mpa" have a non-trivial explicit domain?
  5753. *
  5754. * The explicit domain, if present, is trivial if it represents
  5755. * an (obviously) universe set.
  5756. */
  5757. isl_bool isl_multi_pw_aff_has_non_trivial_domain(
  5758. __isl_keep isl_multi_pw_aff *mpa)
  5759. {
  5760. if (!mpa)
  5761. return isl_bool_error;
  5762. if (!isl_multi_pw_aff_has_explicit_domain(mpa))
  5763. return isl_bool_false;
  5764. return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
  5765. }
  5766. #undef BASE
  5767. #define BASE set
  5768. #include "isl_opt_mpa_templ.c"
  5769. /* Compute the minima of the set dimensions as a function of the
  5770. * parameters, but independently of the other set dimensions.
  5771. */
  5772. __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
  5773. {
  5774. return set_opt_mpa(set, &isl_set_dim_min);
  5775. }
  5776. /* Compute the maxima of the set dimensions as a function of the
  5777. * parameters, but independently of the other set dimensions.
  5778. */
  5779. __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
  5780. {
  5781. return set_opt_mpa(set, &isl_set_dim_max);
  5782. }
  5783. #undef BASE
  5784. #define BASE map
  5785. #include "isl_opt_mpa_templ.c"
  5786. /* Compute the minima of the output dimensions as a function of the
  5787. * parameters and input dimensions, but independently of
  5788. * the other output dimensions.
  5789. */
  5790. __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
  5791. {
  5792. return map_opt_mpa(map, &isl_map_dim_min);
  5793. }
  5794. /* Compute the maxima of the output dimensions as a function of the
  5795. * parameters and input dimensions, but independently of
  5796. * the other output dimensions.
  5797. */
  5798. __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
  5799. {
  5800. return map_opt_mpa(map, &isl_map_dim_max);
  5801. }
  5802. /* Scale the elements of "pma" by the corresponding elements of "mv".
  5803. */
  5804. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
  5805. __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
  5806. {
  5807. int i;
  5808. isl_bool equal_params;
  5809. pma = isl_pw_multi_aff_cow(pma);
  5810. if (!pma || !mv)
  5811. goto error;
  5812. if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
  5813. mv->space, isl_dim_set))
  5814. isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
  5815. "spaces don't match", goto error);
  5816. equal_params = isl_space_has_equal_params(pma->dim, mv->space);
  5817. if (equal_params < 0)
  5818. goto error;
  5819. if (!equal_params) {
  5820. pma = isl_pw_multi_aff_align_params(pma,
  5821. isl_multi_val_get_space(mv));
  5822. mv = isl_multi_val_align_params(mv,
  5823. isl_pw_multi_aff_get_space(pma));
  5824. if (!pma || !mv)
  5825. goto error;
  5826. }
  5827. for (i = 0; i < pma->n; ++i) {
  5828. pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
  5829. isl_multi_val_copy(mv));
  5830. if (!pma->p[i].maff)
  5831. goto error;
  5832. }
  5833. isl_multi_val_free(mv);
  5834. return pma;
  5835. error:
  5836. isl_multi_val_free(mv);
  5837. isl_pw_multi_aff_free(pma);
  5838. return NULL;
  5839. }
  5840. /* This function is called for each entry of an isl_union_pw_multi_aff.
  5841. * If the space of the entry matches that of data->mv,
  5842. * then apply isl_pw_multi_aff_scale_multi_val and return the result.
  5843. * Otherwise, return an empty isl_pw_multi_aff.
  5844. */
  5845. static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
  5846. __isl_take isl_pw_multi_aff *pma, void *user)
  5847. {
  5848. isl_multi_val *mv = user;
  5849. if (!pma)
  5850. return NULL;
  5851. if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
  5852. mv->space, isl_dim_set)) {
  5853. isl_space *space = isl_pw_multi_aff_get_space(pma);
  5854. isl_pw_multi_aff_free(pma);
  5855. return isl_pw_multi_aff_empty(space);
  5856. }
  5857. return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
  5858. }
  5859. /* Scale the elements of "upma" by the corresponding elements of "mv",
  5860. * for those entries that match the space of "mv".
  5861. */
  5862. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
  5863. __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
  5864. {
  5865. struct isl_union_pw_multi_aff_transform_control control = {
  5866. .fn = &union_pw_multi_aff_scale_multi_val_entry,
  5867. .fn_user = mv,
  5868. };
  5869. upma = isl_union_pw_multi_aff_align_params(upma,
  5870. isl_multi_val_get_space(mv));
  5871. mv = isl_multi_val_align_params(mv,
  5872. isl_union_pw_multi_aff_get_space(upma));
  5873. if (!upma || !mv)
  5874. goto error;
  5875. return isl_union_pw_multi_aff_transform(upma, &control);
  5876. isl_multi_val_free(mv);
  5877. return upma;
  5878. error:
  5879. isl_multi_val_free(mv);
  5880. isl_union_pw_multi_aff_free(upma);
  5881. return NULL;
  5882. }
  5883. /* Construct and return a piecewise multi affine expression
  5884. * in the given space with value zero in each of the output dimensions and
  5885. * a universe domain.
  5886. */
  5887. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
  5888. {
  5889. return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
  5890. }
  5891. /* Construct and return a piecewise multi affine expression
  5892. * that is equal to the given piecewise affine expression.
  5893. */
  5894. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
  5895. __isl_take isl_pw_aff *pa)
  5896. {
  5897. int i;
  5898. isl_space *space;
  5899. isl_pw_multi_aff *pma;
  5900. if (!pa)
  5901. return NULL;
  5902. space = isl_pw_aff_get_space(pa);
  5903. pma = isl_pw_multi_aff_alloc_size(space, pa->n);
  5904. for (i = 0; i < pa->n; ++i) {
  5905. isl_set *set;
  5906. isl_multi_aff *ma;
  5907. set = isl_set_copy(pa->p[i].set);
  5908. ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
  5909. pma = isl_pw_multi_aff_add_piece(pma, set, ma);
  5910. }
  5911. isl_pw_aff_free(pa);
  5912. return pma;
  5913. }
  5914. /* Construct and return a piecewise multi affine expression
  5915. * that is equal to the given multi piecewise affine expression
  5916. * on the shared domain of the piecewise affine expressions,
  5917. * in the special case of a 0D multi piecewise affine expression.
  5918. *
  5919. * Create a piecewise multi affine expression with the explicit domain of
  5920. * the 0D multi piecewise affine expression as domain.
  5921. */
  5922. static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
  5923. __isl_take isl_multi_pw_aff *mpa)
  5924. {
  5925. isl_space *space;
  5926. isl_set *dom;
  5927. isl_multi_aff *ma;
  5928. space = isl_multi_pw_aff_get_space(mpa);
  5929. dom = isl_multi_pw_aff_get_explicit_domain(mpa);
  5930. isl_multi_pw_aff_free(mpa);
  5931. ma = isl_multi_aff_zero(space);
  5932. return isl_pw_multi_aff_alloc(dom, ma);
  5933. }
  5934. /* Construct and return a piecewise multi affine expression
  5935. * that is equal to the given multi piecewise affine expression
  5936. * on the shared domain of the piecewise affine expressions.
  5937. */
  5938. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
  5939. __isl_take isl_multi_pw_aff *mpa)
  5940. {
  5941. int i;
  5942. isl_space *space;
  5943. isl_pw_aff *pa;
  5944. isl_pw_multi_aff *pma;
  5945. if (!mpa)
  5946. return NULL;
  5947. if (mpa->n == 0)
  5948. return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
  5949. space = isl_multi_pw_aff_get_space(mpa);
  5950. pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
  5951. pma = isl_pw_multi_aff_from_pw_aff(pa);
  5952. for (i = 1; i < mpa->n; ++i) {
  5953. isl_pw_multi_aff *pma_i;
  5954. pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
  5955. pma_i = isl_pw_multi_aff_from_pw_aff(pa);
  5956. pma = isl_pw_multi_aff_range_product(pma, pma_i);
  5957. }
  5958. pma = isl_pw_multi_aff_reset_space(pma, space);
  5959. isl_multi_pw_aff_free(mpa);
  5960. return pma;
  5961. }
  5962. /* Convenience function that constructs an isl_multi_pw_aff
  5963. * directly from an isl_aff.
  5964. */
  5965. __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
  5966. {
  5967. return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
  5968. }
  5969. /* Construct and return a multi piecewise affine expression
  5970. * that is equal to the given multi affine expression.
  5971. */
  5972. __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
  5973. __isl_take isl_multi_aff *ma)
  5974. {
  5975. int i;
  5976. isl_size n;
  5977. isl_multi_pw_aff *mpa;
  5978. n = isl_multi_aff_dim(ma, isl_dim_out);
  5979. if (n < 0)
  5980. ma = isl_multi_aff_free(ma);
  5981. if (!ma)
  5982. return NULL;
  5983. mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
  5984. for (i = 0; i < n; ++i) {
  5985. isl_pw_aff *pa;
  5986. pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
  5987. mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
  5988. }
  5989. isl_multi_aff_free(ma);
  5990. return mpa;
  5991. }
  5992. /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
  5993. * but is considered as a function on an isl_multi_aff when exported.
  5994. */
  5995. __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
  5996. __isl_take isl_multi_aff *ma)
  5997. {
  5998. return isl_multi_pw_aff_from_multi_aff(ma);
  5999. }
  6000. /* Construct and return a multi piecewise affine expression
  6001. * that is equal to the given piecewise multi affine expression.
  6002. *
  6003. * If the resulting multi piecewise affine expression has
  6004. * an explicit domain, then assign it the domain of the input.
  6005. * In other cases, the domain is stored in the individual elements.
  6006. */
  6007. __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
  6008. __isl_take isl_pw_multi_aff *pma)
  6009. {
  6010. int i;
  6011. isl_size n;
  6012. isl_space *space;
  6013. isl_multi_pw_aff *mpa;
  6014. n = isl_pw_multi_aff_dim(pma, isl_dim_out);
  6015. if (n < 0)
  6016. pma = isl_pw_multi_aff_free(pma);
  6017. space = isl_pw_multi_aff_get_space(pma);
  6018. mpa = isl_multi_pw_aff_alloc(space);
  6019. for (i = 0; i < n; ++i) {
  6020. isl_pw_aff *pa;
  6021. pa = isl_pw_multi_aff_get_pw_aff(pma, i);
  6022. mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
  6023. }
  6024. if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
  6025. isl_set *dom;
  6026. dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
  6027. mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
  6028. }
  6029. isl_pw_multi_aff_free(pma);
  6030. return mpa;
  6031. }
  6032. /* This function performs the same operation as
  6033. * isl_multi_pw_aff_from_pw_multi_aff,
  6034. * but is considered as a function on an isl_pw_multi_aff when exported.
  6035. */
  6036. __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
  6037. __isl_take isl_pw_multi_aff *pma)
  6038. {
  6039. return isl_multi_pw_aff_from_pw_multi_aff(pma);
  6040. }
  6041. /* Do "pa1" and "pa2" represent the same function?
  6042. *
  6043. * We first check if they are obviously equal.
  6044. * If not, we convert them to maps and check if those are equal.
  6045. *
  6046. * If "pa1" or "pa2" contain any NaNs, then they are considered
  6047. * not to be the same. A NaN is not equal to anything, not even
  6048. * to another NaN.
  6049. */
  6050. isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
  6051. __isl_keep isl_pw_aff *pa2)
  6052. {
  6053. isl_bool equal;
  6054. isl_bool has_nan;
  6055. isl_map *map1, *map2;
  6056. if (!pa1 || !pa2)
  6057. return isl_bool_error;
  6058. equal = isl_pw_aff_plain_is_equal(pa1, pa2);
  6059. if (equal < 0 || equal)
  6060. return equal;
  6061. has_nan = either_involves_nan(pa1, pa2);
  6062. if (has_nan < 0)
  6063. return isl_bool_error;
  6064. if (has_nan)
  6065. return isl_bool_false;
  6066. map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
  6067. map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
  6068. equal = isl_map_is_equal(map1, map2);
  6069. isl_map_free(map1);
  6070. isl_map_free(map2);
  6071. return equal;
  6072. }
  6073. /* Do "mpa1" and "mpa2" represent the same function?
  6074. *
  6075. * Note that we cannot convert the entire isl_multi_pw_aff
  6076. * to a map because the domains of the piecewise affine expressions
  6077. * may not be the same.
  6078. */
  6079. isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
  6080. __isl_keep isl_multi_pw_aff *mpa2)
  6081. {
  6082. int i;
  6083. isl_bool equal, equal_params;
  6084. if (!mpa1 || !mpa2)
  6085. return isl_bool_error;
  6086. equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
  6087. if (equal_params < 0)
  6088. return isl_bool_error;
  6089. if (!equal_params) {
  6090. if (!isl_space_has_named_params(mpa1->space))
  6091. return isl_bool_false;
  6092. if (!isl_space_has_named_params(mpa2->space))
  6093. return isl_bool_false;
  6094. mpa1 = isl_multi_pw_aff_copy(mpa1);
  6095. mpa2 = isl_multi_pw_aff_copy(mpa2);
  6096. mpa1 = isl_multi_pw_aff_align_params(mpa1,
  6097. isl_multi_pw_aff_get_space(mpa2));
  6098. mpa2 = isl_multi_pw_aff_align_params(mpa2,
  6099. isl_multi_pw_aff_get_space(mpa1));
  6100. equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
  6101. isl_multi_pw_aff_free(mpa1);
  6102. isl_multi_pw_aff_free(mpa2);
  6103. return equal;
  6104. }
  6105. equal = isl_space_is_equal(mpa1->space, mpa2->space);
  6106. if (equal < 0 || !equal)
  6107. return equal;
  6108. for (i = 0; i < mpa1->n; ++i) {
  6109. equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
  6110. if (equal < 0 || !equal)
  6111. return equal;
  6112. }
  6113. return isl_bool_true;
  6114. }
  6115. /* Do "pma1" and "pma2" represent the same function?
  6116. *
  6117. * First check if they are obviously equal.
  6118. * If not, then convert them to maps and check if those are equal.
  6119. *
  6120. * If "pa1" or "pa2" contain any NaNs, then they are considered
  6121. * not to be the same. A NaN is not equal to anything, not even
  6122. * to another NaN.
  6123. */
  6124. isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
  6125. __isl_keep isl_pw_multi_aff *pma2)
  6126. {
  6127. isl_bool equal;
  6128. isl_bool has_nan;
  6129. isl_map *map1, *map2;
  6130. if (!pma1 || !pma2)
  6131. return isl_bool_error;
  6132. equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
  6133. if (equal < 0 || equal)
  6134. return equal;
  6135. has_nan = isl_pw_multi_aff_involves_nan(pma1);
  6136. if (has_nan >= 0 && !has_nan)
  6137. has_nan = isl_pw_multi_aff_involves_nan(pma2);
  6138. if (has_nan < 0 || has_nan)
  6139. return isl_bool_not(has_nan);
  6140. map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
  6141. map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
  6142. equal = isl_map_is_equal(map1, map2);
  6143. isl_map_free(map1);
  6144. isl_map_free(map2);
  6145. return equal;
  6146. }
  6147. /* Compute the pullback of "mpa" by the function represented by "ma".
  6148. * In other words, plug in "ma" in "mpa".
  6149. *
  6150. * The parameters of "mpa" and "ma" are assumed to have been aligned.
  6151. *
  6152. * If "mpa" has an explicit domain, then it is this domain
  6153. * that needs to undergo a pullback, i.e., a preimage.
  6154. */
  6155. static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
  6156. __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
  6157. {
  6158. int i;
  6159. isl_space *space = NULL;
  6160. mpa = isl_multi_pw_aff_cow(mpa);
  6161. if (!mpa || !ma)
  6162. goto error;
  6163. space = isl_space_join(isl_multi_aff_get_space(ma),
  6164. isl_multi_pw_aff_get_space(mpa));
  6165. if (!space)
  6166. goto error;
  6167. for (i = 0; i < mpa->n; ++i) {
  6168. mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
  6169. isl_multi_aff_copy(ma));
  6170. if (!mpa->u.p[i])
  6171. goto error;
  6172. }
  6173. if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
  6174. mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
  6175. isl_multi_aff_copy(ma));
  6176. if (!mpa->u.dom)
  6177. goto error;
  6178. }
  6179. isl_multi_aff_free(ma);
  6180. isl_space_free(mpa->space);
  6181. mpa->space = space;
  6182. return mpa;
  6183. error:
  6184. isl_space_free(space);
  6185. isl_multi_pw_aff_free(mpa);
  6186. isl_multi_aff_free(ma);
  6187. return NULL;
  6188. }
  6189. /* Compute the pullback of "mpa" by the function represented by "ma".
  6190. * In other words, plug in "ma" in "mpa".
  6191. */
  6192. __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
  6193. __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
  6194. {
  6195. isl_bool equal_params;
  6196. if (!mpa || !ma)
  6197. goto error;
  6198. equal_params = isl_space_has_equal_params(mpa->space, ma->space);
  6199. if (equal_params < 0)
  6200. goto error;
  6201. if (equal_params)
  6202. return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
  6203. mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
  6204. ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
  6205. return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
  6206. error:
  6207. isl_multi_pw_aff_free(mpa);
  6208. isl_multi_aff_free(ma);
  6209. return NULL;
  6210. }
  6211. /* Compute the pullback of "mpa" by the function represented by "pma".
  6212. * In other words, plug in "pma" in "mpa".
  6213. *
  6214. * The parameters of "mpa" and "mpa" are assumed to have been aligned.
  6215. *
  6216. * If "mpa" has an explicit domain, then it is this domain
  6217. * that needs to undergo a pullback, i.e., a preimage.
  6218. */
  6219. static __isl_give isl_multi_pw_aff *
  6220. isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
  6221. __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
  6222. {
  6223. int i;
  6224. isl_space *space = NULL;
  6225. mpa = isl_multi_pw_aff_cow(mpa);
  6226. if (!mpa || !pma)
  6227. goto error;
  6228. space = isl_space_join(isl_pw_multi_aff_get_space(pma),
  6229. isl_multi_pw_aff_get_space(mpa));
  6230. for (i = 0; i < mpa->n; ++i) {
  6231. mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
  6232. mpa->u.p[i], isl_pw_multi_aff_copy(pma));
  6233. if (!mpa->u.p[i])
  6234. goto error;
  6235. }
  6236. if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
  6237. mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
  6238. isl_pw_multi_aff_copy(pma));
  6239. if (!mpa->u.dom)
  6240. goto error;
  6241. }
  6242. isl_pw_multi_aff_free(pma);
  6243. isl_space_free(mpa->space);
  6244. mpa->space = space;
  6245. return mpa;
  6246. error:
  6247. isl_space_free(space);
  6248. isl_multi_pw_aff_free(mpa);
  6249. isl_pw_multi_aff_free(pma);
  6250. return NULL;
  6251. }
  6252. /* Compute the pullback of "mpa" by the function represented by "pma".
  6253. * In other words, plug in "pma" in "mpa".
  6254. */
  6255. __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
  6256. __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
  6257. {
  6258. isl_bool equal_params;
  6259. if (!mpa || !pma)
  6260. goto error;
  6261. equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
  6262. if (equal_params < 0)
  6263. goto error;
  6264. if (equal_params)
  6265. return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
  6266. mpa = isl_multi_pw_aff_align_params(mpa,
  6267. isl_pw_multi_aff_get_space(pma));
  6268. pma = isl_pw_multi_aff_align_params(pma,
  6269. isl_multi_pw_aff_get_space(mpa));
  6270. return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
  6271. error:
  6272. isl_multi_pw_aff_free(mpa);
  6273. isl_pw_multi_aff_free(pma);
  6274. return NULL;
  6275. }
  6276. /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
  6277. * with the domain of "aff". The domain of the result is the same
  6278. * as that of "mpa".
  6279. * "mpa" and "aff" are assumed to have been aligned.
  6280. *
  6281. * We first extract the parametric constant from "aff", defined
  6282. * over the correct domain.
  6283. * Then we add the appropriate combinations of the members of "mpa".
  6284. * Finally, we add the integer divisions through recursive calls.
  6285. */
  6286. static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
  6287. __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
  6288. {
  6289. int i;
  6290. isl_size n_in, n_div, n_mpa_in;
  6291. isl_space *space;
  6292. isl_val *v;
  6293. isl_pw_aff *pa;
  6294. isl_aff *tmp;
  6295. n_in = isl_aff_dim(aff, isl_dim_in);
  6296. n_div = isl_aff_dim(aff, isl_dim_div);
  6297. n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
  6298. if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
  6299. goto error;
  6300. space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
  6301. tmp = isl_aff_copy(aff);
  6302. tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
  6303. tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
  6304. tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
  6305. tmp = isl_aff_reset_domain_space(tmp, space);
  6306. pa = isl_pw_aff_from_aff(tmp);
  6307. for (i = 0; i < n_in; ++i) {
  6308. isl_pw_aff *pa_i;
  6309. if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
  6310. continue;
  6311. v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
  6312. pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
  6313. pa_i = isl_pw_aff_scale_val(pa_i, v);
  6314. pa = isl_pw_aff_add(pa, pa_i);
  6315. }
  6316. for (i = 0; i < n_div; ++i) {
  6317. isl_aff *div;
  6318. isl_pw_aff *pa_i;
  6319. if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
  6320. continue;
  6321. div = isl_aff_get_div(aff, i);
  6322. pa_i = isl_multi_pw_aff_apply_aff_aligned(
  6323. isl_multi_pw_aff_copy(mpa), div);
  6324. pa_i = isl_pw_aff_floor(pa_i);
  6325. v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
  6326. pa_i = isl_pw_aff_scale_val(pa_i, v);
  6327. pa = isl_pw_aff_add(pa, pa_i);
  6328. }
  6329. isl_multi_pw_aff_free(mpa);
  6330. isl_aff_free(aff);
  6331. return pa;
  6332. error:
  6333. isl_multi_pw_aff_free(mpa);
  6334. isl_aff_free(aff);
  6335. return NULL;
  6336. }
  6337. /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
  6338. * with the domain of "aff". The domain of the result is the same
  6339. * as that of "mpa".
  6340. */
  6341. __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
  6342. __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
  6343. {
  6344. isl_bool equal_params;
  6345. if (!aff || !mpa)
  6346. goto error;
  6347. equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
  6348. if (equal_params < 0)
  6349. goto error;
  6350. if (equal_params)
  6351. return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
  6352. aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
  6353. mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
  6354. return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
  6355. error:
  6356. isl_aff_free(aff);
  6357. isl_multi_pw_aff_free(mpa);
  6358. return NULL;
  6359. }
  6360. /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
  6361. * with the domain of "pa". The domain of the result is the same
  6362. * as that of "mpa".
  6363. * "mpa" and "pa" are assumed to have been aligned.
  6364. *
  6365. * We consider each piece in turn. Note that the domains of the
  6366. * pieces are assumed to be disjoint and they remain disjoint
  6367. * after taking the preimage (over the same function).
  6368. */
  6369. static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
  6370. __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
  6371. {
  6372. isl_space *space;
  6373. isl_pw_aff *res;
  6374. int i;
  6375. if (!mpa || !pa)
  6376. goto error;
  6377. space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
  6378. isl_pw_aff_get_space(pa));
  6379. res = isl_pw_aff_empty(space);
  6380. for (i = 0; i < pa->n; ++i) {
  6381. isl_pw_aff *pa_i;
  6382. isl_set *domain;
  6383. pa_i = isl_multi_pw_aff_apply_aff_aligned(
  6384. isl_multi_pw_aff_copy(mpa),
  6385. isl_aff_copy(pa->p[i].aff));
  6386. domain = isl_set_copy(pa->p[i].set);
  6387. domain = isl_set_preimage_multi_pw_aff(domain,
  6388. isl_multi_pw_aff_copy(mpa));
  6389. pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
  6390. res = isl_pw_aff_add_disjoint(res, pa_i);
  6391. }
  6392. isl_pw_aff_free(pa);
  6393. isl_multi_pw_aff_free(mpa);
  6394. return res;
  6395. error:
  6396. isl_pw_aff_free(pa);
  6397. isl_multi_pw_aff_free(mpa);
  6398. return NULL;
  6399. }
  6400. /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
  6401. * with the domain of "pa". The domain of the result is the same
  6402. * as that of "mpa".
  6403. */
  6404. __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
  6405. __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
  6406. {
  6407. isl_bool equal_params;
  6408. if (!pa || !mpa)
  6409. goto error;
  6410. equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
  6411. if (equal_params < 0)
  6412. goto error;
  6413. if (equal_params)
  6414. return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
  6415. pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
  6416. mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
  6417. return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
  6418. error:
  6419. isl_pw_aff_free(pa);
  6420. isl_multi_pw_aff_free(mpa);
  6421. return NULL;
  6422. }
  6423. /* Compute the pullback of "pa" by the function represented by "mpa".
  6424. * In other words, plug in "mpa" in "pa".
  6425. * "pa" and "mpa" are assumed to have been aligned.
  6426. *
  6427. * The pullback is computed by applying "pa" to "mpa".
  6428. */
  6429. static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
  6430. __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
  6431. {
  6432. return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
  6433. }
  6434. /* Compute the pullback of "pa" by the function represented by "mpa".
  6435. * In other words, plug in "mpa" in "pa".
  6436. *
  6437. * The pullback is computed by applying "pa" to "mpa".
  6438. */
  6439. __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
  6440. __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
  6441. {
  6442. return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
  6443. }
  6444. /* Compute the pullback of "mpa1" by the function represented by "mpa2".
  6445. * In other words, plug in "mpa2" in "mpa1".
  6446. *
  6447. * We pullback each member of "mpa1" in turn.
  6448. *
  6449. * If "mpa1" has an explicit domain, then it is this domain
  6450. * that needs to undergo a pullback instead, i.e., a preimage.
  6451. */
  6452. __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
  6453. __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
  6454. {
  6455. int i;
  6456. isl_space *space = NULL;
  6457. isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
  6458. mpa1 = isl_multi_pw_aff_cow(mpa1);
  6459. if (!mpa1 || !mpa2)
  6460. goto error;
  6461. space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
  6462. isl_multi_pw_aff_get_space(mpa1));
  6463. for (i = 0; i < mpa1->n; ++i) {
  6464. mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
  6465. mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
  6466. if (!mpa1->u.p[i])
  6467. goto error;
  6468. }
  6469. if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
  6470. mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
  6471. isl_multi_pw_aff_copy(mpa2));
  6472. if (!mpa1->u.dom)
  6473. goto error;
  6474. }
  6475. mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
  6476. isl_multi_pw_aff_free(mpa2);
  6477. return mpa1;
  6478. error:
  6479. isl_space_free(space);
  6480. isl_multi_pw_aff_free(mpa1);
  6481. isl_multi_pw_aff_free(mpa2);
  6482. return NULL;
  6483. }
  6484. /* Align the parameters of "mpa1" and "mpa2", check that the ranges
  6485. * of "mpa1" and "mpa2" live in the same space, construct map space
  6486. * between the domain spaces of "mpa1" and "mpa2" and call "order"
  6487. * with this map space as extract argument.
  6488. */
  6489. static __isl_give isl_map *isl_multi_pw_aff_order_map(
  6490. __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
  6491. __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
  6492. __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
  6493. {
  6494. int match;
  6495. isl_space *space1, *space2;
  6496. isl_map *res;
  6497. mpa1 = isl_multi_pw_aff_align_params(mpa1,
  6498. isl_multi_pw_aff_get_space(mpa2));
  6499. mpa2 = isl_multi_pw_aff_align_params(mpa2,
  6500. isl_multi_pw_aff_get_space(mpa1));
  6501. if (!mpa1 || !mpa2)
  6502. goto error;
  6503. match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
  6504. mpa2->space, isl_dim_out);
  6505. if (match < 0)
  6506. goto error;
  6507. if (!match)
  6508. isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
  6509. "range spaces don't match", goto error);
  6510. space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
  6511. space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
  6512. space1 = isl_space_map_from_domain_and_range(space1, space2);
  6513. res = order(mpa1, mpa2, space1);
  6514. isl_multi_pw_aff_free(mpa1);
  6515. isl_multi_pw_aff_free(mpa2);
  6516. return res;
  6517. error:
  6518. isl_multi_pw_aff_free(mpa1);
  6519. isl_multi_pw_aff_free(mpa2);
  6520. return NULL;
  6521. }
  6522. /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
  6523. * where the function values are equal. "space" is the space of the result.
  6524. * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
  6525. *
  6526. * "mpa1" and "mpa2" are equal when each of the pairs of elements
  6527. * in the sequences are equal.
  6528. */
  6529. static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
  6530. __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
  6531. __isl_take isl_space *space)
  6532. {
  6533. int i;
  6534. isl_size n;
  6535. isl_map *res;
  6536. n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
  6537. if (n < 0)
  6538. space = isl_space_free(space);
  6539. res = isl_map_universe(space);
  6540. for (i = 0; i < n; ++i) {
  6541. isl_pw_aff *pa1, *pa2;
  6542. isl_map *map;
  6543. pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
  6544. pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
  6545. map = isl_pw_aff_eq_map(pa1, pa2);
  6546. res = isl_map_intersect(res, map);
  6547. }
  6548. return res;
  6549. }
  6550. /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
  6551. * where the function values are equal.
  6552. */
  6553. __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
  6554. __isl_take isl_multi_pw_aff *mpa2)
  6555. {
  6556. return isl_multi_pw_aff_order_map(mpa1, mpa2,
  6557. &isl_multi_pw_aff_eq_map_on_space);
  6558. }
  6559. /* Intersect "map" with the result of applying "order"
  6560. * on two copies of "mpa".
  6561. */
  6562. static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
  6563. __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
  6564. __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
  6565. __isl_take isl_multi_pw_aff *mpa2))
  6566. {
  6567. return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
  6568. }
  6569. /* Return the subset of "map" where the domain and the range
  6570. * have equal "mpa" values.
  6571. */
  6572. __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
  6573. __isl_take isl_multi_pw_aff *mpa)
  6574. {
  6575. return isl_map_order_at_multi_pw_aff(map, mpa,
  6576. &isl_multi_pw_aff_eq_map);
  6577. }
  6578. /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
  6579. * where the function values of "mpa1" lexicographically satisfies
  6580. * "strict_base"/"base" compared to that of "mpa2".
  6581. * "space" is the space of the result.
  6582. * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
  6583. *
  6584. * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
  6585. * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
  6586. * when compared to the i-th element of "mpa2" while all previous elements are
  6587. * pairwise equal.
  6588. * In particular, if i corresponds to the final elements
  6589. * then they need to satisfy "base", while "strict_base" needs to be satisfied
  6590. * for other values of i.
  6591. * If "base" is a strict order, then "base" and "strict_base" are the same.
  6592. */
  6593. static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
  6594. __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
  6595. __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
  6596. __isl_take isl_pw_aff *pa2),
  6597. __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
  6598. __isl_take isl_pw_aff *pa2),
  6599. __isl_take isl_space *space)
  6600. {
  6601. int i;
  6602. isl_size n;
  6603. isl_map *res, *rest;
  6604. n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
  6605. if (n < 0)
  6606. space = isl_space_free(space);
  6607. res = isl_map_empty(isl_space_copy(space));
  6608. rest = isl_map_universe(space);
  6609. for (i = 0; i < n; ++i) {
  6610. int last;
  6611. isl_pw_aff *pa1, *pa2;
  6612. isl_map *map;
  6613. last = i == n - 1;
  6614. pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
  6615. pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
  6616. map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
  6617. map = isl_map_intersect(map, isl_map_copy(rest));
  6618. res = isl_map_union(res, map);
  6619. if (last)
  6620. continue;
  6621. pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
  6622. pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
  6623. map = isl_pw_aff_eq_map(pa1, pa2);
  6624. rest = isl_map_intersect(rest, map);
  6625. }
  6626. isl_map_free(rest);
  6627. return res;
  6628. }
  6629. #undef ORDER
  6630. #define ORDER le
  6631. #undef STRICT_ORDER
  6632. #define STRICT_ORDER lt
  6633. #include "isl_aff_lex_templ.c"
  6634. #undef ORDER
  6635. #define ORDER lt
  6636. #undef STRICT_ORDER
  6637. #define STRICT_ORDER lt
  6638. #include "isl_aff_lex_templ.c"
  6639. #undef ORDER
  6640. #define ORDER ge
  6641. #undef STRICT_ORDER
  6642. #define STRICT_ORDER gt
  6643. #include "isl_aff_lex_templ.c"
  6644. #undef ORDER
  6645. #define ORDER gt
  6646. #undef STRICT_ORDER
  6647. #define STRICT_ORDER gt
  6648. #include "isl_aff_lex_templ.c"
  6649. /* Compare two isl_affs.
  6650. *
  6651. * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
  6652. * than "aff2" and 0 if they are equal.
  6653. *
  6654. * The order is fairly arbitrary. We do consider expressions that only involve
  6655. * earlier dimensions as "smaller".
  6656. */
  6657. int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
  6658. {
  6659. int cmp;
  6660. int last1, last2;
  6661. if (aff1 == aff2)
  6662. return 0;
  6663. if (!aff1)
  6664. return -1;
  6665. if (!aff2)
  6666. return 1;
  6667. cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
  6668. if (cmp != 0)
  6669. return cmp;
  6670. last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
  6671. last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
  6672. if (last1 != last2)
  6673. return last1 - last2;
  6674. return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
  6675. }
  6676. /* Compare two isl_pw_affs.
  6677. *
  6678. * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
  6679. * than "pa2" and 0 if they are equal.
  6680. *
  6681. * The order is fairly arbitrary. We do consider expressions that only involve
  6682. * earlier dimensions as "smaller".
  6683. */
  6684. int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
  6685. __isl_keep isl_pw_aff *pa2)
  6686. {
  6687. int i;
  6688. int cmp;
  6689. if (pa1 == pa2)
  6690. return 0;
  6691. if (!pa1)
  6692. return -1;
  6693. if (!pa2)
  6694. return 1;
  6695. cmp = isl_space_cmp(pa1->dim, pa2->dim);
  6696. if (cmp != 0)
  6697. return cmp;
  6698. if (pa1->n != pa2->n)
  6699. return pa1->n - pa2->n;
  6700. for (i = 0; i < pa1->n; ++i) {
  6701. cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
  6702. if (cmp != 0)
  6703. return cmp;
  6704. cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
  6705. if (cmp != 0)
  6706. return cmp;
  6707. }
  6708. return 0;
  6709. }
  6710. /* Return a piecewise affine expression that is equal to "v" on "domain".
  6711. */
  6712. __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
  6713. __isl_take isl_val *v)
  6714. {
  6715. isl_space *space;
  6716. isl_local_space *ls;
  6717. isl_aff *aff;
  6718. space = isl_set_get_space(domain);
  6719. ls = isl_local_space_from_space(space);
  6720. aff = isl_aff_val_on_domain(ls, v);
  6721. return isl_pw_aff_alloc(domain, aff);
  6722. }
  6723. /* Return a piecewise affine expression that is equal to the parameter
  6724. * with identifier "id" on "domain".
  6725. */
  6726. __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
  6727. __isl_take isl_set *domain, __isl_take isl_id *id)
  6728. {
  6729. isl_space *space;
  6730. isl_aff *aff;
  6731. space = isl_set_get_space(domain);
  6732. space = isl_space_add_param_id(space, isl_id_copy(id));
  6733. domain = isl_set_align_params(domain, isl_space_copy(space));
  6734. aff = isl_aff_param_on_domain_space_id(space, id);
  6735. return isl_pw_aff_alloc(domain, aff);
  6736. }
  6737. /* Return a multi affine expression that is equal to "mv" on domain
  6738. * space "space".
  6739. */
  6740. __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
  6741. __isl_take isl_space *space, __isl_take isl_multi_val *mv)
  6742. {
  6743. int i;
  6744. isl_size n;
  6745. isl_space *space2;
  6746. isl_local_space *ls;
  6747. isl_multi_aff *ma;
  6748. n = isl_multi_val_dim(mv, isl_dim_set);
  6749. if (!space || n < 0)
  6750. goto error;
  6751. space2 = isl_multi_val_get_space(mv);
  6752. space2 = isl_space_align_params(space2, isl_space_copy(space));
  6753. space = isl_space_align_params(space, isl_space_copy(space2));
  6754. space = isl_space_map_from_domain_and_range(space, space2);
  6755. ma = isl_multi_aff_alloc(isl_space_copy(space));
  6756. ls = isl_local_space_from_space(isl_space_domain(space));
  6757. for (i = 0; i < n; ++i) {
  6758. isl_val *v;
  6759. isl_aff *aff;
  6760. v = isl_multi_val_get_val(mv, i);
  6761. aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
  6762. ma = isl_multi_aff_set_aff(ma, i, aff);
  6763. }
  6764. isl_local_space_free(ls);
  6765. isl_multi_val_free(mv);
  6766. return ma;
  6767. error:
  6768. isl_space_free(space);
  6769. isl_multi_val_free(mv);
  6770. return NULL;
  6771. }
  6772. /* This is an alternative name for the function above.
  6773. */
  6774. __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
  6775. __isl_take isl_space *space, __isl_take isl_multi_val *mv)
  6776. {
  6777. return isl_multi_aff_multi_val_on_domain_space(space, mv);
  6778. }
  6779. /* This function performs the same operation as
  6780. * isl_multi_aff_multi_val_on_domain_space,
  6781. * but is considered as a function on an isl_space when exported.
  6782. */
  6783. __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
  6784. __isl_take isl_space *space, __isl_take isl_multi_val *mv)
  6785. {
  6786. return isl_multi_aff_multi_val_on_domain_space(space, mv);
  6787. }
  6788. /* Return a piecewise multi-affine expression
  6789. * that is equal to "mv" on "domain".
  6790. */
  6791. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
  6792. __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
  6793. {
  6794. isl_space *space;
  6795. isl_multi_aff *ma;
  6796. space = isl_set_get_space(domain);
  6797. ma = isl_multi_aff_multi_val_on_space(space, mv);
  6798. return isl_pw_multi_aff_alloc(domain, ma);
  6799. }
  6800. /* This function performs the same operation as
  6801. * isl_pw_multi_aff_multi_val_on_domain,
  6802. * but is considered as a function on an isl_set when exported.
  6803. */
  6804. __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
  6805. __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
  6806. {
  6807. return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
  6808. }
  6809. /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
  6810. * mv is the value that should be attained on each domain set
  6811. * res collects the results
  6812. */
  6813. struct isl_union_pw_multi_aff_multi_val_on_domain_data {
  6814. isl_multi_val *mv;
  6815. isl_union_pw_multi_aff *res;
  6816. };
  6817. /* Create an isl_pw_multi_aff equal to data->mv on "domain"
  6818. * and add it to data->res.
  6819. */
  6820. static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
  6821. void *user)
  6822. {
  6823. struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
  6824. isl_pw_multi_aff *pma;
  6825. isl_multi_val *mv;
  6826. mv = isl_multi_val_copy(data->mv);
  6827. pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
  6828. data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
  6829. return data->res ? isl_stat_ok : isl_stat_error;
  6830. }
  6831. /* Return a union piecewise multi-affine expression
  6832. * that is equal to "mv" on "domain".
  6833. */
  6834. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
  6835. __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
  6836. {
  6837. struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
  6838. isl_space *space;
  6839. space = isl_union_set_get_space(domain);
  6840. data.res = isl_union_pw_multi_aff_empty(space);
  6841. data.mv = mv;
  6842. if (isl_union_set_foreach_set(domain,
  6843. &pw_multi_aff_multi_val_on_domain, &data) < 0)
  6844. data.res = isl_union_pw_multi_aff_free(data.res);
  6845. isl_union_set_free(domain);
  6846. isl_multi_val_free(mv);
  6847. return data.res;
  6848. }
  6849. /* Compute the pullback of data->pma by the function represented by "pma2",
  6850. * provided the spaces match, and add the results to data->res.
  6851. */
  6852. static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
  6853. {
  6854. struct isl_union_pw_multi_aff_bin_data *data = user;
  6855. if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
  6856. pma2->dim, isl_dim_out)) {
  6857. isl_pw_multi_aff_free(pma2);
  6858. return isl_stat_ok;
  6859. }
  6860. pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
  6861. isl_pw_multi_aff_copy(data->pma), pma2);
  6862. data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
  6863. if (!data->res)
  6864. return isl_stat_error;
  6865. return isl_stat_ok;
  6866. }
  6867. /* Compute the pullback of "upma1" by the function represented by "upma2".
  6868. */
  6869. __isl_give isl_union_pw_multi_aff *
  6870. isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
  6871. __isl_take isl_union_pw_multi_aff *upma1,
  6872. __isl_take isl_union_pw_multi_aff *upma2)
  6873. {
  6874. return bin_op(upma1, upma2, &pullback_entry);
  6875. }
  6876. /* Apply "upma2" to "upma1".
  6877. *
  6878. * That is, compute the pullback of "upma2" by "upma1".
  6879. */
  6880. __isl_give isl_union_pw_multi_aff *
  6881. isl_union_pw_multi_aff_apply_union_pw_multi_aff(
  6882. __isl_take isl_union_pw_multi_aff *upma1,
  6883. __isl_take isl_union_pw_multi_aff *upma2)
  6884. {
  6885. return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
  6886. }
  6887. #undef TYPE
  6888. #define TYPE isl_pw_multi_aff
  6889. static
  6890. #include "isl_copy_tuple_id_templ.c"
  6891. /* Given a function "pma1" of the form A[B -> C] -> D and
  6892. * a function "pma2" of the form E -> B,
  6893. * replace the domain of the wrapped relation inside the domain of "pma1"
  6894. * by the preimage with respect to "pma2".
  6895. * In other words, plug in "pma2" in this nested domain.
  6896. * The result is of the form A[E -> C] -> D.
  6897. *
  6898. * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
  6899. * plug that into "pma1".
  6900. */
  6901. __isl_give isl_pw_multi_aff *
  6902. isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
  6903. __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
  6904. {
  6905. isl_space *pma1_space, *pma2_space;
  6906. isl_space *space;
  6907. isl_pw_multi_aff *id;
  6908. pma1_space = isl_pw_multi_aff_peek_space(pma1);
  6909. pma2_space = isl_pw_multi_aff_peek_space(pma2);
  6910. if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
  6911. goto error;
  6912. if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
  6913. isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
  6914. goto error;
  6915. space = isl_space_domain(isl_space_copy(pma1_space));
  6916. space = isl_space_range(isl_space_unwrap(space));
  6917. id = isl_pw_multi_aff_identity_on_domain_space(space);
  6918. pma2 = isl_pw_multi_aff_product(pma2, id);
  6919. pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
  6920. pma1_space, isl_dim_in);
  6921. pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
  6922. pma1_space, isl_dim_in);
  6923. return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
  6924. error:
  6925. isl_pw_multi_aff_free(pma1);
  6926. isl_pw_multi_aff_free(pma2);
  6927. return NULL;
  6928. }
  6929. /* If data->pma and "pma2" are such that
  6930. * data->pma is of the form A[B -> C] -> D and
  6931. * "pma2" is of the form E -> B,
  6932. * then replace the domain of the wrapped relation
  6933. * inside the domain of data->pma by the preimage with respect to "pma2" and
  6934. * add the result to data->res.
  6935. */
  6936. static isl_stat preimage_domain_wrapped_domain_entry(
  6937. __isl_take isl_pw_multi_aff *pma2, void *user)
  6938. {
  6939. struct isl_union_pw_multi_aff_bin_data *data = user;
  6940. isl_space *pma1_space, *pma2_space;
  6941. isl_bool match;
  6942. pma1_space = isl_pw_multi_aff_peek_space(data->pma);
  6943. pma2_space = isl_pw_multi_aff_peek_space(pma2);
  6944. match = isl_space_domain_is_wrapping(pma1_space);
  6945. if (match >= 0 && match)
  6946. match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
  6947. isl_dim_in, pma2_space, isl_dim_out);
  6948. if (match < 0 || !match) {
  6949. isl_pw_multi_aff_free(pma2);
  6950. return match < 0 ? isl_stat_error : isl_stat_ok;
  6951. }
  6952. pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
  6953. isl_pw_multi_aff_copy(data->pma), pma2);
  6954. data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
  6955. return isl_stat_non_null(data->res);
  6956. }
  6957. /* For each pair of functions A[B -> C] -> D in "upma1" and
  6958. * E -> B in "upma2",
  6959. * replace the domain of the wrapped relation inside the domain of the first
  6960. * by the preimage with respect to the second and collect the results.
  6961. * In other words, plug in the second function in this nested domain.
  6962. * The results are of the form A[E -> C] -> D.
  6963. */
  6964. __isl_give isl_union_pw_multi_aff *
  6965. isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
  6966. __isl_take isl_union_pw_multi_aff *upma1,
  6967. __isl_take isl_union_pw_multi_aff *upma2)
  6968. {
  6969. return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
  6970. }
  6971. /* Check that the domain space of "upa" matches "space".
  6972. *
  6973. * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
  6974. * can in principle never fail since the space "space" is that
  6975. * of the isl_multi_union_pw_aff and is a set space such that
  6976. * there is no domain space to match.
  6977. *
  6978. * We check the parameters and double-check that "space" is
  6979. * indeed that of a set.
  6980. */
  6981. static isl_stat isl_union_pw_aff_check_match_domain_space(
  6982. __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
  6983. {
  6984. isl_space *upa_space;
  6985. isl_bool match;
  6986. if (!upa || !space)
  6987. return isl_stat_error;
  6988. match = isl_space_is_set(space);
  6989. if (match < 0)
  6990. return isl_stat_error;
  6991. if (!match)
  6992. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  6993. "expecting set space", return isl_stat_error);
  6994. upa_space = isl_union_pw_aff_get_space(upa);
  6995. match = isl_space_has_equal_params(space, upa_space);
  6996. if (match < 0)
  6997. goto error;
  6998. if (!match)
  6999. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  7000. "parameters don't match", goto error);
  7001. isl_space_free(upa_space);
  7002. return isl_stat_ok;
  7003. error:
  7004. isl_space_free(upa_space);
  7005. return isl_stat_error;
  7006. }
  7007. /* Do the parameters of "upa" match those of "space"?
  7008. */
  7009. static isl_bool isl_union_pw_aff_matching_params(
  7010. __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
  7011. {
  7012. isl_space *upa_space;
  7013. isl_bool match;
  7014. if (!upa || !space)
  7015. return isl_bool_error;
  7016. upa_space = isl_union_pw_aff_get_space(upa);
  7017. match = isl_space_has_equal_params(space, upa_space);
  7018. isl_space_free(upa_space);
  7019. return match;
  7020. }
  7021. /* Internal data structure for isl_union_pw_aff_reset_domain_space.
  7022. * space represents the new parameters.
  7023. * res collects the results.
  7024. */
  7025. struct isl_union_pw_aff_reset_params_data {
  7026. isl_space *space;
  7027. isl_union_pw_aff *res;
  7028. };
  7029. /* Replace the parameters of "pa" by data->space and
  7030. * add the result to data->res.
  7031. */
  7032. static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
  7033. {
  7034. struct isl_union_pw_aff_reset_params_data *data = user;
  7035. isl_space *space;
  7036. space = isl_pw_aff_get_space(pa);
  7037. space = isl_space_replace_params(space, data->space);
  7038. pa = isl_pw_aff_reset_space(pa, space);
  7039. data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
  7040. return data->res ? isl_stat_ok : isl_stat_error;
  7041. }
  7042. /* Replace the domain space of "upa" by "space".
  7043. * Since a union expression does not have a (single) domain space,
  7044. * "space" is necessarily a parameter space.
  7045. *
  7046. * Since the order and the names of the parameters determine
  7047. * the hash value, we need to create a new hash table.
  7048. */
  7049. static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
  7050. __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
  7051. {
  7052. struct isl_union_pw_aff_reset_params_data data = { space };
  7053. isl_bool match;
  7054. match = isl_union_pw_aff_matching_params(upa, space);
  7055. if (match < 0)
  7056. upa = isl_union_pw_aff_free(upa);
  7057. else if (match) {
  7058. isl_space_free(space);
  7059. return upa;
  7060. }
  7061. data.res = isl_union_pw_aff_empty(isl_space_copy(space));
  7062. if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
  7063. data.res = isl_union_pw_aff_free(data.res);
  7064. isl_union_pw_aff_free(upa);
  7065. isl_space_free(space);
  7066. return data.res;
  7067. }
  7068. /* Return the floor of "pa".
  7069. */
  7070. static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
  7071. {
  7072. return isl_pw_aff_floor(pa);
  7073. }
  7074. /* Given f, return floor(f).
  7075. */
  7076. __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
  7077. __isl_take isl_union_pw_aff *upa)
  7078. {
  7079. return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
  7080. }
  7081. /* Compute
  7082. *
  7083. * upa mod m = upa - m * floor(upa/m)
  7084. *
  7085. * with m an integer value.
  7086. */
  7087. __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
  7088. __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
  7089. {
  7090. isl_union_pw_aff *res;
  7091. if (!upa || !m)
  7092. goto error;
  7093. if (!isl_val_is_int(m))
  7094. isl_die(isl_val_get_ctx(m), isl_error_invalid,
  7095. "expecting integer modulo", goto error);
  7096. if (!isl_val_is_pos(m))
  7097. isl_die(isl_val_get_ctx(m), isl_error_invalid,
  7098. "expecting positive modulo", goto error);
  7099. res = isl_union_pw_aff_copy(upa);
  7100. upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
  7101. upa = isl_union_pw_aff_floor(upa);
  7102. upa = isl_union_pw_aff_scale_val(upa, m);
  7103. res = isl_union_pw_aff_sub(res, upa);
  7104. return res;
  7105. error:
  7106. isl_val_free(m);
  7107. isl_union_pw_aff_free(upa);
  7108. return NULL;
  7109. }
  7110. /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
  7111. * pos is the output position that needs to be extracted.
  7112. * res collects the results.
  7113. */
  7114. struct isl_union_pw_multi_aff_get_union_pw_aff_data {
  7115. int pos;
  7116. isl_union_pw_aff *res;
  7117. };
  7118. /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
  7119. * (assuming it has such a dimension) and add it to data->res.
  7120. */
  7121. static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
  7122. {
  7123. struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
  7124. isl_size n_out;
  7125. isl_pw_aff *pa;
  7126. n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
  7127. if (n_out < 0)
  7128. return isl_stat_error;
  7129. if (data->pos >= n_out) {
  7130. isl_pw_multi_aff_free(pma);
  7131. return isl_stat_ok;
  7132. }
  7133. pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
  7134. isl_pw_multi_aff_free(pma);
  7135. data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
  7136. return data->res ? isl_stat_ok : isl_stat_error;
  7137. }
  7138. /* Extract an isl_union_pw_aff corresponding to
  7139. * output dimension "pos" of "upma".
  7140. */
  7141. __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
  7142. __isl_keep isl_union_pw_multi_aff *upma, int pos)
  7143. {
  7144. struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
  7145. isl_space *space;
  7146. if (!upma)
  7147. return NULL;
  7148. if (pos < 0)
  7149. isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
  7150. "cannot extract at negative position", return NULL);
  7151. space = isl_union_pw_multi_aff_get_space(upma);
  7152. data.res = isl_union_pw_aff_empty(space);
  7153. data.pos = pos;
  7154. if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
  7155. &get_union_pw_aff, &data) < 0)
  7156. data.res = isl_union_pw_aff_free(data.res);
  7157. return data.res;
  7158. }
  7159. /* Return a union piecewise affine expression
  7160. * that is equal to "aff" on "domain".
  7161. */
  7162. __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
  7163. __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
  7164. {
  7165. isl_pw_aff *pa;
  7166. pa = isl_pw_aff_from_aff(aff);
  7167. return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
  7168. }
  7169. /* Return a union piecewise affine expression
  7170. * that is equal to the parameter identified by "id" on "domain".
  7171. *
  7172. * Make sure the parameter appears in the space passed to
  7173. * isl_aff_param_on_domain_space_id.
  7174. */
  7175. __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
  7176. __isl_take isl_union_set *domain, __isl_take isl_id *id)
  7177. {
  7178. isl_space *space;
  7179. isl_aff *aff;
  7180. space = isl_union_set_get_space(domain);
  7181. space = isl_space_add_param_id(space, isl_id_copy(id));
  7182. aff = isl_aff_param_on_domain_space_id(space, id);
  7183. return isl_union_pw_aff_aff_on_domain(domain, aff);
  7184. }
  7185. /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
  7186. * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
  7187. * needs to attain.
  7188. * "res" collects the results.
  7189. */
  7190. struct isl_union_pw_aff_pw_aff_on_domain_data {
  7191. isl_pw_aff *pa;
  7192. isl_union_pw_aff *res;
  7193. };
  7194. /* Construct a piecewise affine expression that is equal to data->pa
  7195. * on "domain" and add the result to data->res.
  7196. */
  7197. static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
  7198. {
  7199. struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
  7200. isl_pw_aff *pa;
  7201. isl_size dim;
  7202. pa = isl_pw_aff_copy(data->pa);
  7203. dim = isl_set_dim(domain, isl_dim_set);
  7204. if (dim < 0)
  7205. pa = isl_pw_aff_free(pa);
  7206. pa = isl_pw_aff_from_range(pa);
  7207. pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
  7208. pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
  7209. pa = isl_pw_aff_intersect_domain(pa, domain);
  7210. data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
  7211. return data->res ? isl_stat_ok : isl_stat_error;
  7212. }
  7213. /* Return a union piecewise affine expression
  7214. * that is equal to "pa" on "domain", assuming "domain" and "pa"
  7215. * have been aligned.
  7216. *
  7217. * Construct an isl_pw_aff on each of the sets in "domain" and
  7218. * collect the results.
  7219. */
  7220. static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
  7221. __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
  7222. {
  7223. struct isl_union_pw_aff_pw_aff_on_domain_data data;
  7224. isl_space *space;
  7225. space = isl_union_set_get_space(domain);
  7226. data.res = isl_union_pw_aff_empty(space);
  7227. data.pa = pa;
  7228. if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
  7229. data.res = isl_union_pw_aff_free(data.res);
  7230. isl_union_set_free(domain);
  7231. isl_pw_aff_free(pa);
  7232. return data.res;
  7233. }
  7234. /* Return a union piecewise affine expression
  7235. * that is equal to "pa" on "domain".
  7236. *
  7237. * Check that "pa" is a parametric expression,
  7238. * align the parameters if needed and call
  7239. * isl_union_pw_aff_pw_aff_on_domain_aligned.
  7240. */
  7241. __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
  7242. __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
  7243. {
  7244. isl_bool is_set;
  7245. isl_bool equal_params;
  7246. isl_space *domain_space, *pa_space;
  7247. pa_space = isl_pw_aff_peek_space(pa);
  7248. is_set = isl_space_is_set(pa_space);
  7249. if (is_set < 0)
  7250. goto error;
  7251. if (!is_set)
  7252. isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
  7253. "expecting parametric expression", goto error);
  7254. domain_space = isl_union_set_get_space(domain);
  7255. pa_space = isl_pw_aff_get_space(pa);
  7256. equal_params = isl_space_has_equal_params(domain_space, pa_space);
  7257. if (equal_params >= 0 && !equal_params) {
  7258. isl_space *space;
  7259. space = isl_space_align_params(domain_space, pa_space);
  7260. pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
  7261. domain = isl_union_set_align_params(domain, space);
  7262. } else {
  7263. isl_space_free(domain_space);
  7264. isl_space_free(pa_space);
  7265. }
  7266. if (equal_params < 0)
  7267. goto error;
  7268. return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
  7269. error:
  7270. isl_union_set_free(domain);
  7271. isl_pw_aff_free(pa);
  7272. return NULL;
  7273. }
  7274. /* Internal data structure for isl_union_pw_aff_val_on_domain.
  7275. * "v" is the value that the resulting isl_union_pw_aff needs to attain.
  7276. * "res" collects the results.
  7277. */
  7278. struct isl_union_pw_aff_val_on_domain_data {
  7279. isl_val *v;
  7280. isl_union_pw_aff *res;
  7281. };
  7282. /* Construct a piecewise affine expression that is equal to data->v
  7283. * on "domain" and add the result to data->res.
  7284. */
  7285. static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
  7286. {
  7287. struct isl_union_pw_aff_val_on_domain_data *data = user;
  7288. isl_pw_aff *pa;
  7289. isl_val *v;
  7290. v = isl_val_copy(data->v);
  7291. pa = isl_pw_aff_val_on_domain(domain, v);
  7292. data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
  7293. return data->res ? isl_stat_ok : isl_stat_error;
  7294. }
  7295. /* Return a union piecewise affine expression
  7296. * that is equal to "v" on "domain".
  7297. *
  7298. * Construct an isl_pw_aff on each of the sets in "domain" and
  7299. * collect the results.
  7300. */
  7301. __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
  7302. __isl_take isl_union_set *domain, __isl_take isl_val *v)
  7303. {
  7304. struct isl_union_pw_aff_val_on_domain_data data;
  7305. isl_space *space;
  7306. space = isl_union_set_get_space(domain);
  7307. data.res = isl_union_pw_aff_empty(space);
  7308. data.v = v;
  7309. if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
  7310. data.res = isl_union_pw_aff_free(data.res);
  7311. isl_union_set_free(domain);
  7312. isl_val_free(v);
  7313. return data.res;
  7314. }
  7315. /* Construct a piecewise multi affine expression
  7316. * that is equal to "pa" and add it to upma.
  7317. */
  7318. static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
  7319. void *user)
  7320. {
  7321. isl_union_pw_multi_aff **upma = user;
  7322. isl_pw_multi_aff *pma;
  7323. pma = isl_pw_multi_aff_from_pw_aff(pa);
  7324. *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
  7325. return *upma ? isl_stat_ok : isl_stat_error;
  7326. }
  7327. /* Construct and return a union piecewise multi affine expression
  7328. * that is equal to the given union piecewise affine expression.
  7329. */
  7330. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
  7331. __isl_take isl_union_pw_aff *upa)
  7332. {
  7333. isl_space *space;
  7334. isl_union_pw_multi_aff *upma;
  7335. if (!upa)
  7336. return NULL;
  7337. space = isl_union_pw_aff_get_space(upa);
  7338. upma = isl_union_pw_multi_aff_empty(space);
  7339. if (isl_union_pw_aff_foreach_pw_aff(upa,
  7340. &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
  7341. upma = isl_union_pw_multi_aff_free(upma);
  7342. isl_union_pw_aff_free(upa);
  7343. return upma;
  7344. }
  7345. /* Compute the set of elements in the domain of "pa" where it is zero and
  7346. * add this set to "uset".
  7347. */
  7348. static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
  7349. {
  7350. isl_union_set **uset = (isl_union_set **)user;
  7351. *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
  7352. return *uset ? isl_stat_ok : isl_stat_error;
  7353. }
  7354. /* Return a union set containing those elements in the domain
  7355. * of "upa" where it is zero.
  7356. */
  7357. __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
  7358. __isl_take isl_union_pw_aff *upa)
  7359. {
  7360. isl_union_set *zero;
  7361. zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
  7362. if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
  7363. zero = isl_union_set_free(zero);
  7364. isl_union_pw_aff_free(upa);
  7365. return zero;
  7366. }
  7367. /* Internal data structure for isl_union_pw_aff_bind_id,
  7368. * storing the parameter that needs to be bound and
  7369. * the accumulated results.
  7370. */
  7371. struct isl_bind_id_data {
  7372. isl_id *id;
  7373. isl_union_set *bound;
  7374. };
  7375. /* Bind the piecewise affine function "pa" to the parameter data->id,
  7376. * adding the resulting elements in the domain where the expression
  7377. * is equal to the parameter to data->bound.
  7378. */
  7379. static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
  7380. {
  7381. struct isl_bind_id_data *data = user;
  7382. isl_set *bound;
  7383. bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
  7384. data->bound = isl_union_set_add_set(data->bound, bound);
  7385. return data->bound ? isl_stat_ok : isl_stat_error;
  7386. }
  7387. /* Bind the union piecewise affine function "upa" to the parameter "id",
  7388. * returning the elements in the domain where the expression
  7389. * is equal to the parameter.
  7390. */
  7391. __isl_give isl_union_set *isl_union_pw_aff_bind_id(
  7392. __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
  7393. {
  7394. struct isl_bind_id_data data = { id };
  7395. data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
  7396. if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
  7397. data.bound = isl_union_set_free(data.bound);
  7398. isl_union_pw_aff_free(upa);
  7399. isl_id_free(id);
  7400. return data.bound;
  7401. }
  7402. /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
  7403. * upma is the function that is plugged in.
  7404. * pa is the current part of the function in which upma is plugged in.
  7405. * res collects the results.
  7406. */
  7407. struct isl_union_pw_aff_pullback_upma_data {
  7408. isl_union_pw_multi_aff *upma;
  7409. isl_pw_aff *pa;
  7410. isl_union_pw_aff *res;
  7411. };
  7412. /* Check if "pma" can be plugged into data->pa.
  7413. * If so, perform the pullback and add the result to data->res.
  7414. */
  7415. static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
  7416. {
  7417. struct isl_union_pw_aff_pullback_upma_data *data = user;
  7418. isl_pw_aff *pa;
  7419. if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
  7420. pma->dim, isl_dim_out)) {
  7421. isl_pw_multi_aff_free(pma);
  7422. return isl_stat_ok;
  7423. }
  7424. pa = isl_pw_aff_copy(data->pa);
  7425. pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
  7426. data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
  7427. return data->res ? isl_stat_ok : isl_stat_error;
  7428. }
  7429. /* Check if any of the elements of data->upma can be plugged into pa,
  7430. * add if so add the result to data->res.
  7431. */
  7432. static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
  7433. {
  7434. struct isl_union_pw_aff_pullback_upma_data *data = user;
  7435. isl_stat r;
  7436. data->pa = pa;
  7437. r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
  7438. &pa_pb_pma, data);
  7439. isl_pw_aff_free(pa);
  7440. return r;
  7441. }
  7442. /* Compute the pullback of "upa" by the function represented by "upma".
  7443. * In other words, plug in "upma" in "upa". The result contains
  7444. * expressions defined over the domain space of "upma".
  7445. *
  7446. * Run over all pairs of elements in "upa" and "upma", perform
  7447. * the pullback when appropriate and collect the results.
  7448. * If the hash value were based on the domain space rather than
  7449. * the function space, then we could run through all elements
  7450. * of "upma" and directly pick out the corresponding element of "upa".
  7451. */
  7452. __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
  7453. __isl_take isl_union_pw_aff *upa,
  7454. __isl_take isl_union_pw_multi_aff *upma)
  7455. {
  7456. struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
  7457. isl_space *space;
  7458. space = isl_union_pw_multi_aff_get_space(upma);
  7459. upa = isl_union_pw_aff_align_params(upa, space);
  7460. space = isl_union_pw_aff_get_space(upa);
  7461. upma = isl_union_pw_multi_aff_align_params(upma, space);
  7462. if (!upa || !upma)
  7463. goto error;
  7464. data.upma = upma;
  7465. data.res = isl_union_pw_aff_alloc_same_size(upa);
  7466. if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
  7467. data.res = isl_union_pw_aff_free(data.res);
  7468. isl_union_pw_aff_free(upa);
  7469. isl_union_pw_multi_aff_free(upma);
  7470. return data.res;
  7471. error:
  7472. isl_union_pw_aff_free(upa);
  7473. isl_union_pw_multi_aff_free(upma);
  7474. return NULL;
  7475. }
  7476. #undef BASE
  7477. #define BASE union_pw_aff
  7478. #undef DOMBASE
  7479. #define DOMBASE union_set
  7480. #include <isl_multi_explicit_domain.c>
  7481. #include <isl_multi_union_pw_aff_explicit_domain.c>
  7482. #include <isl_multi_templ.c>
  7483. #include <isl_multi_apply_set.c>
  7484. #include <isl_multi_apply_union_set.c>
  7485. #include <isl_multi_arith_templ.c>
  7486. #include <isl_multi_bind_templ.c>
  7487. #include <isl_multi_coalesce.c>
  7488. #include <isl_multi_dim_id_templ.c>
  7489. #include <isl_multi_floor.c>
  7490. #include <isl_multi_from_base_templ.c>
  7491. #include <isl_multi_gist.c>
  7492. #include <isl_multi_align_set.c>
  7493. #include <isl_multi_align_union_set.c>
  7494. #include <isl_multi_intersect.c>
  7495. #include <isl_multi_nan_templ.c>
  7496. #include <isl_multi_tuple_id_templ.c>
  7497. #include <isl_multi_union_add_templ.c>
  7498. #include <isl_multi_zero_space_templ.c>
  7499. /* Does "mupa" have a non-trivial explicit domain?
  7500. *
  7501. * The explicit domain, if present, is trivial if it represents
  7502. * an (obviously) universe parameter set.
  7503. */
  7504. isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
  7505. __isl_keep isl_multi_union_pw_aff *mupa)
  7506. {
  7507. isl_bool is_params, trivial;
  7508. isl_set *set;
  7509. if (!mupa)
  7510. return isl_bool_error;
  7511. if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
  7512. return isl_bool_false;
  7513. is_params = isl_union_set_is_params(mupa->u.dom);
  7514. if (is_params < 0 || !is_params)
  7515. return isl_bool_not(is_params);
  7516. set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
  7517. trivial = isl_set_plain_is_universe(set);
  7518. isl_set_free(set);
  7519. return isl_bool_not(trivial);
  7520. }
  7521. /* Construct a multiple union piecewise affine expression
  7522. * in the given space with value zero in each of the output dimensions.
  7523. *
  7524. * Since there is no canonical zero value for
  7525. * a union piecewise affine expression, we can only construct
  7526. * a zero-dimensional "zero" value.
  7527. */
  7528. __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
  7529. __isl_take isl_space *space)
  7530. {
  7531. isl_bool params;
  7532. isl_size dim;
  7533. if (!space)
  7534. return NULL;
  7535. params = isl_space_is_params(space);
  7536. if (params < 0)
  7537. goto error;
  7538. if (params)
  7539. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  7540. "expecting proper set space", goto error);
  7541. if (!isl_space_is_set(space))
  7542. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  7543. "expecting set space", goto error);
  7544. dim = isl_space_dim(space, isl_dim_out);
  7545. if (dim < 0)
  7546. goto error;
  7547. if (dim != 0)
  7548. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  7549. "expecting 0D space", goto error);
  7550. return isl_multi_union_pw_aff_alloc(space);
  7551. error:
  7552. isl_space_free(space);
  7553. return NULL;
  7554. }
  7555. /* Construct and return a multi union piecewise affine expression
  7556. * that is equal to the given multi affine expression.
  7557. */
  7558. __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
  7559. __isl_take isl_multi_aff *ma)
  7560. {
  7561. isl_multi_pw_aff *mpa;
  7562. mpa = isl_multi_pw_aff_from_multi_aff(ma);
  7563. return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
  7564. }
  7565. /* This function performs the same operation as
  7566. * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
  7567. * isl_multi_aff when exported.
  7568. */
  7569. __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
  7570. __isl_take isl_multi_aff *ma)
  7571. {
  7572. return isl_multi_union_pw_aff_from_multi_aff(ma);
  7573. }
  7574. /* Construct and return a multi union piecewise affine expression
  7575. * that is equal to the given multi piecewise affine expression.
  7576. */
  7577. __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
  7578. __isl_take isl_multi_pw_aff *mpa)
  7579. {
  7580. int i;
  7581. isl_size n;
  7582. isl_space *space;
  7583. isl_multi_union_pw_aff *mupa;
  7584. n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
  7585. if (n < 0)
  7586. mpa = isl_multi_pw_aff_free(mpa);
  7587. if (!mpa)
  7588. return NULL;
  7589. space = isl_multi_pw_aff_get_space(mpa);
  7590. space = isl_space_range(space);
  7591. mupa = isl_multi_union_pw_aff_alloc(space);
  7592. for (i = 0; i < n; ++i) {
  7593. isl_pw_aff *pa;
  7594. isl_union_pw_aff *upa;
  7595. pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
  7596. upa = isl_union_pw_aff_from_pw_aff(pa);
  7597. mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
  7598. }
  7599. isl_multi_pw_aff_free(mpa);
  7600. return mupa;
  7601. }
  7602. /* Extract the range space of "pma" and assign it to *space.
  7603. * If *space has already been set (through a previous call to this function),
  7604. * then check that the range space is the same.
  7605. */
  7606. static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
  7607. {
  7608. isl_space **space = user;
  7609. isl_space *pma_space;
  7610. isl_bool equal;
  7611. pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
  7612. isl_pw_multi_aff_free(pma);
  7613. if (!pma_space)
  7614. return isl_stat_error;
  7615. if (!*space) {
  7616. *space = pma_space;
  7617. return isl_stat_ok;
  7618. }
  7619. equal = isl_space_is_equal(pma_space, *space);
  7620. isl_space_free(pma_space);
  7621. if (equal < 0)
  7622. return isl_stat_error;
  7623. if (!equal)
  7624. isl_die(isl_space_get_ctx(*space), isl_error_invalid,
  7625. "range spaces not the same", return isl_stat_error);
  7626. return isl_stat_ok;
  7627. }
  7628. /* Construct and return a multi union piecewise affine expression
  7629. * that is equal to the given union piecewise multi affine expression.
  7630. *
  7631. * In order to be able to perform the conversion, the input
  7632. * needs to be non-empty and may only involve a single range space.
  7633. *
  7634. * If the resulting multi union piecewise affine expression has
  7635. * an explicit domain, then assign it the domain of the input.
  7636. * In other cases, the domain is stored in the individual elements.
  7637. */
  7638. __isl_give isl_multi_union_pw_aff *
  7639. isl_multi_union_pw_aff_from_union_pw_multi_aff(
  7640. __isl_take isl_union_pw_multi_aff *upma)
  7641. {
  7642. isl_space *space = NULL;
  7643. isl_multi_union_pw_aff *mupa;
  7644. int i;
  7645. isl_size n;
  7646. n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
  7647. if (n < 0)
  7648. goto error;
  7649. if (n == 0)
  7650. isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
  7651. "cannot extract range space from empty input",
  7652. goto error);
  7653. if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
  7654. &space) < 0)
  7655. goto error;
  7656. if (!space)
  7657. goto error;
  7658. n = isl_space_dim(space, isl_dim_set);
  7659. if (n < 0)
  7660. space = isl_space_free(space);
  7661. mupa = isl_multi_union_pw_aff_alloc(space);
  7662. for (i = 0; i < n; ++i) {
  7663. isl_union_pw_aff *upa;
  7664. upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
  7665. mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
  7666. }
  7667. if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
  7668. isl_union_set *dom;
  7669. isl_union_pw_multi_aff *copy;
  7670. copy = isl_union_pw_multi_aff_copy(upma);
  7671. dom = isl_union_pw_multi_aff_domain(copy);
  7672. mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
  7673. }
  7674. isl_union_pw_multi_aff_free(upma);
  7675. return mupa;
  7676. error:
  7677. isl_space_free(space);
  7678. isl_union_pw_multi_aff_free(upma);
  7679. return NULL;
  7680. }
  7681. /* This function performs the same operation as
  7682. * isl_multi_union_pw_aff_from_union_pw_multi_aff,
  7683. * but is considered as a function on an isl_union_pw_multi_aff when exported.
  7684. */
  7685. __isl_give isl_multi_union_pw_aff *
  7686. isl_union_pw_multi_aff_as_multi_union_pw_aff(
  7687. __isl_take isl_union_pw_multi_aff *upma)
  7688. {
  7689. return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
  7690. }
  7691. /* Try and create an isl_multi_union_pw_aff that is equivalent
  7692. * to the given isl_union_map.
  7693. * The isl_union_map is required to be single-valued in each space.
  7694. * Moreover, it cannot be empty and all range spaces need to be the same.
  7695. * Otherwise, an error is produced.
  7696. */
  7697. __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
  7698. __isl_take isl_union_map *umap)
  7699. {
  7700. isl_union_pw_multi_aff *upma;
  7701. upma = isl_union_pw_multi_aff_from_union_map(umap);
  7702. return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
  7703. }
  7704. /* This function performs the same operation as
  7705. * isl_multi_union_pw_aff_from_union_map,
  7706. * but is considered as a function on an isl_union_map when exported.
  7707. */
  7708. __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
  7709. __isl_take isl_union_map *umap)
  7710. {
  7711. return isl_multi_union_pw_aff_from_union_map(umap);
  7712. }
  7713. /* Return a multiple union piecewise affine expression
  7714. * that is equal to "mv" on "domain", assuming "domain" and "mv"
  7715. * have been aligned.
  7716. *
  7717. * If the resulting multi union piecewise affine expression has
  7718. * an explicit domain, then assign it the input domain.
  7719. * In other cases, the domain is stored in the individual elements.
  7720. */
  7721. static __isl_give isl_multi_union_pw_aff *
  7722. isl_multi_union_pw_aff_multi_val_on_domain_aligned(
  7723. __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
  7724. {
  7725. int i;
  7726. isl_size n;
  7727. isl_space *space;
  7728. isl_multi_union_pw_aff *mupa;
  7729. n = isl_multi_val_dim(mv, isl_dim_set);
  7730. if (!domain || n < 0)
  7731. goto error;
  7732. space = isl_multi_val_get_space(mv);
  7733. mupa = isl_multi_union_pw_aff_alloc(space);
  7734. for (i = 0; i < n; ++i) {
  7735. isl_val *v;
  7736. isl_union_pw_aff *upa;
  7737. v = isl_multi_val_get_val(mv, i);
  7738. upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
  7739. v);
  7740. mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
  7741. }
  7742. if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
  7743. mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
  7744. isl_union_set_copy(domain));
  7745. isl_union_set_free(domain);
  7746. isl_multi_val_free(mv);
  7747. return mupa;
  7748. error:
  7749. isl_union_set_free(domain);
  7750. isl_multi_val_free(mv);
  7751. return NULL;
  7752. }
  7753. /* Return a multiple union piecewise affine expression
  7754. * that is equal to "mv" on "domain".
  7755. */
  7756. __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
  7757. __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
  7758. {
  7759. isl_bool equal_params;
  7760. if (!domain || !mv)
  7761. goto error;
  7762. equal_params = isl_space_has_equal_params(domain->dim, mv->space);
  7763. if (equal_params < 0)
  7764. goto error;
  7765. if (equal_params)
  7766. return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
  7767. domain, mv);
  7768. domain = isl_union_set_align_params(domain,
  7769. isl_multi_val_get_space(mv));
  7770. mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
  7771. return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
  7772. error:
  7773. isl_union_set_free(domain);
  7774. isl_multi_val_free(mv);
  7775. return NULL;
  7776. }
  7777. /* Return a multiple union piecewise affine expression
  7778. * that is equal to "ma" on "domain".
  7779. */
  7780. __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
  7781. __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
  7782. {
  7783. isl_pw_multi_aff *pma;
  7784. pma = isl_pw_multi_aff_from_multi_aff(ma);
  7785. return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
  7786. }
  7787. /* Return a multiple union piecewise affine expression
  7788. * that is equal to "pma" on "domain", assuming "domain" and "pma"
  7789. * have been aligned.
  7790. *
  7791. * If the resulting multi union piecewise affine expression has
  7792. * an explicit domain, then assign it the input domain.
  7793. * In other cases, the domain is stored in the individual elements.
  7794. */
  7795. static __isl_give isl_multi_union_pw_aff *
  7796. isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
  7797. __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
  7798. {
  7799. int i;
  7800. isl_size n;
  7801. isl_space *space;
  7802. isl_multi_union_pw_aff *mupa;
  7803. n = isl_pw_multi_aff_dim(pma, isl_dim_set);
  7804. if (!domain || n < 0)
  7805. goto error;
  7806. space = isl_pw_multi_aff_get_space(pma);
  7807. mupa = isl_multi_union_pw_aff_alloc(space);
  7808. for (i = 0; i < n; ++i) {
  7809. isl_pw_aff *pa;
  7810. isl_union_pw_aff *upa;
  7811. pa = isl_pw_multi_aff_get_pw_aff(pma, i);
  7812. upa = isl_union_pw_aff_pw_aff_on_domain(
  7813. isl_union_set_copy(domain), pa);
  7814. mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
  7815. }
  7816. if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
  7817. mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
  7818. isl_union_set_copy(domain));
  7819. isl_union_set_free(domain);
  7820. isl_pw_multi_aff_free(pma);
  7821. return mupa;
  7822. error:
  7823. isl_union_set_free(domain);
  7824. isl_pw_multi_aff_free(pma);
  7825. return NULL;
  7826. }
  7827. /* Return a multiple union piecewise affine expression
  7828. * that is equal to "pma" on "domain".
  7829. */
  7830. __isl_give isl_multi_union_pw_aff *
  7831. isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
  7832. __isl_take isl_pw_multi_aff *pma)
  7833. {
  7834. isl_bool equal_params;
  7835. isl_space *space;
  7836. space = isl_pw_multi_aff_peek_space(pma);
  7837. equal_params = isl_union_set_space_has_equal_params(domain, space);
  7838. if (equal_params < 0)
  7839. goto error;
  7840. if (equal_params)
  7841. return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
  7842. domain, pma);
  7843. domain = isl_union_set_align_params(domain,
  7844. isl_pw_multi_aff_get_space(pma));
  7845. pma = isl_pw_multi_aff_align_params(pma,
  7846. isl_union_set_get_space(domain));
  7847. return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
  7848. pma);
  7849. error:
  7850. isl_union_set_free(domain);
  7851. isl_pw_multi_aff_free(pma);
  7852. return NULL;
  7853. }
  7854. /* Return a union set containing those elements in the domains
  7855. * of the elements of "mupa" where they are all zero.
  7856. *
  7857. * If there are no elements, then simply return the entire domain.
  7858. */
  7859. __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
  7860. __isl_take isl_multi_union_pw_aff *mupa)
  7861. {
  7862. int i;
  7863. isl_size n;
  7864. isl_union_pw_aff *upa;
  7865. isl_union_set *zero;
  7866. n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
  7867. if (n < 0)
  7868. mupa = isl_multi_union_pw_aff_free(mupa);
  7869. if (!mupa)
  7870. return NULL;
  7871. if (n == 0)
  7872. return isl_multi_union_pw_aff_domain(mupa);
  7873. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
  7874. zero = isl_union_pw_aff_zero_union_set(upa);
  7875. for (i = 1; i < n; ++i) {
  7876. isl_union_set *zero_i;
  7877. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
  7878. zero_i = isl_union_pw_aff_zero_union_set(upa);
  7879. zero = isl_union_set_intersect(zero, zero_i);
  7880. }
  7881. isl_multi_union_pw_aff_free(mupa);
  7882. return zero;
  7883. }
  7884. /* Construct a union map mapping the shared domain
  7885. * of the union piecewise affine expressions to the range of "mupa"
  7886. * in the special case of a 0D multi union piecewise affine expression.
  7887. *
  7888. * Construct a map between the explicit domain of "mupa" and
  7889. * the range space.
  7890. * Note that this assumes that the domain consists of explicit elements.
  7891. */
  7892. static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
  7893. __isl_take isl_multi_union_pw_aff *mupa)
  7894. {
  7895. isl_bool is_params;
  7896. isl_space *space;
  7897. isl_union_set *dom, *ran;
  7898. space = isl_multi_union_pw_aff_get_space(mupa);
  7899. dom = isl_multi_union_pw_aff_domain(mupa);
  7900. ran = isl_union_set_from_set(isl_set_universe(space));
  7901. is_params = isl_union_set_is_params(dom);
  7902. if (is_params < 0)
  7903. dom = isl_union_set_free(dom);
  7904. else if (is_params)
  7905. isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
  7906. "cannot create union map from expression without "
  7907. "explicit domain elements",
  7908. dom = isl_union_set_free(dom));
  7909. return isl_union_map_from_domain_and_range(dom, ran);
  7910. }
  7911. /* Construct a union map mapping the shared domain
  7912. * of the union piecewise affine expressions to the range of "mupa"
  7913. * with each dimension in the range equated to the
  7914. * corresponding union piecewise affine expression.
  7915. *
  7916. * If the input is zero-dimensional, then construct a mapping
  7917. * from its explicit domain.
  7918. */
  7919. __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
  7920. __isl_take isl_multi_union_pw_aff *mupa)
  7921. {
  7922. int i;
  7923. isl_size n;
  7924. isl_space *space;
  7925. isl_union_map *umap;
  7926. isl_union_pw_aff *upa;
  7927. n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
  7928. if (n < 0)
  7929. mupa = isl_multi_union_pw_aff_free(mupa);
  7930. if (!mupa)
  7931. return NULL;
  7932. if (n == 0)
  7933. return isl_union_map_from_multi_union_pw_aff_0D(mupa);
  7934. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
  7935. umap = isl_union_map_from_union_pw_aff(upa);
  7936. for (i = 1; i < n; ++i) {
  7937. isl_union_map *umap_i;
  7938. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
  7939. umap_i = isl_union_map_from_union_pw_aff(upa);
  7940. umap = isl_union_map_flat_range_product(umap, umap_i);
  7941. }
  7942. space = isl_multi_union_pw_aff_get_space(mupa);
  7943. umap = isl_union_map_reset_range_space(umap, space);
  7944. isl_multi_union_pw_aff_free(mupa);
  7945. return umap;
  7946. }
  7947. /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
  7948. * "range" is the space from which to set the range space.
  7949. * "res" collects the results.
  7950. */
  7951. struct isl_union_pw_multi_aff_reset_range_space_data {
  7952. isl_space *range;
  7953. isl_union_pw_multi_aff *res;
  7954. };
  7955. /* Replace the range space of "pma" by the range space of data->range and
  7956. * add the result to data->res.
  7957. */
  7958. static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
  7959. {
  7960. struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
  7961. isl_space *space;
  7962. space = isl_pw_multi_aff_get_space(pma);
  7963. space = isl_space_domain(space);
  7964. space = isl_space_extend_domain_with_range(space,
  7965. isl_space_copy(data->range));
  7966. pma = isl_pw_multi_aff_reset_space(pma, space);
  7967. data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
  7968. return data->res ? isl_stat_ok : isl_stat_error;
  7969. }
  7970. /* Replace the range space of all the piecewise affine expressions in "upma" by
  7971. * the range space of "space".
  7972. *
  7973. * This assumes that all these expressions have the same output dimension.
  7974. *
  7975. * Since the spaces of the expressions change, so do their hash values.
  7976. * We therefore need to create a new isl_union_pw_multi_aff.
  7977. * Note that the hash value is currently computed based on the entire
  7978. * space even though there can only be a single expression with a given
  7979. * domain space.
  7980. */
  7981. static __isl_give isl_union_pw_multi_aff *
  7982. isl_union_pw_multi_aff_reset_range_space(
  7983. __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
  7984. {
  7985. struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
  7986. isl_space *space_upma;
  7987. space_upma = isl_union_pw_multi_aff_get_space(upma);
  7988. data.res = isl_union_pw_multi_aff_empty(space_upma);
  7989. if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
  7990. &reset_range_space, &data) < 0)
  7991. data.res = isl_union_pw_multi_aff_free(data.res);
  7992. isl_space_free(space);
  7993. isl_union_pw_multi_aff_free(upma);
  7994. return data.res;
  7995. }
  7996. /* Construct and return a union piecewise multi affine expression
  7997. * that is equal to the given multi union piecewise affine expression,
  7998. * in the special case of a 0D multi union piecewise affine expression.
  7999. *
  8000. * Construct a union piecewise multi affine expression
  8001. * on top of the explicit domain of the input.
  8002. */
  8003. __isl_give isl_union_pw_multi_aff *
  8004. isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
  8005. __isl_take isl_multi_union_pw_aff *mupa)
  8006. {
  8007. isl_space *space;
  8008. isl_multi_val *mv;
  8009. isl_union_set *domain;
  8010. space = isl_multi_union_pw_aff_get_space(mupa);
  8011. mv = isl_multi_val_zero(space);
  8012. domain = isl_multi_union_pw_aff_domain(mupa);
  8013. return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
  8014. }
  8015. /* Construct and return a union piecewise multi affine expression
  8016. * that is equal to the given multi union piecewise affine expression.
  8017. *
  8018. * If the input is zero-dimensional, then
  8019. * construct a union piecewise multi affine expression
  8020. * on top of the explicit domain of the input.
  8021. */
  8022. __isl_give isl_union_pw_multi_aff *
  8023. isl_union_pw_multi_aff_from_multi_union_pw_aff(
  8024. __isl_take isl_multi_union_pw_aff *mupa)
  8025. {
  8026. int i;
  8027. isl_size n;
  8028. isl_space *space;
  8029. isl_union_pw_multi_aff *upma;
  8030. isl_union_pw_aff *upa;
  8031. n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
  8032. if (n < 0)
  8033. mupa = isl_multi_union_pw_aff_free(mupa);
  8034. if (!mupa)
  8035. return NULL;
  8036. if (n == 0)
  8037. return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
  8038. space = isl_multi_union_pw_aff_get_space(mupa);
  8039. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
  8040. upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
  8041. for (i = 1; i < n; ++i) {
  8042. isl_union_pw_multi_aff *upma_i;
  8043. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
  8044. upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
  8045. upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
  8046. }
  8047. upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
  8048. isl_multi_union_pw_aff_free(mupa);
  8049. return upma;
  8050. }
  8051. /* Intersect the range of "mupa" with "range",
  8052. * in the special case where "mupa" is 0D.
  8053. *
  8054. * Intersect the domain of "mupa" with the constraints on the parameters
  8055. * of "range".
  8056. */
  8057. static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
  8058. __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
  8059. {
  8060. range = isl_set_params(range);
  8061. mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
  8062. return mupa;
  8063. }
  8064. /* Intersect the range of "mupa" with "range".
  8065. * That is, keep only those domain elements that have a function value
  8066. * in "range".
  8067. */
  8068. __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
  8069. __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
  8070. {
  8071. isl_union_pw_multi_aff *upma;
  8072. isl_union_set *domain;
  8073. isl_space *space;
  8074. isl_size n;
  8075. int match;
  8076. n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
  8077. if (n < 0 || !range)
  8078. goto error;
  8079. space = isl_set_get_space(range);
  8080. match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
  8081. space, isl_dim_set);
  8082. isl_space_free(space);
  8083. if (match < 0)
  8084. goto error;
  8085. if (!match)
  8086. isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
  8087. "space don't match", goto error);
  8088. if (n == 0)
  8089. return mupa_intersect_range_0D(mupa, range);
  8090. upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
  8091. isl_multi_union_pw_aff_copy(mupa));
  8092. domain = isl_union_set_from_set(range);
  8093. domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
  8094. mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
  8095. return mupa;
  8096. error:
  8097. isl_multi_union_pw_aff_free(mupa);
  8098. isl_set_free(range);
  8099. return NULL;
  8100. }
  8101. /* Return the shared domain of the elements of "mupa",
  8102. * in the special case where "mupa" is zero-dimensional.
  8103. *
  8104. * Return the explicit domain of "mupa".
  8105. * Note that this domain may be a parameter set, either
  8106. * because "mupa" is meant to live in a set space or
  8107. * because no explicit domain has been set.
  8108. */
  8109. __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
  8110. __isl_take isl_multi_union_pw_aff *mupa)
  8111. {
  8112. isl_union_set *dom;
  8113. dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
  8114. isl_multi_union_pw_aff_free(mupa);
  8115. return dom;
  8116. }
  8117. /* Return the shared domain of the elements of "mupa".
  8118. *
  8119. * If "mupa" is zero-dimensional, then return its explicit domain.
  8120. */
  8121. __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
  8122. __isl_take isl_multi_union_pw_aff *mupa)
  8123. {
  8124. int i;
  8125. isl_size n;
  8126. isl_union_pw_aff *upa;
  8127. isl_union_set *dom;
  8128. n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
  8129. if (n < 0)
  8130. mupa = isl_multi_union_pw_aff_free(mupa);
  8131. if (!mupa)
  8132. return NULL;
  8133. if (n == 0)
  8134. return isl_multi_union_pw_aff_domain_0D(mupa);
  8135. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
  8136. dom = isl_union_pw_aff_domain(upa);
  8137. for (i = 1; i < n; ++i) {
  8138. isl_union_set *dom_i;
  8139. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
  8140. dom_i = isl_union_pw_aff_domain(upa);
  8141. dom = isl_union_set_intersect(dom, dom_i);
  8142. }
  8143. isl_multi_union_pw_aff_free(mupa);
  8144. return dom;
  8145. }
  8146. /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
  8147. * In particular, the spaces have been aligned.
  8148. * The result is defined over the shared domain of the elements of "mupa"
  8149. *
  8150. * We first extract the parametric constant part of "aff" and
  8151. * define that over the shared domain.
  8152. * Then we iterate over all input dimensions of "aff" and add the corresponding
  8153. * multiples of the elements of "mupa".
  8154. * Finally, we consider the integer divisions, calling the function
  8155. * recursively to obtain an isl_union_pw_aff corresponding to the
  8156. * integer division argument.
  8157. */
  8158. static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
  8159. __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
  8160. {
  8161. int i;
  8162. isl_size n_in, n_div;
  8163. isl_union_pw_aff *upa;
  8164. isl_union_set *uset;
  8165. isl_val *v;
  8166. isl_aff *cst;
  8167. n_in = isl_aff_dim(aff, isl_dim_in);
  8168. n_div = isl_aff_dim(aff, isl_dim_div);
  8169. if (n_in < 0 || n_div < 0)
  8170. goto error;
  8171. uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
  8172. cst = isl_aff_copy(aff);
  8173. cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
  8174. cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
  8175. cst = isl_aff_project_domain_on_params(cst);
  8176. upa = isl_union_pw_aff_aff_on_domain(uset, cst);
  8177. for (i = 0; i < n_in; ++i) {
  8178. isl_union_pw_aff *upa_i;
  8179. if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
  8180. continue;
  8181. v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
  8182. upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
  8183. upa_i = isl_union_pw_aff_scale_val(upa_i, v);
  8184. upa = isl_union_pw_aff_add(upa, upa_i);
  8185. }
  8186. for (i = 0; i < n_div; ++i) {
  8187. isl_aff *div;
  8188. isl_union_pw_aff *upa_i;
  8189. if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
  8190. continue;
  8191. div = isl_aff_get_div(aff, i);
  8192. upa_i = multi_union_pw_aff_apply_aff(
  8193. isl_multi_union_pw_aff_copy(mupa), div);
  8194. upa_i = isl_union_pw_aff_floor(upa_i);
  8195. v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
  8196. upa_i = isl_union_pw_aff_scale_val(upa_i, v);
  8197. upa = isl_union_pw_aff_add(upa, upa_i);
  8198. }
  8199. isl_multi_union_pw_aff_free(mupa);
  8200. isl_aff_free(aff);
  8201. return upa;
  8202. error:
  8203. isl_multi_union_pw_aff_free(mupa);
  8204. isl_aff_free(aff);
  8205. return NULL;
  8206. }
  8207. /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
  8208. * with the domain of "aff".
  8209. * Furthermore, the dimension of this space needs to be greater than zero.
  8210. * The result is defined over the shared domain of the elements of "mupa"
  8211. *
  8212. * We perform these checks and then hand over control to
  8213. * multi_union_pw_aff_apply_aff.
  8214. */
  8215. __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
  8216. __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
  8217. {
  8218. isl_size dim;
  8219. isl_space *space1, *space2;
  8220. isl_bool equal;
  8221. mupa = isl_multi_union_pw_aff_align_params(mupa,
  8222. isl_aff_get_space(aff));
  8223. aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
  8224. if (!mupa || !aff)
  8225. goto error;
  8226. space1 = isl_multi_union_pw_aff_get_space(mupa);
  8227. space2 = isl_aff_get_domain_space(aff);
  8228. equal = isl_space_is_equal(space1, space2);
  8229. isl_space_free(space1);
  8230. isl_space_free(space2);
  8231. if (equal < 0)
  8232. goto error;
  8233. if (!equal)
  8234. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  8235. "spaces don't match", goto error);
  8236. dim = isl_aff_dim(aff, isl_dim_in);
  8237. if (dim < 0)
  8238. goto error;
  8239. if (dim == 0)
  8240. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  8241. "cannot determine domains", goto error);
  8242. return multi_union_pw_aff_apply_aff(mupa, aff);
  8243. error:
  8244. isl_multi_union_pw_aff_free(mupa);
  8245. isl_aff_free(aff);
  8246. return NULL;
  8247. }
  8248. /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
  8249. * The space of "mupa" is known to be compatible with the domain of "ma".
  8250. *
  8251. * Construct an isl_multi_union_pw_aff that is equal to "ma"
  8252. * on the domain of "mupa".
  8253. */
  8254. static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
  8255. __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
  8256. {
  8257. isl_union_set *dom;
  8258. dom = isl_multi_union_pw_aff_domain(mupa);
  8259. ma = isl_multi_aff_project_domain_on_params(ma);
  8260. return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
  8261. }
  8262. /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
  8263. * with the domain of "ma".
  8264. * The result is defined over the shared domain of the elements of "mupa"
  8265. */
  8266. __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
  8267. __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
  8268. {
  8269. isl_space *space1, *space2;
  8270. isl_multi_union_pw_aff *res;
  8271. isl_bool equal;
  8272. int i;
  8273. isl_size n_in, n_out;
  8274. mupa = isl_multi_union_pw_aff_align_params(mupa,
  8275. isl_multi_aff_get_space(ma));
  8276. ma = isl_multi_aff_align_params(ma,
  8277. isl_multi_union_pw_aff_get_space(mupa));
  8278. n_in = isl_multi_aff_dim(ma, isl_dim_in);
  8279. n_out = isl_multi_aff_dim(ma, isl_dim_out);
  8280. if (!mupa || n_in < 0 || n_out < 0)
  8281. goto error;
  8282. space1 = isl_multi_union_pw_aff_get_space(mupa);
  8283. space2 = isl_multi_aff_get_domain_space(ma);
  8284. equal = isl_space_is_equal(space1, space2);
  8285. isl_space_free(space1);
  8286. isl_space_free(space2);
  8287. if (equal < 0)
  8288. goto error;
  8289. if (!equal)
  8290. isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
  8291. "spaces don't match", goto error);
  8292. if (n_in == 0)
  8293. return mupa_apply_multi_aff_0D(mupa, ma);
  8294. space1 = isl_space_range(isl_multi_aff_get_space(ma));
  8295. res = isl_multi_union_pw_aff_alloc(space1);
  8296. for (i = 0; i < n_out; ++i) {
  8297. isl_aff *aff;
  8298. isl_union_pw_aff *upa;
  8299. aff = isl_multi_aff_get_aff(ma, i);
  8300. upa = multi_union_pw_aff_apply_aff(
  8301. isl_multi_union_pw_aff_copy(mupa), aff);
  8302. res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
  8303. }
  8304. isl_multi_aff_free(ma);
  8305. isl_multi_union_pw_aff_free(mupa);
  8306. return res;
  8307. error:
  8308. isl_multi_union_pw_aff_free(mupa);
  8309. isl_multi_aff_free(ma);
  8310. return NULL;
  8311. }
  8312. /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
  8313. * The space of "mupa" is known to be compatible with the domain of "pa".
  8314. *
  8315. * Construct an isl_multi_union_pw_aff that is equal to "pa"
  8316. * on the domain of "mupa".
  8317. */
  8318. static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
  8319. __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
  8320. {
  8321. isl_union_set *dom;
  8322. dom = isl_multi_union_pw_aff_domain(mupa);
  8323. pa = isl_pw_aff_project_domain_on_params(pa);
  8324. return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
  8325. }
  8326. /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
  8327. * with the domain of "pa".
  8328. * Furthermore, the dimension of this space needs to be greater than zero.
  8329. * The result is defined over the shared domain of the elements of "mupa"
  8330. */
  8331. __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
  8332. __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
  8333. {
  8334. int i;
  8335. isl_bool equal;
  8336. isl_size n_in;
  8337. isl_space *space, *space2;
  8338. isl_union_pw_aff *upa;
  8339. mupa = isl_multi_union_pw_aff_align_params(mupa,
  8340. isl_pw_aff_get_space(pa));
  8341. pa = isl_pw_aff_align_params(pa,
  8342. isl_multi_union_pw_aff_get_space(mupa));
  8343. if (!mupa || !pa)
  8344. goto error;
  8345. space = isl_multi_union_pw_aff_get_space(mupa);
  8346. space2 = isl_pw_aff_get_domain_space(pa);
  8347. equal = isl_space_is_equal(space, space2);
  8348. isl_space_free(space);
  8349. isl_space_free(space2);
  8350. if (equal < 0)
  8351. goto error;
  8352. if (!equal)
  8353. isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
  8354. "spaces don't match", goto error);
  8355. n_in = isl_pw_aff_dim(pa, isl_dim_in);
  8356. if (n_in < 0)
  8357. goto error;
  8358. if (n_in == 0)
  8359. return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
  8360. space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
  8361. upa = isl_union_pw_aff_empty(space);
  8362. for (i = 0; i < pa->n; ++i) {
  8363. isl_aff *aff;
  8364. isl_set *domain;
  8365. isl_multi_union_pw_aff *mupa_i;
  8366. isl_union_pw_aff *upa_i;
  8367. mupa_i = isl_multi_union_pw_aff_copy(mupa);
  8368. domain = isl_set_copy(pa->p[i].set);
  8369. mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
  8370. aff = isl_aff_copy(pa->p[i].aff);
  8371. upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
  8372. upa = isl_union_pw_aff_union_add(upa, upa_i);
  8373. }
  8374. isl_multi_union_pw_aff_free(mupa);
  8375. isl_pw_aff_free(pa);
  8376. return upa;
  8377. error:
  8378. isl_multi_union_pw_aff_free(mupa);
  8379. isl_pw_aff_free(pa);
  8380. return NULL;
  8381. }
  8382. /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
  8383. * The space of "mupa" is known to be compatible with the domain of "pma".
  8384. *
  8385. * Construct an isl_multi_union_pw_aff that is equal to "pma"
  8386. * on the domain of "mupa".
  8387. */
  8388. static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
  8389. __isl_take isl_multi_union_pw_aff *mupa,
  8390. __isl_take isl_pw_multi_aff *pma)
  8391. {
  8392. isl_union_set *dom;
  8393. dom = isl_multi_union_pw_aff_domain(mupa);
  8394. pma = isl_pw_multi_aff_project_domain_on_params(pma);
  8395. return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
  8396. }
  8397. /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
  8398. * with the domain of "pma".
  8399. * The result is defined over the shared domain of the elements of "mupa"
  8400. */
  8401. __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
  8402. __isl_take isl_multi_union_pw_aff *mupa,
  8403. __isl_take isl_pw_multi_aff *pma)
  8404. {
  8405. isl_space *space1, *space2;
  8406. isl_multi_union_pw_aff *res;
  8407. isl_bool equal;
  8408. int i;
  8409. isl_size n_in, n_out;
  8410. mupa = isl_multi_union_pw_aff_align_params(mupa,
  8411. isl_pw_multi_aff_get_space(pma));
  8412. pma = isl_pw_multi_aff_align_params(pma,
  8413. isl_multi_union_pw_aff_get_space(mupa));
  8414. if (!mupa || !pma)
  8415. goto error;
  8416. space1 = isl_multi_union_pw_aff_get_space(mupa);
  8417. space2 = isl_pw_multi_aff_get_domain_space(pma);
  8418. equal = isl_space_is_equal(space1, space2);
  8419. isl_space_free(space1);
  8420. isl_space_free(space2);
  8421. if (equal < 0)
  8422. goto error;
  8423. if (!equal)
  8424. isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
  8425. "spaces don't match", goto error);
  8426. n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
  8427. n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
  8428. if (n_in < 0 || n_out < 0)
  8429. goto error;
  8430. if (n_in == 0)
  8431. return mupa_apply_pw_multi_aff_0D(mupa, pma);
  8432. space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
  8433. res = isl_multi_union_pw_aff_alloc(space1);
  8434. for (i = 0; i < n_out; ++i) {
  8435. isl_pw_aff *pa;
  8436. isl_union_pw_aff *upa;
  8437. pa = isl_pw_multi_aff_get_pw_aff(pma, i);
  8438. upa = isl_multi_union_pw_aff_apply_pw_aff(
  8439. isl_multi_union_pw_aff_copy(mupa), pa);
  8440. res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
  8441. }
  8442. isl_pw_multi_aff_free(pma);
  8443. isl_multi_union_pw_aff_free(mupa);
  8444. return res;
  8445. error:
  8446. isl_multi_union_pw_aff_free(mupa);
  8447. isl_pw_multi_aff_free(pma);
  8448. return NULL;
  8449. }
  8450. /* Replace the explicit domain of "mupa" by its preimage under "upma".
  8451. * If the explicit domain only keeps track of constraints on the parameters,
  8452. * then only update those constraints.
  8453. */
  8454. static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
  8455. __isl_take isl_multi_union_pw_aff *mupa,
  8456. __isl_keep isl_union_pw_multi_aff *upma)
  8457. {
  8458. isl_bool is_params;
  8459. if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
  8460. return isl_multi_union_pw_aff_free(mupa);
  8461. mupa = isl_multi_union_pw_aff_cow(mupa);
  8462. if (!mupa)
  8463. return NULL;
  8464. is_params = isl_union_set_is_params(mupa->u.dom);
  8465. if (is_params < 0)
  8466. return isl_multi_union_pw_aff_free(mupa);
  8467. upma = isl_union_pw_multi_aff_copy(upma);
  8468. if (is_params)
  8469. mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
  8470. isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
  8471. else
  8472. mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
  8473. mupa->u.dom, upma);
  8474. if (!mupa->u.dom)
  8475. return isl_multi_union_pw_aff_free(mupa);
  8476. return mupa;
  8477. }
  8478. /* Compute the pullback of "mupa" by the function represented by "upma".
  8479. * In other words, plug in "upma" in "mupa". The result contains
  8480. * expressions defined over the domain space of "upma".
  8481. *
  8482. * Run over all elements of "mupa" and plug in "upma" in each of them.
  8483. *
  8484. * If "mupa" has an explicit domain, then it is this domain
  8485. * that needs to undergo a pullback instead, i.e., a preimage.
  8486. */
  8487. __isl_give isl_multi_union_pw_aff *
  8488. isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
  8489. __isl_take isl_multi_union_pw_aff *mupa,
  8490. __isl_take isl_union_pw_multi_aff *upma)
  8491. {
  8492. int i;
  8493. isl_size n;
  8494. mupa = isl_multi_union_pw_aff_align_params(mupa,
  8495. isl_union_pw_multi_aff_get_space(upma));
  8496. upma = isl_union_pw_multi_aff_align_params(upma,
  8497. isl_multi_union_pw_aff_get_space(mupa));
  8498. mupa = isl_multi_union_pw_aff_cow(mupa);
  8499. n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
  8500. if (n < 0 || !upma)
  8501. goto error;
  8502. for (i = 0; i < n; ++i) {
  8503. isl_union_pw_aff *upa;
  8504. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
  8505. upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
  8506. isl_union_pw_multi_aff_copy(upma));
  8507. mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
  8508. }
  8509. if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
  8510. mupa = preimage_explicit_domain(mupa, upma);
  8511. isl_union_pw_multi_aff_free(upma);
  8512. return mupa;
  8513. error:
  8514. isl_multi_union_pw_aff_free(mupa);
  8515. isl_union_pw_multi_aff_free(upma);
  8516. return NULL;
  8517. }
  8518. /* Extract the sequence of elements in "mupa" with domain space "space"
  8519. * (ignoring parameters).
  8520. *
  8521. * For the elements of "mupa" that are not defined on the specified space,
  8522. * the corresponding element in the result is empty.
  8523. */
  8524. __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
  8525. __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
  8526. {
  8527. int i;
  8528. isl_size n;
  8529. isl_space *space_mpa;
  8530. isl_multi_pw_aff *mpa;
  8531. n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
  8532. if (n < 0 || !space)
  8533. goto error;
  8534. space_mpa = isl_multi_union_pw_aff_get_space(mupa);
  8535. space = isl_space_replace_params(space, space_mpa);
  8536. space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
  8537. space_mpa);
  8538. mpa = isl_multi_pw_aff_alloc(space_mpa);
  8539. space = isl_space_from_domain(space);
  8540. space = isl_space_add_dims(space, isl_dim_out, 1);
  8541. for (i = 0; i < n; ++i) {
  8542. isl_union_pw_aff *upa;
  8543. isl_pw_aff *pa;
  8544. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
  8545. pa = isl_union_pw_aff_extract_pw_aff(upa,
  8546. isl_space_copy(space));
  8547. mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
  8548. isl_union_pw_aff_free(upa);
  8549. }
  8550. isl_space_free(space);
  8551. return mpa;
  8552. error:
  8553. isl_space_free(space);
  8554. return NULL;
  8555. }
  8556. /* Data structure that specifies how isl_union_pw_multi_aff_un_op
  8557. * should modify the base expressions in the input.
  8558. *
  8559. * If "filter" is not NULL, then only the base expressions that satisfy "filter"
  8560. * are taken into account.
  8561. * "fn" is applied to each entry in the input.
  8562. */
  8563. struct isl_union_pw_multi_aff_un_op_control {
  8564. isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
  8565. __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
  8566. };
  8567. /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
  8568. * a second argument) for use as an isl_union_pw_multi_aff_transform
  8569. * filter function (which does take a second argument).
  8570. * Simply call control->filter without the second argument.
  8571. */
  8572. static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
  8573. __isl_take isl_pw_multi_aff *pma, void *user)
  8574. {
  8575. struct isl_union_pw_multi_aff_un_op_control *control = user;
  8576. return control->filter(pma);
  8577. }
  8578. /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
  8579. * a second argument) for use as an isl_union_pw_multi_aff_transform
  8580. * base function (which does take a second argument).
  8581. * Simply call control->fn without the second argument.
  8582. */
  8583. static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
  8584. __isl_take isl_pw_multi_aff *pma, void *user)
  8585. {
  8586. struct isl_union_pw_multi_aff_un_op_control *control = user;
  8587. return control->fn(pma);
  8588. }
  8589. /* Construct an isl_union_pw_multi_aff that is obtained by
  8590. * modifying "upma" according to "control".
  8591. *
  8592. * isl_union_pw_multi_aff_transform performs essentially
  8593. * the same operation, but takes a filter and a callback function
  8594. * of a different form (with an extra argument).
  8595. * Call isl_union_pw_multi_aff_transform with wrappers
  8596. * that remove this extra argument.
  8597. */
  8598. static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
  8599. __isl_take isl_union_pw_multi_aff *upma,
  8600. struct isl_union_pw_multi_aff_un_op_control *control)
  8601. {
  8602. struct isl_union_pw_multi_aff_transform_control t_control = {
  8603. .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
  8604. .filter_user = control,
  8605. .fn = &isl_union_pw_multi_aff_un_op_drop_user,
  8606. .fn_user = control,
  8607. };
  8608. return isl_union_pw_multi_aff_transform(upma, &t_control);
  8609. }
  8610. /* For each function in "upma" of the form A -> [B -> C],
  8611. * extract the function A -> B and collect the results.
  8612. */
  8613. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
  8614. __isl_take isl_union_pw_multi_aff *upma)
  8615. {
  8616. struct isl_union_pw_multi_aff_un_op_control control = {
  8617. .filter = &isl_pw_multi_aff_range_is_wrapping,
  8618. .fn = &isl_pw_multi_aff_range_factor_domain,
  8619. };
  8620. return isl_union_pw_multi_aff_un_op(upma, &control);
  8621. }
  8622. /* For each function in "upma" of the form A -> [B -> C],
  8623. * extract the function A -> C and collect the results.
  8624. */
  8625. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
  8626. __isl_take isl_union_pw_multi_aff *upma)
  8627. {
  8628. struct isl_union_pw_multi_aff_un_op_control control = {
  8629. .filter = &isl_pw_multi_aff_range_is_wrapping,
  8630. .fn = &isl_pw_multi_aff_range_factor_range,
  8631. };
  8632. return isl_union_pw_multi_aff_un_op(upma, &control);
  8633. }
  8634. /* Evaluate the affine function "aff" in the void point "pnt".
  8635. * In particular, return the value NaN.
  8636. */
  8637. static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
  8638. __isl_take isl_point *pnt)
  8639. {
  8640. isl_ctx *ctx;
  8641. ctx = isl_point_get_ctx(pnt);
  8642. isl_aff_free(aff);
  8643. isl_point_free(pnt);
  8644. return isl_val_nan(ctx);
  8645. }
  8646. /* Evaluate the affine expression "aff"
  8647. * in the coordinates (with denominator) "pnt".
  8648. */
  8649. static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
  8650. __isl_keep isl_vec *pnt)
  8651. {
  8652. isl_int n, d;
  8653. isl_ctx *ctx;
  8654. isl_val *v;
  8655. if (!aff || !pnt)
  8656. return NULL;
  8657. ctx = isl_vec_get_ctx(aff);
  8658. isl_int_init(n);
  8659. isl_int_init(d);
  8660. isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
  8661. isl_int_mul(d, aff->el[0], pnt->el[0]);
  8662. v = isl_val_rat_from_isl_int(ctx, n, d);
  8663. v = isl_val_normalize(v);
  8664. isl_int_clear(n);
  8665. isl_int_clear(d);
  8666. return v;
  8667. }
  8668. /* Check that the domain space of "aff" is equal to "space".
  8669. */
  8670. static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
  8671. __isl_keep isl_space *space)
  8672. {
  8673. isl_bool ok;
  8674. ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
  8675. if (ok < 0)
  8676. return isl_stat_error;
  8677. if (!ok)
  8678. isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
  8679. "incompatible spaces", return isl_stat_error);
  8680. return isl_stat_ok;
  8681. }
  8682. /* Evaluate the affine function "aff" in "pnt".
  8683. */
  8684. __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
  8685. __isl_take isl_point *pnt)
  8686. {
  8687. isl_bool is_void;
  8688. isl_val *v;
  8689. isl_local_space *ls;
  8690. if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
  8691. goto error;
  8692. is_void = isl_point_is_void(pnt);
  8693. if (is_void < 0)
  8694. goto error;
  8695. if (is_void)
  8696. return eval_void(aff, pnt);
  8697. ls = isl_aff_get_domain_local_space(aff);
  8698. pnt = isl_local_space_lift_point(ls, pnt);
  8699. v = eval(aff->v, isl_point_peek_vec(pnt));
  8700. isl_aff_free(aff);
  8701. isl_point_free(pnt);
  8702. return v;
  8703. error:
  8704. isl_aff_free(aff);
  8705. isl_point_free(pnt);
  8706. return NULL;
  8707. }