Karatsuba multiplication in Linux lazarus / 61-Beginners ---------------------------------------------------------------------- jcaser1948 · 2018-03-14 12:33 Hi,friends I thy to use the following Karatsuba multiplication in Opensuse Linux Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  procedure Karatsuba(PA, PB, PR: PInt64Array; d: integer);  var    AL, AR, BL, BR, ASum, BSum, X1, X2, X3: PInt64Array;    i, halfd: integer;  begin    if d <= 100 then   begin      GradeSchoolMult(Pa, Pb, Pr, d);// normal multiplikation, definiert woanders      exit;    end;    halfd := d shr 1;    AR := @PA[0];    AL := @PA[halfd];    BR := @PB[0];    BL := @PB[halfd];    ASum := @PR[d * 5];    BSum := @PR[d * 5 + halfd];    X1 := @PR[0];    X2 := @PR[d];    X3 := @PR[d * 2];    for i := 0 to halfd - 1 do    begin      asum[i] := al[i] + ar[i];      bsum[i] := bl[i] + br[i];    end;    Karatsuba(AR, Br, X1, halfd);    Karatsuba(AL, BL, X2, halfd);    Karatsuba(ASum, BSum, X3, halfd);    for i := 0 to d - 1 do      X3[i] := X3[i] - X1[i] - X2[i];    for i := 0 to d - 1 do      PR[i + halfd] := PR[i + halfd] + X3[i];  end; begin  Alen := length(self.fDigits);  BLen := Length(I2.fDigits);  iMin := ALen;  iMax := BLen;  if ALen > BLen then  begin    iMin := BLen;    iMax := ALen;  end;  if (iMin < 80) or (imin < Imax shr 4) then  begin    self.Mult(i2);    exit;  end;  d := 1;  while d < iMax do    d := d * 2;  setlength(self.fDigits, d);  setlength(i2.fDigits,d);  PA := @self.fdigits[0];  PB := @i2.fdigits[0];  HR := GlobalAlloc(GMEM_FIXED, d * 6 * Sizeof(int64));  PR := GlobalLock(HR);  FillChar(Pr[0], sizeof(int64) * 6 * d, 0);  Karatsuba(PA, PB, PR, d);  DoCarry(Pr, self.Base, d * 2);  d := 2 * d;  while (d > 0) and (pr[d - 1] = 0) do    Dec(d);  setlength(self.fDigits, d);  setlength(i2.fDigits, BLen);   {Changes input variable ?????????? GDD}  move(pr[0], self.fdigits[0], d * sizeof(int64));  self.Trim;  self.Sign := self.Sign * i2.Sign;  GlobalUnlock(HR);  GlobalFree(HR);end; The compiler does not like the sentences Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i HR := GlobalAlloc(GMEM_FIXED, d * 6 * Sizeof(int64));  PR := GlobalLock(HR); and also Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i GlobalUnlock(HR);  GlobalFree(HR);Has anyone a suggestion how could convert this procedure easily so that it works under Linux Thanks in advance ---------------------------------------------------------------------- Thaddy · 2018-03-14 12:48 The GlobalXXX functions are windows specific. ---------------------------------------------------------------------- rvk · 2018-03-14 12:52 Quote from: Thaddy on March 14, 2018, 01:48:49 pmThe GlobalXXX functions are windows specific. Not really a suggestion as to how to convert this to Linux, is it  Quote from: jcaser1948 on March 14, 2018, 01:33:06 pmHas anyone a suggestion how could convert this procedure easily so that it works under Linux You could try including cmem in the uses and change the GlobalAlloc to malloc and GlobalFree to free. No need to separately lock and unlock it. ---------------------------------------------------------------------- marcov · 2018-03-14 13:04 The question is hard to answer without knowing why globalalloc is used. Under windows 3.1 it was used for nearly every allocation, in that case, replace it with normal getmem/freemem calls. ---------------------------------------------------------------------- Thaddy · 2018-03-14 13:31 Quote from: marcov on March 14, 2018, 02:04:53 pmThe question is hard to answer without knowing why globalalloc is used. Under windows 3.1 it was used for nearly every allocation, in that case, replace it with normal getmem/freemem calls. Probably: indeed. But we can not know because it is not used in the fragmentary example. ---------------------------------------------------------------------- jcaser1948 · 2018-03-15 11:41 Quote from: marcov on March 14, 2018, 02:04:53 pmThe question is hard to answer without knowing why globalalloc is used. Under windows 3.1 it was used for nearly every allocation, in that case, replace it with normal getmem/freemem calls. Thanks and what can I use instead of GMEM_FIXED ? As far as I know this function does not exists in Lazarus (Linux Version) ---------------------------------------------------------------------- Thaddy · 2018-03-15 11:46 You can use the UNIX mmap functionality. Even abstract it away in the interface section to obtain fully cross platform. Note FPC fully supports this. ---------------------------------------------------------------------- jcaser1948 · 2018-03-15 12:04 Thaddy, could you please elaborate on your suggestion? I have added Unix in thr uses clause and replaced GMEM_FIXED   with mmap and it does not compile. ---------------------------------------------------------------------- Thaddy · 2018-03-15 14:26 Quote from: jcaser1948 on March 15, 2018, 01:04:43 pmThaddy, could you please elaborate on your suggestion? I have added Unix in thr uses clause and replaced GMEM_FIXED   with mmap and it does not compile. That is not good. GMEM_FIXED is a flag for the Windows only GlobalAlloc etc API's. Not a function. In your particular case mmap can be used - somewhat - as a replacement for that when you do not persist the memory. There are other options though. As suggested before, replace those calls simply by GetMem/FreeMem. That is likely a better option if you have to ask how to use mmap and probably does what you want.