diff options
author | Christopher Swenson <chris@caswenson.com> | 2018-08-27 10:32:21 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-09-26 15:03:04 +1000 |
commit | 8c656754aa2892cbd36968bfaab1ff7033edeb0f (patch) | |
tree | 30eab37cc6b51eb9fb3785183b8543a25abbb4b8 /py/mpconfig.h | |
parent | 7b452e7466d7fc4058eab5eb60bbbbd125039d88 (diff) |
py/modmath: Add math.factorial, optimised and non-opt implementations.
This commit adds the math.factorial function in two variants:
- squared difference, which is faster than the naive version, relatively
compact, and non-recursive;
- a mildly optimised recursive version, faster than the above one.
There are some more optimisations that could be done, but they tend to take
more code, and more storage space. The recursive version seems like a
sensible compromise.
The new function is disabled by default, and uses the non-optimised version
by default if it is enabled. The options are MICROPY_PY_MATH_FACTORIAL
and MICROPY_OPT_MATH_FACTORIAL.
Diffstat (limited to 'py/mpconfig.h')
-rw-r--r-- | py/mpconfig.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/py/mpconfig.h b/py/mpconfig.h index 8f1411405..cd2f2acdf 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -407,6 +407,12 @@ #define MICROPY_OPT_MPZ_BITWISE (0) #endif + +// Whether math.factorial is large, fast and recursive (1) or small and slow (0). +#ifndef MICROPY_OPT_MATH_FACTORIAL +#define MICROPY_OPT_MATH_FACTORIAL (0) +#endif + /*****************************************************************************/ /* Python internal features */ @@ -988,6 +994,11 @@ typedef double mp_float_t; #define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (0) #endif +// Whether to provide math.factorial function +#ifndef MICROPY_PY_MATH_FACTORIAL +#define MICROPY_PY_MATH_FACTORIAL (0) +#endif + // Whether to provide "cmath" module #ifndef MICROPY_PY_CMATH #define MICROPY_PY_CMATH (0) |