PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/cln-1.3.2/src/integer/algebraic/cl_I_sqrt.cc

#
C++ | 43 lines | 30 code | 8 blank | 5 comment | 1 complexity | 9c7fb750967bff60aba495aac7d13ee1 MD5 | raw file
Possible License(s): GPL-2.0
  1. // isqrt().
  2. // General includes.
  3. #include "base/cl_sysdep.h"
  4. // Specification.
  5. #include "cln/integer.h"
  6. // Implementation.
  7. #include "cln/number.h"
  8. #include "cln/io.h"
  9. #include "cln/integer_io.h"
  10. #include "cln/exception.h"
  11. #include "integer/cl_I.h"
  12. #include "base/digitseq/cl_DS.h"
  13. #include <sstream>
  14. namespace cln {
  15. bool isqrt (const cl_I& x, cl_I* w)
  16. {
  17. if (minusp(x)) {
  18. std::ostringstream buf;
  19. fprint(buf, "isqrt: applied to negative number: ");
  20. fprint(buf, x);
  21. throw runtime_exception(buf.str());
  22. }
  23. CL_ALLOCA_STACK;
  24. var const uintD* x_MSDptr;
  25. var uintC x_len;
  26. var const uintD* x_LSDptr;
  27. I_to_NDS_nocopy(x, x_MSDptr=,x_len=,x_LSDptr=,true,); // Digit sequence >=0 zu x
  28. var DS y;
  29. var bool squarep;
  30. UDS_sqrt(x_MSDptr,x_len,x_LSDptr, &y, squarep=); // Wurzel ziehen
  31. *w = NUDS_to_I(y.MSDptr,y.len); // als Integer
  32. return squarep;
  33. }
  34. // Bit complexity (x of length N): O(M(N)).
  35. } // namespace cln