三維體素空間之拓撲降維映射、三軸同動算子與跨維度零損耗相容機制,跳樓層一維單指標定址

megapx
megapx
megapx
megapx
megapx
megapx
megapx
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <time.h> #ifdef _OPENMP #include <omp.h> #endif // 公理 1 黃金奇數常數:99 x 999 體素空間 (99 mod 64 != 0, 9801 mod 64 != 0) #define W_STRIDE 99 #define FLOOR_STRIDE (99 * 99) // 9801 單層樓跨距 #define TOTAL_NODES (99 * 999) // 98,901 總節點 (記憶體僅 1.51 MB) #define ACTIVE_VOXEL (10 * 99 * 99) // 10 層樓有效體素 #define PAD_OFFSET ((TOTAL_NODES - ACTIVE_VOXEL) / 2) // 核心演算:一維單指針「找鄰居 + 跳樓層」與零乘除位移加減 void step_3d_240fps(int64_t* restrict buf_a, int64_t* restrict buf_b, double* restrict gpu_out, int64_t sub_steps, int64_t S) { const int64_t s = 10; const int64_t bias = 1LL << (s - 1); const int64_t start = FLOOR_STRIDE + W_STRIDE + 1; const int64_t end = TOTAL_NODES - FLOOR_STRIDE - W_STRIDE - 1; int64_t* restrict curr = buf_a; int64_t* restrict nxt = buf_b; // 1. 留在整數世界狂算 (零浮點、零乘除) for (int64_t st = 0; st < sub_steps; ++st) { const int64_t* restrict ptr = curr + start; int64_t* restrict out = nxt + start; const int64_t* restrict lim = curr + end; while (ptr < lim) { int64_t c = *ptr; // 平面找鄰居 (±1, ±99) + 垂直跳樓層 (±9801) int64_t s_xy = (*(ptr - 1)) + (*(ptr + 1)) + (*(ptr - W_STRIDE)) + (*(ptr + W_STRIDE)); int64_t s_z = (*(ptr - FLOOR_STRIDE)) + (*(ptr + FLOOR_STRIDE)); // 權重與耦合常數 10 全數拆解為位移加法 int64_t l_3d = ((s_xy + s_z) << 2) - ((c << 4) + (c << 3)); int64_t d_sc = (l_3d << 3) + (l_3d << 1); int64_t mask = (d_sc >> 63); // 引理 B 無分支對稱捨入 *out = c + ((d_sc + bias + mask) >> s); ptr++; out++; } int64_t* restrict tmp = curr; curr = nxt; nxt = tmp; } // 2. 出場一次還原為浮點數,吐給顯卡渲染 const double inv = 1.0 / (double)(1ULL << S); #pragma omp parallel for schedule(static) for (int64_t i = 0; i < ACTIVE_VOXEL; ++i) { gpu_out[i] = (double)curr[PAD_OFFSET + i] * inv; } } static double now_ms(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (double)ts.tv_sec * 1000.0 + (double)ts.tv_nsec * 1e-6; } int main(void) { const int64_t S = 28, FRAMES = 480, SUB_STEPS = 4; const double BUDGET_MS = 1000.0 / 240.0; // 240 FPS 單幀上限 4.167 ms int64_t* buf_a = calloc(TOTAL_NODES, sizeof(int64_t)); int64_t* buf_b = calloc(TOTAL_NODES, sizeof(int64_t)); double* gpu_out = malloc(ACTIVE_VOXEL * sizeof(double)); // 入口一次封裝:在第 5 層中心注入能量量子 int64_t center = PAD_OFFSET + 5 * FLOOR_STRIDE + 49 * W_STRIDE + 49; buf_a[center] = buf_b[center] = 1000LL << S; // 預熱快取 step_3d_240fps(buf_a, buf_b, gpu_out, 2, S); double sum_ms = 0.0, max_ms = 0.0; for (int f = 0; f < FRAMES; ++f) { double t0 = now_ms(); step_3d_240fps(buf_a, buf_b, gpu_out, SUB_STEPS, S); double dt = now_ms() - t0; sum_ms += dt; if (dt > max_ms) max_ms = dt; } double avg_ms = sum_ms / FRAMES; printf("=== 3D 240 FPS 實測結果 (連續 %lld 幀) ===\n", (long long)FRAMES); printf("平均單幀耗時 : %.3f ms (僅佔 4.167 ms 預算 %.1f%%)\n", avg_ms, (avg_ms / BUDGET_MS) * 100.0); printf("最差單幀峰值 : %.3f ms\n", max_ms); free(buf_a); free(buf_b); free(gpu_out); return 0; }
愛心
74
留言
encourage first comment
有些話想說嗎 快分享出來彼此交流吧!
留言……