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/cl-inline-matrix.h
ViewVC logotype

View of /branches/pure-cfg/src/include/Diderot/cl-inline-matrix.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1290 - (download) (as text) (annotate)
Tue Jun 7 16:22:05 2011 UTC (10 years, 3 months ago) by jhr
File size: 5765 byte(s)
Editing CL header files
/*! \file inline-matrix.h
 *
 * \author John Reppy & Lamont Samuels
 */

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

#ifndef _DIDEROT_CL_INLINE_MATRIX_H_
#define _DIDEROT_CL_INLINE_MATRIX_H_

#ifndef _DIDEROT_CL_TYPES_H_
#include "cl-types.h"
#endif

/********** 2x2 matrix functions **********/

STATIC_INLINE void zero2x2f (Diderot_Mat2x2_t dst)
{
    dst[0] = (float2)(0.0, 0.0);
    dst[1] = (float2)(0.0, 0.0);
}

STATIC_INLINE void identity2x2f (Diderot_Mat2x2_t dst)
{
    dst[0] = (float2)(1.0, 0.0);
    dst[1] = (float2)(0.0, 1.0);
}

/*STATIC_INLINE float2 column2x2f (Diderot_Mat2x2_t m, int i)
{
    return (float2)(m[0].s[i], m[1].r[i]);
}*/

STATIC_INLINE void copy2x2f (Diderot_Mat2x2_t dst, Diderot_Mat2x2_t src)
{
    dst[0] = src[0];
    dst[1] = src[1];
}

STATIC_INLINE void scale2x2f (Diderot_Mat2x2_t dst, float s, Diderot_Mat2x2_t src)
{
    float2 scale = float2(s, s);
    dst[0] = scale * src[0];
    dst[1] = scale * src[1];
}

STATIC_INLINE void add2x2f (Diderot_Mat2x2_t dst, Diderot_Mat2x2_t a, Diderot_Mat2x2_t b)
{
    dst[0] = a[0] + b[0];
    dst[1] = a[1] + b[1];
}

STATIC_INLINE void sub2x2f (Diderot_Mat2x2_t dst, Diderot_Mat2x2_t a, Diderot_Mat2x2_t b)
{
    dst[0] = a[0] - b[0];
    dst[1] = a[1] - b[1];
}

STATIC_INLINE float2 mulVec3Mat2x2f (float2 v, Diderot_Mat2x2_t m)
{
    return (float2)(
	dot(v, column2x2f(m, 0)),
	dot(v, column2x2f(m, 1)));
}

STATIC_INLINE float2 mulMat2x2Vec2f (Diderot_Mat2x2_t m, float2 v)
{
    return (float2)(dot(m[0], v), dot(m[1], v));
}

STATIC_INLINE void mulMat2x2Mat2x2f (Diderot_Mat2x2_t dst, Diderot_Mat2x2_t m1, Diderot_Mat2x2_t m2)
{
    dst[0] = vec2f(
	dot(m1[0].v, column2x2f(m2, 0)),
	dot(m1[0].v, column2x2f(m2, 1)));
    dst[1] = vec2f(
	dot(m1[1].v, column2x2f(m2, 0)),
	dot(m1[1].v, column2x2f(m2, 1)));
}

STATIC_INLINE void transpose2x2f (Diderot_Mat2x2_t dst, Diderot_Mat2x2_t src)
{
    dst[0] = column2x2f(src, 0);
    dst[1] = column2x2f(src, 1);
}

STATIC_INLINE float trace2x2f (Diderot_Mat2x2_t m)
{
    return m[0].r[0] + m[1].r[1];
}

// The Frobenius norm of a matrix is the sqrt of the sum of the squares of the elements
STATIC_INLINE float norm2x2f (Diderot_Mat2x2_t m)
{
    return sqrtf(dot(m[0],m[0]) + dot(m[1],m[1]));
}


/********** 3x3 matrix functions **********/

STATIC_INLINE void zero3x3f (Diderot_Mat3x3_t dst)
{
    dst[0] = (float3)(0.0, 0.0, 0.0);
    dst[1] = (float3)(0.0, 0.0, 0.0);
    dst[2] = (float3)(0.0, 0.0, 0.0);
}

STATIC_INLINE void identity3x3f (Diderot_Mat3x3_t dst)
{
    dst[0] = (float3)(1.0, 0.0, 0.0);
    dst[1] = (float3)(0.0, 1.0, 0.0);
    dst[2] = (float3)(0.0, 0.0, 1.0);
}

STATIC_INLINE float3 column3x3f (Diderot_Mat3x3_t m, int i)
{
    return (float3)(m[0].r[i], m[1].r[i], m[2].r[i]);
}

STATIC_INLINE void copy3x3f (Diderot_Mat3x3_t dst, Diderot_Mat3x3_t src)
{
    dst[0] = src[0];
    dst[1] = src[1];
    dst[2] = src[2];
}

STATIC_INLINE void scale3x3f (Diderot_Mat3x3_t dst, float s, Diderot_Mat3x3_t src)
{
    float3 scale = vec3f(s, s, s);
    dst[0] = scale * src[0];
    dst[1] = scale * src[1];
    dst[2] = scale * src[2];
}

STATIC_INLINE void add3x3f (Diderot_Mat3x3_t dst, Diderot_Mat3x3_t a, Diderot_Mat3x3_t b)
{
    dst[0] = a[0].v + b[0].v;
    dst[1] = a[1].v + b[1].v;
    dst[2] = a[2].v + b[2].v;
}

STATIC_INLINE void sub3x3f (Diderot_Mat3x3_t dst, Diderot_Mat3x3_t a, Diderot_Mat3x3_t b)
{
    dst[0] = a[0].v - b[0].v;
    dst[1] = a[1].v - b[1].v;
    dst[2] = a[2].v - b[2].v;
}

STATIC_INLINE float3 mulVec3Mat3x3f (float3 v, Diderot_Mat3x3_t m)
{
    return (float3)(
	dot(v, column3x3f(m, 0)),
	dot(v, column3x3f(m, 1)),
	dot(v, column3x3f(m, 2)));
}

STATIC_INLINE float3 mulMat3x3Vec3f (Diderot_Mat3x3_t m, float3 v)
{
    return (float3)(dot(m[0], v), dof(m[1], v), dot(m[2], v));
}

STATIC_INLINE void mulMat3x3Mat3x3f (Diderot_Mat3x3_t dst, Diderot_Mat3x3_t m1, Diderot_Mat3x3_t m2)
{
    dst[0] = (float3)(
	dot(m1[0], column3x3f(m2, 0)),
	dot(m1[0], column3x3f(m2, 1)),
	dot(m1[0], column3x3f(m2, 2)));
    dst[1] = (float3)(
	dot(m1[1], column3x3f(m2, 0)),
	dot(m1[1], column3x3f(m2, 1)),
	dot(m1[1], column3x3f(m2, 2)));
    dst[2] = (float3)(
	dot(m1[2], column3x3f(m2, 0)),
	dot(m1[2], column3x3f(m2, 1)),
	dot(m1[2], column3x3f(m2, 2)));
}

STATIC_INLINE void transpose3x3f (Diderot_Mat3x3_t dst, Diderot_Mat3x3_t src)
{
    dst[0] = column3x3f(src, 0);
    dst[1] = column3x3f(src, 1);
    dst[2] = column3x3f(src, 2);
}

STATIC_INLINE float trace3x3f (Diderot_Mat3x3_t m)
{
    return m[0] + m[1] + m[2];
}

// The Frobenius norm of a matrix is the sqrt of the sum of the squares of the elements
STATIC_INLINE float norm3x3f (Diderot_Mat3x3_t m)
{
    return sqrtf(dot(m[0],m[0]) + dot(m[1],m[1]) + dot(m[2],m[2]));
}


/********** 4x4 matrix functions **********/

STATIC_INLINE void copy4x4f (Diderot_Mat4x4_t dst, Diderot_Mat4x4_t src)
{
    dst[0] = src[0];
    dst[1] = src[1];
    dst[2] = src[2];
    dst[3] = src[3];
}

STATIC_INLINE float4 column4x4f (Diderot_Mat4x4_t m, int i)
{
    return (float4)(((union4f_t)m[0]).r[i], ((union4f_t)m[1]).r[i], ((union4f_t)m[2]).r[i], ((union4f_t)m[3]).r[i]);
}

STATIC_INLINE void transpose4x4f (Diderot_Mat4x4_t dst, Diderot_Mat4x4_t src)
{
    dst[0] = column4x4f(src, 0);
    dst[1] = column4x4f(src, 1);
    dst[2] = column4x4f(src, 2);
    dst[3] = column4x4f(src, 3);
}

STATIC_INLINE float trace4x4f (Diderot_Mat4x4_t m)
{
    return m[0] + m[1] + m[2] + m[3];
}

// The Frobenius norm of a matrix is the sqrt of the sum of the squares of the elements
STATIC_INLINE float norm4x4f (Diderot_Mat4x4_t m)
{
    return sqrt(dot(m[0],m[0]) + dot(m[1],m[1]) + dot(m[2],m[2]) + dot(m[3],m[3]));
}

#endif /* !_DIDEROT_CL_INLINE_MATRIX_H_ */

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