// ======================================================================== //
// Copyright 2009-2015 Intel Corporation                                    //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//     http://www.apache.org/licenses/LICENSE-2.0                           //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
// ======================================================================== //

#pragma once

////////////////////////////////////////////////////////////////////////////////
/// Constants
////////////////////////////////////////////////////////////////////////////////

#define inf     (1e100f)  //floatbits(0x7F800000)
#define pos_inf (1e100f)  //floatbits(0x7F800000)
#define neg_inf (-1e100f) //floatbits(0xFF800000)

#define M_PI     3.14159265358979323846f
#define pi       3.14159265358979323846f
#define two_pi   6.283185307179586232f
#define four_pi 12.566370614359172464f

#define one_over_pi      0.31830988618379069122f
#define one_over_two_pi  0.15915494309189534561f
#define one_over_four_pi 0.079577471545947672804f

#define sqrtf sqrt
#define sinf sin
#define cosf cos
#define powf pow

////////////////////////////////////////////////////////////////////////////////
/// Functions
////////////////////////////////////////////////////////////////////////////////

inline uniform float sqr(const uniform float f) { return f*f; }
inline varying float sqr(const varying float f) { return f*f; }

inline uniform float rcp_safe(uniform float f) { return rcp((abs(f) < 1e-8f) ? 1e-8f : f); }
inline varying float rcp_safe(varying float f) { return rcp((abs(f) < 1e-8f) ? 1e-8f : f); }

inline uniform float clamp (const uniform float v) { return max(0.0f,min(v,1.0f)); }
inline varying float clamp (const varying float v) { return max(0.0f,min(v,1.0f)); }

inline uniform float clamp (const uniform float v, const uniform float lower, const uniform float upper) { return max(lower,min(v,upper)); }
inline varying float clamp (const varying float v, const varying float lower, const varying float upper) { return max(lower,min(v,upper)); }

inline uniform int clamp (const uniform int v, const uniform int lower, const uniform int upper) { return max(lower,min(v,upper)); }
inline varying int clamp (const varying int v, const varying int lower, const varying int upper) { return max(lower,min(v,upper)); }

inline uniform float deg2rad (const uniform float x)  { return x * 1.74532925199432957692e-2f; }
inline varying float deg2rad (const varying float x)  { return x * 1.74532925199432957692e-2f; }

inline uniform float rad2deg (const uniform float x)  { return x * 5.72957795130823208768e1f; }
inline varying float rad2deg (const varying float x)  { return x * 5.72957795130823208768e1f; }

inline uniform float sin2cos ( const uniform float x )  { return sqrt(max(0.0f,1.0f-x*x)); }
inline varying float sin2cos ( const varying float x )  { return sqrt(max(0.0f,1.0f-x*x)); }

inline uniform float cos2sin ( const uniform float x )  { return sin2cos(x); }
inline varying float cos2sin ( const varying float x )  { return sin2cos(x); }
