int64p.inc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. {$define FPC_SYSTEM_HAS_DIV_QWORD}
  15. function fpc_div_qword(n,z : qword) : qword; compilerproc;
  16. var
  17. signmask, tmpz: qword;
  18. tmpq, rest: int64;
  19. begin
  20. { emulate unsigned division using signed division, see
  21. http://hackers-delight.org.ua/060.htm }
  22. signmask:=qword(not(SarInt64(int64(n),63)));
  23. tmpz:=z and signmask;
  24. tmpq:=(int64(tmpz shr 1) div int64(n)) * 2;
  25. rest:=z-tmpq*n;
  26. fpc_div_qword:=tmpq+ord(rest>=n);
  27. end;
  28. {$endif FPC_SYSTEM_HAS_DIV_QWORD}
  29. {$ifndef FPC_SYSTEM_HAS_MOD_QWORD}
  30. {$define FPC_SYSTEM_HAS_MOD_QWORD}
  31. function fpc_mod_qword(n,z : qword) : qword; compilerproc;
  32. var
  33. signmask, tmpz: qword;
  34. tmpq: int64;
  35. begin
  36. { emulate unsigned division using signed division, see
  37. http://hackers-delight.org.ua/060.htm }
  38. signmask:=qword(not(SarInt64(int64(n),63)));
  39. tmpz:=z and signmask;
  40. tmpq:=(int64(tmpz shr 1) div int64(n)) * 2;
  41. fpc_mod_qword:=z-tmpq*n;
  42. if fpc_mod_qword>=n then
  43. dec(fpc_mod_qword,n);
  44. end;
  45. {$endif FPC_SYSTEM_HAS_MOD_QWORD}
  46. { lie to prevent two overloads for sqr(jlong) }
  47. {$define FPC_SYSTEM_HAS_SQR_QWORD}