linux_cpu.c 4.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*****************************************************************************
 * linux_cpu.c: CPU detection code for Linux
 *****************************************************************************
 * Copyright (C) 2012 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <vlc_common.h>
#include <vlc_cpu.h>

#undef CPU_FLAGS
#if defined (__arm__)
# define CPU_FLAGS "Features\t:"

#elif defined (__i386__) || defined (__x86_64__)
# define CPU_FLAGS "flags\t\t:"

#elif defined (__powerpc__) || defined (__powerpc64__)
# define CPU_FLAGS "cpu\t\t:"

#endif

#ifdef CPU_FLAGS
static uint32_t cpu_flags = 0;

static void vlc_CPU_init (void)
{
    FILE *info = fopen ("/proc/cpuinfo", "rt");
    if (info == NULL)
        return;

    char *line = NULL;
    size_t linelen = 0;
    uint_fast32_t all_caps = 0xFFFFFFFF;

    while (getline (&line, &linelen, info) != -1)
    {
        if (strncmp (line, CPU_FLAGS, strlen (CPU_FLAGS)))
            continue;

        char *p = line, *cap;
        uint_fast32_t core_caps = 0;

        while ((cap = strsep (&p, " ")) != NULL)
        {
#if defined (__arm__)
            if (!strcmp (cap, "neon"))
67
                core_caps |= VLC_CPU_ARM_NEON;
68 69 70

#elif defined (__i386__) || defined (__x86_64__)
            if (!strcmp (cap, "mmx"))
71
                core_caps |= VLC_CPU_MMX;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
# ifndef __SSE__
            if (!strcmp (cap, "sse"))
                core_caps |= CPU_CAPABILITY_SSE | CPU_CAPABILITY_MMXEXT;
            if (!strcmp (cap, "mmxext"))
                core_caps |= CPU_CAPABILITY_MMXEXT;
# endif
# ifndef __SSE2__
            if (!strcmp (cap, "sse2"))
                core_caps |= CPU_CAPABILITY_SSE2;
# endif
# ifndef __SSE3__
            if (!strcmp (cap, "pni"))
                core_caps |= CPU_CAPABILITY_SSE3;
# endif
# ifndef __SSSE3__
            if (!strcmp (cap, "ssse3"))
                core_caps |= CPU_CAPABILITY_SSSE3;
# endif
# ifndef __SSE4_1__
            if (!strcmp (cap, "sse4_1"))
                core_caps |= CPU_CAPABILITY_SSE4_1;
# endif
# ifndef __SSE4_2__
            if (!strcmp (cap, "sse4_2"))
                core_caps |= CPU_CAPABILITY_SSE4_1;
# endif
98 99
            if (!strcmp (cap, "sse4a"))
                core_caps |= CPU_CAPABILITY_SSE4A;
100 101 102 103 104 105 106
# ifndef __3dNOW__
            if (!strcmp (cap, "3dnow"))
                core_caps |= CPU_CAPABILITY_3DNOW;
# endif

#elif defined (__powerpc__) || defined (__powerpc64__)
            if (!strcmp (cap, "altivec supported"))
107
                core_caps |= VLC_CPU_ALTIVEC;
108 109 110 111 112 113 114 115 116 117 118 119 120
#endif
        }

        /* Take the intersection of capabilities of each processor */
        all_caps &= core_caps;
    }
    fclose (info);
    free (line);

    if (all_caps == 0xFFFFFFFF) /* Error parsing of cpuinfo? */
        all_caps = 0; /* Do not assume any capability! */

    /* Always enable capabilities that were forced during compilation */
121
#if defined (__i386__) || defined (__x86_64__)
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
# ifdef __SSE__
    all_caps |= CPU_CAPABILITY_SSE | CPU_CAPABILITY_MMXEXT;
# endif
# ifdef __SSE2__
    all_caps |= CPU_CAPABILITY_SSE2;
# endif
# ifdef __SSE3__
    all_caps |= CPU_CAPABILITY_SSE3;
# endif
# ifdef __SSSE3__
    all_caps |= CPU_CAPABILITY_SSSE3;
# endif
# ifdef __SSE4_1__
    all_caps |= CPU_CAPABILITY_SSE4_1;
# endif
# ifdef __SSE4_2__
    all_caps |= CPU_CAPABILITY_SSE4_2;
# endif
# ifdef __3dNOW__
    all_caps |= CPU_CAPABILITY_3DNOW;
# endif

#endif
    cpu_flags = all_caps;
}

unsigned vlc_CPU (void)
{
    static pthread_once_t once = PTHREAD_ONCE_INIT;

    pthread_once (&once, vlc_CPU_init);
    return cpu_flags;
}
#else /* CPU_FLAGS */
unsigned vlc_CPU (void)
{
    return 0;
}
#endif