test/testplatform.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@1421
     1
slouken@1421
     2
#include <stdio.h>
slouken@1421
     3
slouken@1421
     4
#include "SDL.h"
slouken@1421
     5
#include "SDL_endian.h"
slouken@1421
     6
#include "SDL_cpuinfo.h"
slouken@1421
     7
slouken@1421
     8
/*
slouken@1421
     9
 * Watcom C flags these as Warning 201: "Unreachable code" if you just
slouken@1421
    10
 *  compare them directly, so we push it through a function to keep the
slouken@1421
    11
 *  compiler quiet.  --ryan.
slouken@1421
    12
 */
slouken@1662
    13
static int
slouken@1668
    14
badsize(size_t sizeoftype, size_t hardcodetype)
slouken@1421
    15
{
slouken@1421
    16
    return sizeoftype != hardcodetype;
slouken@1421
    17
}
slouken@1421
    18
slouken@1662
    19
int
slouken@1668
    20
TestTypes(SDL_bool verbose)
slouken@1421
    21
{
slouken@1662
    22
    int error = 0;
slouken@1421
    23
slouken@1668
    24
    if (badsize(sizeof(Uint8), 1)) {
slouken@1662
    25
        if (verbose)
slouken@1668
    26
            printf("sizeof(Uint8) != 1, instead = %ul\n", sizeof(Uint8));
slouken@1662
    27
        ++error;
slouken@1662
    28
    }
slouken@1668
    29
    if (badsize(sizeof(Uint16), 2)) {
slouken@1662
    30
        if (verbose)
slouken@1668
    31
            printf("sizeof(Uint16) != 2, instead = %ul\n", sizeof(Uint16));
slouken@1662
    32
        ++error;
slouken@1662
    33
    }
slouken@1668
    34
    if (badsize(sizeof(Uint32), 4)) {
slouken@1662
    35
        if (verbose)
slouken@1668
    36
            printf("sizeof(Uint32) != 4, instead = %ul\n", sizeof(Uint32));
slouken@1662
    37
        ++error;
slouken@1662
    38
    }
slouken@1421
    39
#ifdef SDL_HAS_64BIT_TYPE
slouken@1668
    40
    if (badsize(sizeof(Uint64), 8)) {
slouken@1662
    41
        if (verbose)
slouken@1668
    42
            printf("sizeof(Uint64) != 8, instead = %ul\n", sizeof(Uint64));
slouken@1662
    43
        ++error;
slouken@1662
    44
    }
slouken@1421
    45
#else
slouken@1662
    46
    if (verbose) {
slouken@1668
    47
        printf("WARNING: No 64-bit datatype on this platform\n");
slouken@1662
    48
    }
slouken@1421
    49
#endif
slouken@1662
    50
    if (verbose && !error)
slouken@1668
    51
        printf("All data types are the expected size.\n");
slouken@1421
    52
slouken@1662
    53
    return (error ? 1 : 0);
slouken@1421
    54
}
slouken@1421
    55
slouken@1662
    56
int
slouken@1668
    57
TestEndian(SDL_bool verbose)
slouken@1421
    58
{
slouken@1662
    59
    int error = 0;
slouken@1662
    60
    Uint16 value = 0x1234;
slouken@1662
    61
    int real_byteorder;
slouken@1662
    62
    Uint16 value16 = 0xCDAB;
slouken@1662
    63
    Uint16 swapped16 = 0xABCD;
slouken@1662
    64
    Uint32 value32 = 0xEFBEADDE;
slouken@1662
    65
    Uint32 swapped32 = 0xDEADBEEF;
slouken@1421
    66
#ifdef SDL_HAS_64BIT_TYPE
slouken@1662
    67
    Uint64 value64, swapped64;
slouken@1662
    68
    value64 = 0xEFBEADDE;
slouken@1662
    69
    value64 <<= 32;
slouken@1662
    70
    value64 |= 0xCDAB3412;
slouken@1662
    71
    swapped64 = 0x1234ABCD;
slouken@1662
    72
    swapped64 <<= 32;
slouken@1662
    73
    swapped64 |= 0xDEADBEEF;
slouken@1421
    74
#endif
slouken@1421
    75
slouken@1662
    76
    if (verbose) {
slouken@1668
    77
        printf("Detected a %s endian machine.\n",
slouken@1668
    78
               (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
slouken@1662
    79
    }
slouken@1662
    80
    if ((*((char *) &value) >> 4) == 0x1) {
slouken@1662
    81
        real_byteorder = SDL_BIG_ENDIAN;
slouken@1662
    82
    } else {
slouken@1662
    83
        real_byteorder = SDL_LIL_ENDIAN;
slouken@1662
    84
    }
slouken@1662
    85
    if (real_byteorder != SDL_BYTEORDER) {
slouken@1662
    86
        if (verbose) {
slouken@1668
    87
            printf("Actually a %s endian machine!\n",
slouken@1668
    88
                   (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
slouken@1662
    89
        }
slouken@1662
    90
        ++error;
slouken@1662
    91
    }
slouken@1662
    92
    if (verbose) {
slouken@1668
    93
        printf("Value 16 = 0x%X, swapped = 0x%X\n", value16,
slouken@1668
    94
               SDL_Swap16(value16));
slouken@1662
    95
    }
slouken@1668
    96
    if (SDL_Swap16(value16) != swapped16) {
slouken@1662
    97
        if (verbose) {
slouken@1668
    98
            printf("16 bit value swapped incorrectly!\n");
slouken@1662
    99
        }
slouken@1662
   100
        ++error;
slouken@1662
   101
    }
slouken@1662
   102
    if (verbose) {
slouken@1668
   103
        printf("Value 32 = 0x%X, swapped = 0x%X\n", value32,
slouken@1668
   104
               SDL_Swap32(value32));
slouken@1662
   105
    }
slouken@1668
   106
    if (SDL_Swap32(value32) != swapped32) {
slouken@1662
   107
        if (verbose) {
slouken@1668
   108
            printf("32 bit value swapped incorrectly!\n");
slouken@1662
   109
        }
slouken@1662
   110
        ++error;
slouken@1662
   111
    }
slouken@1421
   112
#ifdef SDL_HAS_64BIT_TYPE
slouken@1662
   113
    if (verbose) {
slouken@1615
   114
#ifdef _MSC_VER
slouken@1668
   115
        printf("Value 64 = 0x%I64X, swapped = 0x%I64X\n", value64,
slouken@1668
   116
               SDL_Swap64(value64));
slouken@1615
   117
#else
slouken@1668
   118
        printf("Value 64 = 0x%llX, swapped = 0x%llX\n", value64,
slouken@1668
   119
               SDL_Swap64(value64));
slouken@1615
   120
#endif
slouken@1662
   121
    }
slouken@1668
   122
    if (SDL_Swap64(value64) != swapped64) {
slouken@1662
   123
        if (verbose) {
slouken@1668
   124
            printf("64 bit value swapped incorrectly!\n");
slouken@1662
   125
        }
slouken@1662
   126
        ++error;
slouken@1662
   127
    }
slouken@1421
   128
#endif
slouken@1662
   129
    return (error ? 1 : 0);
slouken@1421
   130
}
slouken@1421
   131
slouken@1421
   132
slouken@1662
   133
int
slouken@1668
   134
TestCPUInfo(SDL_bool verbose)
slouken@1421
   135
{
slouken@1662
   136
    if (verbose) {
slouken@1668
   137
        printf("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
slouken@1668
   138
        printf("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
slouken@1668
   139
        printf("MMX Ext %s\n", SDL_HasMMXExt()? "detected" : "not detected");
slouken@1668
   140
        printf("3DNow %s\n", SDL_Has3DNow()? "detected" : "not detected");
slouken@1668
   141
        printf("3DNow Ext %s\n",
slouken@1668
   142
               SDL_Has3DNowExt()? "detected" : "not detected");
slouken@1668
   143
        printf("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
slouken@1668
   144
        printf("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
slouken@1668
   145
        printf("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
slouken@1662
   146
    }
slouken@1662
   147
    return (0);
slouken@1421
   148
}
slouken@1421
   149
slouken@1662
   150
int
slouken@1668
   151
main(int argc, char *argv[])
slouken@1421
   152
{
slouken@1662
   153
    SDL_bool verbose = SDL_TRUE;
slouken@1662
   154
    int status = 0;
slouken@1421
   155
slouken@1668
   156
    if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
slouken@1662
   157
        verbose = SDL_FALSE;
slouken@1662
   158
    }
slouken@1662
   159
    if (verbose) {
slouken@1668
   160
        printf("This system is running %s\n",
slouken@1421
   161
#if __AIX__
slouken@1668
   162
               "AIX"
slouken@1421
   163
#elif __AMIGA__
slouken@1668
   164
               "AmigaOS"
slouken@1421
   165
#elif __BEOS__
slouken@1668
   166
               "BeOS"
slouken@1421
   167
#elif __BSDI__
slouken@1668
   168
               "BSDI"
slouken@1421
   169
#elif __DREAMCAST__
slouken@1668
   170
               "Dreamcast"
slouken@1421
   171
#elif __FREEBSD__
slouken@1668
   172
               "FreeBSD"
slouken@1421
   173
#elif __HPUX__
slouken@1668
   174
               "HP-UX"
slouken@1421
   175
#elif __IRIX__
slouken@1668
   176
               "Irix"
slouken@1421
   177
#elif __LINUX__
slouken@1668
   178
               "Linux"
slouken@1421
   179
#elif __MINT__
slouken@1668
   180
               "Atari MiNT"
slouken@1421
   181
#elif __MACOS__
slouken@1668
   182
               "MacOS Classic"
slouken@1421
   183
#elif __MACOSX__
slouken@1668
   184
               "Mac OS X"
slouken@1421
   185
#elif __NETBSD__
slouken@1668
   186
               "NetBSD"
slouken@1421
   187
#elif __OPENBSD__
slouken@1668
   188
               "OpenBSD"
slouken@1421
   189
#elif __OS2__
slouken@1668
   190
               "OS/2"
slouken@1421
   191
#elif __OSF__
slouken@1668
   192
               "OSF/1"
slouken@1421
   193
#elif __QNXNTO__
slouken@1668
   194
               "QNX Neutrino"
slouken@1421
   195
#elif __RISCOS__
slouken@1668
   196
               "RISC OS"
slouken@1421
   197
#elif __SOLARIS__
slouken@1668
   198
               "Solaris"
slouken@1421
   199
#elif __WIN32__
slouken@1421
   200
#ifdef _WIN32_WCE
slouken@1668
   201
               "Windows CE"
slouken@1421
   202
#else
slouken@1668
   203
               "Windows"
slouken@1421
   204
#endif
slouken@1421
   205
#else
slouken@1668
   206
               "an unknown operating system! (see SDL_platform.h)"
slouken@1421
   207
#endif
slouken@1662
   208
            );
slouken@1662
   209
    }
slouken@1421
   210
slouken@1668
   211
    status += TestTypes(verbose);
slouken@1668
   212
    status += TestEndian(verbose);
slouken@1668
   213
    status += TestCPUInfo(verbose);
slouken@1662
   214
    return status;
slouken@1421
   215
}