1254853Sdumbbell/*
2254853Sdumbbell * Copyright 2009 Red Hat Inc.
3254853Sdumbbell *
4254853Sdumbbell * Permission is hereby granted, free of charge, to any person obtaining a
5254853Sdumbbell * copy of this software and associated documentation files (the "Software"),
6254853Sdumbbell * to deal in the Software without restriction, including without limitation
7254853Sdumbbell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8254853Sdumbbell * and/or sell copies of the Software, and to permit persons to whom the
9254853Sdumbbell * Software is furnished to do so, subject to the following conditions:
10254853Sdumbbell *
11254853Sdumbbell * The above copyright notice and this permission notice shall be included in
12254853Sdumbbell * all copies or substantial portions of the Software.
13254853Sdumbbell *
14254853Sdumbbell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15254853Sdumbbell * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16254853Sdumbbell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17254853Sdumbbell * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18254853Sdumbbell * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19254853Sdumbbell * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20254853Sdumbbell * OTHER DEALINGS IN THE SOFTWARE.
21254853Sdumbbell *
22254853Sdumbbell * Authors: Dave Airlie
23254853Sdumbbell */
24254853Sdumbbell
25254853Sdumbbell#include <sys/cdefs.h>
26254853Sdumbbell__FBSDID("$FreeBSD$");
27254853Sdumbbell
28254853Sdumbbell#ifndef DRM_FIXED_H
29254853Sdumbbell#define DRM_FIXED_H
30254853Sdumbbell
31254853Sdumbbelltypedef union dfixed {
32254853Sdumbbell	u32 full;
33254853Sdumbbell} fixed20_12;
34254853Sdumbbell
35254853Sdumbbell
36254853Sdumbbell#define dfixed_const(A) (u32)(((A) << 12))/*  + ((B + 0.000122)*4096)) */
37254853Sdumbbell#define dfixed_const_half(A) (u32)(((A) << 12) + 2048)
38254853Sdumbbell#define dfixed_const_666(A) (u32)(((A) << 12) + 2731)
39254853Sdumbbell#define dfixed_const_8(A) (u32)(((A) << 12) + 3277)
40254853Sdumbbell#define dfixed_mul(A, B) ((u64)((u64)(A).full * (B).full + 2048) >> 12)
41254853Sdumbbell#define dfixed_init(A) { .full = dfixed_const((A)) }
42254853Sdumbbell#define dfixed_init_half(A) { .full = dfixed_const_half((A)) }
43254853Sdumbbell#define dfixed_trunc(A) ((A).full >> 12)
44254853Sdumbbell#define dfixed_frac(A) ((A).full & ((1 << 12) - 1))
45254853Sdumbbell
46254853Sdumbbellstatic inline u32 dfixed_floor(fixed20_12 A)
47254853Sdumbbell{
48254853Sdumbbell	u32 non_frac = dfixed_trunc(A);
49254853Sdumbbell
50254853Sdumbbell	return dfixed_const(non_frac);
51254853Sdumbbell}
52254853Sdumbbell
53254853Sdumbbellstatic inline u32 dfixed_ceil(fixed20_12 A)
54254853Sdumbbell{
55254853Sdumbbell	u32 non_frac = dfixed_trunc(A);
56254853Sdumbbell
57254853Sdumbbell	if (A.full > dfixed_const(non_frac))
58254853Sdumbbell		return dfixed_const(non_frac + 1);
59254853Sdumbbell	else
60254853Sdumbbell		return dfixed_const(non_frac);
61254853Sdumbbell}
62254853Sdumbbell
63254853Sdumbbellstatic inline u32 dfixed_div(fixed20_12 A, fixed20_12 B)
64254853Sdumbbell{
65254853Sdumbbell	u64 tmp = ((u64)A.full << 13);
66254853Sdumbbell
67254853Sdumbbell	do_div(tmp, B.full);
68254853Sdumbbell	tmp += 1;
69254853Sdumbbell	tmp /= 2;
70254853Sdumbbell	return lower_32_bits(tmp);
71254853Sdumbbell}
72254853Sdumbbell#endif
73