2 * Copyright 2012-15 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "dm_services.h"
27 #include "include/fixed32_32.h"
29 static uint64_t u64_div(uint64_t n, uint64_t d)
33 uint64_t q = div64_u64_rem(n, d, &r);
35 for (i = 0; i < 32; ++i) {
36 uint64_t sbit = q & (1ULL<<63);
52 struct fixed32_32 dal_fixed32_32_from_fraction(uint32_t n, uint32_t d)
56 fx.value = u64_div((uint64_t)n << 32, (uint64_t)d << 32);
60 struct fixed32_32 dal_fixed32_32_add(
61 struct fixed32_32 lhs,
62 struct fixed32_32 rhs)
64 struct fixed32_32 fx = {lhs.value + rhs.value};
66 ASSERT(fx.value >= rhs.value);
70 struct fixed32_32 dal_fixed32_32_add_int(struct fixed32_32 lhs, uint32_t rhs)
72 struct fixed32_32 fx = {lhs.value + ((uint64_t)rhs << 32)};
74 ASSERT(fx.value >= (uint64_t)rhs << 32);
78 struct fixed32_32 dal_fixed32_32_sub(
79 struct fixed32_32 lhs,
80 struct fixed32_32 rhs)
84 ASSERT(lhs.value >= rhs.value);
85 fx.value = lhs.value - rhs.value;
89 struct fixed32_32 dal_fixed32_32_sub_int(struct fixed32_32 lhs, uint32_t rhs)
93 ASSERT(lhs.value >= ((uint64_t)rhs<<32));
94 fx.value = lhs.value - ((uint64_t)rhs<<32);
98 struct fixed32_32 dal_fixed32_32_mul(
99 struct fixed32_32 lhs,
100 struct fixed32_32 rhs)
102 struct fixed32_32 fx;
103 uint64_t lhs_int = lhs.value>>32;
104 uint64_t lhs_frac = (uint32_t)lhs.value;
105 uint64_t rhs_int = rhs.value>>32;
106 uint64_t rhs_frac = (uint32_t)rhs.value;
107 uint64_t ahbh = lhs_int * rhs_int;
108 uint64_t ahbl = lhs_int * rhs_frac;
109 uint64_t albh = lhs_frac * rhs_int;
110 uint64_t albl = lhs_frac * rhs_frac;
112 ASSERT((ahbh>>32) == 0);
114 fx.value = (ahbh<<32) + ahbl + albh + (albl>>32);
119 struct fixed32_32 dal_fixed32_32_mul_int(struct fixed32_32 lhs, uint32_t rhs)
121 struct fixed32_32 fx;
122 uint64_t lhsi = (lhs.value>>32) * (uint64_t)rhs;
125 ASSERT((lhsi>>32) == 0);
126 lhsf = ((uint32_t)lhs.value) * (uint64_t)rhs;
127 ASSERT((lhsi<<32) + lhsf >= lhsf);
128 fx.value = (lhsi<<32) + lhsf;
132 struct fixed32_32 dal_fixed32_32_div(
133 struct fixed32_32 lhs,
134 struct fixed32_32 rhs)
136 struct fixed32_32 fx;
138 fx.value = u64_div(lhs.value, rhs.value);
142 struct fixed32_32 dal_fixed32_32_div_int(struct fixed32_32 lhs, uint32_t rhs)
144 struct fixed32_32 fx;
146 fx.value = u64_div(lhs.value, (uint64_t)rhs << 32);
150 uint32_t dal_fixed32_32_ceil(struct fixed32_32 v)
152 ASSERT((uint32_t)v.value ? (v.value >> 32) + 1 >= 1 : true);
153 return (v.value>>32) + ((uint32_t)v.value ? 1 : 0);
156 uint32_t dal_fixed32_32_round(struct fixed32_32 v)
158 ASSERT(v.value + (1ULL<<31) >= (1ULL<<31));
159 return (v.value + (1ULL<<31))>>32;