diff options
author | Qiang Yu <[email protected]> | 2017-06-18 00:37:39 +0800 |
---|---|---|
committer | Qiang Yu <[email protected]> | 2019-04-11 09:57:53 +0800 |
commit | 509dd6e20b1bfa8f42966da1ea421a0b7057485c (patch) | |
tree | 903fc11997b4b20aaa246d174ba7ffa415731176 /src/util/u_math.h | |
parent | c73fd79ceef9124faba2c075742ce18d06649af6 (diff) |
u_math: add ushort_to_float/float_to_ushort
v2:
- return 0 for NaN too
Signed-off-by: Qiang Yu <[email protected]>
Reviewed-by: Roland Scheidegger <[email protected]>
Diffstat (limited to 'src/util/u_math.h')
-rw-r--r-- | src/util/u_math.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/util/u_math.h b/src/util/u_math.h index e7dbbe5ca22..5e712dadb4a 100644 --- a/src/util/u_math.h +++ b/src/util/u_math.h @@ -389,6 +389,37 @@ float_to_ubyte(float f) } } +/** + * Convert ushort to float in [0, 1]. + */ +static inline float +ushort_to_float(ushort us) +{ + return (float) us * (1.0f / 65535.0f); +} + + +/** + * Convert float in [0,1] to ushort in [0,65535] with clamping. + */ +static inline ushort +float_to_ushort(float f) +{ + /* return 0 for NaN too */ + if (!(f > 0.0f)) { + return (ushort) 0; + } + else if (f >= 1.0f) { + return (ushort) 65535; + } + else { + union fi tmp; + tmp.f = f; + tmp.f = tmp.f * (65535.0f/65536.0f) + 128.0f; + return (ushort) tmp.i; + } +} + static inline float byte_to_float_tex(int8_t b) { |