blob: 509e95d17a529ded434cad94919f860505dc2ce3 [file] [log] [blame]
Pierre Ossman82c7f312009-03-09 13:21:27 +00001;
2; jsimdext.inc - common declarations
3;
4; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5;
6; Based on
7; x86 SIMD extension for IJG JPEG library - version 1.02
8;
9; Copyright (C) 1999-2006, MIYASAKA Masaru.
10;
11; This software is provided 'as-is', without any express or implied
12; warranty. In no event will the authors be held liable for any damages
13; arising from the use of this software.
14;
15; Permission is granted to anyone to use this software for any purpose,
16; including commercial applications, and to alter it and redistribute it
17; freely, subject to the following restrictions:
18;
19; 1. The origin of this software must not be misrepresented; you must not
20; claim that you wrote the original software. If you use this software
21; in a product, an acknowledgment in the product documentation would be
22; appreciated but is not required.
23; 2. Altered source versions must be plainly marked as such, and must not be
24; misrepresented as being the original software.
25; 3. This notice may not be removed or altered from any source distribution.
26;
27; [TAB8]
28
29; ==========================================================================
30; System-dependent configurations
31
32%ifdef WIN32 ; ----(nasm -fwin32 -DWIN32 ...)--------
33; * Microsoft Visual C++
34; * MinGW (Minimalist GNU for Windows)
35; * CygWin
36; * LCC-Win32
37
38; -- segment definition --
39;
40%define SEG_TEXT .text align=16 public use32 class=CODE
41%define SEG_CONST .rdata align=16 public use32 class=CONST
42
43%elifdef OBJ32 ; ----(nasm -fobj -DOBJ32 ...)----------
44; * Borland C++ (Win32)
45
46; -- segment definition --
47;
48%define SEG_TEXT .text align=16 public use32 class=CODE
49%define SEG_CONST .data align=16 public use32 class=DATA
50
51%elifdef ELF ; ----(nasm -felf -DELF ...)------------
52; * Linux
53; * *BSD family Unix using elf format
54; * Unix System V, including Solaris x86, UnixWare and SCO Unix
55
56; -- segment definition --
57;
58%define SEG_TEXT .text progbits alloc exec nowrite align=16
59%define SEG_CONST .rodata progbits alloc noexec nowrite align=16
60
61; To make the code position-independent, append -DPIC to the commandline
62;
63%define GOT_SYMBOL _GLOBAL_OFFSET_TABLE_ ; ELF supports PIC
64%define EXTN(name) name ; foo() -> foo
65
66%elifdef AOUT ; ----(nasm -faoutb/aout -DAOUT ...)----
67; * Older Linux using a.out format (nasm -f aout -DAOUT ...)
68; * *BSD family Unix using a.out format (nasm -f aoutb -DAOUT ...)
69
70; -- segment definition --
71;
72%define SEG_TEXT .text
73%define SEG_CONST .data
74
75; To make the code position-independent, append -DPIC to the commandline
76;
77%define GOT_SYMBOL __GLOBAL_OFFSET_TABLE_ ; BSD-style a.out supports PIC
78
79%elifdef MACHO ; ----(nasm -fmacho -DMACHO ...)--------
80; * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (Mach-O format)
81
82; -- segment definition --
83;
84%define SEG_TEXT .text ;align=16 ; nasm doesn't accept align=16. why?
85%define SEG_CONST .rodata align=16
86
87; The generation of position-independent code (PIC) is the default on Darwin.
88;
89%define PIC
90%define GOT_SYMBOL _MACHO_PIC_ ; Mach-O style code-relative addressing
91
92%else ; ----(Other case)----------------------
93
94; -- segment definition --
95;
96%define SEG_TEXT .text
97%define SEG_CONST .data
98
99%endif ; ----------------------------------------------
100
101; ==========================================================================
102
103; --------------------------------------------------------------------------
104; Common types
105;
106%define POINTER dword ; general pointer type
107%define SIZEOF_POINTER SIZEOF_DWORD ; sizeof(POINTER)
108%define POINTER_BIT DWORD_BIT ; sizeof(POINTER)*BYTE_BIT
109
Pierre Ossman3e0e2de2009-03-09 13:25:30 +0000110%define INT dword ; signed integer type
111%define SIZEOF_INT SIZEOF_DWORD ; sizeof(INT)
112%define INT_BIT DWORD_BIT ; sizeof(INT)*BYTE_BIT
113
Pierre Ossman2c2e54b2009-03-09 13:28:10 +0000114%define FP32 dword ; IEEE754 single
115%define SIZEOF_FP32 SIZEOF_DWORD ; sizeof(FP32)
116%define FP32_BIT DWORD_BIT ; sizeof(FP32)*BYTE_BIT
117
Pierre Ossman3e0e2de2009-03-09 13:25:30 +0000118%define MMWORD qword ; int64 (MMX register)
119%define SIZEOF_MMWORD SIZEOF_QWORD ; sizeof(MMWORD)
120%define MMWORD_BIT QWORD_BIT ; sizeof(MMWORD)*BYTE_BIT
121
122%define SIZEOF_BYTE 1 ; sizeof(BYTE)
123%define SIZEOF_WORD 2 ; sizeof(WORD)
124%define SIZEOF_DWORD 4 ; sizeof(DWORD)
125%define SIZEOF_QWORD 8 ; sizeof(QWORD)
126
127%define BYTE_BIT 8 ; CHAR_BIT in C
128%define WORD_BIT 16 ; sizeof(WORD)*BYTE_BIT
129%define DWORD_BIT 32 ; sizeof(DWORD)*BYTE_BIT
130%define QWORD_BIT 64 ; sizeof(QWORD)*BYTE_BIT
131
Pierre Ossman82c7f312009-03-09 13:21:27 +0000132; --------------------------------------------------------------------------
133; External Symbol Name
134;
135%ifndef EXTN
136%define EXTN(name) _ %+ name ; foo() -> _foo
137%endif
138
139; --------------------------------------------------------------------------
140; Macros for position-independent code (PIC) support
141;
142%ifndef GOT_SYMBOL
143%undef PIC
144%endif
145
146%ifdef PIC ; -------------------------------------------
147
148%ifidn GOT_SYMBOL,_MACHO_PIC_ ; --------------------
149
150; At present, nasm doesn't seem to support PIC generation for Mach-O.
151; The PIC support code below is a little tricky.
152
153 SECTION SEG_CONST
154const_base:
155
156%define GOTOFF(got,sym) (got) + (sym) - const_base
157
158%imacro get_GOT 1
159 ; NOTE: this macro destroys ecx resister.
160 call %%geteip
161 add ecx, byte (%%ref - $)
162 jmp short %%adjust
163%%geteip:
164 mov ecx, POINTER [esp]
165 ret
166%%adjust:
167 push ebp
168 xor ebp,ebp ; ebp = 0
169%ifidni %1,ebx ; (%1 == ebx)
170 ; db 0x8D,0x9C + jmp near const_base =
171 ; lea ebx, [ecx+ebp*8+(const_base-%%ref)] ; 8D,9C,E9,(offset32)
172 db 0x8D,0x9C ; 8D,9C
173 jmp near const_base ; E9,(const_base-%%ref)
174%%ref:
175%else ; (%1 != ebx)
176 ; db 0x8D,0x8C + jmp near const_base =
177 ; lea ecx, [ecx+ebp*8+(const_base-%%ref)] ; 8D,8C,E9,(offset32)
178 db 0x8D,0x8C ; 8D,8C
179 jmp near const_base ; E9,(const_base-%%ref)
180%%ref: mov %1, ecx
181%endif ; (%1 == ebx)
182 pop ebp
183%endmacro
184
185%else ; GOT_SYMBOL != _MACHO_PIC_ ----------------
186
187%define GOTOFF(got,sym) (got) + (sym) wrt ..gotoff
188
189%imacro get_GOT 1
190 extern GOT_SYMBOL
191 call %%geteip
192 add %1, GOT_SYMBOL + $$ - $ wrt ..gotpc
193 jmp short %%done
194%%geteip:
195 mov %1, POINTER [esp]
196 ret
197%%done:
198%endmacro
199
200%endif ; GOT_SYMBOL == _MACHO_PIC_ ----------------
201
202%imacro pushpic 1.nolist
203 push %1
204%endmacro
205%imacro poppic 1.nolist
206 pop %1
207%endmacro
208%imacro movpic 2.nolist
209 mov %1,%2
210%endmacro
211
212%else ; !PIC -----------------------------------------
213
214%define GOTOFF(got,sym) (sym)
215
216%imacro get_GOT 1.nolist
217%endmacro
218%imacro pushpic 1.nolist
219%endmacro
220%imacro poppic 1.nolist
221%endmacro
222%imacro movpic 2.nolist
223%endmacro
224
225%endif ; PIC -----------------------------------------
226
227; --------------------------------------------------------------------------
228; Align the next instruction on {2,4,8,16,..}-byte boundary.
229; ".balign n,,m" in GNU as
230;
231%define MSKLE(x,y) (~(((y) & 0xFFFF) - ((x) & 0xFFFF)) >> 16)
232%define FILLB(b,n) (($$-(b)) & ((n)-1))
233
234%imacro alignx 1-2.nolist 0xFFFF
235%%bs: times MSKLE(FILLB(%%bs,%1),%2) & MSKLE(16,FILLB($,%1)) & FILLB($,%1) \
236 db 0x90 ; nop
237 times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/9 \
238 db 0x8D,0x9C,0x23,0x00,0x00,0x00,0x00 ; lea ebx,[ebx+0x00000000]
239 times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/7 \
240 db 0x8D,0xAC,0x25,0x00,0x00,0x00,0x00 ; lea ebp,[ebp+0x00000000]
241 times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/6 \
242 db 0x8D,0xAD,0x00,0x00,0x00,0x00 ; lea ebp,[ebp+0x00000000]
243 times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/4 \
244 db 0x8D,0x6C,0x25,0x00 ; lea ebp,[ebp+0x00]
245 times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/3 \
246 db 0x8D,0x6D,0x00 ; lea ebp,[ebp+0x00]
247 times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/2 \
248 db 0x8B,0xED ; mov ebp,ebp
249 times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/1 \
250 db 0x90 ; nop
251%endmacro
252
253; Align the next data on {2,4,8,16,..}-byte boundary.
254;
255%imacro alignz 1.nolist
256 align %1, db 0 ; filling zeros
257%endmacro
258
259
260; --------------------------------------------------------------------------
261; Defines picked up from the C headers
262;
263%include "simd/jsimdcfg.inc"
264
265; --------------------------------------------------------------------------