slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
authorwenzelm
Tue Nov 14 20:12:47 2017 +0100 (17 months ago)
changeset 670745da20135f560
parent 67073 74bd55f1206d
child 67075 eada9bd5fff2
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
src/Pure/General/integer.ML
     1.1 --- a/src/Pure/General/integer.ML	Tue Nov 14 16:17:08 2017 +0100
     1.2 +++ b/src/Pure/General/integer.ML	Tue Nov 14 20:12:47 2017 +0100
     1.3 @@ -40,7 +40,20 @@
     1.4  
     1.5  fun square x = x * x;
     1.6  
     1.7 -fun pow k l = IntInf.pow (l, k);
     1.8 +fun pow k l =
     1.9 +  let
    1.10 +    fun pw 0 _ = 1
    1.11 +      | pw 1 l = l
    1.12 +      | pw k l =
    1.13 +          let
    1.14 +            val (k', r) = div_mod k 2;
    1.15 +            val l' = pw k' (l * l);
    1.16 +          in if r = 0 then l' else l' * l end;
    1.17 +  in
    1.18 +    if k < 0
    1.19 +    then IntInf.pow (l, k)
    1.20 +    else pw k l
    1.21 +  end;
    1.22  
    1.23  fun gcd x y = PolyML.IntInf.gcd (x, y);
    1.24  fun lcm x y = abs (PolyML.IntInf.lcm (x, y));
    1.25 @@ -52,3 +65,10 @@
    1.26    | lcms (x :: xs) = abs (Library.foldl PolyML.IntInf.lcm (x, xs));
    1.27  
    1.28  end;
    1.29 +
    1.30 +(*slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore*)
    1.31 +structure IntInf =
    1.32 +struct
    1.33 +  open IntInf;
    1.34 +  fun pow (i, n) = Integer.pow n i;
    1.35 +end;