test/testplatform.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 23:02:35 -0800
changeset 5263 e1122f31fec5
parent 5259 6a65c1fc07af
child 5389 24903690f48a
permissions -rw-r--r--
Fixed SSE4 detection, and split it into SSE 4.1 and 4.2
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@3663
     7
#include "SDL_assert.h"
slouken@1421
     8
slouken@1421
     9
/*
slouken@1421
    10
 * Watcom C flags these as Warning 201: "Unreachable code" if you just
slouken@1421
    11
 *  compare them directly, so we push it through a function to keep the
slouken@1421
    12
 *  compiler quiet.  --ryan.
slouken@1421
    13
 */
slouken@1895
    14
static int
slouken@1895
    15
badsize(size_t sizeoftype, size_t hardcodetype)
slouken@1421
    16
{
slouken@1421
    17
    return sizeoftype != hardcodetype;
slouken@1421
    18
}
slouken@1421
    19
slouken@1895
    20
int
slouken@1895
    21
TestTypes(SDL_bool verbose)
slouken@1421
    22
{
slouken@1895
    23
    int error = 0;
slouken@1421
    24
slouken@1895
    25
    if (badsize(sizeof(Uint8), 1)) {
slouken@1895
    26
        if (verbose)
slouken@3687
    27
            printf("sizeof(Uint8) != 1, instead = %u\n",
slouken@3687
    28
                   (unsigned int)sizeof(Uint8));
slouken@1895
    29
        ++error;
slouken@1895
    30
    }
slouken@1895
    31
    if (badsize(sizeof(Uint16), 2)) {
slouken@1895
    32
        if (verbose)
slouken@3687
    33
            printf("sizeof(Uint16) != 2, instead = %u\n",
slouken@3687
    34
                   (unsigned int)sizeof(Uint16));
slouken@1895
    35
        ++error;
slouken@1895
    36
    }
slouken@1895
    37
    if (badsize(sizeof(Uint32), 4)) {
slouken@1895
    38
        if (verbose)
slouken@3687
    39
            printf("sizeof(Uint32) != 4, instead = %u\n",
slouken@3687
    40
                   (unsigned int)sizeof(Uint32));
slouken@1895
    41
        ++error;
slouken@1895
    42
    }
slouken@1421
    43
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
    44
    if (badsize(sizeof(Uint64), 8)) {
slouken@1895
    45
        if (verbose)
slouken@3687
    46
            printf("sizeof(Uint64) != 8, instead = %u\n",
slouken@3687
    47
                   (unsigned int)sizeof(Uint64));
slouken@1895
    48
        ++error;
slouken@1895
    49
    }
slouken@1421
    50
#else
slouken@1895
    51
    if (verbose) {
slouken@1895
    52
        printf("WARNING: No 64-bit datatype on this platform\n");
slouken@1895
    53
    }
slouken@1421
    54
#endif
slouken@1895
    55
    if (verbose && !error)
slouken@1895
    56
        printf("All data types are the expected size.\n");
slouken@1421
    57
slouken@1895
    58
    return (error ? 1 : 0);
slouken@1421
    59
}
slouken@1421
    60
slouken@1895
    61
int
slouken@1895
    62
TestEndian(SDL_bool verbose)
slouken@1421
    63
{
slouken@1895
    64
    int error = 0;
slouken@1895
    65
    Uint16 value = 0x1234;
slouken@1895
    66
    int real_byteorder;
slouken@1895
    67
    Uint16 value16 = 0xCDAB;
slouken@1895
    68
    Uint16 swapped16 = 0xABCD;
slouken@1895
    69
    Uint32 value32 = 0xEFBEADDE;
slouken@1895
    70
    Uint32 swapped32 = 0xDEADBEEF;
slouken@1421
    71
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
    72
    Uint64 value64, swapped64;
slouken@1895
    73
    value64 = 0xEFBEADDE;
slouken@1895
    74
    value64 <<= 32;
slouken@1895
    75
    value64 |= 0xCDAB3412;
slouken@1895
    76
    swapped64 = 0x1234ABCD;
slouken@1895
    77
    swapped64 <<= 32;
slouken@1895
    78
    swapped64 |= 0xDEADBEEF;
slouken@1421
    79
#endif
slouken@1421
    80
slouken@1895
    81
    if (verbose) {
slouken@1895
    82
        printf("Detected a %s endian machine.\n",
slouken@1895
    83
               (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
slouken@1895
    84
    }
slouken@1895
    85
    if ((*((char *) &value) >> 4) == 0x1) {
slouken@1895
    86
        real_byteorder = SDL_BIG_ENDIAN;
slouken@1895
    87
    } else {
slouken@1895
    88
        real_byteorder = SDL_LIL_ENDIAN;
slouken@1895
    89
    }
slouken@1895
    90
    if (real_byteorder != SDL_BYTEORDER) {
slouken@1895
    91
        if (verbose) {
slouken@1895
    92
            printf("Actually a %s endian machine!\n",
slouken@1895
    93
                   (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
slouken@1895
    94
        }
slouken@1895
    95
        ++error;
slouken@1895
    96
    }
slouken@1895
    97
    if (verbose) {
slouken@1895
    98
        printf("Value 16 = 0x%X, swapped = 0x%X\n", value16,
slouken@1895
    99
               SDL_Swap16(value16));
slouken@1895
   100
    }
slouken@1895
   101
    if (SDL_Swap16(value16) != swapped16) {
slouken@1895
   102
        if (verbose) {
slouken@1895
   103
            printf("16 bit value swapped incorrectly!\n");
slouken@1895
   104
        }
slouken@1895
   105
        ++error;
slouken@1895
   106
    }
slouken@1895
   107
    if (verbose) {
slouken@1895
   108
        printf("Value 32 = 0x%X, swapped = 0x%X\n", value32,
slouken@1895
   109
               SDL_Swap32(value32));
slouken@1895
   110
    }
slouken@1895
   111
    if (SDL_Swap32(value32) != swapped32) {
slouken@1895
   112
        if (verbose) {
slouken@1895
   113
            printf("32 bit value swapped incorrectly!\n");
slouken@1895
   114
        }
slouken@1895
   115
        ++error;
slouken@1895
   116
    }
slouken@1421
   117
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
   118
    if (verbose) {
slouken@1615
   119
#ifdef _MSC_VER
slouken@1895
   120
        printf("Value 64 = 0x%I64X, swapped = 0x%I64X\n", value64,
slouken@1895
   121
               SDL_Swap64(value64));
slouken@1615
   122
#else
slouken@1895
   123
        printf("Value 64 = 0x%llX, swapped = 0x%llX\n", value64,
slouken@1895
   124
               SDL_Swap64(value64));
slouken@1615
   125
#endif
slouken@1895
   126
    }
slouken@1895
   127
    if (SDL_Swap64(value64) != swapped64) {
slouken@1895
   128
        if (verbose) {
slouken@1895
   129
            printf("64 bit value swapped incorrectly!\n");
slouken@1895
   130
        }
slouken@1895
   131
        ++error;
slouken@1895
   132
    }
slouken@1421
   133
#endif
slouken@1895
   134
    return (error ? 1 : 0);
slouken@1421
   135
}
slouken@1421
   136
slouken@1421
   137
slouken@1895
   138
int
slouken@1895
   139
TestCPUInfo(SDL_bool verbose)
slouken@1421
   140
{
slouken@1895
   141
    if (verbose) {
slouken@3579
   142
        printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5122
   143
	printf("CPU cache line size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   144
        printf("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
slouken@1895
   145
        printf("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
slouken@1895
   146
        printf("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
slouken@1895
   147
        printf("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
slouken@5259
   148
        printf("SSE3 %s\n", SDL_HasSSE3()? "detected" : "not detected");
slouken@5263
   149
        printf("SSE4.1 %s\n", SDL_HasSSE41()? "detected" : "not detected");
slouken@5263
   150
        printf("SSE4.2 %s\n", SDL_HasSSE42()? "detected" : "not detected");
slouken@1895
   151
    }
slouken@1895
   152
    return (0);
slouken@1421
   153
}
slouken@1421
   154
slouken@1895
   155
int
slouken@3663
   156
TestAssertions(SDL_bool verbose)
slouken@3663
   157
{
slouken@3663
   158
    SDL_assert(1);
slouken@3663
   159
    SDL_assert_release(1);
slouken@3663
   160
    SDL_assert_paranoid(1);
slouken@3663
   161
    SDL_assert(0 || 1);
slouken@3663
   162
    SDL_assert_release(0 || 1);
slouken@3663
   163
    SDL_assert_paranoid(0 || 1);
slouken@3663
   164
slouken@3663
   165
#if 0   /* enable this to test assertion failures. */
slouken@3663
   166
    SDL_assert_release(1 == 2);
slouken@3663
   167
    SDL_assert_release(5 < 4);
slouken@3663
   168
    SDL_assert_release(0 && "This is a test");
slouken@3663
   169
#endif
slouken@3663
   170
slouken@3663
   171
    return (0);
slouken@3663
   172
}
slouken@3663
   173
slouken@3663
   174
int
slouken@1895
   175
main(int argc, char *argv[])
slouken@1421
   176
{
slouken@1895
   177
    SDL_bool verbose = SDL_TRUE;
slouken@1895
   178
    int status = 0;
slouken@1421
   179
slouken@1895
   180
    if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
slouken@1895
   181
        verbose = SDL_FALSE;
slouken@1895
   182
    }
slouken@1895
   183
    if (verbose) {
slouken@3319
   184
        printf("This system is running %s\n", SDL_GetPlatform());
slouken@1895
   185
    }
slouken@1421
   186
slouken@1895
   187
    status += TestTypes(verbose);
slouken@1895
   188
    status += TestEndian(verbose);
slouken@1895
   189
    status += TestCPUInfo(verbose);
slouken@3663
   190
    status += TestAssertions(verbose);
slouken@3663
   191
slouken@1895
   192
    return status;
slouken@1421
   193
}