source: libcfa/src/vec/vec.hfa @ 417e8ea

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 417e8ea was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[eef8dfb]1//
2// Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// io/types.hfa --
8//
9// Author           : Dimitry Kobets
10// Created On       :
11// Last Modified By :
12// Last Modified On :
13// Update Count     :
14//
15
[3376ec9]16#pragma once
17
18#include <math.hfa>
19
[fd54fef]20trait fromint(T) {
[3376ec9]21    void ?{}(T&, int);
22};
[fd54fef]23trait zeroinit(T) {
[3376ec9]24    void ?{}(T&, zero_t);
25};
[fd54fef]26trait zero_assign(T) {
[3376ec9]27    T ?=?(T&, zero_t);
28};
[fd54fef]29trait subtract(T) {
[3376ec9]30    T ?-?(T, T);
31};
[fd54fef]32trait negate(T) {
[3376ec9]33    T -?(T);
34};
[fd54fef]35trait add(T) {
[3376ec9]36    T ?+?(T, T);
37};
[fd54fef]38trait multiply(T) {
[3376ec9]39    T ?*?(T, T);
40};
[fd54fef]41trait divide(T) {
[3376ec9]42    T ?/?(T, T);
43};
[fd54fef]44trait lessthan(T) {
[3376ec9]45    int ?<?(T, T);
46};
[fd54fef]47trait equality(T) {
[3376ec9]48    int ?==?(T, T);
49};
[fd54fef]50trait sqrt(T) {
[3376ec9]51    T sqrt(T);
52};
53
54static inline {
55// int
56int ?=?(int& n, zero_t) { return n = 0.f; }
[ae3db00]57// unsigned int
58int ?=?(unsigned int& n, zero_t) { return n = 0.f; }
[3376ec9]59/* float */
60void ?{}(float& a, int b) { a = b; }
61float ?=?(float& n, zero_t) { return n = 0.f; }
62/* double */
63void ?{}(double& a, int b) { a = b; }
64double ?=?(double& n, zero_t) { return n = 0L; }
65// long double
66void ?{}(long double& a, int b) { a = b; }
67long double ?=?(long double& n, zero_t) { return n = 0L; }
68}
69
[fd54fef]70trait dottable(V, T) {
[3376ec9]71    T dot(V, V);
72};
73
74static inline {
75
[fd54fef]76forall(T | sqrt(T), V | dottable(V, T))
[3376ec9]77T length(V v) {
78   return sqrt(dot(v, v));
79}
80
[fd54fef]81forall(T, V | dottable(V, T))
[3376ec9]82T length_squared(V v) {
83   return dot(v, v);
84}
85
[fd54fef]86forall(T, V | { T length(V); } | subtract(V))
[3376ec9]87T distance(V v1, V v2) {
88    return length(v1 - v2);
89}
90
[fd54fef]91forall(T, V | { T length(V); V ?/?(V, T); })
[3376ec9]92V normalize(V v) {
93    return v / length(v);
94}
95
96// Project vector u onto vector v
[fd54fef]97forall(T, V | dottable(V, T) | { V normalize(V); V ?*?(V, T); })
[3376ec9]98V project(V u, V v) {
99    V v_norm = normalize(v);
100    return v_norm * dot(u, v_norm);
101}
102
103// Reflect incident vector v with respect to surface with normal n
[fd54fef]104forall(T | fromint(T), V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); })
[3376ec9]105V reflect(V v, V n) {
106    return v - (T){2} * project(v, n);
107}
108
109// Refract incident vector v with respect to surface with normal n
110// eta is the ratio of indices of refraction between starting material and
111// entering material (i.e., from air to water, eta = 1/1.33)
112// v and n must already be normalized
[fd54fef]113forall(T | fromint(T) | subtract(T) | multiply(T) | add(T) | lessthan(T) | sqrt(T),
114       V | dottable(V, T) | { V ?*?(T, V); V ?-?(V,V); void ?{}(V&, zero_t); })
[3376ec9]115V refract(V v, V n, T eta) {
116    T dotValue = dot(n, v);
117    T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue);
118    if (k < (T){0}) {
119        return 0;
120    }
121    return eta * v - (eta * dotValue + sqrt(k)) * n;
122}
123
124// Given a perturbed normal and a geometric normal,
125// flip the perturbed normal if the geometric normal is pointing away
126// from the observer.
127// n is the perturbed vector that we want to align
128// i is the incident vector
129// ng is the geometric normal of the surface
[fd54fef]130forall(T | lessthan(T) | zeroinit(T), V | dottable(V, T) | negate(V))
[3376ec9]131V faceforward(V n, V i, V ng) {
132    return dot(ng, i) < (T){0} ? n : -n;
133}
134
135} // inline
Note: See TracBrowser for help on using the repository browser.