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

SCM Repository

[diderot] View of /tests/vec-tests/vec.h
ViewVC logotype

View of /tests/vec-tests/vec.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4640 - (download) (as text) (annotate)
Tue Sep 27 20:54:47 2016 UTC (2 years, 8 months ago) by glk
File size: 5888 byte(s)
initial result of svn export --username anonsvn --password=anonsvn https://svn.smlnj-gforge.cs.uchicago.edu/svn/diderot/branches/vis15/src/tests/
/*! \file vec.h
 *
 * \author John Reppy
 */

/*
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 */

#ifndef _VEC_H_
#define _VEC_H_

#include <ostream>
#include "raw-vectors.h"


/***** Representations of vectors using raw vector types *****/

struct rep2f {
    raw2f v0;
    rep2f (const raw2f &v) : v0(v) { }
    rep2f (float a, float b) : v0((raw2f){a, b}) { }
    rep2f (const float *m) : v0((raw2f){m[0], m[1]}) { }
    float operator[] (unsigned int const &i) const { return this->v0[i]; }
};

inline rep2f vec_add (rep2f const &a, rep2f const &b) { return rep2f(a.v0 + b.v0); }
inline rep2f vec_sub (rep2f const &a, rep2f const &b) { return rep2f(a.v0 - b.v0); }
inline rep2f vec_scale (float s, rep2f const &a) { return rep2f((raw2f){s, s} * a.v0); }
inline rep2f vec_mul (rep2f const &a, rep2f const &b) { return rep2f(a.v0 * b.v0); }
inline float vec_sum (rep2f const &a) { return a.v0[0] + a.v0[1]; }
inline void vec_store (float *m, rep2f const &a) { m[0] = a.v0[0]; m[1] = a.v0[1]; }

struct rep3f {
#if (MAX_VEC_WID >= 16)
    raw4f	v0;	// padded
    rep3f (const raw4f &v) : v0(v) { }
    rep3f (float a, float b, float c) : v0((raw4f){a, b, c, 0.0f}) { }
    rep3f (const float *m) : v0((raw4f){m[0], m[1], m[2], 0.0f}) { }
    float operator[] (unsigned int const &i) const { return this->v0[i]; }
#else // !(MAX_VEC_WID >= 16)
    raw2f	v0;
    float	v1;
    rep3f (const raw2f &x01, float x2) : v0(x01), v1(x2) { }
    rep3f (float a, float b, float c) : v0((raw2f){a, b}), v1(c) { }
    rep3f (const float *m) : v0((raw2f){m[0], m[1]}), v1(m[2]) { }
    float operator[] (unsigned int const &i) const
    {
	return (i < 2) ? this->v0[i] : this->v1;
    }
#endif
};

#if (MAX_VEC_WID >= 16)
inline rep3f vec_add (rep3f const &a, rep3f const &b) { return rep3f(a.v0 + b.v0); }
inline rep3f vec_sub (rep3f const &a, rep3f const &b) { return rep3f(a.v0 - b.v0); }
inline rep3f vec_scale (float s, rep3f const &a) { return rep3f((raw4f){s, s, s, s} * a.v0); }
inline rep3f vec_mul (rep3f const &a, rep3f const &b) { return rep3f(a.v0 * b.v0); }
inline float vec_sum (rep3f const &a) { return a.v0[0] + a.v0[1] + a.v0[2]; }
inline void vec_store (float *m, rep3f const &a) { m[0] = a.v0[0]; m[1] = a.v0[1]; m[2] = a.v0[2]; }
#else // !(MAX_VEC_WID >= 16)
inline rep3f vec_add (rep3f const &a, rep3f const &b) { return rep3f(a.v0 + b.v0, a.v1 + b.v1); }
inline rep3f vec_sub (rep3f const &a, rep3f const &b) { return rep3f(a.v0 - b.v0, a.v1 - b.v1); }
inline rep3f vec_scale (float s, rep3f const &a) { return rep3f((raw2f){s, s} * a.v0, s * a.v1); }
inline rep3f vec_mul (rep3f const &a, rep3f const &b) { return rep3f(a.v0 * b.v0, a.v1 * b.v1); }
inline float vec_sum (rep3f const &a) { return a.v0[0] + a.v0[1] + a.v1; }
inline void vec_store (float *m, rep3f const &a) { m[0] = a.v0[0]; m[1] = a.v0[1]; m[2] = a.v1; }
#endif


struct rep4f {
    raw4f	v0;
    rep4f (const raw4f &v) : v0(v) { }
    rep4f (float a, float b, float c, float d) : v0((raw4f){a, b, c, d}) { }
    rep4f (const float *m) : v0((raw4f){m[0], m[1], m[2], m[3]}) { }
    float operator[] (unsigned int const &i) const { return this->v0[i]; }
};

inline rep4f vec_add (rep4f const &a, rep4f const &b) { return rep4f(a.v0 + b.v0); }
inline rep4f vec_sub (rep4f const &a, rep4f const &b) { return rep4f(a.v0 - b.v0); }
inline rep4f vec_scale (float s, rep4f const &a) { return rep4f((raw4f){s, s, s, s} * a.v0); }
inline rep4f vec_mul (rep4f const &a, rep4f const &b) { return rep4f(a.v0 * b.v0); }
inline float vec_sum (rep4f const &a) { return a.v0[0] + a.v0[1] + a.v0[1] + a.v0[2]; }
inline void vec_store (float *m, rep4f const &a) {
    m[0] = a.v0[0]; m[1] = a.v0[1]; m[2] = a.v0[2]; m[3] = a.v0[3];
}


struct rep5f {
#if (MAX_VEC_WID >= 32)
    raw8f	v0;	// padded
#else
    raw4f	v0;
    float	v1;
#endif
};


struct rep6f {
#if (MAX_VEC_WID >= 32)
    raw8f	v0;	// padded
#else
    raw4f	v0;
    raw2f	v1;
#endif
};


struct rep7f {
    raw8f	v0;	// padded
};


struct rep8f {
    raw8f	v0;
};


/***** Generic vector type *****/

// T is element type, R is representation type, d is dimension
template <typename T, typename R, unsigned int d>
struct Vec
{
    R _rep;

    Vec (R const &r) : _rep(r) { }
    Vec (const T *mem) : _rep(mem) { };

    void store (T *mem) { vec_store(mem, this->_rep); }

    T operator[] (unsigned int const &i) const { return this->_rep[i]; }
    T sum () const;

    union View {
	T _arr[d];
	R _raw;
	View () { }
    };

};


/***** Specialized vector types *****/

typedef Vec<float,rep2f,2> vec2f;
typedef Vec<float,rep3f,3> vec3f;
typedef Vec<float,rep4f,4> vec4f;


/***** Generic vector operations *****/

template <typename T, typename R, unsigned int d>
inline T Vec<T,R,d>::sum () const
{
    return Vec<T,R,d>(vec_sum (this->_rep));
}

template <typename T, typename R, unsigned int d>
inline Vec<T,R,d> operator+ (Vec<T,R,d> const &a, Vec<T,R,d> const &b)
{
    return Vec<T,R,d>(vec_add (a._rep, b._rep));
}

template <typename T, typename R, unsigned int d>
inline Vec<T,R,d> operator- (Vec<T,R,d> const &a, Vec<T,R,d> const &b)
{
    return Vec<T,R,d>(vec_sub (a._rep, b._rep));
}

template <typename T, typename R, unsigned int d>
inline Vec<T,R,d> scale (T s, Vec<T,R,d> const &a)
{
    return Vec<T,R,d>(vec_scale (s, a));
}

template <typename T, typename R, unsigned int d>
inline Vec<T,R,d> operator* (Vec<T,R,d> const &a, Vec<T,R,d> const &b)
{
    return Vec<T,R,d>(vec_mul (a._rep, b._rep));
}

template <typename T, typename R, unsigned int d>
std::ostream& operator<< (std::ostream& s, Vec<T,R,d> const &v)
{
    union Vec<T,R,d>::View view;
    view._raw = v._rep;
    s << "[" << view._arr[0];
    for (int i = 1;  i < d;  i++) {
	s << "," << view._arr[i];
    }
    s << "]";
    return s;
}

#endif // !_VEC_H_

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