Rewrite this code to be more performant:  unsafe static void calcCore<Rule>(ReadOnlyMemory<TState> km, Memory<TState> nm, int dfrom, int dto, int height, int width) where Rule : IRule3D<TState>, new()
    {
        var k = km.Span;
        ref var refK = ref System.Runtime.InteropServices.MemoryMarshal.GetReference(k);
        var n = nm.Span;
        ref var refN = ref System.Runtime.InteropServices.MemoryMarshal.GetReference(n);

        int hw = height * width;
        int w = width;
        int h = height;
        for (int z = dfrom; z < dto - 1; z++)
        {
            int zM = (z - 1) * hw;
            int z_ = z * hw;
            int zP = (z + 1) * hw;
            for (int y = 1; y < height - 1; y++)
            {
                int zMyM = zM + (y - 1) * w;
                int zMy_ = zM + y * w;
                int zMyP = zM + (y + 1) * w;
                int z_yM = z_ + (y - 1) * w;
                int z_y_ = z_ + y * w;
                int z_yP = z_ + (y + 1) * w;
                int zPyM = zP + (y - 1) * w;
                int zPy_ = zP + y * w;
                int zPyP = zP + (y + 1) * w;

                for (int x = 1; x < width - 1; x++)
                {

                    int zMyMxM = zMyM + x - 1;
                    int zMyMx_ = zMyM + x;
                    int zMyMxP = zMyM + x + 1;
                    int zMy_xM = zMy_ + x - 1;
                    int zMy_x_ = zMy_ + x;
                    int zMy_xP = zMy_ + x + 1;
                    int zMyPxM = zMyP + x - 1;
                    int zMyPx_ = zMyP + x;
                    int zMyPxP = zMyP + x + 1;
                    int z_yMxM = z_yM + x - 1;
                    int z_yMx_ = z_yM + x;
                    int z_yMxP = z_yM + x + 1;
                    int z_y_xM = z_y_ + x - 1;
                    int z_y_x_ = z_y_ + x;
                    int z_y_xP = z_y_ + x + 1;
                    int z_yPxM = z_yP + x - 1;
                    int z_yPx_ = z_yP + x;
                    int z_yPxP = z_yP + x + 1;
                    int zPyMxM = zPyM + x - 1;
                    int zPyMx_ = zPyM + x;
                    int zPyMxP = zPyM + x + 1;
                    int zPy_xM = zPy_ + x - 1;
                    int zPy_x_ = zPy_ + x;
                    int zPy_xP = zPy_ + x + 1;
                    int zPyPxM = zPyP + x - 1;
                    int zPyPx_ = zPyP + x;
                    int zPyPxP = zPyP + x + 1;

                    Unsafe.Add(ref refN, z_y_x_) = new Rule().Next
                   (
                         Unsafe.Add(ref refK, zMyMxM), Unsafe.Add(ref refK, zMyMx_), Unsafe.Add(ref refK, zMyMxP),
                         Unsafe.Add(ref refK, zMy_xM), Unsafe.Add(ref refK, zMy_x_), Unsafe.Add(ref refK, zMy_xP),
                         Unsafe.Add(ref refK, zMyPxM), Unsafe.Add(ref refK, zMyPx_), Unsafe.Add(ref refK, zMyPxP),

                         Unsafe.Add(ref refK, z_yMxM), Unsafe.Add(ref refK, z_yMx_), Unsafe.Add(ref refK, z_yMxP),
                         Unsafe.Add(ref refK, z_y_xM), Unsafe.Add(ref refK, z_y_x_), Unsafe.Add(ref refK, z_y_xP),
                         Unsafe.Add(ref refK, z_yPxM), Unsafe.Add(ref refK, z_yPx_), Unsafe.Add(ref refK, z_yPxP),

                         Unsafe.Add(ref refK, zPyMxM), Unsafe.Add(ref refK, zPyMx_), Unsafe.Add(ref refK, zPyMxP),
                         Unsafe.Add(ref refK, zPy_xM), Unsafe.Add(ref refK, zPy_x_), Unsafe.Add(ref refK, zPy_xP),
                         Unsafe.Add(ref refK, zPyPxM), Unsafe.Add(ref refK, zPyPx_), Unsafe.Add(ref refK, zPyPxP)
                   );

                }
            }
        }
    }