summaryrefslogtreecommitdiff
path: root/zap/src/fma.c
blob: b2f45ad41780afef031a7fa6a0ca1c491c497c8a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
	Copyright 2022 Gabriel Jensen.
	This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
	If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#include <zap/priv.h>

#include <zap/math.h>

#include <stdint.h>

#if zap_priv_fastimpl
__asm__ (
	".globl zap_fma_c\n"
	".globl zap_fma_i\n"
	".globl zap_fma_l\n"
	".globl zap_fma_ll\n"
	".globl zap_fma_s\n"
	".globl zap_fma_uc\n"
	".globl zap_fma_ui\n"
	".globl zap_fma_ul\n"
	".globl zap_fma_ull\n"
	".globl zap_fma_us\n"

	"zap_fma_c:\n"
		/*
			signed char a
			signed char b
			signed char c
		*/
#if defined(sus_arch_amd64)
		"movb %sil,%al\n"
		"imulb %dl\n"
		"addb %dil,%al\n"
		"ret\n"
#endif

	"zap_fma_i:\n"
		/*
			int a
			int b
			int c
		*/
#if defined(sus_arch_amd64)
		"movl %edx,%eax\n"
		"imull %esi\n"
		"addl %edi,%eax\n"
		"ret\n"
#endif

	"zap_fma_l:\n"
		/*
			long a
			long b
			long c
		*/
#if defined(sus_arch_amd64)
		"movq %rdx,%rax\n"
		"imulq %rsi\n"
		"addq %rdi,%rax\n"
		"ret\n"
#endif

	"zap_fma_ll:\n"
		/*
			long long a
			long long b
			long long c
		*/
#if defined(sus_arch_amd64)
		"movq %rdx,%rax\n"
		"imulq %rsi\n"
		"addq %rdi,%rax\n"
		"ret\n"
#endif

	"zap_fma_s:\n"
		/*
			short a
			short b
			short c
		*/
#if defined(sus_arch_amd64)
		"movw %dx,%ax\n"
		"imulw %si\n"
		"addw %di,%ax\n"
		"ret\n"
#endif

	"zap_fma_uc:\n"
		/*
			unsigned char a
			unsigned char b
			unsigned char c
		*/
#if defined(sus_arch_amd64)
		"movb %sil,%al\n" /* mulb uses ax instead of al:dl (like the other variants), so we don't need to worry about it overwritting dl. */
		"mulb %dl\n"
		"addb %dil,%al\n"
		"ret\n"
#endif

	"zap_fma_ui:\n"
		/*
			unsigned int a
			unsigned int b
			unsigned int c
		*/
#if defined(sus_arch_amd64)
		"movl %edx,%eax\n"
		"mull %esi\n"
		"addl %edi,%eax\n"
		"ret\n"
#endif

	"zap_fma_ul:\n"
		/*
			unsigned long a
			unsigned long b
			unsigned long c
		*/
#if defined(sus_arch_amd64)
		"movq %rdx,%rax\n"
		"mulq %rsi\n"
		"addq %rdi,%rax\n"
		"ret\n"
#endif

	"zap_fma_ull:\n"
		/*
			unsigned long long a
			unsigned long long b
			unsigned long long c
		*/
#if defined(sus_arch_amd64)
		"movq %rdx,%rax\n" /* rdx get overwritten by mulq, so might as well just make it the first operand (in multiplication, the order is meaningless). */
		"mulq %rsi\n"
		"addq %rdi,%rax\n"
		"ret\n"
#endif

	"zap_fma_us:\n"
		/*
			unsigned short a
			unsigned short b
			unsigned short c
		*/
#if defined(sus_arch_amd64)
		"movw %dx,%ax\n"
		"mulw %si\n"
		"addw %di,%ax\n"
		"ret\n"
#endif
);
#else
#define zap_local_fma(_typ,_sufx) \
	_typ zap_fma_ ## _sufx (_typ const _a,_typ const _b,_typ const _c) {return _a + _b * _c;}

zap_local_fma(signed char,c)
zap_local_fma(int,i)
zap_local_fma(long,l)
zap_local_fma(long long,ll)
zap_local_fma(short,s)
zap_local_fma(unsigned char,uc)
zap_local_fma(unsigned int,ui)
zap_local_fma(unsigned long,ul)
zap_local_fma(unsigned long long,ull)
zap_local_fma(unsigned short,us)

#endif