Indentations break the feed.
[SquirrelJME.git] / nanocoat / lib / base / fixed.c
blob6a2b4217fbe9f0d35e4c736a66321131049b46b1
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include "sjme/fixed.h"
12 /** The number of bits to shift for fractions. */
13 #define SJME_FIXED_SHIFT 16
15 /** The number of bits in an entire fixed value. */
16 #define SJME_FIXED_FULL_BITS 32
18 /** The value one. */
19 #define SJME_FIXED_ONE 0x10000
21 /** The masked for shifted values. */
22 #define SJME_FIXED_MASK 0xFFFF
24 /** The masked for rounding values. */
25 #define SJME_FIXED_ROUND_MASK 0x800
27 sjme_fixed sjme_fixed_ceil(
28 sjme_attrInValue sjme_jint v)
30 sjme_fixed z;
32 z = v & (~SJME_FIXED_MASK);
33 if ((v & SJME_FIXED_MASK) != 0)
34 return z + SJME_FIXED_ONE;
35 return z;
38 sjme_fixed sjme_fixed_div(
39 sjme_attrInValue sjme_fixed num,
40 sjme_attrInValue sjme_fixed den)
42 if (den == 0)
43 return 0;
45 return (sjme_fixed)((((int64_t)num) << SJME_FIXED_SHIFT) / den);
48 sjme_fixed sjme_fixed_floor(
49 sjme_attrInValue sjme_jint v)
51 return v & (~SJME_FIXED_MASK);
54 sjme_fixed sjme_fixed_fraction(
55 sjme_attrInValue sjme_jint num,
56 sjme_attrInValue sjme_jint den)
58 if (den == 0)
59 return 0;
61 return sjme_fixed_div(sjme_fixed_hi(num),
62 sjme_fixed_hi(den));
65 sjme_fixed sjme_fixed_hi(
66 sjme_attrInValue sjme_jint val)
68 return val << SJME_FIXED_SHIFT;
71 sjme_jint sjme_fixed_int(
72 sjme_attrInValue sjme_fixed val)
74 return val >> SJME_FIXED_SHIFT;
77 sjme_fixed sjme_fixed_mul(
78 sjme_attrInValue sjme_fixed a,
79 sjme_attrInValue sjme_fixed b)
81 return (sjme_fixed)(((int64_t)a) * ((int64_t)b) >> SJME_FIXED_SHIFT);
84 sjme_fixed sjme_fixed_round(
85 sjme_attrInValue sjme_jint v)
87 if (((v & SJME_FIXED_ROUND_MASK) != 0) == (v < 0))
88 return sjme_fixed_ceil(v);
89 return sjme_fixed_floor(v);