1a50b01d2SDag-Erling Smørgrav /*-
2a50b01d2SDag-Erling Smørgrav * Copyright (c) 2015 Dag-Erling Smørgrav
3a50b01d2SDag-Erling Smørgrav * All rights reserved.
4a50b01d2SDag-Erling Smørgrav *
5a50b01d2SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
6a50b01d2SDag-Erling Smørgrav * modification, are permitted provided that the following conditions
7a50b01d2SDag-Erling Smørgrav * are met:
8a50b01d2SDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright
9a50b01d2SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer.
10a50b01d2SDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright
11a50b01d2SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the
12a50b01d2SDag-Erling Smørgrav * documentation and/or other materials provided with the distribution.
13a50b01d2SDag-Erling Smørgrav *
14a50b01d2SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a50b01d2SDag-Erling Smørgrav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a50b01d2SDag-Erling Smørgrav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a50b01d2SDag-Erling Smørgrav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a50b01d2SDag-Erling Smørgrav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a50b01d2SDag-Erling Smørgrav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a50b01d2SDag-Erling Smørgrav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a50b01d2SDag-Erling Smørgrav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a50b01d2SDag-Erling Smørgrav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a50b01d2SDag-Erling Smørgrav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a50b01d2SDag-Erling Smørgrav * SUCH DAMAGE.
25a50b01d2SDag-Erling Smørgrav */
26a50b01d2SDag-Erling Smørgrav
27a50b01d2SDag-Erling Smørgrav #ifndef FP16_H_INCLUDED
28a50b01d2SDag-Erling Smørgrav #define FP16_H_INCLUDED
29a50b01d2SDag-Erling Smørgrav
30a50b01d2SDag-Erling Smørgrav typedef signed long long fp16_t;
31a50b01d2SDag-Erling Smørgrav
32a50b01d2SDag-Erling Smørgrav #define ItoFP16(n) ((signed long long)(n) << 16)
33a50b01d2SDag-Erling Smørgrav #define FP16toI(n) ((signed long long)(n) >> 16)
34a50b01d2SDag-Erling Smørgrav
35a50b01d2SDag-Erling Smørgrav #ifndef _KERNEL
36a50b01d2SDag-Erling Smørgrav #define FP16toF(n) ((n) / 65536.0)
37a50b01d2SDag-Erling Smørgrav #endif
38a50b01d2SDag-Erling Smørgrav
39a50b01d2SDag-Erling Smørgrav /* add a and b */
40a50b01d2SDag-Erling Smørgrav static inline fp16_t
fp16_add(fp16_t a,fp16_t b)41a50b01d2SDag-Erling Smørgrav fp16_add(fp16_t a, fp16_t b)
42a50b01d2SDag-Erling Smørgrav {
43a50b01d2SDag-Erling Smørgrav
44a50b01d2SDag-Erling Smørgrav return (a + b);
45a50b01d2SDag-Erling Smørgrav }
46a50b01d2SDag-Erling Smørgrav
47a50b01d2SDag-Erling Smørgrav /* subtract b from a */
48a50b01d2SDag-Erling Smørgrav static inline fp16_t
fp16_sub(fp16_t a,fp16_t b)49a50b01d2SDag-Erling Smørgrav fp16_sub(fp16_t a, fp16_t b)
50a50b01d2SDag-Erling Smørgrav {
51a50b01d2SDag-Erling Smørgrav
52a50b01d2SDag-Erling Smørgrav return (a - b);
53a50b01d2SDag-Erling Smørgrav }
54a50b01d2SDag-Erling Smørgrav
55a50b01d2SDag-Erling Smørgrav /* multiply a by b */
56a50b01d2SDag-Erling Smørgrav static inline fp16_t
fp16_mul(fp16_t a,fp16_t b)57a50b01d2SDag-Erling Smørgrav fp16_mul(fp16_t a, fp16_t b)
58a50b01d2SDag-Erling Smørgrav {
59a50b01d2SDag-Erling Smørgrav
60a50b01d2SDag-Erling Smørgrav return (a * b >> 16);
61a50b01d2SDag-Erling Smørgrav }
62a50b01d2SDag-Erling Smørgrav
63a50b01d2SDag-Erling Smørgrav /* divide a by b */
64a50b01d2SDag-Erling Smørgrav static inline fp16_t
fp16_div(fp16_t a,fp16_t b)65a50b01d2SDag-Erling Smørgrav fp16_div(fp16_t a, fp16_t b)
66a50b01d2SDag-Erling Smørgrav {
67a50b01d2SDag-Erling Smørgrav
68a50b01d2SDag-Erling Smørgrav return ((a << 16) / b);
69a50b01d2SDag-Erling Smørgrav }
70a50b01d2SDag-Erling Smørgrav
71a50b01d2SDag-Erling Smørgrav /* square root */
72a50b01d2SDag-Erling Smørgrav fp16_t fp16_sqrt(fp16_t);
73a50b01d2SDag-Erling Smørgrav
74a50b01d2SDag-Erling Smørgrav #define FP16_2PI 411774
75a50b01d2SDag-Erling Smørgrav #define FP16_PI 205887
76a50b01d2SDag-Erling Smørgrav #define FP16_PI_2 102943
77a50b01d2SDag-Erling Smørgrav #define FP16_PI_4 51471
78a50b01d2SDag-Erling Smørgrav
79*23e6278eSDag-Erling Smørgrav /* sine and cosine */
80*23e6278eSDag-Erling Smørgrav fp16_t fp16_sin(fp16_t);
81a50b01d2SDag-Erling Smørgrav fp16_t fp16_cos(fp16_t);
82a50b01d2SDag-Erling Smørgrav
83a50b01d2SDag-Erling Smørgrav #endif
84