src/stdlib/SDL_stdlib.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 29 Aug 2017 00:36:17 -0400
changeset 11404 bd5b569b2a1b
parent 11341 8b3231952c22
child 11682 b26412a89fbb
permissions -rw-r--r--
stdlib: An implementation of SDL_scalbn using ldexp() (thanks, Ozkan!).

Fixes Bugzilla #3767.
slouken@1330
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@1330
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1330
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1330
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1330
    20
*/
icculus@9306
    21
icculus@9306
    22
#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
icculus@9306
    23
#define SDL_DISABLE_ANALYZE_MACROS 1
icculus@9306
    24
#endif
icculus@9306
    25
icculus@8093
    26
#include "../SDL_internal.h"
slouken@1330
    27
slouken@1330
    28
/* This file contains portable stdlib functions for SDL */
slouken@1330
    29
slouken@1354
    30
#include "SDL_stdinc.h"
slouken@7351
    31
#include "../libm/math_libm.h"
slouken@1330
    32
slouken@7351
    33
slouken@7351
    34
double
slouken@7351
    35
SDL_atan(double x)
slouken@7351
    36
{
slouken@10505
    37
#if defined(HAVE_ATAN)
slouken@7351
    38
    return atan(x);
slouken@7351
    39
#else
slouken@7351
    40
    return SDL_uclibc_atan(x);
slouken@7351
    41
#endif /* HAVE_ATAN */
slouken@7351
    42
}
slouken@7351
    43
slouken@7351
    44
double
slouken@7351
    45
SDL_atan2(double x, double y)
slouken@7351
    46
{
slouken@7351
    47
#if defined(HAVE_ATAN2)
slouken@7351
    48
    return atan2(x, y);
slouken@7351
    49
#else
slouken@7351
    50
    return SDL_uclibc_atan2(x, y);
slouken@7351
    51
#endif /* HAVE_ATAN2 */
slouken@7351
    52
}
slouken@7351
    53
slouken@7351
    54
double
icculus@8056
    55
SDL_acos(double val)
icculus@8056
    56
{
icculus@8056
    57
#if defined(HAVE_ACOS)
icculus@8056
    58
    return acos(val);
icculus@8056
    59
#else
icculus@8056
    60
    double result;
icculus@8056
    61
    if (val == -1.0) {
icculus@8056
    62
        result = M_PI;
icculus@8056
    63
    } else {
icculus@8056
    64
        result = SDL_atan(SDL_sqrt(1.0 - val * val) / val);
icculus@8056
    65
        if (result < 0.0)
icculus@8056
    66
        {
icculus@8056
    67
            result += M_PI;
icculus@8056
    68
        }
icculus@8056
    69
    }
icculus@8056
    70
    return result;
icculus@8056
    71
#endif
icculus@8056
    72
}
icculus@8056
    73
icculus@8056
    74
double
icculus@8056
    75
SDL_asin(double val)
icculus@8056
    76
{
icculus@8056
    77
#if defined(HAVE_ASIN)
icculus@8056
    78
    return asin(val);
icculus@8056
    79
#else
icculus@8056
    80
    double result;
icculus@8056
    81
    if (val == -1.0) {
icculus@8056
    82
        result = -(M_PI / 2.0);
icculus@8056
    83
    } else {
icculus@8056
    84
        result = (M_PI / 2.0) - SDL_acos(val);
icculus@8056
    85
    }
icculus@8056
    86
    return result;
icculus@8056
    87
#endif
icculus@8056
    88
}
icculus@8056
    89
icculus@8056
    90
double
slouken@7351
    91
SDL_ceil(double x)
slouken@7351
    92
{
slouken@10505
    93
#if defined(HAVE_CEIL)
slouken@7351
    94
    return ceil(x);
slouken@7351
    95
#else
slouken@8033
    96
    double integer = SDL_floor(x);
slouken@8033
    97
    double fraction = x - integer;
slouken@8033
    98
    if (fraction > 0.0) {
slouken@8033
    99
        integer += 1.0;
slouken@8033
   100
    }
slouken@8033
   101
    return integer;
slouken@7351
   102
#endif /* HAVE_CEIL */
slouken@7351
   103
}
slouken@7351
   104
slouken@7351
   105
double
slouken@7351
   106
SDL_copysign(double x, double y)
slouken@7351
   107
{
slouken@7351
   108
#if defined(HAVE_COPYSIGN)
slouken@7351
   109
    return copysign(x, y);
dludwig@8478
   110
#elif defined(HAVE__COPYSIGN)
dludwig@8478
   111
    return _copysign(x, y);
slouken@11341
   112
#elif defined(__WATCOMC__) && defined(__386__)
slouken@11341
   113
    /* this is nasty as hell, but it works.. */
slouken@11341
   114
    unsigned int *xi = (unsigned int *) &x,
slouken@11341
   115
                 *yi = (unsigned int *) &y;
slouken@11341
   116
    xi[1] = (yi[1] & 0x80000000) | (xi[1] & 0x7fffffff);
slouken@11341
   117
    return x;
slouken@7351
   118
#else
slouken@7351
   119
    return SDL_uclibc_copysign(x, y);
slouken@7351
   120
#endif /* HAVE_COPYSIGN */
slouken@7351
   121
}
slouken@7351
   122
slouken@7351
   123
double
slouken@7351
   124
SDL_cos(double x)
slouken@7351
   125
{
slouken@7351
   126
#if defined(HAVE_COS)
slouken@7351
   127
    return cos(x);
slouken@7351
   128
#else
slouken@7351
   129
    return SDL_uclibc_cos(x);
slouken@7351
   130
#endif /* HAVE_COS */
slouken@7351
   131
}
slouken@7351
   132
slouken@7351
   133
float
slouken@7351
   134
SDL_cosf(float x)
slouken@7351
   135
{
slouken@10505
   136
#if defined(HAVE_COSF)
slouken@7351
   137
    return cosf(x);
slouken@7351
   138
#else
slouken@7351
   139
    return (float)SDL_cos((double)x);
slouken@7351
   140
#endif
slouken@7351
   141
}
slouken@7351
   142
slouken@7351
   143
double
slouken@7351
   144
SDL_fabs(double x)
slouken@7351
   145
{
slouken@7351
   146
#if defined(HAVE_FABS)
slouken@7351
   147
    return fabs(x); 
slouken@7351
   148
#else
slouken@7351
   149
    return SDL_uclibc_fabs(x);
slouken@7351
   150
#endif /* HAVE_FABS */
slouken@7351
   151
}
slouken@7351
   152
slouken@7351
   153
double
slouken@7351
   154
SDL_floor(double x)
slouken@7351
   155
{
slouken@7351
   156
#if defined(HAVE_FLOOR)
slouken@7351
   157
    return floor(x);
slouken@7351
   158
#else
slouken@7351
   159
    return SDL_uclibc_floor(x);
slouken@7351
   160
#endif /* HAVE_FLOOR */
slouken@7351
   161
}
slouken@7351
   162
slouken@7351
   163
double
slouken@7351
   164
SDL_log(double x)
slouken@7351
   165
{
slouken@7351
   166
#if defined(HAVE_LOG)
slouken@7351
   167
    return log(x);
slouken@7351
   168
#else
slouken@7351
   169
    return SDL_uclibc_log(x);
slouken@7351
   170
#endif /* HAVE_LOG */
slouken@7351
   171
}
slouken@7351
   172
slouken@7351
   173
double
slouken@7351
   174
SDL_pow(double x, double y)
slouken@7351
   175
{
slouken@7351
   176
#if defined(HAVE_POW)
slouken@7351
   177
    return pow(x, y);
slouken@7351
   178
#else
slouken@7351
   179
    return SDL_uclibc_pow(x, y);
slouken@7351
   180
#endif /* HAVE_POW */
slouken@7351
   181
}
slouken@7351
   182
slouken@7351
   183
double
slouken@7351
   184
SDL_scalbn(double x, int n)
slouken@7351
   185
{
slouken@7351
   186
#if defined(HAVE_SCALBN)
slouken@7351
   187
    return scalbn(x, n);
dludwig@8478
   188
#elif defined(HAVE__SCALB)
dludwig@8478
   189
    return _scalb(x, n);
icculus@11404
   190
#elif defined(HAVE_LIBC) && defined(HAVE_FLOAT_H) && (FLT_RADIX == 2)
icculus@11404
   191
/* from scalbn(3): If FLT_RADIX equals 2 (which is
icculus@11404
   192
 * usual), then scalbn() is equivalent to ldexp(3). */
icculus@11404
   193
    return ldexp(x, n);
slouken@7351
   194
#else
slouken@7351
   195
    return SDL_uclibc_scalbn(x, n);
slouken@7351
   196
#endif /* HAVE_SCALBN */
slouken@7351
   197
}
slouken@7351
   198
slouken@7351
   199
double
slouken@7351
   200
SDL_sin(double x)
slouken@7351
   201
{
slouken@7351
   202
#if defined(HAVE_SIN)
slouken@7351
   203
    return sin(x);
slouken@7351
   204
#else
slouken@7351
   205
    return SDL_uclibc_sin(x);
slouken@7351
   206
#endif /* HAVE_SIN */
slouken@7351
   207
}
slouken@7351
   208
slouken@7351
   209
float 
slouken@7351
   210
SDL_sinf(float x)
slouken@7351
   211
{
slouken@10505
   212
#if defined(HAVE_SINF)
slouken@7351
   213
    return sinf(x);
slouken@7351
   214
#else
slouken@7351
   215
    return (float)SDL_sin((double)x);
slouken@7351
   216
#endif /* HAVE_SINF */
slouken@7351
   217
}
slouken@7351
   218
slouken@7351
   219
double
slouken@7351
   220
SDL_sqrt(double x)
slouken@7351
   221
{
slouken@7351
   222
#if defined(HAVE_SQRT)
slouken@7351
   223
    return sqrt(x);
slouken@7351
   224
#else
slouken@7351
   225
    return SDL_uclibc_sqrt(x);
slouken@7351
   226
#endif
slouken@7351
   227
}
slouken@7351
   228
slouken@8840
   229
float
slouken@8840
   230
SDL_sqrtf(float x)
slouken@8840
   231
{
slouken@8840
   232
#if defined(HAVE_SQRTF)
slouken@8840
   233
    return sqrtf(x);
slouken@8840
   234
#else
slouken@8840
   235
    return (float)SDL_sqrt((double)x);
slouken@8840
   236
#endif
slouken@8840
   237
}
slouken@8840
   238
slouken@8840
   239
double
slouken@8840
   240
SDL_tan(double x)
slouken@8840
   241
{
slouken@8840
   242
#if defined(HAVE_TAN)
slouken@8840
   243
    return tan(x);
slouken@8840
   244
#else
slouken@8840
   245
    return SDL_uclibc_tan(x);
slouken@8840
   246
#endif
slouken@8840
   247
}
slouken@8840
   248
slouken@8840
   249
float
slouken@8840
   250
SDL_tanf(float x)
slouken@8840
   251
{
slouken@8840
   252
#if defined(HAVE_TANF)
slouken@8840
   253
    return tanf(x);
slouken@8840
   254
#else
slouken@8840
   255
    return (float)SDL_tan((double)x);
slouken@8840
   256
#endif
slouken@8840
   257
}
slouken@8840
   258
slouken@7351
   259
int SDL_abs(int x)
slouken@7351
   260
{
slouken@10505
   261
#if defined(HAVE_ABS)
slouken@7351
   262
    return abs(x);
slouken@7351
   263
#else
slouken@7351
   264
    return ((x) < 0 ? -(x) : (x));
slouken@7351
   265
#endif
slouken@7351
   266
}
slouken@7351
   267
slouken@10505
   268
#if defined(HAVE_CTYPE_H)
slouken@7351
   269
int SDL_isdigit(int x) { return isdigit(x); }
slouken@7351
   270
int SDL_isspace(int x) { return isspace(x); }
slouken@7351
   271
int SDL_toupper(int x) { return toupper(x); }
slouken@7351
   272
int SDL_tolower(int x) { return tolower(x); }
slouken@7351
   273
#else
slouken@7351
   274
int SDL_isdigit(int x) { return ((x) >= '0') && ((x) <= '9'); }
philipp@7357
   275
int SDL_isspace(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n') || ((x) == '\f') || ((x) == '\v'); }
slouken@7351
   276
int SDL_toupper(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A'+((x)-'a')) : (x); }
slouken@7351
   277
int SDL_tolower(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a'+((x)-'A')) : (x); }
slouken@7351
   278
#endif
icculus@7003
   279
icculus@7003
   280
slouken@1330
   281
#ifndef HAVE_LIBC
slouken@1330
   282
/* These are some C runtime intrinsics that need to be defined */
slouken@1330
   283
slouken@3255
   284
#if defined(_MSC_VER)
slouken@1330
   285
slouken@1433
   286
#ifndef __FLTUSED__
slouken@1433
   287
#define __FLTUSED__
slouken@3255
   288
__declspec(selectany) int _fltused = 1;
slouken@1433
   289
#endif
slouken@3255
   290
icculus@9716
   291
/* The optimizer on Visual Studio 2005 and later generates memcpy() calls */
slouken@10550
   292
#if (_MSC_VER >= 1400) && defined(_WIN64) && !defined(_DEBUG) && !(_MSC_VER >= 1900 && defined(_MT))
slouken@5455
   293
#include <intrin.h>
slouken@5455
   294
slouken@5455
   295
#pragma function(memcpy)
slouken@5455
   296
void * memcpy ( void * destination, const void * source, size_t num )
slouken@5455
   297
{
slouken@5455
   298
    const Uint8 *src = (const Uint8 *)source;
slouken@5455
   299
    Uint8 *dst = (Uint8 *)destination;
slouken@5455
   300
    size_t i;
slouken@5455
   301
    
slouken@5455
   302
    /* All WIN64 architectures have SSE, right? */
slouken@5455
   303
    if (!((uintptr_t) src & 15) && !((uintptr_t) dst & 15)) {
slouken@5455
   304
        __m128 values[4];
slouken@5455
   305
        for (i = num / 64; i--;) {
slouken@5455
   306
            _mm_prefetch(src, _MM_HINT_NTA);
slouken@5455
   307
            values[0] = *(__m128 *) (src + 0);
slouken@5455
   308
            values[1] = *(__m128 *) (src + 16);
slouken@5455
   309
            values[2] = *(__m128 *) (src + 32);
slouken@5455
   310
            values[3] = *(__m128 *) (src + 48);
slouken@5455
   311
            _mm_stream_ps((float *) (dst + 0), values[0]);
slouken@5455
   312
            _mm_stream_ps((float *) (dst + 16), values[1]);
slouken@5455
   313
            _mm_stream_ps((float *) (dst + 32), values[2]);
slouken@5455
   314
            _mm_stream_ps((float *) (dst + 48), values[3]);
slouken@5455
   315
            src += 64;
slouken@5455
   316
            dst += 64;
slouken@5455
   317
        }
slouken@5455
   318
        num &= 63;
slouken@5455
   319
    }
slouken@5455
   320
slouken@5455
   321
    while (num--) {
slouken@5455
   322
        *dst++ = *src++;
slouken@5455
   323
    }
slouken@5455
   324
    return destination;
slouken@5455
   325
}
slouken@5455
   326
#endif /* _MSC_VER == 1600 && defined(_WIN64) && !defined(_DEBUG) */
slouken@5455
   327
slouken@5086
   328
#ifdef _M_IX86
slouken@1433
   329
slouken@1346
   330
/* Float to long */
slouken@3253
   331
void
slouken@3253
   332
__declspec(naked)
slouken@3253
   333
_ftol()
slouken@1345
   334
{
slouken@1895
   335
    /* *INDENT-OFF* */
slouken@1895
   336
    __asm {
slouken@1895
   337
        push        ebp
slouken@1895
   338
        mov         ebp,esp
slouken@1895
   339
        sub         esp,20h
slouken@1895
   340
        and         esp,0FFFFFFF0h
slouken@1895
   341
        fld         st(0)
slouken@1895
   342
        fst         dword ptr [esp+18h]
slouken@1895
   343
        fistp       qword ptr [esp+10h]
slouken@1895
   344
        fild        qword ptr [esp+10h]
slouken@1895
   345
        mov         edx,dword ptr [esp+18h]
slouken@1895
   346
        mov         eax,dword ptr [esp+10h]
slouken@1895
   347
        test        eax,eax
slouken@1895
   348
        je          integer_QnaN_or_zero
slouken@1346
   349
arg_is_not_integer_QnaN:
slouken@1895
   350
        fsubp       st(1),st
slouken@1895
   351
        test        edx,edx
slouken@1895
   352
        jns         positive
slouken@1895
   353
        fstp        dword ptr [esp]
slouken@1895
   354
        mov         ecx,dword ptr [esp]
slouken@1895
   355
        xor         ecx,80000000h
slouken@1895
   356
        add         ecx,7FFFFFFFh
slouken@1895
   357
        adc         eax,0
slouken@1895
   358
        mov         edx,dword ptr [esp+14h]
slouken@1895
   359
        adc         edx,0
slouken@1895
   360
        jmp         localexit
slouken@1346
   361
positive:
slouken@1895
   362
        fstp        dword ptr [esp]
slouken@1895
   363
        mov         ecx,dword ptr [esp]
slouken@1895
   364
        add         ecx,7FFFFFFFh
slouken@1895
   365
        sbb         eax,0
slouken@1895
   366
        mov         edx,dword ptr [esp+14h]
slouken@1895
   367
        sbb         edx,0
slouken@1895
   368
        jmp         localexit
slouken@1346
   369
integer_QnaN_or_zero:
slouken@1895
   370
        mov         edx,dword ptr [esp+14h]
slouken@1895
   371
        test        edx,7FFFFFFFh
slouken@1895
   372
        jne         arg_is_not_integer_QnaN
slouken@1895
   373
        fstp        dword ptr [esp+18h]
slouken@1895
   374
        fstp        dword ptr [esp+18h]
slouken@1346
   375
localexit:
slouken@1895
   376
        leave
slouken@1895
   377
        ret
slouken@1895
   378
    }
slouken@1895
   379
    /* *INDENT-ON* */
slouken@1345
   380
}
slouken@1895
   381
slouken@1895
   382
void
slouken@1895
   383
_ftol2_sse()
slouken@1330
   384
{
slouken@1895
   385
    _ftol();
slouken@1330
   386
}
slouken@1330
   387
slouken@10587
   388
/* 64-bit math operators for 32-bit systems */
slouken@10587
   389
void
slouken@10587
   390
__declspec(naked)
slouken@10587
   391
_allmul()
slouken@10587
   392
{
slouken@10587
   393
    /* *INDENT-OFF* */
slouken@10587
   394
    __asm {
slouken@10587
   395
        mov         eax, dword ptr[esp+8]
slouken@10587
   396
        mov         ecx, dword ptr[esp+10h]
slouken@10587
   397
        or          ecx, eax
slouken@10587
   398
        mov         ecx, dword ptr[esp+0Ch]
slouken@10587
   399
        jne         hard
slouken@10587
   400
        mov         eax, dword ptr[esp+4]
slouken@10587
   401
        mul         ecx
slouken@10587
   402
        ret         10h
slouken@10587
   403
hard:
slouken@10587
   404
        push        ebx
slouken@10587
   405
        mul         ecx
slouken@10587
   406
        mov         ebx, eax
slouken@10587
   407
        mov         eax, dword ptr[esp+8]
slouken@10587
   408
        mul         dword ptr[esp+14h]
slouken@10587
   409
        add         ebx, eax
slouken@10587
   410
        mov         eax, dword ptr[esp+8]
slouken@10587
   411
        mul         ecx
slouken@10587
   412
        add         edx, ebx
slouken@10587
   413
        pop         ebx
slouken@10587
   414
        ret         10h
slouken@10587
   415
    }
slouken@10587
   416
    /* *INDENT-ON* */
slouken@1330
   417
}
slouken@2735
   418
slouken@1895
   419
void
slouken@1895
   420
__declspec(naked)
slouken@1895
   421
_alldiv()
slouken@1330
   422
{
slouken@1895
   423
    /* *INDENT-OFF* */
slouken@1895
   424
    __asm {
slouken@1895
   425
        push        edi
slouken@1895
   426
        push        esi
slouken@1895
   427
        push        ebx
slouken@1895
   428
        xor         edi,edi
slouken@1895
   429
        mov         eax,dword ptr [esp+14h]
slouken@1895
   430
        or          eax,eax
slouken@1895
   431
        jge         L1
slouken@1895
   432
        inc         edi
slouken@1895
   433
        mov         edx,dword ptr [esp+10h]
slouken@1895
   434
        neg         eax
slouken@1895
   435
        neg         edx
slouken@1895
   436
        sbb         eax,0
slouken@1895
   437
        mov         dword ptr [esp+14h],eax
slouken@1895
   438
        mov         dword ptr [esp+10h],edx
slouken@1346
   439
L1:
slouken@1895
   440
        mov         eax,dword ptr [esp+1Ch]
slouken@1895
   441
        or          eax,eax
slouken@1895
   442
        jge         L2
slouken@1895
   443
        inc         edi
slouken@1895
   444
        mov         edx,dword ptr [esp+18h]
slouken@1895
   445
        neg         eax
slouken@1895
   446
        neg         edx
slouken@1895
   447
        sbb         eax,0
slouken@1895
   448
        mov         dword ptr [esp+1Ch],eax
slouken@1895
   449
        mov         dword ptr [esp+18h],edx
slouken@1346
   450
L2:
slouken@1895
   451
        or          eax,eax
slouken@1895
   452
        jne         L3
slouken@1895
   453
        mov         ecx,dword ptr [esp+18h]
slouken@1895
   454
        mov         eax,dword ptr [esp+14h]
slouken@1895
   455
        xor         edx,edx
slouken@1895
   456
        div         ecx
slouken@1895
   457
        mov         ebx,eax
slouken@1895
   458
        mov         eax,dword ptr [esp+10h]
slouken@1895
   459
        div         ecx
slouken@1895
   460
        mov         edx,ebx
slouken@1895
   461
        jmp         L4
slouken@1346
   462
L3:
slouken@1895
   463
        mov         ebx,eax
slouken@1895
   464
        mov         ecx,dword ptr [esp+18h]
slouken@1895
   465
        mov         edx,dword ptr [esp+14h]
slouken@1895
   466
        mov         eax,dword ptr [esp+10h]
slouken@1346
   467
L5:
slouken@1895
   468
        shr         ebx,1
slouken@1895
   469
        rcr         ecx,1
slouken@1895
   470
        shr         edx,1
slouken@1895
   471
        rcr         eax,1
slouken@1895
   472
        or          ebx,ebx
slouken@1895
   473
        jne         L5
slouken@1895
   474
        div         ecx
slouken@1895
   475
        mov         esi,eax
slouken@1895
   476
        mul         dword ptr [esp+1Ch]
slouken@1895
   477
        mov         ecx,eax
slouken@1895
   478
        mov         eax,dword ptr [esp+18h]
slouken@1895
   479
        mul         esi
slouken@1895
   480
        add         edx,ecx
slouken@1895
   481
        jb          L6
slouken@1895
   482
        cmp         edx,dword ptr [esp+14h]
slouken@1895
   483
        ja          L6
slouken@1895
   484
        jb          L7
slouken@1895
   485
        cmp         eax,dword ptr [esp+10h]
slouken@1895
   486
        jbe         L7
slouken@1346
   487
L6:
slouken@1895
   488
        dec         esi
slouken@1346
   489
L7:
slouken@1895
   490
        xor         edx,edx
slouken@1895
   491
        mov         eax,esi
slouken@1346
   492
L4:
slouken@1895
   493
        dec         edi
slouken@1895
   494
        jne         L8
slouken@1895
   495
        neg         edx
slouken@1895
   496
        neg         eax
slouken@1895
   497
        sbb         edx,0
slouken@1346
   498
L8:
slouken@1895
   499
        pop         ebx
slouken@1895
   500
        pop         esi
slouken@1895
   501
        pop         edi
slouken@1895
   502
        ret         10h
slouken@1895
   503
    }
slouken@1895
   504
    /* *INDENT-ON* */
slouken@1330
   505
}
slouken@2735
   506
slouken@1895
   507
void
slouken@1895
   508
__declspec(naked)
slouken@1895
   509
_aulldiv()
slouken@1330
   510
{
slouken@1895
   511
    /* *INDENT-OFF* */
slouken@1895
   512
    __asm {
slouken@1895
   513
        push        ebx
slouken@1895
   514
        push        esi
slouken@1895
   515
        mov         eax,dword ptr [esp+18h]
slouken@1895
   516
        or          eax,eax
slouken@1895
   517
        jne         L1
slouken@1895
   518
        mov         ecx,dword ptr [esp+14h]
slouken@1895
   519
        mov         eax,dword ptr [esp+10h]
slouken@1895
   520
        xor         edx,edx
slouken@1895
   521
        div         ecx
slouken@1895
   522
        mov         ebx,eax
slouken@1895
   523
        mov         eax,dword ptr [esp+0Ch]
slouken@1895
   524
        div         ecx
slouken@1895
   525
        mov         edx,ebx
slouken@1895
   526
        jmp         L2
slouken@1346
   527
L1:
slouken@1895
   528
        mov         ecx,eax
slouken@1895
   529
        mov         ebx,dword ptr [esp+14h]
slouken@1895
   530
        mov         edx,dword ptr [esp+10h]
slouken@1895
   531
        mov         eax,dword ptr [esp+0Ch]
slouken@1346
   532
L3:
slouken@1895
   533
        shr         ecx,1
slouken@1895
   534
        rcr         ebx,1
slouken@1895
   535
        shr         edx,1
slouken@1895
   536
        rcr         eax,1
slouken@1895
   537
        or          ecx,ecx
slouken@1895
   538
        jne         L3
slouken@1895
   539
        div         ebx
slouken@1895
   540
        mov         esi,eax
slouken@1895
   541
        mul         dword ptr [esp+18h]
slouken@1895
   542
        mov         ecx,eax
slouken@1895
   543
        mov         eax,dword ptr [esp+14h]
slouken@1895
   544
        mul         esi
slouken@1895
   545
        add         edx,ecx
slouken@1895
   546
        jb          L4
slouken@1895
   547
        cmp         edx,dword ptr [esp+10h]
slouken@1895
   548
        ja          L4
slouken@1895
   549
        jb          L5
slouken@1895
   550
        cmp         eax,dword ptr [esp+0Ch]
slouken@1895
   551
        jbe         L5
slouken@1346
   552
L4:
slouken@1895
   553
        dec         esi
slouken@1346
   554
L5:
slouken@1895
   555
        xor         edx,edx
slouken@1895
   556
        mov         eax,esi
slouken@1346
   557
L2:
slouken@1895
   558
        pop         esi
slouken@1895
   559
        pop         ebx
slouken@1895
   560
        ret         10h
slouken@1895
   561
    }
slouken@1895
   562
    /* *INDENT-ON* */
slouken@1330
   563
}
slouken@2735
   564
slouken@1895
   565
void
slouken@1895
   566
__declspec(naked)
slouken@1895
   567
_allrem()
slouken@1330
   568
{
slouken@1895
   569
    /* *INDENT-OFF* */
slouken@1895
   570
    __asm {
slouken@1895
   571
        push        ebx
slouken@1895
   572
        push        edi
slouken@1895
   573
        xor         edi,edi
slouken@1895
   574
        mov         eax,dword ptr [esp+10h]
slouken@1895
   575
        or          eax,eax
slouken@1895
   576
        jge         L1
slouken@1895
   577
        inc         edi
slouken@1895
   578
        mov         edx,dword ptr [esp+0Ch]
slouken@1895
   579
        neg         eax
slouken@1895
   580
        neg         edx
slouken@1895
   581
        sbb         eax,0
slouken@1895
   582
        mov         dword ptr [esp+10h],eax
slouken@1895
   583
        mov         dword ptr [esp+0Ch],edx
slouken@1346
   584
L1:
slouken@1895
   585
        mov         eax,dword ptr [esp+18h]
slouken@1895
   586
        or          eax,eax
slouken@1895
   587
        jge         L2
slouken@1895
   588
        mov         edx,dword ptr [esp+14h]
slouken@1895
   589
        neg         eax
slouken@1895
   590
        neg         edx
slouken@1895
   591
        sbb         eax,0
slouken@1895
   592
        mov         dword ptr [esp+18h],eax
slouken@1895
   593
        mov         dword ptr [esp+14h],edx
slouken@1346
   594
L2:
slouken@1895
   595
        or          eax,eax
slouken@1895
   596
        jne         L3
slouken@1895
   597
        mov         ecx,dword ptr [esp+14h]
slouken@1895
   598
        mov         eax,dword ptr [esp+10h]
slouken@1895
   599
        xor         edx,edx
slouken@1895
   600
        div         ecx
slouken@1895
   601
        mov         eax,dword ptr [esp+0Ch]
slouken@1895
   602
        div         ecx
slouken@1895
   603
        mov         eax,edx
slouken@1895
   604
        xor         edx,edx
slouken@1895
   605
        dec         edi
slouken@1895
   606
        jns         L4
slouken@1895
   607
        jmp         L8
slouken@1346
   608
L3:
slouken@1895
   609
        mov         ebx,eax
slouken@1895
   610
        mov         ecx,dword ptr [esp+14h]
slouken@1895
   611
        mov         edx,dword ptr [esp+10h]
slouken@1895
   612
        mov         eax,dword ptr [esp+0Ch]
slouken@1346
   613
L5:
slouken@1895
   614
        shr         ebx,1
slouken@1895
   615
        rcr         ecx,1
slouken@1895
   616
        shr         edx,1
slouken@1895
   617
        rcr         eax,1
slouken@1895
   618
        or          ebx,ebx
slouken@1895
   619
        jne         L5
slouken@1895
   620
        div         ecx
slouken@1895
   621
        mov         ecx,eax
slouken@1895
   622
        mul         dword ptr [esp+18h]
slouken@1895
   623
        xchg        eax,ecx
slouken@1895
   624
        mul         dword ptr [esp+14h]
slouken@1895
   625
        add         edx,ecx
slouken@1895
   626
        jb          L6
slouken@1895
   627
        cmp         edx,dword ptr [esp+10h]
slouken@1895
   628
        ja          L6
slouken@1895
   629
        jb          L7
slouken@1895
   630
        cmp         eax,dword ptr [esp+0Ch]
slouken@1895
   631
        jbe         L7
slouken@1346
   632
L6:
slouken@1895
   633
        sub         eax,dword ptr [esp+14h]
slouken@1895
   634
        sbb         edx,dword ptr [esp+18h]
slouken@1346
   635
L7:
slouken@1895
   636
        sub         eax,dword ptr [esp+0Ch]
slouken@1895
   637
        sbb         edx,dword ptr [esp+10h]
slouken@1895
   638
        dec         edi
slouken@1895
   639
        jns         L8
slouken@1346
   640
L4:
slouken@1895
   641
        neg         edx
slouken@1895
   642
        neg         eax
slouken@1895
   643
        sbb         edx,0
slouken@1346
   644
L8:
slouken@1895
   645
        pop         edi
slouken@1895
   646
        pop         ebx
slouken@1895
   647
        ret         10h
slouken@1895
   648
    }
slouken@1895
   649
    /* *INDENT-ON* */
slouken@1330
   650
}
slouken@2735
   651
slouken@1895
   652
void
slouken@1895
   653
__declspec(naked)
slouken@1895
   654
_aullrem()
slouken@1330
   655
{
slouken@1895
   656
    /* *INDENT-OFF* */
slouken@1895
   657
    __asm {
slouken@1895
   658
        push        ebx
slouken@1895
   659
        mov         eax,dword ptr [esp+14h]
slouken@1895
   660
        or          eax,eax
slouken@1895
   661
        jne         L1
slouken@1895
   662
        mov         ecx,dword ptr [esp+10h]
slouken@1895
   663
        mov         eax,dword ptr [esp+0Ch]
slouken@1895
   664
        xor         edx,edx
slouken@1895
   665
        div         ecx
slouken@1895
   666
        mov         eax,dword ptr [esp+8]
slouken@1895
   667
        div         ecx
slouken@1895
   668
        mov         eax,edx
slouken@1895
   669
        xor         edx,edx
slouken@1895
   670
        jmp         L2
slouken@1346
   671
L1:
slouken@1895
   672
        mov         ecx,eax
slouken@1895
   673
        mov         ebx,dword ptr [esp+10h]
slouken@1895
   674
        mov         edx,dword ptr [esp+0Ch]
slouken@1895
   675
        mov         eax,dword ptr [esp+8]
slouken@1346
   676
L3:
slouken@1895
   677
        shr         ecx,1
slouken@1895
   678
        rcr         ebx,1
slouken@1895
   679
        shr         edx,1
slouken@1895
   680
        rcr         eax,1
slouken@1895
   681
        or          ecx,ecx
slouken@1895
   682
        jne         L3
slouken@1895
   683
        div         ebx
slouken@1895
   684
        mov         ecx,eax
slouken@1895
   685
        mul         dword ptr [esp+14h]
slouken@1895
   686
        xchg        eax,ecx
slouken@1895
   687
        mul         dword ptr [esp+10h]
slouken@1895
   688
        add         edx,ecx
slouken@1895
   689
        jb          L4
slouken@1895
   690
        cmp         edx,dword ptr [esp+0Ch]
slouken@1895
   691
        ja          L4
slouken@1895
   692
        jb          L5
slouken@1895
   693
        cmp         eax,dword ptr [esp+8]
slouken@1895
   694
        jbe         L5
slouken@1346
   695
L4:
slouken@1895
   696
        sub         eax,dword ptr [esp+10h]
slouken@1895
   697
        sbb         edx,dword ptr [esp+14h]
slouken@1346
   698
L5:
slouken@1895
   699
        sub         eax,dword ptr [esp+8]
slouken@1895
   700
        sbb         edx,dword ptr [esp+0Ch]
slouken@1895
   701
        neg         edx
slouken@1895
   702
        neg         eax
slouken@1895
   703
        sbb         edx,0
slouken@1346
   704
L2:
slouken@1895
   705
        pop         ebx
slouken@1895
   706
        ret         10h
slouken@1895
   707
    }
slouken@1895
   708
    /* *INDENT-ON* */
slouken@1330
   709
}
slouken@2735
   710
slouken@1895
   711
void
slouken@1895
   712
__declspec(naked)
slouken@1895
   713
_alldvrm()
slouken@1330
   714
{
slouken@1895
   715
    /* *INDENT-OFF* */
slouken@1895
   716
    __asm {
slouken@1895
   717
        push        edi
slouken@1895
   718
        push        esi
slouken@1895
   719
        push        ebp
slouken@1895
   720
        xor         edi,edi
slouken@1895
   721
        xor         ebp,ebp
slouken@1895
   722
        mov         eax,dword ptr [esp+14h]
slouken@1895
   723
        or          eax,eax
slouken@1895
   724
        jge         L1
slouken@1895
   725
        inc         edi
slouken@1895
   726
        inc         ebp
slouken@1895
   727
        mov         edx,dword ptr [esp+10h]
slouken@1895
   728
        neg         eax
slouken@1895
   729
        neg         edx
slouken@1895
   730
        sbb         eax,0
slouken@1895
   731
        mov         dword ptr [esp+14h],eax
slouken@1895
   732
        mov         dword ptr [esp+10h],edx
slouken@1346
   733
L1:
slouken@1895
   734
        mov         eax,dword ptr [esp+1Ch]
slouken@1895
   735
        or          eax,eax
slouken@1895
   736
        jge         L2
slouken@1895
   737
        inc         edi
slouken@1895
   738
        mov         edx,dword ptr [esp+18h]
slouken@1895
   739
        neg         eax
slouken@1895
   740
        neg         edx
slouken@1895
   741
        sbb         eax,0
slouken@1895
   742
        mov         dword ptr [esp+1Ch],eax
slouken@1895
   743
        mov         dword ptr [esp+18h],edx
slouken@1346
   744
L2:
slouken@1895
   745
        or          eax,eax
slouken@1895
   746
        jne         L3
slouken@1895
   747
        mov         ecx,dword ptr [esp+18h]
slouken@1895
   748
        mov         eax,dword ptr [esp+14h]
slouken@1895
   749
        xor         edx,edx
slouken@1895
   750
        div         ecx
slouken@1895
   751
        mov         ebx,eax
slouken@1895
   752
        mov         eax,dword ptr [esp+10h]
slouken@1895
   753
        div         ecx
slouken@1895
   754
        mov         esi,eax
slouken@1895
   755
        mov         eax,ebx
slouken@1895
   756
        mul         dword ptr [esp+18h]
slouken@1895
   757
        mov         ecx,eax
slouken@1895
   758
        mov         eax,esi
slouken@1895
   759
        mul         dword ptr [esp+18h]
slouken@1895
   760
        add         edx,ecx
slouken@1895
   761
        jmp         L4
slouken@1346
   762
L3:
slouken@1895
   763
        mov         ebx,eax
slouken@1895
   764
        mov         ecx,dword ptr [esp+18h]
slouken@1895
   765
        mov         edx,dword ptr [esp+14h]
slouken@1895
   766
        mov         eax,dword ptr [esp+10h]
slouken@1346
   767
L5:
slouken@1895
   768
        shr         ebx,1
slouken@1895
   769
        rcr         ecx,1
slouken@1895
   770
        shr         edx,1
slouken@1895
   771
        rcr         eax,1
slouken@1895
   772
        or          ebx,ebx
slouken@1895
   773
        jne         L5
slouken@1895
   774
        div         ecx
slouken@1895
   775
        mov         esi,eax
slouken@1895
   776
        mul         dword ptr [esp+1Ch]
slouken@1895
   777
        mov         ecx,eax
slouken@1895
   778
        mov         eax,dword ptr [esp+18h]
slouken@1895
   779
        mul         esi
slouken@1895
   780
        add         edx,ecx
slouken@1895
   781
        jb          L6
slouken@1895
   782
        cmp         edx,dword ptr [esp+14h]
slouken@1895
   783
        ja          L6
slouken@1895
   784
        jb          L7
slouken@1895
   785
        cmp         eax,dword ptr [esp+10h]
slouken@1895
   786
        jbe         L7
slouken@1346
   787
L6:
slouken@1895
   788
        dec         esi
slouken@1895
   789
        sub         eax,dword ptr [esp+18h]
slouken@1895
   790
        sbb         edx,dword ptr [esp+1Ch]
slouken@1346
   791
L7:
slouken@1895
   792
        xor         ebx,ebx
slouken@1346
   793
L4:
slouken@1895
   794
        sub         eax,dword ptr [esp+10h]
slouken@1895
   795
        sbb         edx,dword ptr [esp+14h]
slouken@1895
   796
        dec         ebp
slouken@1895
   797
        jns         L9
slouken@1895
   798
        neg         edx
slouken@1895
   799
        neg         eax
slouken@1895
   800
        sbb         edx,0
slouken@1346
   801
L9:
slouken@1895
   802
        mov         ecx,edx
slouken@1895
   803
        mov         edx,ebx
slouken@1895
   804
        mov         ebx,ecx
slouken@1895
   805
        mov         ecx,eax
slouken@1895
   806
        mov         eax,esi
slouken@1895
   807
        dec         edi
slouken@1895
   808
        jne         L8
slouken@1895
   809
        neg         edx
slouken@1895
   810
        neg         eax
slouken@1895
   811
        sbb         edx,0
slouken@1346
   812
L8:
slouken@1895
   813
        pop         ebp
slouken@1895
   814
        pop         esi
slouken@1895
   815
        pop         edi
slouken@1895
   816
        ret         10h
slouken@1895
   817
    }
slouken@1895
   818
    /* *INDENT-ON* */
slouken@1330
   819
}
slouken@2735
   820
slouken@1895
   821
void
slouken@1895
   822
__declspec(naked)
slouken@1895
   823
_aulldvrm()
slouken@1330
   824
{
slouken@1895
   825
    /* *INDENT-OFF* */
slouken@1895
   826
    __asm {
slouken@1895
   827
        push        esi
slouken@1895
   828
        mov         eax,dword ptr [esp+14h]
slouken@1895
   829
        or          eax,eax
slouken@1895
   830
        jne         L1
slouken@1895
   831
        mov         ecx,dword ptr [esp+10h]
slouken@1895
   832
        mov         eax,dword ptr [esp+0Ch]
slouken@1895
   833
        xor         edx,edx
slouken@1895
   834
        div         ecx
slouken@1895
   835
        mov         ebx,eax
slouken@1895
   836
        mov         eax,dword ptr [esp+8]
slouken@1895
   837
        div         ecx
slouken@1895
   838
        mov         esi,eax
slouken@1895
   839
        mov         eax,ebx
slouken@1895
   840
        mul         dword ptr [esp+10h]
slouken@1895
   841
        mov         ecx,eax
slouken@1895
   842
        mov         eax,esi
slouken@1895
   843
        mul         dword ptr [esp+10h]
slouken@1895
   844
        add         edx,ecx
slouken@1895
   845
        jmp         L2
slouken@1346
   846
L1:
slouken@1895
   847
        mov         ecx,eax
slouken@1895
   848
        mov         ebx,dword ptr [esp+10h]
slouken@1895
   849
        mov         edx,dword ptr [esp+0Ch]
slouken@1895
   850
        mov         eax,dword ptr [esp+8]
slouken@1346
   851
L3:
slouken@1895
   852
        shr         ecx,1
slouken@1895
   853
        rcr         ebx,1
slouken@1895
   854
        shr         edx,1
slouken@1895
   855
        rcr         eax,1
slouken@1895
   856
        or          ecx,ecx
slouken@1895
   857
        jne         L3
slouken@1895
   858
        div         ebx
slouken@1895
   859
        mov         esi,eax
slouken@1895
   860
        mul         dword ptr [esp+14h]
slouken@1895
   861
        mov         ecx,eax
slouken@1895
   862
        mov         eax,dword ptr [esp+10h]
slouken@1895
   863
        mul         esi
slouken@1895
   864
        add         edx,ecx
slouken@1895
   865
        jb          L4
slouken@1895
   866
        cmp         edx,dword ptr [esp+0Ch]
slouken@1895
   867
        ja          L4
slouken@1895
   868
        jb          L5
slouken@1895
   869
        cmp         eax,dword ptr [esp+8]
slouken@1895
   870
        jbe         L5
slouken@1346
   871
L4:
slouken@1895
   872
        dec         esi
slouken@1895
   873
        sub         eax,dword ptr [esp+10h]
slouken@1895
   874
        sbb         edx,dword ptr [esp+14h]
slouken@1346
   875
L5:
slouken@1895
   876
        xor         ebx,ebx
slouken@1346
   877
L2:
slouken@1895
   878
        sub         eax,dword ptr [esp+8]
slouken@1895
   879
        sbb         edx,dword ptr [esp+0Ch]
slouken@1895
   880
        neg         edx
slouken@1895
   881
        neg         eax
slouken@1895
   882
        sbb         edx,0
slouken@1895
   883
        mov         ecx,edx
slouken@1895
   884
        mov         edx,ebx
slouken@1895
   885
        mov         ebx,ecx
slouken@1895
   886
        mov         ecx,eax
slouken@1895
   887
        mov         eax,esi
slouken@1895
   888
        pop         esi
slouken@1895
   889
        ret         10h
slouken@1895
   890
    }
slouken@1895
   891
    /* *INDENT-ON* */
slouken@1330
   892
}
slouken@2735
   893
slouken@1895
   894
void
slouken@1895
   895
__declspec(naked)
slouken@1895
   896
_allshl()
slouken@1330
   897
{
slouken@1895
   898
    /* *INDENT-OFF* */
slouken@1895
   899
    __asm {
slouken@1895
   900
        cmp         cl,40h
slouken@1895
   901
        jae         RETZERO
slouken@1895
   902
        cmp         cl,20h
slouken@1895
   903
        jae         MORE32
slouken@1895
   904
        shld        edx,eax,cl
slouken@1895
   905
        shl         eax,cl
slouken@1895
   906
        ret
slouken@1346
   907
MORE32:
slouken@1895
   908
        mov         edx,eax
slouken@1895
   909
        xor         eax,eax
slouken@1895
   910
        and         cl,1Fh
slouken@1895
   911
        shl         edx,cl
slouken@1895
   912
        ret
slouken@1346
   913
RETZERO:
slouken@1895
   914
        xor         eax,eax
slouken@1895
   915
        xor         edx,edx
slouken@1895
   916
        ret
slouken@1895
   917
    }
slouken@1895
   918
    /* *INDENT-ON* */
slouken@1330
   919
}
slouken@2735
   920
slouken@1895
   921
void
slouken@1895
   922
__declspec(naked)
slouken@2760
   923
_allshr()
slouken@2760
   924
{
slouken@2760
   925
    /* *INDENT-OFF* */
slouken@2760
   926
    __asm {
slouken@10587
   927
        cmp         cl,3Fh
slouken@10587
   928
        jae         RETSIGN
slouken@2760
   929
        cmp         cl,20h
slouken@2760
   930
        jae         MORE32
slouken@2760
   931
        shrd        eax,edx,cl
slouken@2760
   932
        sar         edx,cl
slouken@2760
   933
        ret
slouken@2760
   934
MORE32:
slouken@2760
   935
        mov         eax,edx
slouken@10587
   936
        sar         edx,1Fh
slouken@2760
   937
        and         cl,1Fh
slouken@2760
   938
        sar         eax,cl
slouken@2760
   939
        ret
slouken@10587
   940
RETSIGN:
slouken@10587
   941
        sar         edx,1Fh
slouken@10587
   942
        mov         eax,edx
slouken@2760
   943
        ret
slouken@2760
   944
    }
slouken@2760
   945
    /* *INDENT-ON* */
slouken@2760
   946
}
slouken@2760
   947
slouken@2760
   948
void
slouken@2760
   949
__declspec(naked)
slouken@1895
   950
_aullshr()
slouken@1330
   951
{
slouken@1895
   952
    /* *INDENT-OFF* */
slouken@1895
   953
    __asm {
slouken@1895
   954
        cmp         cl,40h
slouken@1895
   955
        jae         RETZERO
slouken@1895
   956
        cmp         cl,20h
slouken@1895
   957
        jae         MORE32
slouken@1895
   958
        shrd        eax,edx,cl
slouken@1895
   959
        shr         edx,cl
slouken@1895
   960
        ret
slouken@1346
   961
MORE32:
slouken@1895
   962
        mov         eax,edx
slouken@1895
   963
        xor         edx,edx
slouken@1895
   964
        and         cl,1Fh
slouken@1895
   965
        shr         eax,cl
slouken@1895
   966
        ret
slouken@1346
   967
RETZERO:
slouken@1895
   968
        xor         eax,eax
slouken@1895
   969
        xor         edx,edx
slouken@1895
   970
        ret
slouken@1895
   971
    }
slouken@1895
   972
    /* *INDENT-ON* */
slouken@1330
   973
}
slouken@1330
   974
slouken@5455
   975
#endif /* _M_IX86 */
slouken@3255
   976
slouken@1330
   977
#endif /* MSC_VER */
slouken@1330
   978
slouken@1331
   979
#endif /* !HAVE_LIBC */
slouken@1895
   980
slouken@1895
   981
/* vi: set ts=4 sw=4 expandtab: */