ntdll: Make NTDLL_mergesort() stable.
[wine/multimedia.git] / dlls / ntdll / misc.c
bloba2c08c5815eea23da919d90067155534439b7457
1 /*
2 * Helper functions for ntdll
4 * Copyright 2000 Juergen Schmied
5 * Copyright 2010 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <time.h>
25 #include <math.h>
26 #ifdef HAVE_SYS_UTSNAME_H
27 #include <sys/utsname.h>
28 #endif
30 #include "wine/library.h"
31 #include "wine/debug.h"
32 #include "ntdll_misc.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
36 #if defined(__GNUC__) && defined(__i386__)
37 #define DO_FPU(x,y) __asm__ __volatile__( x " %0;fwait" : "=m" (y) : )
38 #define POP_FPU(x) DO_FPU("fstpl",x)
39 #endif
41 LPCSTR debugstr_ObjectAttributes(const OBJECT_ATTRIBUTES *oa)
43 if (!oa) return "<null>";
44 return wine_dbg_sprintf( "{name=%s, attr=0x%08x, hRoot=%p, sd=%p}\n",
45 debugstr_us(oa->ObjectName), oa->Attributes,
46 oa->RootDirectory, oa->SecurityDescriptor );
49 LPCSTR debugstr_us( const UNICODE_STRING *us )
51 if (!us) return "<null>";
52 return debugstr_wn(us->Buffer, us->Length / sizeof(WCHAR));
55 /*********************************************************************
56 * _ftol (NTDLL.@)
58 * VERSION
59 * [GNUC && i386]
61 #if defined(__GNUC__) && defined(__i386__)
62 LONGLONG CDECL NTDLL__ftol(void)
64 /* don't just do DO_FPU("fistp",retval), because the rounding
65 * mode must also be set to "round towards zero"... */
66 double fl;
67 POP_FPU(fl);
68 return (LONGLONG)fl;
70 #endif /* defined(__GNUC__) && defined(__i386__) */
72 /*********************************************************************
73 * _ftol (NTDLL.@)
75 * FIXME
76 * Should be register function
77 * VERSION
78 * [!GNUC && i386]
80 #if !defined(__GNUC__) && defined(__i386__)
81 LONGLONG CDECL NTDLL__ftol(double fl)
83 FIXME("should be register function\n");
84 return (LONGLONG)fl;
86 #endif /* !defined(__GNUC__) && defined(__i386__) */
88 /*********************************************************************
89 * _CIpow (NTDLL.@)
90 * VERSION
91 * [GNUC && i386]
93 #if defined(__GNUC__) && defined(__i386__)
94 double CDECL NTDLL__CIpow(void)
96 double x,y;
97 POP_FPU(y);
98 POP_FPU(x);
99 return pow(x,y);
101 #endif /* defined(__GNUC__) && defined(__i386__) */
104 /*********************************************************************
105 * _CIpow (NTDLL.@)
107 * FIXME
108 * Should be register function
110 * VERSION
111 * [!GNUC && i386]
113 #if !defined(__GNUC__) && defined(__i386__)
114 double CDECL NTDLL__CIpow(double x,double y)
116 FIXME("should be register function\n");
117 return pow(x,y);
119 #endif /* !defined(__GNUC__) && defined(__i386__) */
121 /*********************************************************************
122 * wine_get_version (NTDLL.@)
124 const char * CDECL NTDLL_wine_get_version(void)
126 return wine_get_version();
129 /*********************************************************************
130 * wine_get_build_id (NTDLL.@)
132 const char * CDECL NTDLL_wine_get_build_id(void)
134 return wine_get_build_id();
137 /*********************************************************************
138 * wine_get_host_version (NTDLL.@)
140 void CDECL NTDLL_wine_get_host_version( const char **sysname, const char **release )
142 #ifdef HAVE_SYS_UTSNAME_H
143 static struct utsname buf;
144 static int init_done;
146 if (!init_done)
148 uname( &buf );
149 init_done = 1;
151 if (sysname) *sysname = buf.sysname;
152 if (release) *release = buf.release;
153 #else
154 if (sysname) *sysname = "";
155 if (release) *release = "";
156 #endif
159 /*********************************************************************
160 * abs (NTDLL.@)
162 int CDECL NTDLL_abs( int i )
164 return abs( i );
167 /*********************************************************************
168 * labs (NTDLL.@)
170 LONG CDECL NTDLL_labs( LONG i )
172 return labs( i );
175 /*********************************************************************
176 * atan (NTDLL.@)
178 double CDECL NTDLL_atan( double d )
180 return atan( d );
183 /*********************************************************************
184 * ceil (NTDLL.@)
186 double CDECL NTDLL_ceil( double d )
188 return ceil( d );
191 /*********************************************************************
192 * cos (NTDLL.@)
194 double CDECL NTDLL_cos( double d )
196 return cos( d );
199 /*********************************************************************
200 * fabs (NTDLL.@)
202 double CDECL NTDLL_fabs( double d )
204 return fabs( d );
207 /*********************************************************************
208 * floor (NTDLL.@)
210 double CDECL NTDLL_floor( double d )
212 return floor( d );
215 /*********************************************************************
216 * log (NTDLL.@)
218 double CDECL NTDLL_log( double d )
220 return log( d );
223 /*********************************************************************
224 * pow (NTDLL.@)
226 double CDECL NTDLL_pow( double x, double y )
228 return pow( x, y );
231 /*********************************************************************
232 * sin (NTDLL.@)
234 double CDECL NTDLL_sin( double d )
236 return sin( d );
239 /*********************************************************************
240 * sqrt (NTDLL.@)
242 double CDECL NTDLL_sqrt( double d )
244 return sqrt( d );
247 /*********************************************************************
248 * tan (NTDLL.@)
250 double CDECL NTDLL_tan( double d )
252 return tan( d );
256 /* Merge Sort. Algorithm taken from http://www.linux-related.de/index.html?/coding/sort/sort_merge.htm */
257 static void
258 NTDLL_mergesort( void *arr, void *barr, size_t elemsize, int(__cdecl *compar)(const void *, const void *),
259 size_t left, size_t right )
261 if(right>left) {
262 size_t i, j, k, m;
263 m=(right+left)/2;
264 NTDLL_mergesort( arr, barr, elemsize, compar, left, m);
265 NTDLL_mergesort( arr, barr, elemsize, compar, m+1, right);
267 #define X(a,i) ((char*)a+elemsize*(i))
268 for (i=m+1; i>left; i--)
269 memcpy (X(barr,(i-1)),X(arr,(i-1)),elemsize);
270 for (j=m; j<right; j++)
271 memcpy (X(barr,(right+m-j)),X(arr,(j+1)),elemsize);
273 /* i=left; j=right; */
274 for (k=left; i<=m && j>m; k++) {
275 if (compar(X(barr,i),X(barr,j))<=0) {
276 memcpy(X(arr,k),X(barr,i),elemsize);
277 i++;
278 } else {
279 memcpy(X(arr,k),X(barr,j),elemsize);
280 j--;
283 for (; i<=m; i++, k++)
284 memcpy(X(arr,k),X(barr,i),elemsize);
285 for (; j>m; j--, k++)
286 memcpy(X(arr,k),X(barr,j),elemsize);
288 #undef X
291 /*********************************************************************
292 * qsort (NTDLL.@)
294 void __cdecl NTDLL_qsort( void *base, size_t nmemb, size_t size,
295 int(__cdecl *compar)(const void *, const void *) )
297 void *secondarr = RtlAllocateHeap (GetProcessHeap(), 0, nmemb*size);
298 NTDLL_mergesort( base, secondarr, size, compar, 0, nmemb-1 );
299 RtlFreeHeap (GetProcessHeap(),0, secondarr);
302 /*********************************************************************
303 * bsearch (NTDLL.@)
305 void * __cdecl
306 NTDLL_bsearch( const void *key, const void *base, size_t nmemb,
307 size_t size, int (__cdecl *compar)(const void *, const void *) )
309 ssize_t min = 0;
310 ssize_t max = nmemb - 1;
312 while (min <= max)
314 ssize_t cursor = (min + max) / 2;
315 int ret = compar(key,(const char *)base+(cursor*size));
316 if (!ret)
317 return (char*)base+(cursor*size);
318 if (ret < 0)
319 max = cursor - 1;
320 else
321 min = cursor + 1;
323 return NULL;
327 /*********************************************************************
328 * _lfind (NTDLL.@)
330 void * __cdecl _lfind( const void *key, const void *base, unsigned int *nmemb,
331 size_t size, int(__cdecl *compar)(const void *, const void *) )
333 size_t i, n = *nmemb;
335 for (i=0;i<n;i++)
336 if (!compar(key,(char*)base+(size*i)))
337 return (char*)base+(size*i);
338 return NULL;