Skip to content

Commit

Permalink
Experimental WASM support
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Dec 7, 2021
1 parent c90e5a5 commit dd5d731
Show file tree
Hide file tree
Showing 10 changed files with 7,046 additions and 11,034 deletions.
40 changes: 36 additions & 4 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,40 @@
#include <float.h> //FLT_EPSILON
#include <limits.h>
#include <math.h>
#include <nifti2_io.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef __aarch64__
#include "arm_malloc.h"
#include "arm_malloc.h"
#else
#include <immintrin.h>
#include <immintrin.h>
#endif

#ifdef USING_WASM
#define WASM_EXPORT(name) \
__attribute__((export_name(#name))) \
name

// Pull these in from walloc.c.

void* WASM_EXPORT(walloc)(size_t size) {
return xmalloc(size);
}

void WASM_EXPORT(wfree)(void* ptr) {
xfree(ptr);
}

void xmemcpy ( void * destination, const void * source, size_t num ) {
uint8_t * s = (uint8_t*) source;
uint8_t * d = (uint8_t*) destination;
for (size_t i = 0; i < num; i++)
d[i] = s[i];
}
#define staticx
#include <nifti2_wasm.h>
#else
#define xmemcpy memcpy
#include <nifti2_io.h>
#endif

#ifndef M_PI
Expand Down Expand Up @@ -107,6 +134,7 @@ int nii_otsu(int* H, int nBin, int mode) {
return thresh;
}

#ifndef USING_WASM
int nifti_save(nifti_image *nim, const char *postfix) {
char extnii[5] = ".nii"; /* modifiable, for possible uppercase */
char exthdr[5] = ".hdr";
Expand Down Expand Up @@ -251,7 +279,7 @@ nifti_image *nifti_image_read2(const char *hname, int read_data) {
nim->intent_name[15] = '\0';
return nim;
}

#endif //not USING_WASM
vec4 setVec4(float x, float y, float z) {
vec4 v = {{x, y, z, 1}};
return v;
Expand All @@ -275,6 +303,7 @@ float vertexDisplacement(float x, float y, float z, mat44 m, mat44 m2) {
return sqrt(sqr(pos.v[0] - pos2.v[0]));
}

#ifndef USING_WASM
float max_displacement_mm(nifti_image *nim, nifti_image *nim2) {
//examines each corner of two NIfTI images and returns the max difference in vertex location
// used to detect if two volumes are aligned
Expand All @@ -290,6 +319,7 @@ float max_displacement_mm(nifti_image *nim, nifti_image *nim2) {
mx = MAX(mx, vertexDisplacement(0, 0, nim->nz - 1, m, m2));
return mx;
}
#endif

in_hdr set_input_hdr(nifti_image *nim) {
//remember input datatype, slope and intercept in case user saves back to this
Expand All @@ -300,6 +330,7 @@ in_hdr set_input_hdr(nifti_image *nim) {
return ihdr;
}

#ifndef USING_WASM
int nifti_image_change_datatype(nifti_image *nim, int dt, in_hdr *ihdr) {
//returns -1 on failure, 0 if okay
if (nim->datatype == dt)
Expand Down Expand Up @@ -664,6 +695,7 @@ int *make_kernel_file(nifti_image *nim, int *nkernel, char *fin) {
nifti_image_free(nim2);
return kernel;
} //make_kernel_file()
#endif //not USING_WASM

int *make_kernel_sphere(nifti_image *nim, int *nkernel, double mm) {
// sphere of radius <size> mm centered on target voxel
Expand Down
25 changes: 16 additions & 9 deletions src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ extern "C" {
#include <limits.h>


#ifdef __EMSCRIPTEN__
#define _mm_malloc(size, alignment) malloc(size)
#define _mm_free(ptr) free(ptr)
#ifdef USING_WASM
void *xmalloc(size_t size);
void xfree(void *p);
#define _mm_malloc(size, alignment) xmalloc(size)
#define _mm_free(ptr) xfree(ptr)
#endif

//CORE32 and CORE64 handle Float32 and Float64 operations, CORE handles shared code
Expand Down Expand Up @@ -126,29 +128,34 @@ typedef struct { /** x4 vector struct **/
float v[4] ;
} vec4 ;

#ifndef USING_WASM
#ifdef USING_WASM
void xmemcpy ( void * destination, const void * source, size_t num );
#else
int nifti_save(nifti_image * nim, const char *postfix);
nifti_image *nifti_image_read2( const char *hname , int read_data );
int * make_kernel_file(nifti_image * nim, int * nkernel, char * fin);
mat44 xform(nifti_image * nim);
int nifti_image_change_datatype ( nifti_image * nim, int dt , in_hdr * ihdr);
float max_displacement_mm( nifti_image * nim, nifti_image * nim2);
#endif
vec4 setVec4(float x, float y, float z);
vec4 nifti_vect44mat44_mul(vec4 v, mat44 m );
mat44 xform(nifti_image * nim);
int neg_determ(nifti_image * nim);
int nii_otsu(int* H, int nBin, int mode);
float max_displacement_mm( nifti_image * nim, nifti_image * nim2);
float vertexDisplacement(float x, float y, float z, mat44 m, mat44 m2);
in_hdr set_input_hdr(nifti_image * nim);
int nifti_image_change_datatype ( nifti_image * nim, int dt , in_hdr * ihdr);
int * make_kernel(nifti_image * nim, int * nkernel, int x, int y, int z);
int * make_kernel_sphere(nifti_image * nim, int * nkernel, double mm);
//CLIST * createFilter(int srcXsize, int dstXsize, double (*filterf)(), double fwidth);
CLIST * createFilter(int srcXsize, int dstXsize, int filterMethod);
CLIST * createFilter(int srcXsize, int dstXsize, int filterMethod);
double qginv( double p );
double qg( double x );

#ifdef USING_WASM
//#define printfx(...) printf(__VA_ARGS__)
#define printfx(...)
#else
#define printfx(...) fprintf(stderr, __VA_ARGS__)
#endif

#ifndef MAX //from Christian Gaser's TFCE example
#define MAX(A,B) ((A) > (B) ? (A) : (B))
Expand Down
Loading

0 comments on commit dd5d731

Please sign in to comment.