blob: a327094de2bd7e5d3576d9888cfae8ea40549fdb [file] [log] [blame]
Jani Nikula2d332ee2018-11-16 14:07:25 +02001/* SPDX-License-Identifier: MIT */
2/*
3 * Copyright © 2018 Intel Corporation
4 */
5
6#ifndef _I915_FIXED_H_
7#define _I915_FIXED_H_
8
Jani Nikulac75299a2019-06-26 17:40:10 +03009#include <linux/bug.h>
10#include <linux/kernel.h>
11#include <linux/math64.h>
12#include <linux/types.h>
13
Jani Nikula2d332ee2018-11-16 14:07:25 +020014typedef struct {
Jani Nikula3122b9c2018-11-16 14:07:26 +020015 u32 val;
Jani Nikula2d332ee2018-11-16 14:07:25 +020016} uint_fixed_16_16_t;
17
Jani Nikulae7a23812018-11-16 14:07:27 +020018#define FP_16_16_MAX ((uint_fixed_16_16_t){ .val = UINT_MAX })
Jani Nikula2d332ee2018-11-16 14:07:25 +020019
20static inline bool is_fixed16_zero(uint_fixed_16_16_t val)
21{
Jani Nikulaf1eea892018-11-16 14:07:28 +020022 return val.val == 0;
Jani Nikula2d332ee2018-11-16 14:07:25 +020023}
24
Jani Nikula3122b9c2018-11-16 14:07:26 +020025static inline uint_fixed_16_16_t u32_to_fixed16(u32 val)
Jani Nikula2d332ee2018-11-16 14:07:25 +020026{
Jani Nikulaf48cc642018-11-16 14:07:29 +020027 uint_fixed_16_16_t fp = { .val = val << 16 };
Jani Nikula2d332ee2018-11-16 14:07:25 +020028
29 WARN_ON(val > U16_MAX);
30
Jani Nikula2d332ee2018-11-16 14:07:25 +020031 return fp;
32}
33
Jani Nikula3122b9c2018-11-16 14:07:26 +020034static inline u32 fixed16_to_u32_round_up(uint_fixed_16_16_t fp)
Jani Nikula2d332ee2018-11-16 14:07:25 +020035{
36 return DIV_ROUND_UP(fp.val, 1 << 16);
37}
38
Jani Nikula3122b9c2018-11-16 14:07:26 +020039static inline u32 fixed16_to_u32(uint_fixed_16_16_t fp)
Jani Nikula2d332ee2018-11-16 14:07:25 +020040{
41 return fp.val >> 16;
42}
43
44static inline uint_fixed_16_16_t min_fixed16(uint_fixed_16_16_t min1,
45 uint_fixed_16_16_t min2)
46{
Jani Nikulaf48cc642018-11-16 14:07:29 +020047 uint_fixed_16_16_t min = { .val = min(min1.val, min2.val) };
Jani Nikula2d332ee2018-11-16 14:07:25 +020048
Jani Nikula2d332ee2018-11-16 14:07:25 +020049 return min;
50}
51
52static inline uint_fixed_16_16_t max_fixed16(uint_fixed_16_16_t max1,
53 uint_fixed_16_16_t max2)
54{
Jani Nikulaf48cc642018-11-16 14:07:29 +020055 uint_fixed_16_16_t max = { .val = max(max1.val, max2.val) };
Jani Nikula2d332ee2018-11-16 14:07:25 +020056
Jani Nikula2d332ee2018-11-16 14:07:25 +020057 return max;
58}
59
Jani Nikula3122b9c2018-11-16 14:07:26 +020060static inline uint_fixed_16_16_t clamp_u64_to_fixed16(u64 val)
Jani Nikula2d332ee2018-11-16 14:07:25 +020061{
Jani Nikulaf48cc642018-11-16 14:07:29 +020062 uint_fixed_16_16_t fp = { .val = (u32)val };
63
Jani Nikula2d332ee2018-11-16 14:07:25 +020064 WARN_ON(val > U32_MAX);
Jani Nikulaf48cc642018-11-16 14:07:29 +020065
Jani Nikula2d332ee2018-11-16 14:07:25 +020066 return fp;
67}
68
Jani Nikula3122b9c2018-11-16 14:07:26 +020069static inline u32 div_round_up_fixed16(uint_fixed_16_16_t val,
70 uint_fixed_16_16_t d)
Jani Nikula2d332ee2018-11-16 14:07:25 +020071{
72 return DIV_ROUND_UP(val.val, d.val);
73}
74
Jani Nikula3122b9c2018-11-16 14:07:26 +020075static inline u32 mul_round_up_u32_fixed16(u32 val, uint_fixed_16_16_t mul)
Jani Nikula2d332ee2018-11-16 14:07:25 +020076{
Jani Nikulaf48cc642018-11-16 14:07:29 +020077 u64 tmp;
Jani Nikula2d332ee2018-11-16 14:07:25 +020078
Ville Syrjäläd492a292019-04-08 18:27:01 +030079 tmp = mul_u32_u32(val, mul.val);
Jani Nikulaf48cc642018-11-16 14:07:29 +020080 tmp = DIV_ROUND_UP_ULL(tmp, 1 << 16);
81 WARN_ON(tmp > U32_MAX);
82
83 return (u32)tmp;
Jani Nikula2d332ee2018-11-16 14:07:25 +020084}
85
86static inline uint_fixed_16_16_t mul_fixed16(uint_fixed_16_16_t val,
87 uint_fixed_16_16_t mul)
88{
Jani Nikulaf48cc642018-11-16 14:07:29 +020089 u64 tmp;
Jani Nikula2d332ee2018-11-16 14:07:25 +020090
Ville Syrjäläd492a292019-04-08 18:27:01 +030091 tmp = mul_u32_u32(val.val, mul.val);
Jani Nikulaf48cc642018-11-16 14:07:29 +020092 tmp = tmp >> 16;
93
94 return clamp_u64_to_fixed16(tmp);
Jani Nikula2d332ee2018-11-16 14:07:25 +020095}
96
Jani Nikula3122b9c2018-11-16 14:07:26 +020097static inline uint_fixed_16_16_t div_fixed16(u32 val, u32 d)
Jani Nikula2d332ee2018-11-16 14:07:25 +020098{
Jani Nikulaf48cc642018-11-16 14:07:29 +020099 u64 tmp;
Jani Nikula2d332ee2018-11-16 14:07:25 +0200100
Jani Nikulaf48cc642018-11-16 14:07:29 +0200101 tmp = (u64)val << 16;
102 tmp = DIV_ROUND_UP_ULL(tmp, d);
103
104 return clamp_u64_to_fixed16(tmp);
Jani Nikula2d332ee2018-11-16 14:07:25 +0200105}
106
Jani Nikula3122b9c2018-11-16 14:07:26 +0200107static inline u32 div_round_up_u32_fixed16(u32 val, uint_fixed_16_16_t d)
Jani Nikula2d332ee2018-11-16 14:07:25 +0200108{
Jani Nikulaf48cc642018-11-16 14:07:29 +0200109 u64 tmp;
Jani Nikula2d332ee2018-11-16 14:07:25 +0200110
Jani Nikulaf48cc642018-11-16 14:07:29 +0200111 tmp = (u64)val << 16;
112 tmp = DIV_ROUND_UP_ULL(tmp, d.val);
113 WARN_ON(tmp > U32_MAX);
114
115 return (u32)tmp;
Jani Nikula2d332ee2018-11-16 14:07:25 +0200116}
117
Jani Nikula3122b9c2018-11-16 14:07:26 +0200118static inline uint_fixed_16_16_t mul_u32_fixed16(u32 val, uint_fixed_16_16_t mul)
Jani Nikula2d332ee2018-11-16 14:07:25 +0200119{
Jani Nikulaf48cc642018-11-16 14:07:29 +0200120 u64 tmp;
Jani Nikula2d332ee2018-11-16 14:07:25 +0200121
Ville Syrjäläd492a292019-04-08 18:27:01 +0300122 tmp = mul_u32_u32(val, mul.val);
Jani Nikulaf48cc642018-11-16 14:07:29 +0200123
124 return clamp_u64_to_fixed16(tmp);
Jani Nikula2d332ee2018-11-16 14:07:25 +0200125}
126
127static inline uint_fixed_16_16_t add_fixed16(uint_fixed_16_16_t add1,
128 uint_fixed_16_16_t add2)
129{
Jani Nikulaf48cc642018-11-16 14:07:29 +0200130 u64 tmp;
Jani Nikula2d332ee2018-11-16 14:07:25 +0200131
Jani Nikulaf48cc642018-11-16 14:07:29 +0200132 tmp = (u64)add1.val + add2.val;
133
134 return clamp_u64_to_fixed16(tmp);
Jani Nikula2d332ee2018-11-16 14:07:25 +0200135}
136
137static inline uint_fixed_16_16_t add_fixed16_u32(uint_fixed_16_16_t add1,
Jani Nikula3122b9c2018-11-16 14:07:26 +0200138 u32 add2)
Jani Nikula2d332ee2018-11-16 14:07:25 +0200139{
Jani Nikulaf48cc642018-11-16 14:07:29 +0200140 uint_fixed_16_16_t tmp_add2 = u32_to_fixed16(add2);
141 u64 tmp;
Jani Nikula2d332ee2018-11-16 14:07:25 +0200142
Jani Nikulaf48cc642018-11-16 14:07:29 +0200143 tmp = (u64)add1.val + tmp_add2.val;
144
145 return clamp_u64_to_fixed16(tmp);
Jani Nikula2d332ee2018-11-16 14:07:25 +0200146}
147
148#endif /* _I915_FIXED_H_ */