commit 32d0e08281f6cf6e9789a57fcf785095787f9322
parent 8130652542ad730322d8e663b96a5f78547eefc1
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 7 Sep 2022 09:37:17 +0200
Update the hash computation: use sha256_ctx API
Diffstat:
3 files changed, 22 insertions(+), 93 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -25,7 +25,7 @@ option(NO_TEST "Do not build tests" OFF)
################################################################################
find_package(Embree 3.6 REQUIRED)
find_package(RCMake 0.4 REQUIRED)
-find_package(RSys 0.12 REQUIRED)
+find_package(RSys 0.13 REQUIRED)
if(NOT NO_TEST)
find_package(StarMesh)
endif()
@@ -91,7 +91,7 @@ if(NOT NO_TEST)
${SUVM_SOURCE_DIR}/test_suvm_utils.h)
target_link_libraries(${_name} suvm RSys ${ARGN})
endfunction()
-
+
function(new_test _name)
build_test(${_name} ${ARGN})
add_test(${_name} ${_name})
diff --git a/src/suvm_volume.c b/src/suvm_volume.c
@@ -23,18 +23,6 @@
#include <rsys/dynamic_array_size_t.h>
#include <rsys/ref_count.h>
-struct hash_context {
- struct cpnt {
- const void* mem;
- size_t size; /* Size in bytes */
- size_t chunk_min; /* Inclusive */
- size_t chunk_max; /* Exclusive */
- } cpnts[4];
- int ncpnts;
- int icpnt;
-};
-static const struct hash_context HASH_CONTEXT_NULL;
-
/* Generate the dynamic array of RTCBuildPrimitive */
#define DARRAY_NAME rtc_prim
#define DARRAY_DATA struct RTCBuildPrimitive
@@ -521,40 +509,6 @@ error:
}
static void
-get_chunk(char dst[64], const size_t ichunk, void* context)
-{
- struct hash_context* ctx = context;
- const struct cpnt* cpnt = NULL;
- const char* chunk = NULL;
- size_t cpnt_offset = 0;
- size_t chunk_sz = 0;
- ASSERT(dst && ctx && ctx->icpnt < 4);
-
- /* Fetch the component to handle */
- cpnt = ctx->cpnts + ctx->icpnt;
- ASSERT(cpnt->chunk_min <= ichunk);
- ASSERT(cpnt->chunk_max > ichunk);
-
- /* Compute the offset into the component up to the data to copy */
- cpnt_offset = (ichunk - cpnt->chunk_min) * 64;
-
- /* Fetch the address toward the data to copy and compute the data size */
- chunk = (const char*)cpnt->mem + cpnt_offset;
- chunk_sz = MMIN(cpnt->size - cpnt_offset, 64);
-
- /* Copy the component data toward the chunk */
- memcpy(dst, chunk, chunk_sz);
-
- /* Clean up the chunk from the last written byte to the end of the chunk */
- if(chunk_sz < 64) memset(dst + chunk_sz, 0, 64 - chunk_sz);
-
- /* Progress into the component */
- while(ichunk == ctx->cpnts[ctx->icpnt].chunk_max - 1 && ctx->icpnt < 4) {
- ++ctx->icpnt;
- }
-}
-
-static void
volume_release(ref_T* ref)
{
struct suvm_volume* vol = NULL;
@@ -701,10 +655,7 @@ suvm_volume_compute_hash
const int cpnt_mask,
hash256_T hash)
{
- struct chunked_data_desc chunked_data = CHUNKED_DATA_DESC_NULL;
- struct hash_context ctx = HASH_CONTEXT_NULL;
- size_t sz = 0;
- int i = 0;
+ struct sha256_ctx ctx;
res_T res = RES_OK;
if(!vol || !hash) {
@@ -712,50 +663,28 @@ suvm_volume_compute_hash
goto error;
}
- /* Setup the components to hash */
+ sha256_ctx_init(&ctx);
+
if(cpnt_mask & SUVM_POSITIONS) {
- ctx.cpnts[i].mem = darray_float_cdata_get(&vol->positions);
- ctx.cpnts[i].size = darray_float_size_get(&vol->positions) * sizeof(float);
- ++i;
+ const float* pos = darray_float_cdata_get(&vol->positions);
+ const size_t n = darray_float_size_get(&vol->positions);
+ sha256_ctx_update(&ctx, (const char*)pos, sizeof(*pos)*n);
}
if(cpnt_mask & SUVM_INDICES) {
- ctx.cpnts[i].mem = darray_u32_cdata_get(&vol->indices);
- ctx.cpnts[i].size = darray_u32_size_get(&vol->indices) * sizeof(uint32_t);
- ++i;
+ const uint32_t* ids = darray_u32_cdata_get(&vol->indices);
+ const size_t n = darray_u32_size_get(&vol->indices);
+ sha256_ctx_update(&ctx, (const char*)ids, sizeof(*ids)*n);
}
if(cpnt_mask & SUVM_PRIMITIVE_DATA) {
- ctx.cpnts[i].mem = vol->prim_data.mem;
- ctx.cpnts[i].size = vol->prim_data.size * vol->prim_data.elmt_stride;
- ++i;
+ const size_t sz = vol->prim_data.size * vol->prim_data.elmt_stride;
+ sha256_ctx_update(&ctx, vol->prim_data.mem, sz);
}
if(cpnt_mask & SUVM_VERTEX_DATA) {
- ctx.cpnts[i].mem = vol->vert_data.mem;
- ctx.cpnts[i].size = vol->vert_data.size * vol->vert_data.elmt_stride;
- ++i;
+ const size_t sz = vol->vert_data.size * vol->vert_data.elmt_stride;
+ sha256_ctx_update(&ctx, vol->vert_data.mem, sz);
}
- ctx.ncpnts = i;
-
- /* Precompute the range of chunks overlapped by each component */
- FOR_EACH(i, 0, ctx.ncpnts) {
- const size_t nchunks = (ctx.cpnts[i].size + 63/*ceil*/) / 64;
- sz += nchunks * 64;
- if(i == 0) {
- ctx.cpnts[i].chunk_min = 0;
- ctx.cpnts[i].chunk_max = nchunks;
- } else {
- ctx.cpnts[i].chunk_min = ctx.cpnts[i-1].chunk_max;
- ctx.cpnts[i].chunk_max = ctx.cpnts[i-1].chunk_max + nchunks;
- }
- }
-
- /* Setup the index of the 1st component to hash */
- ctx.icpnt = 0;
- /* Hash the volume data */
- chunked_data.get_chunk512 = get_chunk;
- chunked_data.size = sz;
- chunked_data.context = &ctx;
- hash_sha256_chunked_data(&chunked_data, hash);
+ sha256_ctx_finalize(&ctx, hash);
exit:
return res;
diff --git a/src/test_suvm_volume.c b/src/test_suvm_volume.c
@@ -517,6 +517,7 @@ check_hash
const int has_prim_data,
const int has_vert_data)
{
+ struct suvm_mesh_desc msh_desc;
void* mem = NULL;
float* pos = NULL;
uint32_t* ids = NULL;
@@ -536,15 +537,14 @@ check_hash
hash_sha256(NULL, 0, hash1);
CHK(hash256_eq(hash0, hash1));
- /* Compute data size to hash. Note that SUVM align the data to hash on 64
- * bytes by padding data with null bytes if necessary */
- sz_pos = ALIGN_SIZE(msh->nvertices*sizeof(float[3]), 64u);
- sz_ids = ALIGN_SIZE(msh->ntetrahedra*sizeof(uint32_t[4]), 64u);
+ /* Compute data size to hash */
+ sz_pos = msh->nvertices*sizeof(float[3]);
+ sz_ids = msh->ntetrahedra*sizeof(uint32_t[4]);
if(has_prim_data) {
- sz_prims = ALIGN_SIZE(msh->ntetrahedra*sizeof(size_t[4]), 64u);
+ sz_prims = msh->ntetrahedra*sizeof(size_t[4]);
}
if(has_vert_data) {
- sz_verts = ALIGN_SIZE(msh->nvertices*sizeof(double[3]), 64u);
+ sz_verts = msh->nvertices*sizeof(double[3]);
}
mem = mem_calloc(1, sz_pos + sz_ids + sz_prims + sz_verts);
CHK(mem != NULL);