ASH and LONGINT

Usage of the framework, compiler and tools
Post Reply
User avatar
Robert
Posts: 177
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

ASH and LONGINT

Post by Robert »

If m is INTEGER and x is REAL the code

Code: Select all

m := 70; x := ASH (1L, m)
crashes, which is not unexpected.
But the code

Code: Select all

m := 70; x := 1.0 * ASH (1L, m)
gives the "correct" answer 1180591620717411303424 (or, apparently as printed 1180591620717411300000).

What is going on? Is the compiler expanding ASH (1L, m) into a floating point expression somehow?

Should we expext x := 1.0 * ASH (1L, m) to work with any compiler?
luowy
Posts: 87
Joined: Thu Dec 17, 2015 1:32 pm

Re: ASH and LONGINT

Post by luowy »

a fixup, DevCPB.CheckAssign

Code: Select all

StdCoder.Decode ..,, ..oN....3QwdONl9RhOO9vRbf9b8R7fJHPNGomCrlAyIhgs,CbKBhZ
 xi2,CoruKu4qouqm8rtuGfa4.hOO9vRb1Y66wb8RTfQ9vQRtIdvPZHWKqtCa.E.U5U,o.6.5Qw
 dONlnayKmKKqCLLCJuGqayKm6F9vQ5nsH3.bnayKmKa2,Cor.kay4.qorGqmQCU2,CJuyKtQC9
 8P9PP7ONbXmb.2.Iu1k5EZD.,6.,.z586.QC18RdfQHfMf9R9vQ7ONb1E.kHU.EBU.U,2.I3tf
 j1.0E65.Q,AU0Ky8.,UgU.UO.,.16.c8.2UwK.s06.T,3cUZj0E.696.c4E.k.Ue.4.072Uh.R
 T.QU0Ky8.,..e,2UgW.Ue.0E.2UE04222wz72eGxd1hc2heGhcUQcchgXRh,RinBhbphe2Yc2j
 u2Y2hgqRcEZeiQeoJipRgoRbUoe,JeUAjixhYhgu2Y2hAEbyKmKqIin4aEIeGEWLEenS0mwmGE
 CKoKqlQiUAgnRidlqKKrGLECqrqKs4KuaKlaKqaKuaLEemIqk2aEf4Id0GnmGESKR0mdWob8Je
 aIbGpR0mwmGE8KR0GWK4sI.PM03OFDOGRO1HM0n96pND,Fry4R7Rn9Qr76B96pND,7SRdNTfQP
 vC,tN,dCv76nf9BvPZXBAV7Ad43YcAjixBQggBgnRiUgbUodoBjkhgd2YDJeU2Zt,.Es8rrCqI
 0GH0GIOKECGE0Jt6Jn9QH76d8G9eH,NNZfQFNAZdBH769eH786rN1HM05OEbOF,dN,tHBO1HM0
 t96feP7HnmGECJu8rouqnWHK0mdG5AaqYZUIctZiZJbBAV7EVyqrmKK0mdKKuen4ak2akYsN,t
 6,dN,7JFOF.4nNaGEKIbgV7AVwNGR9Rl79,NGRnMOHK0mYsAZ79,NGoaoYZUIeZBggRamY3ZON
 oaoIb72Ze2YHJeUIZdg,a.Wmn0mYuIEaKrGrdKKuaGEyIdsNEtKqkmqdKKuc7,,HeH,,.d0PM0
 AV7lwu4.UXxhiRio3YI3d3765uPRfR9fQd98nfP69,7SHN1HM0akWmodaIX0GzGomOrVc9HeP5
 9Pf9N9vQFdNN76aGEGJYkm.gV7A,HMFUc.a4Ua2Za3Y7Ft.6Jk4ak2ak2Qcjphq,.sCPM0ak2K
 IbGo4UANF,NNZfQ.EbIklb8QcjphoJijZhghgmRiiQeotEdfQN9F9vQ59.XDJ..oZ1xhiZCU2h
 gnRg.sEYiVdP.IC...Qii..70,cwE.E8E.k22.0..4E,5TeK4ZORNPNZvQRtIIepZBG20ksH3.
 bf9ZORNPNG20EtD.0E.ses,sc6.,k,,UnpZHldGrwmqmGomCb.AS.c9Ajg,0EtXU.6..W.e32.
 86.c918R.Y6CK.Y.2..EGE.4E.E.EECOh6.0Eyuv.2bXl.k.E.0.3gwJ.0..I16.M.EJ.,U.2m
 ,.,..g30...
 --- end of encoding ---
User avatar
Robert
Posts: 177
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: ASH and LONGINT

Post by Robert »

luowy

Can you explain - What is the precise problem that your fixup solves?

My problem is I don't understand how, or why, the code "m := 70; x := 1.0 * ASH (1L, m)" gives the mathematical answer 2^70 which is more than the expression "ASH (1L,m)" can ever give.
luowy
Posts: 87
Joined: Thu Dec 17, 2015 1:32 pm

Re: ASH and LONGINT

Post by luowy »

the result of ASH(1L, 70) is 1.0*2^70, which is larger than MAX(LONGINT),
an overflow exception occurred when converting a floating point number to an integer;

please note:
1,the BB compiler use FPU to compute ASH(longint,shift), the result is float in ST(0) ;
it had to convert to integer when the final result need to be longint: FISTP [sp]
when the ST(0) is larger than max integer, the overflow exception occured!

2, not same as c language,the "shift>64" is valid for the ASH(l, shift),the result can be larger than MAX(LONGINT),

about your example:
1), x:=ASH(1L,70); // exception raised
=> real := longint
for the result type of ash() is LONGINT, the ST(0) has to convert to longint(FISTP), overflow exception occured;

after fixup, promote the type of ash() to float;avoid FISTP;
=> real := real(longint)

2), x := 1.0 * ASH(1L, 70); // no exception;
=> real := real * real(longint)
the ash() type promote to float, no FISTP needed;

you can compare the dasm code:

Code: Select all

StdCoder.Decode ..,, ..26,...3QwdONl9RhOO9vRbf9b8R7fJHPNGomCrlAyIhgs,CbKBhZ
 xi2,CoruKu4qouqm8rtuGfa4.hOO9vRb1Y66wb8RTfQ9vQRtIdvPZHWKqtCa.E.U5UFC06.5Qw
 dONlnayKmKKqCLLCJuGqayKm6F9vQ5nsH3.bnayKmKa2,Cor.kay4.qorGqmQCU2,CJuyKtQC9
 8P9PP7ONbXmb.2.QvDk5kdzU.2U.2.ot1U00.bnUGLu8ro8quGrmCLWKqt.2.o36.I16.M.EJY
 jyC.6.VQ.E.UegiJYe6r.3Qwd0ZORNPNZvQRtIdHdKLq6F9vQ59.XDJ.QiiIepZhZ7F6.Zz.6.
 ,UlW5UXW.2.52.CLLC3b8Rn9P99F9vQ0ks,Uikwm46.ZD06..W.e32.86.c918R..,E0E...7,
 ,M.6.,.,iLF1,Eyuv.2bXFQC41,0.y34E,9T3U.ENU.kG0.0UZhN6.9jjRBuvxfHza9QurRYmL
 T5U.E.07c5c.3gwJ.,6.Y32.I16.K46.J7..0E6,p.U1IkmL,2U.EQE.EBU.YJ2.I3VuI3.0E6
 ,P.4E.E.UegCUmT,2U.k1l1km0.0U1K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lFWU1x7EM...
 l,O2Eb..U,,7WtM0CZEtIA4.sEMC9WHV7.U2VrEM..U,haR,kQO225Z,00.ke..UqML.A6p,7m
 OcFkdG3r04XR,UsUtI6O2UB.MCp76,FV.kY.6BQbm,X,.EEk4MAkVEWaXk.,,.Y7A7.UqML.Ic
 m.oc2,50.EOeHEYc27FX,b8JHEe..O1A5C283..WHR0GE2570C2EM...10O2Eb..U1,7WtM0CZ
 EtIA4.MFMC90..O2S1..U1xaRBVBo4UlU235Z,00.ke..UqML.QcrI5EMG2.U7MH..Uq..58FE
 Wg4.b8JQ9MA.EW0HR0GEaHV.UB.MAp76,7C3WpkakWC3KIVM1h,sAMB7m20pbMF3m2.6BsEbVG
 ,8ssHpmsETfPdfQT9PNPNZvQRtIGqVGLtmKWKqtCK.4D..umVyKrG5EWE.Q6AA.cQ...sQR,.G
 20EtV.UIU.U76.0E..k.8c9.,UVF.SU.MK6.,k,,0.E2kTE.0.326..EGE.4E.E.EECOhU.YD.
 l,1.0.3I3U...p6.6.M.EJYD2.0E65.2..pZ,...
 --- end of encoding ---
luowy
luowy
Posts: 87
Joined: Thu Dec 17, 2015 1:32 pm

Re: ASH and LONGINT

Post by luowy »

Code: Select all

m := 70; x := 1.0 * ASH (1L, m)
Is this ASH() result legal? it seem that we should coincide with other language(C),
Otherwise it is difficult to port other languages to or from oberon!
So better give up my fixup
User avatar
Robert
Posts: 177
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: ASH and LONGINT

Post by Robert »

The Platform Specific Issues document says

"The Component Pascal Language Report does not specify the out-of-range behaviour of the built in arithmetic and set functions, but deliberately allows the compiler writer freedom to make implementation decisions."

and
"Note carefully that using a compound expression in case of out-of-range values is not functionally equivalent to assigning the intermediate LONGINT value to a variable."

So I now think what the BlackBox compiler does is fine; no change is wanted.


PS - I did look at the assembler code generated by my examples, but didn't really understand it!!!
Post Reply