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

SCM Repository

[diderot] View of /branches/staging/src/include/Diderot/inline-vec4.h
ViewVC logotype

View of /branches/staging/src/include/Diderot/inline-vec4.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2635 - (download) (as text) (annotate)
Mon May 26 14:06:39 2014 UTC (5 years, 3 months ago) by jhr
File size: 2658 byte(s)
  Merging bug fixes and other changes from vis12 branch into staging branch
/*! \file inline-vec4.h
 *
 * \author John Reppy
 *
 * \brief inline functions for 4D vectors
 */

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

#ifndef _DIDEROT_INLINE_VEC4_H_
#define _DIDEROT_INLINE_VEC4_H_

#ifndef _DIDEROT_TYPES_H_
#include "Diderot/types.h"
#endif

STATIC_INLINE Diderot_vec4_t vec4 (Diderot_real_t a, Diderot_real_t b, Diderot_real_t c, Diderot_real_t d)
{
    return __extension__ (Diderot_vec4_t){ a, b, c, d };
}

STATIC_INLINE Diderot_vec4_t loadVec4 (void *vp)
{
    Diderot_real_t *p = (Diderot_real_t *)vp;
    return vec4(p[0], p[1], p[2], p[3]);
}

STATIC_INLINE Diderot_ivec4_t vec4rtoi (Diderot_vec4_t v)
{
    Diderot_union4_t u;
    u.v = v;
    return __extension__ (Diderot_ivec4_t){
            (Diderot_int_t)u.r[0],
            (Diderot_int_t)u.r[1],
            (Diderot_int_t)u.r[2],
            (Diderot_int_t)u.r[3]
        };
}

STATIC_INLINE Diderot_vec4_t vec4itor (Diderot_ivec4_t v)
{
    union4i_t u;
    u.v = v;
    return __extension__ (Diderot_vec4_t){ (Diderot_real_t)u.i[0], (Diderot_real_t)u.i[1], (Diderot_real_t)u.i[2], (Diderot_real_t)u.i[3] };
}

STATIC_INLINE Diderot_vec4_t scale4 (Diderot_real_t s, Diderot_vec4_t v)
{
    return vec4(s, s, s, s) * v;
}

STATIC_INLINE Diderot_vec4_t clamp4 (Diderot_vec4_t lo, Diderot_vec4_t hi, Diderot_vec4_t v)
{
  // FIXME: there is probably a vectorized way to compute this
    Diderot_union4_t a, b, c;
    a.v = lo; b.v = hi; c.v = v;
    return vec4(
	clamp(a.r[0], b.r[0], c.r[0]),
	clamp(a.r[1], b.r[1], c.r[1]),
	clamp(a.r[2], b.r[2], c.r[2]),
	clamp(a.r[3], b.r[3], c.r[3]));
}

STATIC_INLINE Diderot_vec4_t lerp4 (Diderot_vec4_t a, Diderot_vec4_t b, Diderot_real_t t)
{
    return a + scale4(t, b - a);
}

STATIC_INLINE Diderot_vec4_t floor4 (Diderot_vec4_t v)
{
    Diderot_union4_t u;
    u.v = v;
    return vec4(FLOOR(u.r[0]), FLOOR(u.r[1]), FLOOR(u.r[2]), FLOOR(u.r[3]));
}

STATIC_INLINE Diderot_ivec4_t truncToInt4 (Diderot_vec4_t v)
{
    Diderot_union4_t t;
    t.v = v;
    return __extension__ (Diderot_ivec4_t){
	(Diderot_int_t)TRUNC(t.r[0]),
	(Diderot_int_t)TRUNC(t.r[1]),
	(Diderot_int_t)TRUNC(t.r[2]),
	(Diderot_int_t)TRUNC(t.r[3]) };
}

STATIC_INLINE Diderot_real_t dot4 (Diderot_vec4_t u, Diderot_vec4_t v)
{
    Diderot_union4_t uv = __extension__ (Diderot_union4_t)(u*v);
    return uv.r[0] + uv.r[1] + uv.r[2] + uv.r[3];
}

STATIC_INLINE Diderot_real_t length4 (Diderot_vec4_t v)
{
    return SQRT(dot4(v, v));
}

STATIC_INLINE Diderot_vec4_t normalize4 (Diderot_vec4_t v)
{
    return scale4(1.0 / length4(v), v);
}

#endif /* !_DIDEROT_INLINE_VEC4_H_ */

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