Initialize Tizen 2.3
[external/libjpeg-turbo.git] / simd / jsimdext.inc
1 ;
2 ; jsimdext.inc - common declarations
3 ;
4 ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5 ; Copyright 2010 D. R. Commander
6 ;
7 ; Based on
8 ; x86 SIMD extension for IJG JPEG library - version 1.02
9 ;
10 ; Copyright (C) 1999-2006, MIYASAKA Masaru.
11 ;
12 ; This software is provided 'as-is', without any express or implied
13 ; warranty.  In no event will the authors be held liable for any damages
14 ; arising from the use of this software.
15 ;
16 ; Permission is granted to anyone to use this software for any purpose,
17 ; including commercial applications, and to alter it and redistribute it
18 ; freely, subject to the following restrictions:
19 ;
20 ; 1. The origin of this software must not be misrepresented; you must not
21 ;    claim that you wrote the original software. If you use this software
22 ;    in a product, an acknowledgment in the product documentation would be
23 ;    appreciated but is not required.
24 ; 2. Altered source versions must be plainly marked as such, and must not be
25 ;    misrepresented as being the original software.
26 ; 3. This notice may not be removed or altered from any source distribution.
27 ;
28 ; [TAB8]
29
30 ; ==========================================================================
31 ;  System-dependent configurations
32
33 %ifdef WIN32    ; ----(nasm -fwin32 -DWIN32 ...)--------
34 ; * Microsoft Visual C++
35 ; * MinGW (Minimalist GNU for Windows)
36 ; * CygWin
37 ; * LCC-Win32
38
39 ; -- segment definition --
40 ;
41 %ifdef __YASM_VER__
42 %define SEG_TEXT    .text  align=16
43 %define SEG_CONST   .rdata align=16
44 %else
45 %define SEG_TEXT    .text  align=16 public use32 class=CODE
46 %define SEG_CONST   .rdata align=16 public use32 class=CONST
47 %endif
48
49 %elifdef WIN64  ; ----(nasm -fwin64 -DWIN64 ...)--------
50 ; * Microsoft Visual C++
51
52 ; -- segment definition --
53 ;
54 %ifdef __YASM_VER__
55 %define SEG_TEXT    .text  align=16
56 %define SEG_CONST   .rdata align=16
57 %else
58 %define SEG_TEXT    .text  align=16 public use64 class=CODE
59 %define SEG_CONST   .rdata align=16 public use64 class=CONST
60 %endif
61 %define EXTN(name)  name                        ; foo() -> foo
62
63 %elifdef OBJ32  ; ----(nasm -fobj -DOBJ32 ...)----------
64 ; * Borland C++ (Win32)
65
66 ; -- segment definition --
67 ;
68 %define SEG_TEXT    .text  align=16 public use32 class=CODE
69 %define SEG_CONST   .data  align=16 public use32 class=DATA
70
71 %elifdef ELF    ; ----(nasm -felf[64] -DELF ...)------------
72 ; * Linux
73 ; * *BSD family Unix using elf format
74 ; * Unix System V, including Solaris x86, UnixWare and SCO Unix
75
76 ; mark stack as non-executable
77 section .note.GNU-stack noalloc noexec nowrite progbits
78
79 ; -- segment definition --
80 ;
81 %ifdef __x86_64__
82 %define SEG_TEXT    .text   progbits align=16
83 %define SEG_CONST   .rodata progbits align=16
84 %else
85 %define SEG_TEXT    .text   progbits alloc exec   nowrite align=16
86 %define SEG_CONST   .rodata progbits alloc noexec nowrite align=16
87 %endif
88
89 %define STRICT_MEMORY_ACCESS 1
90
91 ; To make the code position-independent, append -DPIC to the commandline
92 ;
93 %define GOT_SYMBOL  _GLOBAL_OFFSET_TABLE_       ; ELF supports PIC
94 %define EXTN(name)  name                        ; foo() -> foo
95
96 %elifdef AOUT   ; ----(nasm -faoutb/aout -DAOUT ...)----
97 ; * Older Linux using a.out format  (nasm -f aout -DAOUT ...)
98 ; * *BSD family Unix using a.out format  (nasm -f aoutb -DAOUT ...)
99
100 ; -- segment definition --
101 ;
102 %define SEG_TEXT    .text
103 %define SEG_CONST   .data
104
105 ; To make the code position-independent, append -DPIC to the commandline
106 ;
107 %define GOT_SYMBOL  __GLOBAL_OFFSET_TABLE_      ; BSD-style a.out supports PIC
108
109 %elifdef MACHO  ; ----(nasm -fmacho -DMACHO ...)--------
110 ; * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (Mach-O format)
111
112 ; -- segment definition --
113 ;
114 %define SEG_TEXT    .text  ;align=16    ; nasm doesn't accept align=16. why?
115 %define SEG_CONST   .rodata align=16
116
117 ; The generation of position-independent code (PIC) is the default on Darwin.
118 ;
119 %define PIC
120 %define GOT_SYMBOL  _MACHO_PIC_         ; Mach-O style code-relative addressing
121
122 %else           ; ----(Other case)----------------------
123
124 ; -- segment definition --
125 ;
126 %define SEG_TEXT    .text
127 %define SEG_CONST   .data
128
129 %endif  ; ----------------------------------------------
130
131 ; ==========================================================================
132
133 ; --------------------------------------------------------------------------
134 ;  Common types
135 ;
136 %ifdef __x86_64__
137 %define POINTER                 qword           ; general pointer type
138 %define SIZEOF_POINTER          SIZEOF_QWORD    ; sizeof(POINTER)
139 %define POINTER_BIT             QWORD_BIT       ; sizeof(POINTER)*BYTE_BIT
140 %else
141 %define POINTER                 dword           ; general pointer type
142 %define SIZEOF_POINTER          SIZEOF_DWORD    ; sizeof(POINTER)
143 %define POINTER_BIT             DWORD_BIT       ; sizeof(POINTER)*BYTE_BIT
144 %endif
145
146 %define INT                     dword           ; signed integer type
147 %define SIZEOF_INT              SIZEOF_DWORD    ; sizeof(INT)
148 %define INT_BIT                 DWORD_BIT       ; sizeof(INT)*BYTE_BIT
149
150 %define FP32                    dword           ; IEEE754 single
151 %define SIZEOF_FP32             SIZEOF_DWORD    ; sizeof(FP32)
152 %define FP32_BIT                DWORD_BIT       ; sizeof(FP32)*BYTE_BIT
153
154 %define MMWORD                  qword           ; int64  (MMX register)
155 %define SIZEOF_MMWORD           SIZEOF_QWORD    ; sizeof(MMWORD)
156 %define MMWORD_BIT              QWORD_BIT       ; sizeof(MMWORD)*BYTE_BIT
157
158 ; NASM is buggy and doesn't properly handle operand sizes for SSE
159 ; instructions, so for now we have to define XMMWORD as blank.
160 %define XMMWORD                                 ; int128 (SSE register)
161 %define SIZEOF_XMMWORD          SIZEOF_OWORD    ; sizeof(XMMWORD)
162 %define XMMWORD_BIT             OWORD_BIT       ; sizeof(XMMWORD)*BYTE_BIT
163
164 ; Similar hacks for when we load a dword or MMWORD into an xmm# register
165 %define XMM_DWORD
166 %define XMM_MMWORD
167
168 %define SIZEOF_BYTE             1               ; sizeof(BYTE)
169 %define SIZEOF_WORD             2               ; sizeof(WORD)
170 %define SIZEOF_DWORD            4               ; sizeof(DWORD)
171 %define SIZEOF_QWORD            8               ; sizeof(QWORD)
172 %define SIZEOF_OWORD            16              ; sizeof(OWORD)
173
174 %define BYTE_BIT                8               ; CHAR_BIT in C
175 %define WORD_BIT                16              ; sizeof(WORD)*BYTE_BIT
176 %define DWORD_BIT               32              ; sizeof(DWORD)*BYTE_BIT
177 %define QWORD_BIT               64              ; sizeof(QWORD)*BYTE_BIT
178 %define OWORD_BIT               128             ; sizeof(OWORD)*BYTE_BIT
179
180 ; --------------------------------------------------------------------------
181 ;  External Symbol Name
182 ;
183 %ifndef EXTN
184 %define EXTN(name)   _ %+ name          ; foo() -> _foo
185 %endif
186
187 ; --------------------------------------------------------------------------
188 ;  Macros for position-independent code (PIC) support
189 ;
190 %ifndef GOT_SYMBOL
191 %undef PIC
192 %endif
193
194 %ifdef PIC ; -------------------------------------------
195
196 %ifidn GOT_SYMBOL,_MACHO_PIC_ ; --------------------
197
198 ; At present, nasm doesn't seem to support PIC generation for Mach-O.
199 ; The PIC support code below is a little tricky.
200
201         SECTION SEG_CONST
202 const_base:
203
204 %define GOTOFF(got,sym) (got) + (sym) - const_base
205
206 %imacro get_GOT 1
207         ; NOTE: this macro destroys ecx resister.
208         call    %%geteip
209         add     ecx, byte (%%ref - $)
210         jmp     short %%adjust
211 %%geteip:
212         mov     ecx, POINTER [esp]
213         ret
214 %%adjust:
215         push    ebp
216         xor     ebp,ebp         ; ebp = 0
217 %ifidni %1,ebx  ; (%1 == ebx)
218         ; db 0x8D,0x9C + jmp near const_base =
219         ;   lea ebx, [ecx+ebp*8+(const_base-%%ref)] ; 8D,9C,E9,(offset32)
220         db      0x8D,0x9C               ; 8D,9C
221         jmp     near const_base         ; E9,(const_base-%%ref)
222 %%ref:
223 %else  ; (%1 != ebx)
224         ; db 0x8D,0x8C + jmp near const_base =
225         ;   lea ecx, [ecx+ebp*8+(const_base-%%ref)] ; 8D,8C,E9,(offset32)
226         db      0x8D,0x8C               ; 8D,8C
227         jmp     near const_base         ; E9,(const_base-%%ref)
228 %%ref:  mov     %1, ecx
229 %endif ; (%1 == ebx)
230         pop     ebp
231 %endmacro
232
233 %else   ; GOT_SYMBOL != _MACHO_PIC_ ----------------
234
235 %define GOTOFF(got,sym) (got) + (sym) wrt ..gotoff
236
237 %imacro get_GOT 1
238         extern  GOT_SYMBOL
239         call    %%geteip
240         add     %1, GOT_SYMBOL + $$ - $ wrt ..gotpc
241         jmp     short %%done
242 %%geteip:
243         mov     %1, POINTER [esp]
244         ret
245 %%done:
246 %endmacro
247
248 %endif  ; GOT_SYMBOL == _MACHO_PIC_ ----------------
249
250 %imacro pushpic 1.nolist
251         push    %1
252 %endmacro
253 %imacro poppic  1.nolist
254         pop     %1
255 %endmacro
256 %imacro movpic  2.nolist
257         mov     %1,%2
258 %endmacro
259
260 %else   ; !PIC -----------------------------------------
261
262 %define GOTOFF(got,sym) (sym)
263
264 %imacro get_GOT 1.nolist
265 %endmacro
266 %imacro pushpic 1.nolist
267 %endmacro
268 %imacro poppic  1.nolist
269 %endmacro
270 %imacro movpic  2.nolist
271 %endmacro
272
273 %endif  ;  PIC -----------------------------------------
274
275 ; --------------------------------------------------------------------------
276 ;  Align the next instruction on {2,4,8,16,..}-byte boundary.
277 ;  ".balign n,,m" in GNU as
278 ;
279 %define MSKLE(x,y)  (~(((y) & 0xFFFF) - ((x) & 0xFFFF)) >> 16)
280 %define FILLB(b,n)  (($$-(b)) & ((n)-1))
281
282 %imacro alignx 1-2.nolist 0xFFFF
283 %%bs:   times MSKLE(FILLB(%%bs,%1),%2) & MSKLE(16,FILLB($,%1)) & FILLB($,%1) \
284                db 0x90                               ; nop
285         times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/9 \
286                db 0x8D,0x9C,0x23,0x00,0x00,0x00,0x00 ; lea ebx,[ebx+0x00000000]
287         times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/7 \
288                db 0x8D,0xAC,0x25,0x00,0x00,0x00,0x00 ; lea ebp,[ebp+0x00000000]
289         times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/6 \
290                db 0x8D,0xAD,0x00,0x00,0x00,0x00      ; lea ebp,[ebp+0x00000000]
291         times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/4 \
292                db 0x8D,0x6C,0x25,0x00                ; lea ebp,[ebp+0x00]
293         times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/3 \
294                db 0x8D,0x6D,0x00                     ; lea ebp,[ebp+0x00]
295         times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/2 \
296                db 0x8B,0xED                          ; mov ebp,ebp
297         times MSKLE(FILLB(%%bs,%1),%2) & FILLB($,%1)/1 \
298                db 0x90                               ; nop
299 %endmacro
300
301 ; Align the next data on {2,4,8,16,..}-byte boundary.
302 ;
303 %imacro alignz 1.nolist
304         align %1, db 0          ; filling zeros
305 %endmacro
306
307 %ifdef __x86_64__
308
309 %ifdef WIN64
310
311 %imacro collect_args 0
312         push r12
313         push r13
314         push r14
315         push r15
316         mov r10, rcx
317         mov r11, rdx
318         mov r12, r8
319         mov r13, r9
320         mov r14, [rax+48]
321         mov r15, [rax+56]
322         push rsi
323         push rdi
324         sub     rsp, SIZEOF_XMMWORD
325         movlpd  XMMWORD [rsp], xmm6
326         sub     rsp, SIZEOF_XMMWORD
327         movlpd  XMMWORD [rsp], xmm7
328 %endmacro
329
330 %imacro uncollect_args 0
331         movlpd  xmm7, XMMWORD [rsp]
332         add     rsp, SIZEOF_XMMWORD
333         movlpd  xmm6, XMMWORD [rsp]
334         add     rsp, SIZEOF_XMMWORD
335         pop rdi
336         pop rsi
337         pop r15
338         pop r14
339         pop r13
340         pop r12
341 %endmacro
342
343 %else
344
345 %imacro collect_args 0
346         push r10
347         push r11
348         push r12
349         push r13
350         push r14
351         push r15
352         mov r10, rdi
353         mov r11, rsi
354         mov r12, rdx
355         mov r13, rcx
356         mov r14, r8
357         mov r15, r9
358 %endmacro
359
360 %imacro uncollect_args 0
361         pop r15
362         pop r14
363         pop r13
364         pop r12
365         pop r11
366         pop r10
367 %endmacro
368
369 %endif
370
371 %endif
372
373 ; --------------------------------------------------------------------------
374 ;  Defines picked up from the C headers
375 ;
376 %include "jsimdcfg.inc"
377
378 ; --------------------------------------------------------------------------