michael@0: /* michael@0: * jsimd_arm.c michael@0: * michael@0: * Copyright 2009 Pierre Ossman for Cendio AB michael@0: * Copyright 2009-2011 D. R. Commander michael@0: * michael@0: * Based on the x86 SIMD extension for IJG JPEG library, michael@0: * Copyright (C) 1999-2006, MIYASAKA Masaru. michael@0: * For conditions of distribution and use, see copyright notice in jsimdext.inc michael@0: * michael@0: * This file contains the interface between the "normal" portions michael@0: * of the library and the SIMD implementations when running on michael@0: * ARM architecture. michael@0: * michael@0: * Based on the stubs from 'jsimd_none.c' michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "../jinclude.h" michael@0: #include "../jpeglib.h" michael@0: #include "../jsimd.h" michael@0: #include "../jdct.h" michael@0: #include "../jsimddct.h" michael@0: #include "jsimd.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: static unsigned int simd_support = ~0; michael@0: michael@0: #if defined(__linux__) || defined(ANDROID) || defined(__ANDROID__) michael@0: michael@0: #define SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT (1024 * 1024) michael@0: michael@0: LOCAL(int) michael@0: check_feature (char *buffer, char *feature) michael@0: { michael@0: char *p; michael@0: if (*feature == 0) michael@0: return 0; michael@0: if (strncmp(buffer, "Features", 8) != 0) michael@0: return 0; michael@0: buffer += 8; michael@0: while (isspace(*buffer)) michael@0: buffer++; michael@0: michael@0: /* Check if 'feature' is present in the buffer as a separate word */ michael@0: while ((p = strstr(buffer, feature))) { michael@0: if (p > buffer && !isspace(*(p - 1))) { michael@0: buffer++; michael@0: continue; michael@0: } michael@0: p += strlen(feature); michael@0: if (*p != 0 && !isspace(*p)) { michael@0: buffer++; michael@0: continue; michael@0: } michael@0: return 1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: LOCAL(int) michael@0: parse_proc_cpuinfo (int bufsize) michael@0: { michael@0: char *buffer = (char *)malloc(bufsize); michael@0: FILE *fd; michael@0: simd_support = 0; michael@0: michael@0: if (!buffer) michael@0: return 0; michael@0: michael@0: fd = fopen("/proc/cpuinfo", "r"); michael@0: if (fd) { michael@0: while (fgets(buffer, bufsize, fd)) { michael@0: if (!strchr(buffer, '\n') && !feof(fd)) { michael@0: /* "impossible" happened - insufficient size of the buffer! */ michael@0: fclose(fd); michael@0: free(buffer); michael@0: return 0; michael@0: } michael@0: if (check_feature(buffer, "neon")) michael@0: simd_support |= JSIMD_ARM_NEON; michael@0: } michael@0: fclose(fd); michael@0: } michael@0: free(buffer); michael@0: return 1; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: /* michael@0: * Check what SIMD accelerations are supported. michael@0: * michael@0: * FIXME: This code is racy under a multi-threaded environment. michael@0: */ michael@0: LOCAL(void) michael@0: init_simd (void) michael@0: { michael@0: char *env = NULL; michael@0: #if !defined(__ARM_NEON__) && defined(__linux__) || defined(ANDROID) || defined(__ANDROID__) michael@0: int bufsize = 1024; /* an initial guess for the line buffer size limit */ michael@0: #endif michael@0: michael@0: if (simd_support != ~0U) michael@0: return; michael@0: michael@0: simd_support = 0; michael@0: michael@0: #if defined(__ARM_NEON__) michael@0: simd_support |= JSIMD_ARM_NEON; michael@0: #elif defined(__linux__) || defined(ANDROID) || defined(__ANDROID__) michael@0: /* We still have a chance to use NEON regardless of globally used michael@0: * -mcpu/-mfpu options passed to gcc by performing runtime detection via michael@0: * /proc/cpuinfo parsing on linux/android */ michael@0: while (!parse_proc_cpuinfo(bufsize)) { michael@0: bufsize *= 2; michael@0: if (bufsize > SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT) michael@0: break; michael@0: } michael@0: #endif michael@0: michael@0: /* Force different settings through environment variables */ michael@0: env = getenv("JSIMD_FORCE_ARM_NEON"); michael@0: if ((env != NULL) && (strcmp(env, "1") == 0)) michael@0: simd_support &= JSIMD_ARM_NEON; michael@0: env = getenv("JSIMD_FORCE_NO_SIMD"); michael@0: if ((env != NULL) && (strcmp(env, "1") == 0)) michael@0: simd_support = 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_rgb_ycc (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: /* The code is optimised for these values only */ michael@0: if (BITS_IN_JSAMPLE != 8) michael@0: return 0; michael@0: if (sizeof(JDIMENSION) != 4) michael@0: return 0; michael@0: if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4)) michael@0: return 0; michael@0: michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_rgb_gray (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_ycc_rgb (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: /* The code is optimised for these values only */ michael@0: if (BITS_IN_JSAMPLE != 8) michael@0: return 0; michael@0: if (sizeof(JDIMENSION) != 4) michael@0: return 0; michael@0: if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4)) michael@0: return 0; michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_rgb_ycc_convert (j_compress_ptr cinfo, michael@0: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, michael@0: JDIMENSION output_row, int num_rows) michael@0: { michael@0: void (*neonfct)(JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int); michael@0: michael@0: switch(cinfo->in_color_space) michael@0: { michael@0: case JCS_EXT_RGB: michael@0: neonfct=jsimd_extrgb_ycc_convert_neon; michael@0: break; michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_RGBA: michael@0: neonfct=jsimd_extrgbx_ycc_convert_neon; michael@0: break; michael@0: case JCS_EXT_BGR: michael@0: neonfct=jsimd_extbgr_ycc_convert_neon; michael@0: break; michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_BGRA: michael@0: neonfct=jsimd_extbgrx_ycc_convert_neon; michael@0: break; michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_ABGR: michael@0: neonfct=jsimd_extxbgr_ycc_convert_neon; michael@0: break; michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_ARGB: michael@0: neonfct=jsimd_extxrgb_ycc_convert_neon; michael@0: break; michael@0: default: michael@0: neonfct=jsimd_extrgb_ycc_convert_neon; michael@0: break; michael@0: } michael@0: michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: neonfct(cinfo->image_width, input_buf, michael@0: output_buf, output_row, num_rows); michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_rgb_gray_convert (j_compress_ptr cinfo, michael@0: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, michael@0: JDIMENSION output_row, int num_rows) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_ycc_rgb_convert (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION input_row, michael@0: JSAMPARRAY output_buf, int num_rows) michael@0: { michael@0: void (*neonfct)(JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, int); michael@0: michael@0: switch(cinfo->out_color_space) michael@0: { michael@0: case JCS_EXT_RGB: michael@0: neonfct=jsimd_ycc_extrgb_convert_neon; michael@0: break; michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_RGBA: michael@0: neonfct=jsimd_ycc_extrgbx_convert_neon; michael@0: break; michael@0: case JCS_EXT_BGR: michael@0: neonfct=jsimd_ycc_extbgr_convert_neon; michael@0: break; michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_BGRA: michael@0: neonfct=jsimd_ycc_extbgrx_convert_neon; michael@0: break; michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_ABGR: michael@0: neonfct=jsimd_ycc_extxbgr_convert_neon; michael@0: break; michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_ARGB: michael@0: neonfct=jsimd_ycc_extxrgb_convert_neon; michael@0: break; michael@0: default: michael@0: neonfct=jsimd_ycc_extrgb_convert_neon; michael@0: break; michael@0: } michael@0: michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: neonfct(cinfo->output_width, input_buf, michael@0: input_row, output_buf, num_rows); michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_h2v2_downsample (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_h2v1_downsample (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, JSAMPARRAY output_data) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, JSAMPARRAY output_data) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_h2v2_upsample (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_h2v1_upsample (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_h2v2_upsample (j_decompress_ptr cinfo, michael@0: jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, michael@0: JSAMPARRAY * output_data_ptr) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_h2v1_upsample (j_decompress_ptr cinfo, michael@0: jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, michael@0: JSAMPARRAY * output_data_ptr) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_h2v2_fancy_upsample (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_h2v1_fancy_upsample (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: /* The code is optimised for these values only */ michael@0: if (BITS_IN_JSAMPLE != 8) michael@0: return 0; michael@0: if (sizeof(JDIMENSION) != 4) michael@0: return 0; michael@0: michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_h2v2_fancy_upsample (j_decompress_ptr cinfo, michael@0: jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, michael@0: JSAMPARRAY * output_data_ptr) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_h2v1_fancy_upsample (j_decompress_ptr cinfo, michael@0: jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, michael@0: JSAMPARRAY * output_data_ptr) michael@0: { michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: jsimd_h2v1_fancy_upsample_neon(cinfo->max_v_samp_factor, michael@0: compptr->downsampled_width, input_data, output_data_ptr); michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_h2v2_merged_upsample (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_h2v1_merged_upsample (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_h2v2_merged_upsample (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, michael@0: JDIMENSION in_row_group_ctr, michael@0: JSAMPARRAY output_buf) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_h2v1_merged_upsample (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, michael@0: JDIMENSION in_row_group_ctr, michael@0: JSAMPARRAY output_buf) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_convsamp (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: /* The code is optimised for these values only */ michael@0: if (DCTSIZE != 8) michael@0: return 0; michael@0: if (BITS_IN_JSAMPLE != 8) michael@0: return 0; michael@0: if (sizeof(JDIMENSION) != 4) michael@0: return 0; michael@0: if (sizeof(DCTELEM) != 2) michael@0: return 0; michael@0: michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_convsamp_float (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_convsamp (JSAMPARRAY sample_data, JDIMENSION start_col, michael@0: DCTELEM * workspace) michael@0: { michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: jsimd_convsamp_neon(sample_data, start_col, workspace); michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_convsamp_float (JSAMPARRAY sample_data, JDIMENSION start_col, michael@0: FAST_FLOAT * workspace) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_fdct_islow (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_fdct_ifast (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: /* The code is optimised for these values only */ michael@0: if (DCTSIZE != 8) michael@0: return 0; michael@0: if (sizeof(DCTELEM) != 2) michael@0: return 0; michael@0: michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_fdct_float (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_fdct_islow (DCTELEM * data) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_fdct_ifast (DCTELEM * data) michael@0: { michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: jsimd_fdct_ifast_neon(data); michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_fdct_float (FAST_FLOAT * data) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_quantize (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: /* The code is optimised for these values only */ michael@0: if (DCTSIZE != 8) michael@0: return 0; michael@0: if (sizeof(JCOEF) != 2) michael@0: return 0; michael@0: if (sizeof(DCTELEM) != 2) michael@0: return 0; michael@0: michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_quantize_float (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_quantize (JCOEFPTR coef_block, DCTELEM * divisors, michael@0: DCTELEM * workspace) michael@0: { michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: jsimd_quantize_neon(coef_block, divisors, workspace); michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_quantize_float (JCOEFPTR coef_block, FAST_FLOAT * divisors, michael@0: FAST_FLOAT * workspace) michael@0: { michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_idct_2x2 (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: /* The code is optimised for these values only */ michael@0: if (DCTSIZE != 8) michael@0: return 0; michael@0: if (sizeof(JCOEF) != 2) michael@0: return 0; michael@0: if (BITS_IN_JSAMPLE != 8) michael@0: return 0; michael@0: if (sizeof(JDIMENSION) != 4) michael@0: return 0; michael@0: if (sizeof(ISLOW_MULT_TYPE) != 2) michael@0: return 0; michael@0: michael@0: if ((simd_support & JSIMD_ARM_NEON)) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_idct_4x4 (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: /* The code is optimised for these values only */ michael@0: if (DCTSIZE != 8) michael@0: return 0; michael@0: if (sizeof(JCOEF) != 2) michael@0: return 0; michael@0: if (BITS_IN_JSAMPLE != 8) michael@0: return 0; michael@0: if (sizeof(JDIMENSION) != 4) michael@0: return 0; michael@0: if (sizeof(ISLOW_MULT_TYPE) != 2) michael@0: return 0; michael@0: michael@0: if ((simd_support & JSIMD_ARM_NEON)) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JCOEFPTR coef_block, JSAMPARRAY output_buf, michael@0: JDIMENSION output_col) michael@0: { michael@0: if ((simd_support & JSIMD_ARM_NEON)) michael@0: jsimd_idct_2x2_neon(compptr->dct_table, coef_block, output_buf, output_col); michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JCOEFPTR coef_block, JSAMPARRAY output_buf, michael@0: JDIMENSION output_col) michael@0: { michael@0: if ((simd_support & JSIMD_ARM_NEON)) michael@0: jsimd_idct_4x4_neon(compptr->dct_table, coef_block, output_buf, output_col); michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_idct_islow (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: /* The code is optimised for these values only */ michael@0: if (DCTSIZE != 8) michael@0: return 0; michael@0: if (sizeof(JCOEF) != 2) michael@0: return 0; michael@0: if (BITS_IN_JSAMPLE != 8) michael@0: return 0; michael@0: if (sizeof(JDIMENSION) != 4) michael@0: return 0; michael@0: if (sizeof(ISLOW_MULT_TYPE) != 2) michael@0: return 0; michael@0: michael@0: if (simd_support & JSIMD_ARM_NEON) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_idct_ifast (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: /* The code is optimised for these values only */ michael@0: if (DCTSIZE != 8) michael@0: return 0; michael@0: if (sizeof(JCOEF) != 2) michael@0: return 0; michael@0: if (BITS_IN_JSAMPLE != 8) michael@0: return 0; michael@0: if (sizeof(JDIMENSION) != 4) michael@0: return 0; michael@0: if (sizeof(IFAST_MULT_TYPE) != 2) michael@0: return 0; michael@0: if (IFAST_SCALE_BITS != 2) michael@0: return 0; michael@0: michael@0: if ((simd_support & JSIMD_ARM_NEON)) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(int) michael@0: jsimd_can_idct_float (void) michael@0: { michael@0: init_simd(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JCOEFPTR coef_block, JSAMPARRAY output_buf, michael@0: JDIMENSION output_col) michael@0: { michael@0: if ((simd_support & JSIMD_ARM_NEON)) michael@0: jsimd_idct_islow_neon(compptr->dct_table, coef_block, output_buf, output_col); michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JCOEFPTR coef_block, JSAMPARRAY output_buf, michael@0: JDIMENSION output_col) michael@0: { michael@0: if ((simd_support & JSIMD_ARM_NEON)) michael@0: jsimd_idct_ifast_neon(compptr->dct_table, coef_block, output_buf, output_col); michael@0: } michael@0: michael@0: GLOBAL(void) michael@0: jsimd_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JCOEFPTR coef_block, JSAMPARRAY output_buf, michael@0: JDIMENSION output_col) michael@0: { michael@0: } michael@0: