Skip to content

Instantly share code, notes, and snippets.

@primo-ppcg
Created July 19, 2023 12:38
Show Gist options
  • Save primo-ppcg/b915d352dbe5cc2f338587d199e2ba1f to your computer and use it in GitHub Desktop.
Save primo-ppcg/b915d352dbe5cc2f338587d199e2ba1f to your computer and use it in GitHub Desktop.
cmath.c
/*
* Copyright (c) 2023 Calvin Rose
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <janet.h>
#include <math.h>
double _mod_impl(double a, double m) {
if (m == 0) return a;
return a - floor(a / m) * m;
}
double _mulmod_impl(double a, double b, double m) {
double res = 0;
a = _mod_impl(a, m);
b = _mod_impl(b, m);
if (a < b) {
double t = a;
a = b;
b = t;
}
if (b < 0) b -= m;
while (b > 0) {
if (_mod_impl(b, 2) == 1) {
res = _mod_impl(res + a, m);
b -= 1;
}
a = _mod_impl(a * 2, m);
b /= 2;
}
return res;
}
double _modinv_impl(double a, double m) {
double x = 0;
double u = 1;
double n = abs(m);
a = _mod_impl(a, n);
while (a != 0) {
double tmpx = x;
x = u;
u = tmpx - floor(n / a) * u;
double tmpa = a;
a = _mod_impl(n, a);
n = tmpa;
}
if (n == 1) return x;
#ifdef NAN
return NAN;
#else
return 0.0 / 0.0;
#endif
}
JANET_FN(cfun_cmath_modinv,
"(cmath/modinv a m)",
"Modular multiplicative inverse of a mod m.") {
janet_fixarity(argc, 2);
double a = janet_getnumber(argv, 0);
double m = janet_getnumber(argv, 1);
return janet_wrap_number(_modinv_impl(a, m));
}
JANET_FN(cfun_cmath_mulmod,
"(cmath/mulmod a b m)",
"Modular multiplication of a and b mod m.") {
janet_fixarity(argc, 3);
double a = janet_getnumber(argv, 0);
double b = janet_getnumber(argv, 1);
double m = janet_getnumber(argv, 2);
return janet_wrap_number(_mulmod_impl(a, b, m));
}
JANET_FN(cfun_cmath_powmod,
"(cmath/powmod a b m)",
"Modular exponentiation of a to the power of b mod m.") {
janet_fixarity(argc, 3);
double res = 1;
double a = janet_getnumber(argv, 0);
double b = janet_getnumber(argv, 1);
double m = janet_getnumber(argv, 2);
if (b < 0) {
a = _modinv_impl(a, m);
if (isnan(a)) return janet_wrap_number(a);
b = -b;
}
while (b > 0) {
if (_mod_impl(b, 2) == 1) {
res = _mulmod_impl(res, a, m);
b -= 1;
}
a = _mulmod_impl(a, a, m);
b /= 2;
}
return janet_wrap_number(res);
}
JANET_MODULE_ENTRY(JanetTable *env) {
JanetRegExt cfuns[] = {
JANET_REG("modinv", cfun_cmath_modinv),
JANET_REG("mulmod", cfun_cmath_mulmod),
JANET_REG("powmod", cfun_cmath_powmod),
JANET_REG_END
};
janet_cfuns_ext(env, "cmath", cfuns);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment