Plaquette
 
Loading...
Searching...
No Matches
pq_constrain.h
1/*
2 * pq_constrain.h
3 *
4 * Utility functions clamping.
5 *
6 * (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21#ifndef PQ_CONSTRAIN_H_
22#define PQ_CONSTRAIN_H_
23
24#if (defined(ARDUINO) && ARDUINO >= 100) || defined(EPOXY_DUINO)
25#include <Arduino.h>
26#else
27#include <WProgram.h>
28#endif
29
31inline float constrain01(float x) {
32#if defined(PQ_IEEE_754_SUPPORTED)
33 uint32_t ui;
34 memcpy(&ui, &x, sizeof(ui)); // safe bit copy
35
36 if (ui & 0x80000000u) return 0.0f; // check sign: negative => return 0
37 if (ui > 0x3F800000u) return 1.0f; // check if > 1.0f => return 1
38 return x; // already in [0,1]
39#else
40 return (x < 0.0f) ? 0.0f : (x > 1.0f ? 1.0f : x);
41#endif
42}
43
44#endif