Code organisation of mini-gmp

Niels Möller nisse at lysator.liu.se
Tue Sep 11 14:11:37 CEST 2012


nisse at lysator.liu.se (Niels Möller) writes:

> If it's possible to divide the features into pieces based on at most,
> say, ten #defines available for users, then I think it would make sense
> to keep at as a single file and manage dependencies manually at
> preprocessor time.

Let's sketch how functions might be grouped:

* Unconditionally included: mpz_init, mpz_clear, mpz_set, mpz_set_ui,
  mpz_cmp,...

* Add: mpz_add, mpz_sub

* Shifts.

* Multiplication: mpz_mul. (Possibly split out mpz_mul_1?)

* Division by single words.

* Base conversion (split into diffent pieces for conversion to and from
  mpz_t, and conversion with power-of-two bases and others?).

* General division.

* Gcd functions (split out gcdext?)

* Powering.

* Roots (possibly splitting out square roots).

* mp_set_memory_functions (could be optional, with a little macrology).

* Import/export.

* Bit manipulation: mpz_tstbit, ...

* Bitwise logic: mpz_and, ...

* Misc: mpz_popcount, mpz_hamdist, mpz_scan0, mpz_scan1, ... Maybe these should
  have one define per function?

The dependencies between groups are more-or-less obvious. Do you think
such a division could be both suitable for applications, and
maintainable for us?

Using the preprocessor has one advantage over a one-function-per-file
organization: It's easy to disable features (e.g., base conversion with
bases other than powers of two, or the mp_set_memory_functions
interface) which don't correspond to separate functions.

I've been playing a little with cflow and dot, but it's not as
enlightning as I had hoped. I used the following script:

  #! /bin/sh
  
  set -e
  cat mini-gmp.c | sed 's,static,,' \
    | cflow --omit-arguments --omit-symbol-names -d2 --level-indent='0=+ ' /dev/stdin \
    | sed 's,(.*$,,' > mini-gmp.cflow
  
  awk '{ if ($1 == "+")
  	 printf(" %s", $2);
         else
  	 printf("\n%s", $1);
       }
       END { printf("\n"); }' \
  < mini-gmp.cflow | grep -v '^$' > mini-gmp.deps \
  
  awk 'BEGIN { printf("strict digraph mini_gmp {\n"); }
       function use_symbol(s, use) {
         use = match(s, "^mp[nz]") > 0 && s != mpz_init && s != "mpz_clear";
         return use
       }
       {
         if ($1 == "+") {
           if (current && use_symbol($2))
  	   printf("%s -> %s\n", current, $2);
         }
         else
           current = use_symbol($1) ? $1 : 0;
       }
       END { printf("}\n"); }' \
  < mini-gmp.cflow > mini-gmp.dot
  
  dot -Tpdf -o mini-gmp.pdf mini-gmp.dot

Resulting graph at http://www.lysator.liu.se/~nisse/misc/mini-gmp.pdf

Regards,
/Niels

-- 
Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26.
Internet email is subject to wholesale government surveillance.


More information about the gmp-devel mailing list