001    // --- BEGIN LICENSE BLOCK ---
002    /* 
003     * Copyright (c) 2009, Mikio L. Braun
004     * All rights reserved.
005     * 
006     * Redistribution and use in source and binary forms, with or without
007     * modification, are permitted provided that the following conditions are
008     * met:
009     * 
010     *     * Redistributions of source code must retain the above copyright
011     *       notice, this list of conditions and the following disclaimer.
012     * 
013     *     * Redistributions in binary form must reproduce the above
014     *       copyright notice, this list of conditions and the following
015     *       disclaimer in the documentation and/or other materials provided
016     *       with the distribution.
017     * 
018     *     * Neither the name of the Technische Universit?t Berlin nor the
019     *       names of its contributors may be used to endorse or promote
020     *       products derived from this software without specific prior
021     *       written permission.
022     * 
023     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
024     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
025     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
026     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
027     * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
028     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
029     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
030     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
031     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
033     * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034     */
035    // --- END LICENSE BLOCK ---
036    package org.jblas.util;
037    
038    /**
039     *
040     */
041    public class ArchFlavor {
042    
043        static {
044            try { 
045                System.loadLibrary("jblas_arch_flavor");
046            } catch (UnsatisfiedLinkError e) {
047                Logger.getLogger().config("ArchFlavor native library not found in path. Copying native library "
048                        + "libjblas_arch_flavor from the archive. Consider installing the library somewhere "
049                        + "in the path (for Windows: PATH, for Linux: LD_LIBRARY_PATH).");
050                new org.jblas.util.LibraryLoader().loadLibrary("jblas_arch_flavor", false);
051            }
052        }
053        private static String fixedFlavor = null;
054    
055        public static final int SSE = 1;
056        public static final int SSE2 = 2;
057        public static final int SSE3 = 3;
058        public static final int NO_SSE = -1; // for platforms where it isn't applicable.
059    
060        public static native int SSELevel();
061    
062        public static String archFlavor() {
063            if (fixedFlavor != null)
064                return fixedFlavor;
065    
066            String arch = System.getProperty("os.arch");
067            String name = System.getProperty("os.name");
068    
069            if (name.startsWith("Windows") && arch.equals("amd64")) {
070                return null;
071            }
072            else if(arch.equals("i386") || arch.equals("x86") || arch.equals("x86_64") || arch.equals("amd64")) {
073                switch (SSELevel()) {
074                    case SSE:
075                        return "sse";
076                    case SSE2:
077                        return "sse2";
078                    case SSE3:
079                        return "sse3";
080                    default:
081                        return null;
082                }
083            } else {
084                return null;
085            }
086        }
087    
088        public static void overrideArchFlavor(String flavor) {
089            fixedFlavor = flavor;
090        }
091    }