Welcome to mirror list, hosted at ThFree Co, Russian Federation.

math_base_safe_inline.c « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49f4c5b0c19c0c27e8ac585234b742f42b20005d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* SPDX-License-Identifier: GPL-2.0-or-later */

#ifndef __MATH_BASE_SAFE_INLINE_C__
#define __MATH_BASE_SAFE_INLINE_C__

#include "BLI_math_base_safe.h"
#include "BLI_utildefines.h"

#ifdef __cplusplus
extern "C" {
#endif

MINLINE float safe_divide(float a, float b)
{
  return (b != 0.0f) ? a / b : 0.0f;
}

MINLINE float safe_modf(float a, float b)
{
  return (b != 0.0f) ? fmodf(a, b) : 0.0f;
}

MINLINE float safe_logf(float a, float base)
{
  if (UNLIKELY(a <= 0.0f || base <= 0.0f)) {
    return 0.0f;
  }
  return safe_divide(logf(a), logf(base));
}

MINLINE float safe_sqrtf(float a)
{
  return sqrtf(MAX2(a, 0.0f));
}

MINLINE float safe_inverse_sqrtf(float a)
{
  return (a > 0.0f) ? 1.0f / sqrtf(a) : 0.0f;
}

MINLINE float safe_asinf(float a)
{
  CLAMP(a, -1.0f, 1.0f);
  return asinf(a);
}

MINLINE float safe_acosf(float a)
{
  CLAMP(a, -1.0f, 1.0f);
  return acosf(a);
}

MINLINE float safe_powf(float base, float exponent)
{
  if (UNLIKELY(base < 0.0f && exponent != (int)exponent)) {
    return 0.0f;
  }
  return powf(base, exponent);
}

#ifdef __cplusplus
}
#endif

#endif /* __MATH_BASE_SAFE_INLINE_C__ */