test/testplatform.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:33:31 -0800
changeset 8148 56ba41ac64fe
parent 7823 4ab3a5ee041d
child 8149 681eb46b8ac4
permissions -rw-r--r--
Fixed bug 2376 - no SDL_HasAVX

Haneef Mubarak

AVX is the successor to SSE* and is fairly widely available. As such, it really ought to be detectable.

This functionality ought to be trivial to implement, and not having it means being forced to write an ugly workaround to check for AVX (so that normal SSE can be used if AVX is not available).

Here is an example on detecting AVX from SO (it actually shows ways to cehck for all of teh fancy instructions):

http://stackoverflow.com/questions/6121792/how-to-check-if-a-cpu-supports-the-sse3-instruction-set
     1 /*
     2   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 #include <stdio.h>
    14 
    15 #include "SDL.h"
    16 #include "SDL_endian.h"
    17 #include "SDL_cpuinfo.h"
    18 #include "SDL_assert.h"
    19 
    20 /*
    21  * Watcom C flags these as Warning 201: "Unreachable code" if you just
    22  *  compare them directly, so we push it through a function to keep the
    23  *  compiler quiet.  --ryan.
    24  */
    25 static int
    26 badsize(size_t sizeoftype, size_t hardcodetype)
    27 {
    28     return sizeoftype != hardcodetype;
    29 }
    30 
    31 int
    32 TestTypes(SDL_bool verbose)
    33 {
    34     int error = 0;
    35 
    36     if (badsize(sizeof(Uint8), 1)) {
    37         if (verbose)
    38             SDL_Log("sizeof(Uint8) != 1, instead = %u\n",
    39                    (unsigned int)sizeof(Uint8));
    40         ++error;
    41     }
    42     if (badsize(sizeof(Uint16), 2)) {
    43         if (verbose)
    44             SDL_Log("sizeof(Uint16) != 2, instead = %u\n",
    45                    (unsigned int)sizeof(Uint16));
    46         ++error;
    47     }
    48     if (badsize(sizeof(Uint32), 4)) {
    49         if (verbose)
    50             SDL_Log("sizeof(Uint32) != 4, instead = %u\n",
    51                    (unsigned int)sizeof(Uint32));
    52         ++error;
    53     }
    54     if (badsize(sizeof(Uint64), 8)) {
    55         if (verbose)
    56             SDL_Log("sizeof(Uint64) != 8, instead = %u\n",
    57                    (unsigned int)sizeof(Uint64));
    58         ++error;
    59     }
    60     if (verbose && !error)
    61         SDL_Log("All data types are the expected size.\n");
    62 
    63     return (error ? 1 : 0);
    64 }
    65 
    66 int
    67 TestEndian(SDL_bool verbose)
    68 {
    69     int error = 0;
    70     Uint16 value = 0x1234;
    71     int real_byteorder;
    72     Uint16 value16 = 0xCDAB;
    73     Uint16 swapped16 = 0xABCD;
    74     Uint32 value32 = 0xEFBEADDE;
    75     Uint32 swapped32 = 0xDEADBEEF;
    76     Uint64 value64, swapped64;
    77 
    78     value64 = 0xEFBEADDE;
    79     value64 <<= 32;
    80     value64 |= 0xCDAB3412;
    81     swapped64 = 0x1234ABCD;
    82     swapped64 <<= 32;
    83     swapped64 |= 0xDEADBEEF;
    84 
    85     if (verbose) {
    86         SDL_Log("Detected a %s endian machine.\n",
    87                (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
    88     }
    89     if ((*((char *) &value) >> 4) == 0x1) {
    90         real_byteorder = SDL_BIG_ENDIAN;
    91     } else {
    92         real_byteorder = SDL_LIL_ENDIAN;
    93     }
    94     if (real_byteorder != SDL_BYTEORDER) {
    95         if (verbose) {
    96             SDL_Log("Actually a %s endian machine!\n",
    97                    (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
    98         }
    99         ++error;
   100     }
   101     if (verbose) {
   102         SDL_Log("Value 16 = 0x%X, swapped = 0x%X\n", value16,
   103                SDL_Swap16(value16));
   104     }
   105     if (SDL_Swap16(value16) != swapped16) {
   106         if (verbose) {
   107             SDL_Log("16 bit value swapped incorrectly!\n");
   108         }
   109         ++error;
   110     }
   111     if (verbose) {
   112         SDL_Log("Value 32 = 0x%X, swapped = 0x%X\n", value32,
   113                SDL_Swap32(value32));
   114     }
   115     if (SDL_Swap32(value32) != swapped32) {
   116         if (verbose) {
   117             SDL_Log("32 bit value swapped incorrectly!\n");
   118         }
   119         ++error;
   120     }
   121     if (verbose) {
   122 #ifdef _MSC_VER
   123         SDL_Log("Value 64 = 0x%I64X, swapped = 0x%I64X\n", value64,
   124                SDL_Swap64(value64));
   125 #else
   126         SDL_Log("Value 64 = 0x%llX, swapped = 0x%llX\n",
   127                (unsigned long long) value64,
   128                (unsigned long long) SDL_Swap64(value64));
   129 #endif
   130     }
   131     if (SDL_Swap64(value64) != swapped64) {
   132         if (verbose) {
   133             SDL_Log("64 bit value swapped incorrectly!\n");
   134         }
   135         ++error;
   136     }
   137     return (error ? 1 : 0);
   138 }
   139 
   140 
   141 int
   142 TestCPUInfo(SDL_bool verbose)
   143 {
   144     if (verbose) {
   145         SDL_Log("CPU count: %d\n", SDL_GetCPUCount());
   146         SDL_Log("CPU cache line size: %d\n", SDL_GetCPUCacheLineSize());
   147         SDL_Log("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
   148         SDL_Log("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
   149         SDL_Log("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
   150         SDL_Log("3DNow! %s\n", SDL_Has3DNow()? "detected" : "not detected");
   151         SDL_Log("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
   152         SDL_Log("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
   153         SDL_Log("SSE3 %s\n", SDL_HasSSE3()? "detected" : "not detected");
   154         SDL_Log("SSE4.1 %s\n", SDL_HasSSE41()? "detected" : "not detected");
   155         SDL_Log("SSE4.2 %s\n", SDL_HasSSE42()? "detected" : "not detected");
   156         SDL_Log("AVX %s\n", SDL_HasAVX()? "detected" : "not detected");
   157         SDL_Log("System RAM %d MB\n", SDL_GetSystemRAM());
   158     }
   159     return (0);
   160 }
   161 
   162 int
   163 TestAssertions(SDL_bool verbose)
   164 {
   165     SDL_assert(1);
   166     SDL_assert_release(1);
   167     SDL_assert_paranoid(1);
   168     SDL_assert(0 || 1);
   169     SDL_assert_release(0 || 1);
   170     SDL_assert_paranoid(0 || 1);
   171 
   172 #if 0   /* enable this to test assertion failures. */
   173     SDL_assert_release(1 == 2);
   174     SDL_assert_release(5 < 4);
   175     SDL_assert_release(0 && "This is a test");
   176 #endif
   177 
   178     {
   179         const SDL_assert_data *item = SDL_GetAssertionReport();
   180         while (item) {
   181             SDL_Log("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\n",
   182                 item->condition, item->function, item->filename,
   183                 item->linenum, item->trigger_count,
   184                 item->always_ignore ? "yes" : "no");
   185             item = item->next;
   186         }
   187     }
   188     return (0);
   189 }
   190 
   191 int
   192 main(int argc, char *argv[])
   193 {
   194     SDL_bool verbose = SDL_TRUE;
   195     int status = 0;
   196 
   197     /* Enable standard application logging */
   198     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   199 
   200     if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
   201         verbose = SDL_FALSE;
   202     }
   203     if (verbose) {
   204         SDL_Log("This system is running %s\n", SDL_GetPlatform());
   205     }
   206 
   207     status += TestTypes(verbose);
   208     status += TestEndian(verbose);
   209     status += TestCPUInfo(verbose);
   210     status += TestAssertions(verbose);
   211 
   212     return status;
   213 }