jint64.inc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. This file contains some helper routines for qword
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$Q- no overflow checking }
  12. {$R- no range checking }
  13. {$ifndef FPC_SYSTEM_HAS_DIV_QWORD}
  14. function fpc_div_qword(n,z : qword) : qword; compilerproc;
  15. var
  16. signmask, tmpz: qword;
  17. tmpq, rest: int64;
  18. begin
  19. { emulate unsigned division using signed division, see
  20. http://hackers-delight.org.ua/060.htm }
  21. signmask:=qword(not(SarInt64(int64(n),63)));
  22. tmpz:=z and signmask;
  23. tmpq:=(int64(tmpz shr 1) div int64(n)) * 2;
  24. rest:=z-tmpq*n;
  25. fpc_div_qword:=tmpq+ord(rest>=n);
  26. end;
  27. {$endif FPC_SYSTEM_HAS_DIV_QWORD}
  28. {$ifndef FPC_SYSTEM_HAS_MOD_QWORD}
  29. function fpc_mod_qword(n,z : qword) : qword; compilerproc;
  30. var
  31. signmask, tmpz: qword;
  32. tmpq: int64;
  33. begin
  34. { emulate unsigned division using signed division, see
  35. http://hackers-delight.org.ua/060.htm }
  36. signmask:=qword(not(SarInt64(int64(n),63)));
  37. tmpz:=z and signmask;
  38. tmpq:=(int64(tmpz shr 1) div int64(n)) * 2;
  39. fpc_mod_qword:=z-tmpq*n;
  40. if fpc_mod_qword>=n then
  41. dec(fpc_mod_qword,n);
  42. end;
  43. {$endif FPC_SYSTEM_HAS_MOD_QWORD}