Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/pure-cfg/src/include/Diderot/diderot.h
ViewVC logotype

View of /branches/pure-cfg/src/include/Diderot/diderot.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 561 - (download) (as text) (annotate)
Wed Feb 23 00:31:32 2011 UTC (8 years, 9 months ago) by jhr
File size: 6262 byte(s)
  Working on C target and code generation
/*! \file diderot.h
 *
 * \author John Reppy
 *
 * This is the interface to the Diderot runtime for the C target.  For now,
 * we are targetting single-precision computations.
 */

/*
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 */

#ifndef _DIDEROT_H_
#define _DIDEROT_H_

#define STATIC_INLINE	static inline

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>

/* library-call status */
typedef enum { DIDEROT_OK = 0, DIDEROT_FAIL = -1 } Status_t;

/* SSE vector types */
typedef float __m128 __attribute__ ((__vector_size__ (16), __may_alias__));
typedef float vec2f_t __attribute__ ((vector_size (8)));
typedef float vec4f_t __attribute__ ((vector_size (16)));
typedef float vec8f_t __attribute__ ((vector_size (32)));

typedef int32_t vec2i_t __attribute__ ((vector_size (8)));
typedef int32_t vec4i_t __attribute__ ((vector_size (16)));
typedef int32_t vec8i_t __attribute__ ((vector_size (32)));

typedef float vec2d_t __attribute__ ((vector_size (16)));
typedef float vec4d_t __attribute__ ((vector_size (32)));
typedef float vec8d_t __attribute__ ((vector_size (64)));

typedef int64_t vec2l_t __attribute__ ((vector_size (16)));
typedef int64_t vec4l_t __attribute__ ((vector_size (32)));
typedef int64_t vec8l_t __attribute__ ((vector_size (64)));

typedef union { float r[2]; vec2f_t v; } union2f_t;
typedef union { float r[4]; vec4f_t v; } union4f_t;
typedef union { float r[8]; vec8f_t v; } union8f_t;

typedef union { int32_t i[2]; vec2i_t v; } union2i_t;
typedef union { int32_t i[4]; vec4i_t v; } union4i_t;
typedef union { int32_t i[8]; vec8i_t v; } union8i_t;

typedef union { double r[2]; vec2d_t v; } union2d_t;
typedef union { double r[4]; vec4d_t v; } union4d_t;
typedef union { double r[8]; vec8d_t v; } union8d_t;

typedef union { int64_t r[2]; vec2l_t v; } union2l_t;
typedef union { int64_t r[4]; vec4l_t v; } union4l_t;
typedef union { int64_t r[8]; vec8l_t v; } union8l_t;

/* vector lengths must be power of 2, but vec3 is useful, so we pad to 4 */
typedef vec4f_t vec3f_t;
typedef union4f_t union3f_t;
typedef vec4d_t vec3d_t;
typedef union4d_t union3d_t;
typedef vec4i_t vec3i_t;
typedef union4i_t union3i_t;
typedef vec4l_t vec3l_t;
typedef union4l_t union3l_t;

/* typedefs for Diderot types */
#if defined(DIDEROT_SINGLE_PRECISION)
typedef int32_t Diderot_int_t;
typedef float Diderot_real_t;
typedef union4f_t Diderot_vec3_t;	// padded to fit in SSE register
typedef union4f_t Diderot_vec4_t;
#else
typedef int64_t Diderot_int_t;
typedef double Diderot_real_t;
typedef union4d_t Diderot_vec3_t;	// padded to fit in SSE register
typedef union4d_t Diderot_vec4_t;
#endif
typedef const char *Diderot_string_t;

typedef Diderot_real_t Diderot_Mat2x2_t[4];
typedef Diderot_real_t Diderot_Mat3x3_t[9];
typedef Diderot_real_t Diderot_Mat4x4_t[16];

typedef struct {		// wrapper for 1D image data
    uint32_t	dim;		// dimension (== 1)
    uint32_t	size[1];
    void	*data;
    Diderot_Mat2x2_t m;		// image to world-space transform
    Diderot_Mat2x2_t mInv;	// world to image-space transform (m inverse)
} Diderot_image1D_t;

typedef struct {		// wrapper for 2D image data
    uint32_t	dim;		// dimension (== 2)
    uint32_t	size[2];	// sizes (fast to slow)
    void	*data;
    Diderot_Mat3x3_t m;		// image to world-space transform
    Diderot_Mat3x3_t mInv;	// world to image-space transform (m inverse)
    Diderot_Mat3x3_t mInvT;	// image to world-space transform for gradients
				// (m inverse transpose)
} Diderot_image2D_t;

typedef struct {		// wrapper for 3D image data
    uint32_t	dim;		// dimension (== 3)
    uint32_t	size[3];	// sizes (fast to slow)
    void	*data;
    Diderot_Mat4x4_t m;		// image to world-space transform
    Diderot_Mat4x4_t mInv;	// world to image-space transform (m inverse)
    Diderot_Mat4x4_t mInvT;	// image to world-space transform for gradients
				// (m inverse transpose)
} Diderot_image3D_t;

/* Diderot library functions */

/* load image data from Nrrd files */
extern Status_t Diderot_LoadImage1D (Diderot_string_t name, Diderot_image1D_t **img);
extern Status_t Diderot_LoadImage2D (Diderot_string_t name, Diderot_image2D_t **img);
extern Status_t Diderot_LoadImage3D (Diderot_string_t name, Diderot_image3D_t **img);

/* functions to get input-parameter values */
extern Status_t Diderot_InputString (const char *, const char **, bool);
extern Status_t Diderot_Inputf (const char *, float *, bool);
extern Status_t Diderot_InputVec3f (const char *, vec3f_t *, bool);

/********** scalar math functions **********/

STATIC_INLINE float maxf (float a, float b)
{
    return (a < b)? b : a;
}

/********** 3-element vector functions **********/

STATIC_INLINE vec3f_t vec3f (float a, float b, float c)
{
    return __extension__ (vec4f_t){ a, b, c, 0.0f };
}

STATIC_INLINE vec3f_t scale3f (float s, vec3f_t v)
{
    return vec3f(s, s, s) * v;
}

STATIC_INLINE vec3f_t floor3f (vec3f_t v)
{
    union4f_t u;
    u.v = v;
    return vec3f(floorf(u.r[0]), floorf(u.r[1]), floorf(u.r[2]));
}

STATIC_INLINE float length3f (vec3f_t v)
{
    return 0.0; // FIXME
}

STATIC_INLINE vec3i_t truncToInt3f (vec3f_t v)
{
    union4f_t t;
    t.v = v;
    return __extension__ (vec4i_t){
	(int32_t)truncf(t.r[0]),
	(int32_t)truncf(t.r[1]),
	(int32_t)truncf(t.r[2]),
	0 };
}


/********** 4-element vector functions **********/

STATIC_INLINE vec4f_t vec4f (float a, float b, float c, float d)
{
    return __extension__ (vec4f_t){ a, b, c, d };
}

STATIC_INLINE float dot4f (vec4f_t u, vec4f_t v)
{
    return 0.0f; // FIXME
}


/********** other Diderot support functions **********/

// check if pos is inside the img, assuming that we have a border of width s.
//
STATIC_INLINE bool Diderot_Inside3f (vec3f_t posArg, Diderot_image3D_t *img, int s)
{
// NOTE: there might be a vectorized way to do this compare!
// cvtps2pi -- converts vector of floats to vector of int32_t values

    union4f_t pos;
    pos.v = posArg;
    return ((s <= pos.r[0]) && (pos.r[0] < (img->size[0] - s))
	&&  (s <= pos.r[1]) && (pos.r[1] < (img->size[1] - s))
	&&  (s <= pos.r[2]) && (pos.r[2] < (img->size[2] - s)));
}

vec3f_t Diderot_ToImageSpace3f (Diderot_image3D_t *img, vec3f_t posArg);

#endif /* !_DIDEROT_H_ */

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0