Path-Hilbert-XS

 view release on metacpan or  search on metacpan

XS.xs  view on Meta::CPAN


/* convert (x,y) to d */
static IV hilbert_xy2d(IV side, IV x, IV y) {
    IV n = hilbert_valid_n(side);
    IV d = 0;
    IV s;

    for (s = n / 2; s > 0; s /= 2) {
      IV rx = (x & s) > 0;
      IV ry = (y & s) > 0;
      d += s * s * ((3 * rx) ^ ry);
      hilbert_rot(s, &x, &y, rx, ry);
    }

    return d * side / n;
}

/* convert d to (x,y) */
static void hilbert_d2xy(IV side, IV d, IV *x, IV *y) {
    IV n = hilbert_valid_n(side);
    IV t = d;
    IV s;

    *x = 0;
    *y = 0;

    for (s = 1; s < n; s *= 2) {
      IV rx = 1 & (t / 2);
      IV ry = 1 & (t ^ rx);
      hilbert_rot(s, x, y, rx, ry);
      *x += s * rx;
      *y += s * ry;
      t /= 4;
    }
    *x *= side / n;
    *y *= side / n;
}


MODULE = Path::Hilbert::XS		PACKAGE = Path::Hilbert::XS
PROTOTYPES: DISABLE

# convert (x,y) to d
IV
xy2d(IV side, IV x, IV y)

  CODE:
    RETVAL = hilbert_xy2d(side, x, y);

  OUTPUT: RETVAL


# convert d to (x,y)
void
d2xy(IV side, IV d)

  PREINIT:
    IV x;
    IV y;

  PPCODE:
    hilbert_d2xy(side, d, &x, &y);

    EXTEND(SP, 2);
    mPUSHi(x);
    mPUSHi(y);



( run in 2.019 seconds using v1.01-cache-2.11-cpan-5511b514fd6 )