/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=4 sts=4 sw=4 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright (c) 2008-2016 Stefan Krah. All rights reserved.                    â
â                                                                              â
â Redistribution and use in source and binary forms, with or without           â
â modification, are permitted provided that the following conditions           â
â are met:                                                                     â
â                                                                              â
â 1. Redistributions of source code must retain the above copyright            â
â    notice, this list of conditions and the following disclaimer.             â
â                                                                              â
â 2. Redistributions in binary form must reproduce the above copyright         â
â    notice, this list of conditions and the following disclaimer in           â
â    the documentation and/or other materials provided with the                â
â    distribution.                                                             â
â                                                                              â
â THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND         â
â ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE        â
â IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR           â
â PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS         â
â BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,          â
â OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT         â
â OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR           â
â BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,        â
â WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE         â
â OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,            â
â EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "third_party/python/Modules/_decimal/libmpdec/fourstep.h"
#include "third_party/python/Modules/_decimal/libmpdec/mpdecimal.h"
#include "third_party/python/Modules/_decimal/libmpdec/numbertheory.h"
#include "third_party/python/Modules/_decimal/libmpdec/sixstep.h"
#include "third_party/python/Modules/_decimal/libmpdec/transpose.h"
#include "third_party/python/Modules/_decimal/libmpdec/umodarith.h"
/* clang-format off */

asm(".ident\t\"\\n\\n\
libmpdec (BSD-2)\\n\
Copyright 2008-2016 Stefan Krah\"");
asm(".include \"libc/disclaimer.inc\"");

/*
                 Cache Efficient Matrix Fourier Transform
                        for arrays of form 3Ã2â¿


The Matrix Fourier Transform
ââââââââââââââââââââââââââââ

In libmpdec, the Matrix Fourier Transform [1] is called four-step
transform after a variant that appears in [2]. The algorithm requires
that the input array can be viewed as an R*C matrix.

All operations are done modulo p. For readability, the proofs drop all
instances of (mod p).


Algorithm four-step (forward transform)
âââââââââââââââââââââââââââââââââââââââ

  a := input array
  d := len(a) = R * C
  p := prime
  w := primitive root of unity of the prime field
  r := w**((p-1)/d)
  A := output array

  1) Apply a length R FNT to each column.

  2) Multiply each matrix element (addressed by j*C+m) by r**(j*m).

  3) Apply a length C FNT to each row.

  4) Transpose the matrix.


Proof (forward transform)
âââââââââââââââââââââââââ

  The algorithm can be derived starting from the regular definition of
  the finite-field transform of length d:

            d-1
           ,ââââ
           \
    A[k] =  |  a[l]  Ã r**(k Ã l)
           /
           `ââââ
           l = 0


  The sum can be rearranged into the sum of the sums of columns:

            C-1     R-1
           ,ââââ   ,ââââ
           \       \
         =  |       |  a[i Ã C + j] Ã r**(k Ã (i Ã C + j))
           /       /
           `ââââ   `ââââ
           j = 0   i = 0


  Extracting a constant from the inner sum:

            C-1           R-1
           ,ââââ         ,ââââ
           \             \
         =  |  ráµÃj    Ã  |  a[i Ã C + j] Ã r**(k Ã i Ã C)
           /             /
           `ââââ         `ââââ
           j = 0         i = 0


  Without any loss of generality, let k = n Ã R + m,
  where n < C and m < R:

                C-1                          R-1
               ,ââââ                        ,ââââ
               \                            \
    A[nÃR+m] =  |  r**(RÃnÃj) Ã r**(mÃj)  Ã  |  a[iÃC+j] Ã r**(RÃCÃnÃi) Ã r**(CÃmÃi)
               /                            /
               `ââââ                        `ââââ
               j = 0                        i = 0


  Since r = w ** ((p-1) / (RÃC)):

     a) r**(RÃCÃnÃi) = w**((p-1)ÃnÃi) = 1

     b) r**(CÃmÃi) = w**((p-1) / R) ** (mÃi) = r_R ** (mÃi)

     c) r**(RÃnÃj) = w**((p-1) / C) ** (nÃj) = r_C ** (nÃj)

     r_R := root of the subfield of length R.
     r_C := root of the subfield of length C.


                C-1                             R-1
               ,ââââ                           ,ââââ
               \                               \
    A[nÃR+m] =  |  r_C**(nÃj) Ã [ r**(mÃj)  Ã   |  a[iÃC+j] Ã r_R**(mÃi) ]
               /                     ^         /
               `ââââ                 |         `ââââ    1) transform the columns
               j = 0                 |         i = 0
                 ^                   |
                 |                   `-- 2) multiply
                 |
                 `-- 3) transform the rows


   Note that the entire RHS is a function of n and m and that the results
   for each pair (n, m) are stored in Fortran order.

   Let the term in square brackets be ð(m, j). Step 1) and 2) precalculate
   the term for all (m, j). After that, the original matrix is now a lookup
   table with the mth element in the jth column at location m Ã C + j.

   Let the complete RHS be g(m, n). Step 3) does an in-place transform of
   length n on all rows. After that, the original matrix is now a lookup
   table with the mth element in the nth column at location m Ã C + n.

   But each (m, n) pair should be written to location n Ã R + m. Therefore,
   step 4) transposes the result of step 3).



Algorithm four-step (inverse transform)
âââââââââââââââââââââââââââââââââââââââ

  A  := input array
  d  := len(A) = R Ã C
  p  := prime
  dâ² := dâ½áµâ»Â²â¾               # inverse of d
  w  := primitive root of unity of the prime field
  r  := w**((p-1)/d)         # root of the subfield
  râ² := w**((p-1) - (p-1)/d) # inverse of r
  a  := output array

  0) View the matrix as a CÃR matrix.

  1) Transpose the matrix, producing an RÃC matrix.

  2) Apply a length C FNT to each row.

  3) Multiply each matrix element (addressed by iÃC+n) by r**(iÃn).

  4) Apply a length R FNT to each column.


Proof (inverse transform)
âââââââââââââââââââââââââ

  The algorithm can be derived starting from the regular definition of
  the finite-field inverse transform of length d:

                  d-1
                 ,ââââ
                 \
    a[k] =  dâ² Ã  |  A[l]  Ã râ² ** (k Ã l)
                 /
                 `ââââ
                 l = 0


  The sum can be rearranged into the sum of the sums of columns. Note
  that at this stage we still have a C*R matrix, so C denotes the number
  of rows:

                  R-1     C-1
                 ,ââââ   ,ââââ
                 \       \
         =  dâ² Ã  |       |  a[j Ã R + i] Ã râ² ** (k Ã (j Ã R + i))
                 /       /
                 `ââââ   `ââââ
                 i = 0   j = 0


  Extracting a constant from the inner sum:

                  R-1                C-1
                 ,ââââ              ,ââââ
                 \                  \
         =  dâ² Ã  |  râ² ** (kÃi)  Ã  |  a[j Ã R + i] Ã râ² ** (k Ã j Ã R)
                 /                  /
                 `ââââ              `ââââ
                 i = 0              j = 0


  Without any loss of generality, let k = m * C + n,
  where m < R and n < C:

                     R-1                                  C-1
                    ,ââââ                                ,ââââ
                    \                                    \
    A[mÃC+n] = dâ² Ã  |  râ² ** (CÃmÃi) Ã  râ² ** (nÃi)   Ã  |  a[jÃR+i] Ã râ² ** (RÃCÃmÃj) Ã râ² ** (RÃnÃj)
                    /                                    /
                    `ââââ                                `ââââ
                    i = 0                                j = 0


  Since râ² = w**((p-1) - (p-1)/d) and d = RÃC:

     a) râ² ** (RÃCÃmÃj) = w**((p-1)ÃRÃCÃmÃj - (p-1)ÃmÃj) = 1

     b) râ² ** (CÃmÃi) = w**((p-1)ÃC - (p-1)/R) ** (mÃi) = r_Râ² ** (mÃi)

     c) râ² ** (RÃnÃj) = r_Câ² ** (nÃj)

     d) dâ² = dâ½áµâ»Â²â¾ = (RÃC)â½áµâ»Â²â¾ = Râ½áµâ»Â²â¾ Ã Câ½áµâ»Â²â¾ = Râ² Ã Câ²

     r_Râ² := inverse of the root of the subfield of length R.
     r_Câ² := inverse of the root of the subfield of length C.
     Râ²   := inverse of R
     Câ²   := inverse of C


                     R-1                                      C-1
                    ,ââââ                                    ,ââââ  2) transform the rows of a^T
                    \                                        \
    A[mÃC+n] = Râ² Ã  |  r_Râ² ** (mÃi) Ã [ râ² ** (nÃi) Ã Câ² Ã  |  a[jÃR+i] Ã r_Câ² ** (nÃj) ]
                    /                           ^            /       ^
                    `ââââ                       |            `ââââ   |
                    i = 0                       |            j = 0   |
                      ^                         |                    `ââ 1) Transpose input matrix
                      |                         `ââ 3) multiply             to address elements by
                      |                                                     i Ã C + j
                      `ââ 3) transform the columns



   Note that the entire RHS is a function of m and n and that the results
   for each pair (m, n) are stored in C order.

   Let the term in square brackets be ð(n, i). Without step 1), the sum
   would perform a length C transform on the columns of the input matrix.
   This is a) inefficient and b) the results are needed in C order, so
   step 1) exchanges rows and columns.

   Step 2) and 3) precalculate ð(n, i) for all (n, i). After that, the
   original matrix is now a lookup table with the ith element in the nth
   column at location i Ã C + n.

   Let the complete RHS be g(m, n). Step 4) does an in-place transform of
   length m on all columns. After that, the original matrix is now a lookup
   table with the mth element in the nth column at location m Ã C + n,
   which means that all A[k] = A[m Ã C + n] are in the correct order.


ââ

  [1] Joerg Arndt: "Matters Computational"
      http://www.jjj.de/fxt/
  [2] David H. Bailey: FFTs in External or Hierarchical Memory
      http://crd.lbl.gov/~dhbailey/dhbpapers/
*/

static inline void
std_size3_ntt(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3,
              mpd_uint_t w3table[3], mpd_uint_t umod)
{
    mpd_uint_t r1, r2;
    mpd_uint_t w;
    mpd_uint_t s, tmp;
    /* k = 0 -> w = 1 */
    s = *x1;
    s = addmod(s, *x2, umod);
    s = addmod(s, *x3, umod);
    r1 = s;
    /* k = 1 */
    s = *x1;
    w = w3table[1];
    tmp = MULMOD(*x2, w);
    s = addmod(s, tmp, umod);
    w = w3table[2];
    tmp = MULMOD(*x3, w);
    s = addmod(s, tmp, umod);
    r2 = s;
    /* k = 2 */
    s = *x1;
    w = w3table[2];
    tmp = MULMOD(*x2, w);
    s = addmod(s, tmp, umod);
    w = w3table[1];
    tmp = MULMOD(*x3, w);
    s = addmod(s, tmp, umod);
    *x3 = s;
    *x2 = r2;
    *x1 = r1;
}

/* forward transform, sign = -1; transform length = 3 * 2**n */
int
four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum)
{
    mpd_size_t R = 3; /* number of rows */
    mpd_size_t C = n / 3; /* number of columns */
    mpd_uint_t w3table[3];
    mpd_uint_t kernel, w0, w1, wstep;
    mpd_uint_t *s, *p0, *p1, *p2;
    mpd_uint_t umod;
    mpd_size_t i, k;
    assert(n >= 48);
    assert(n <= 3*MPD_MAXTRANSFORM_2N);
    /* Length R transform on the columns. */
    SETMODULUS(modnum);
    _mpd_init_w3table(w3table, -1, modnum);
    for (p0=a, p1=p0+C, p2=p0+2*C; p0<a+C; p0++,p1++,p2++) {
        SIZE3_NTT(p0, p1, p2, w3table);
    }
    /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */
    kernel = _mpd_getkernel(n, -1, modnum);
    for (i = 1; i < R; i++) {
        w0 = 1;                  /* r**(i*0): initial value for k=0 */
        w1 = POWMOD(kernel, i);  /* r**(i*1): initial value for k=1 */
        wstep = MULMOD(w1, w1);  /* r**(2*i) */
        for (k = 0; k < C-1; k += 2) {
            mpd_uint_t x0 = a[i*C+k];
            mpd_uint_t x1 = a[i*C+k+1];
            MULMOD2(&x0, w0, &x1, w1);
            MULMOD2C(&w0, &w1, wstep);  /* r**(i*(k+2)) = r**(i*k) * r**(2*i) */
            a[i*C+k] = x0;
            a[i*C+k+1] = x1;
        }
    }
    /* Length C transform on the rows. */
    for (s = a; s < a+n; s += C) {
        if (!six_step_fnt(s, C, modnum)) {
            return 0;
        }
    }
#if 0
    /* An unordered transform is sufficient for convolution. */
    /* Transpose the matrix. */
    transpose_3xpow2(a, R, C);
#endif
    return 1;
}

/* backward transform, sign = 1; transform length = 3 * 2**n */
int
inv_four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum)
{
    mpd_size_t R = 3; /* number of rows */
    mpd_size_t C = n / 3; /* number of columns */
    mpd_uint_t w3table[3];
    mpd_uint_t kernel, w0, w1, wstep;
    mpd_uint_t *s, *p0, *p1, *p2;
    mpd_uint_t umod;
    mpd_size_t i, k;
    assert(n >= 48);
    assert(n <= 3*MPD_MAXTRANSFORM_2N);
#if 0
    /* An unordered transform is sufficient for convolution. */
    /* Transpose the matrix, producing an R*C matrix. */
    transpose_3xpow2(a, C, R);
#endif
    /* Length C transform on the rows. */
    for (s = a; s < a+n; s += C) {
        if (!inv_six_step_fnt(s, C, modnum)) {
            return 0;
        }
    }
    /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */
    SETMODULUS(modnum);
    kernel = _mpd_getkernel(n, 1, modnum);
    for (i = 1; i < R; i++) {
        w0 = 1;
        w1 = POWMOD(kernel, i);
        wstep = MULMOD(w1, w1);
        for (k = 0; k < C; k += 2) {
            mpd_uint_t x0 = a[i*C+k];
            mpd_uint_t x1 = a[i*C+k+1];
            MULMOD2(&x0, w0, &x1, w1);
            MULMOD2C(&w0, &w1, wstep);
            a[i*C+k] = x0;
            a[i*C+k+1] = x1;
        }
    }
    /* Length R transform on the columns. */
    _mpd_init_w3table(w3table, 1, modnum);
    for (p0=a, p1=p0+C, p2=p0+2*C; p0<a+C; p0++,p1++,p2++) {
        SIZE3_NTT(p0, p1, p2, w3table);
    }
    return 1;
}
