Skip to content
Myr

stb_image_resize2.h

This file

Path

stb_image_resize2.h

10,679 lines · 447.1 KiB · C

Last changed by

stb_image_resize 2.18Sean Barrettbefore adoption904aa67

History of this file

Take it

1/* stb_image_resize2 - v2.18 - public domain image resizing23   by Jeff Roberts (v2) and Jorge L Rodriguez4   http://github.com/nothings/stb56   Can be threaded with the extended API. SSE2, AVX, Neon and WASM SIMD support. Only7   scaling and translation is supported, no rotations or shears.89   COMPILING & LINKING10      In one C/C++ file that #includes this file, do this:11         #define STB_IMAGE_RESIZE_IMPLEMENTATION12      before the #include. That will create the implementation in that file.1314   EASY API CALLS:15     Easy API downsamples w/Mitchell filter, upsamples w/cubic interpolation, clamps to edge.1617     stbir_resize_uint8_srgb( input_pixels,  input_w,  input_h,  input_stride_in_bytes,18                              output_pixels, output_w, output_h, output_stride_in_bytes,19                              pixel_layout_enum )2021     stbir_resize_uint8_linear( input_pixels,  input_w,  input_h,  input_stride_in_bytes,22                                output_pixels, output_w, output_h, output_stride_in_bytes,23                                pixel_layout_enum )2425     stbir_resize_float_linear( input_pixels,  input_w,  input_h,  input_stride_in_bytes,26                                output_pixels, output_w, output_h, output_stride_in_bytes,27                                pixel_layout_enum )2829     If you pass NULL or zero for the output_pixels, we will allocate the output buffer30     for you and return it from the function (free with free() or STBIR_FREE).31     As a special case, XX_stride_in_bytes of 0 means packed continuously in memory.3233   API LEVELS34      There are three levels of API - easy-to-use, medium-complexity and extended-complexity.3536      See the "header file" section of the source for API documentation.3738   ADDITIONAL DOCUMENTATION3940      MEMORY ALLOCATION41         By default, we use malloc and free for memory allocation.  To override the42         memory allocation, before the implementation #include, add a:4344            #define STBIR_MALLOC(size,user_data) ...45            #define STBIR_FREE(ptr,user_data)   ...4647         Each resize makes exactly one call to malloc/free (unless you use the48         extended API where you can do one allocation for many resizes). Under49         address sanitizer, we do separate allocations to find overread/writes.5051      PERFORMANCE52         This library was written with an emphasis on performance. When testing53         stb_image_resize with RGBA, the fastest mode is STBIR_4CHANNEL with54         STBIR_TYPE_UINT8 pixels and CLAMPed edges (which is what many other resize55         libs do by default). Also, make sure SIMD is turned on of course (default56         for 64-bit targets). Avoid WRAP edge mode if you want the fastest speed.5758         This library also comes with profiling built-in. If you define STBIR_PROFILE,59         you can use the advanced API and get low-level profiling information by60         calling stbir_resize_extended_profile_info() or stbir_resize_split_profile_info()61         after a resize.6263      SIMD64         Most of the routines have optimized SSE2, AVX, NEON and WASM versions.6566         On Microsoft compilers, we automatically turn on SIMD for 64-bit x64 and67         ARM; for 32-bit x86 and ARM, you select SIMD mode by defining STBIR_SSE2 or68         STBIR_NEON. For AVX and AVX2, we auto-select it by detecting the /arch:AVX69         or /arch:AVX2 switches. You can also always manually turn SSE2, AVX or AVX270         support on by defining STBIR_SSE2, STBIR_AVX or STBIR_AVX2.7172         On Linux, SSE2 and Neon is on by default for 64-bit x64 or ARM64. For 32-bit,73         we select x86 SIMD mode by whether you have -msse2, -mavx or -mavx2 enabled74         on the command line. For 32-bit ARM, you must pass -mfpu=neon-vfpv4 for both75         clang and GCC, but GCC also requires an additional -mfp16-format=ieee to76         automatically enable NEON.7778         On x86 platforms, you can also define STBIR_FP16C to turn on FP16C instructions79         for converting back and forth to half-floats. This is autoselected when we80         are using AVX2. Clang and GCC also require the -mf16c switch. ARM always uses81         the built-in half float hardware NEON instructions.8283         You can also tell us to use multiply-add instructions with STBIR_USE_FMA.84         Because x86 doesn't always have fma, we turn it off by default to maintain85         determinism across all platforms. If you don't care about non-FMA determinism86         and are willing to restrict yourself to more recent x86 CPUs (around the AVX87         timeframe), then fma will give you around a 15% speedup.8889         You can force off SIMD in all cases by defining STBIR_NO_SIMD. You can turn90         off AVX or AVX2 specifically with STBIR_NO_AVX or STBIR_NO_AVX2. AVX is 10%91         to 40% faster, and AVX2 is generally another 12%.9293      ALPHA CHANNEL94         Most of the resizing functions provide the ability to control how the alpha95         channel of an image is processed.9697         When alpha represents transparency, it is important that when combining98         colors with filtering, the pixels should not be treated equally; they99         should use a weighted average based on their alpha values. For example,100         if a pixel is 1% opaque bright green and another pixel is 99% opaque101         black and you average them, the average will be 50% opaque, but the102         unweighted average and will be a middling green color, while the weighted103         average will be nearly black. This means the unweighted version introduced104         green energy that didn't exist in the source image.105106         (If you want to know why this makes sense, you can work out the math for107         the following: consider what happens if you alpha composite a source image108         over a fixed color and then average the output, vs. if you average the109         source image pixels and then composite that over the same fixed color.110         Only the weighted average produces the same result as the ground truth111         composite-then-average result.)112113         Therefore, it is in general best to "alpha weight" the pixels when applying114         filters to them. This essentially means multiplying the colors by the alpha115         values before combining them, and then dividing by the alpha value at the116         end.117118         The computer graphics industry introduced a technique called "premultiplied119         alpha" or "associated alpha" in which image colors are stored in image files120         already multiplied by their alpha. This saves some math when compositing,121         and also avoids the need to divide by the alpha at the end (which is quite122         inefficient). However, while premultiplied alpha is common in the movie CGI123         industry, it is not commonplace in other industries like videogames, and most124         consumer file formats are generally expected to contain not-premultiplied125         colors. For example, Photoshop saves PNG files "unpremultiplied", and web126         browsers like Chrome and Firefox expect PNG images to be unpremultiplied.127128         Note that there are three possibilities that might describe your image129         and resize expectation:130131             1. images are not premultiplied, alpha weighting is desired132             2. images are not premultiplied, alpha weighting is not desired133             3. images are premultiplied134135         Both case #2 and case #3 require the exact same math: no alpha weighting136         should be applied or removed. Only case 1 requires extra math operations;137         the other two cases can be handled identically.138139         stb_image_resize expects case #1 by default, applying alpha weighting to140         images, expecting the input images to be unpremultiplied. This is what the141         COLOR+ALPHA buffer types tell the resizer to do.142143         When you use the pixel layouts STBIR_RGBA, STBIR_BGRA, STBIR_ARGB,144         STBIR_ABGR, STBIR_RA, or STBIR_AR you are telling us that the pixels are145         non-premultiplied. In these cases, the resizer will alpha weight the colors146         (effectively creating the premultiplied image), do the filtering, and then147         convert back to non-premult on exit.148149         When you use the pixel layouts STBIR_RGBA_PM, STBIR_BGRA_PM, STBIR_ARGB_PM,150         STBIR_ABGR_PM, STBIR_RA_PM or STBIR_AR_PM, you are telling that the pixels151         ARE premultiplied. In this case, the resizer doesn't have to do the152         premultipling - it can filter directly on the input. This about twice as153         fast as the non-premultiplied case, so it's the right option if your data is154         already setup correctly.155156         When you use the pixel layout STBIR_4CHANNEL or STBIR_2CHANNEL, you are157         telling us that there is no channel that represents transparency; it may be158         RGB and some unrelated fourth channel that has been stored in the alpha159         channel, but it is actually not alpha. No special processing will be160         performed.161162         The difference between the generic 4 or 2 channel layouts, and the163         specialized _PM versions is with the _PM versions you are telling us that164         the data *is* alpha, just don't premultiply it. That's important when165         using SRGB pixel formats, we need to know where the alpha is, because166         it is converted linearly (rather than with the SRGB converters).167168         Because alpha weighting produces the same effect as premultiplying, you169         even have the option with non-premultiplied inputs to let the resizer170         produce a premultiplied output. Because the intially computed alpha-weighted171         output image is effectively premultiplied, this is actually more performant172         than the normal path which un-premultiplies the output image as a final step.173174         Finally, when converting both in and out of non-premulitplied space (for175         example, when using STBIR_RGBA), we go to somewhat heroic measures to176         ensure that areas with zero alpha value pixels get something reasonable177         in the RGB values. If you don't care about the RGB values of zero alpha178         pixels, you can call the stbir_set_non_pm_alpha_speed_over_quality()179         function - this runs a premultiplied resize about 25% faster. That said,180         when you really care about speed, using premultiplied pixels for both in181         and out (STBIR_RGBA_PM, etc) much faster than both of these premultiplied182         options.183184      PIXEL LAYOUT CONVERSION185         The resizer can convert from some pixel layouts to others. When using the186         stbir_set_pixel_layouts(), you can, for example, specify STBIR_RGBA187         on input, and STBIR_ARGB on output, and it will re-organize the channels188         during the resize. Currently, you can only convert between two pixel189         layouts with the same number of channels.190191      DETERMINISM192         We commit to being deterministic (from x64 to ARM to scalar to SIMD, etc).193         This requires compiling with fast-math off (using at least /fp:precise).194         Also, you must turn off fp-contracting (which turns mult+adds into fmas)!195         We attempt to do this with pragmas, but with Clang, you usually want to add196         -ffp-contract=off to the command line as well.197198         For 32-bit x86, you must use SSE and SSE2 codegen for determinism. That is,199         if the scalar x87 unit gets used at all, we immediately lose determinism.200         On Microsoft Visual Studio 2008 and earlier, from what we can tell there is201         no way to be deterministic in 32-bit x86 (some x87 always leaks in, even202         with fp:strict). On 32-bit x86 GCC, determinism requires both -msse2 and203         -fpmath=sse.204205         Note that we will not be deterministic with float data containing NaNs -206         the NaNs will propagate differently on different SIMD and platforms.207208         If you turn on STBIR_USE_FMA, then we will be deterministic with other209         fma targets, but we will differ from non-fma targets (this is unavoidable,210         because a fma isn't simply an add with a mult - it also introduces a211         rounding difference compared to non-fma instruction sequences.212213      FLOAT PIXEL FORMAT RANGE214         Any range of values can be used for the non-alpha float data that you pass215         in (0 to 1, -1 to 1, whatever). However, if you are inputting float values216         but *outputting* bytes or shorts, you must use a range of 0 to 1 so that we217         scale back properly. The alpha channel must also be 0 to 1 for any format218         that does premultiplication prior to resizing.219220         Note also that with float output, using filters with negative lobes, the221         output filtered values might go slightly out of range. You can define222         STBIR_FLOAT_LOW_CLAMP and/or STBIR_FLOAT_HIGH_CLAMP to specify the range223         to clamp to on output, if that's important.224225      MAX/MIN SCALE FACTORS226         The input pixel resolutions are in integers, and we do the internal pointer227         resolution in size_t sized integers. However, the scale ratio from input228         resolution to output resolution is calculated in float form. This means229         the effective possible scale ratio is limited to 24 bits (or 16 million230         to 1). As you get close to the size of the float resolution (again, 16231         million pixels wide or high), you might start seeing float inaccuracy232         issues in general in the pipeline. If you have to do extreme resizes,233         you can usually do this is multiple stages (using float intermediate234         buffers).235236      FLIPPED IMAGES237         Stride is just the delta from one scanline to the next. This means you can238         use a negative stride to handle inverted images (point to the final239         scanline and use a negative stride). You can invert the input or output,240         using negative strides.241242      DEFAULT FILTERS243         For functions which don't provide explicit control over what filters to244         use, you can change the compile-time defaults with:245246            #define STBIR_DEFAULT_FILTER_UPSAMPLE     STBIR_FILTER_something247            #define STBIR_DEFAULT_FILTER_DOWNSAMPLE   STBIR_FILTER_something248249         See stbir_filter in the header-file section for the list of filters.250251      NEW FILTERS252         A number of 1D filter kernels are supplied. For a list of supported253         filters, see the stbir_filter enum. You can install your own filters by254         using the stbir_set_filter_callbacks function.255256      PROGRESS257         For interactive use with slow resize operations, you can use the 258         scanline callbacks in the extended API. It would have to be a *very* large259         image resample to need progress though - we're very fast.260261      CEIL and FLOOR262         In scalar mode, the only functions we use from math.h are ceilf and floorf,263         but if you have your own versions, you can define the STBIR_CEILF(v) and264         STBIR_FLOORF(v) macros and we'll use them instead. In SIMD, we just use265         our own versions.266267      ASSERT268         Define STBIR_ASSERT(boolval) to override assert() and not use assert.h269270     PORTING FROM VERSION 1271        The API has changed. You can continue to use the old version of stb_image_resize.h,272        which is available in the "deprecated/" directory.273274        If you're using the old simple-to-use API, porting is straightforward.275        (For more advanced APIs, read the documentation.)276277          stbir_resize_uint8():278            - call `stbir_resize_uint8_linear`, cast channel count to `stbir_pixel_layout`279280          stbir_resize_float():281            - call `stbir_resize_float_linear`, cast channel count to `stbir_pixel_layout`282283          stbir_resize_uint8_srgb():284            - function name is unchanged285            - cast channel count to `stbir_pixel_layout`286            - above is sufficient unless your image has alpha and it's not RGBA/BGRA287              - in that case, follow the below instructions for stbir_resize_uint8_srgb_edgemode288289          stbir_resize_uint8_srgb_edgemode()290            - switch to the "medium complexity" API291            - stbir_resize(), very similar API but a few more parameters:292              - pixel_layout: cast channel count to `stbir_pixel_layout`293              - data_type:    STBIR_TYPE_UINT8_SRGB294              - edge:         unchanged (STBIR_EDGE_WRAP, etc.)295              - filter:       STBIR_FILTER_DEFAULT296            - which channel is alpha is specified in stbir_pixel_layout, see enum for details297298      FUTURE TODOS299        *  For polyphase integral filters, we just memcpy the coeffs to dupe300           them, but we should indirect and use the same coeff memory.301        *  Add pixel layout conversions for sensible different channel counts302           (maybe, 1->3/4, 3->4, 4->1, 3->1).303         * For SIMD encode and decode scanline routines, do any pre-aligning304           for bad input/output buffer alignments and pitch?305         * For very wide scanlines, we should we do vertical strips to stay within306           L2 cache. Maybe do chunks of 1K pixels at a time. There would be307           some pixel reconversion, but probably dwarfed by things falling out308           of cache. Probably also something possible with alternating between309           scattering and gathering at high resize scales?310         * Should we have a multiple MIPs at the same time function (could keep311           more memory in cache during multiple resizes)?312         * Rewrite the coefficient generator to do many at once.313         * AVX-512 vertical kernels - worried about downclocking here.314         * Convert the reincludes to macros when we know they aren't changing.315         * Experiment with pivoting the horizontal and always using the316           vertical filters (which are faster, but perhaps not enough to overcome317           the pivot cost and the extra memory touches). Need to buffer the whole318           image so have to balance memory use.319         * Most of our code is internally function pointers, should we compile320           all the SIMD stuff always and dynamically dispatch?321322   CONTRIBUTORS323      Jeff Roberts: 2.0 implementation, optimizations, SIMD324      Martins Mozeiko: NEON simd, WASM simd, clang and GCC whisperer325      Fabian Giesen: half float and srgb converters326      Sean Barrett: API design, optimizations327      Jorge L Rodriguez: Original 1.0 implementation328      Aras Pranckevicius: bugfixes329      Nathan Reed: warning fixes for 1.0330331   REVISIONS332      2.18 (2026-03-25) fixed coefficient calculation when skipping a coefficient off 333                          the left side of the window, added non-aligned access safe 334                          memcpy mode for scalar path, fixed various typos, and fixed 335                          define error in the float clamp output mode. 336      2.17 (2025-10-25) silly format bug in easy-to-use APIs.337      2.16 (2025-10-21) fixed the easy-to-use APIs to allow inverted bitmaps (negative338                          strides), fix vertical filter kernel callback, fix threaded339                          gather buffer priming (and assert).340                          (thanks adipose, TainZerL, and Harrison Green)341      2.15 (2025-07-17) fixed an assert in debug mode when using floats with input342                          callbacks, work around GCC warning when adding to null ptr343                          (thanks Johannes Spohr and Pyry Kovanen).344      2.14 (2025-05-09) fixed a bug using downsampling gather horizontal first, and 345                          scatter with vertical first.346      2.13 (2025-02-27) fixed a bug when using input callbacks, turned off simd for 347                          tiny-c, fixed some variables that should have been static,348                          fixes a bug when calculating temp memory with resizes that349                          exceed 2GB of temp memory (very large resizes).350      2.12 (2024-10-18) fix incorrect use of user_data with STBIR_FREE351      2.11 (2024-09-08) fix harmless asan warnings in 2-channel and 3-channel mode352                          with AVX-2, fix some weird scaling edge conditions with353                          point sample mode.354      2.10 (2024-07-27) fix the defines GCC and mingw for loop unroll control,355                          fix MSVC 32-bit arm half float routines.356      2.09 (2024-06-19) fix the defines for 32-bit ARM GCC builds (was selecting357                          hardware half floats).358      2.08 (2024-06-10) fix for RGB->BGR three channel flips and add SIMD (thanks359                          to Ryan Salsbury), fix for sub-rect resizes, use the360                          pragmas to control unrolling when they are available.361      2.07 (2024-05-24) fix for slow final split during threaded conversions of very 362                          wide scanlines when downsampling (caused by extra input 363                          converting), fix for wide scanline resamples with many 364                          splits (int overflow), fix GCC warning.365      2.06 (2024-02-10) fix for identical width/height 3x or more down-scaling 366                          undersampling a single row on rare resize ratios (about 1%).367      2.05 (2024-02-07) fix for 2 pixel to 1 pixel resizes with wrap (thanks Aras),368                        fix for output callback (thanks Julien Koenen).369      2.04 (2023-11-17) fix for rare AVX bug, shadowed symbol (thanks Nikola Smiljanic).370      2.03 (2023-11-01) ASAN and TSAN warnings fixed, minor tweaks.371      2.00 (2023-10-10) mostly new source: new api, optimizations, simd, vertical-first, etc372                          2x-5x faster without simd, 4x-12x faster with simd,373                          in some cases, 20x to 40x faster esp resizing large to very small.374      0.96 (2019-03-04) fixed warnings375      0.95 (2017-07-23) fixed warnings376      0.94 (2017-03-18) fixed warnings377      0.93 (2017-03-03) fixed bug with certain combinations of heights378      0.92 (2017-01-02) fix integer overflow on large (>2GB) images379      0.91 (2016-04-02) fix warnings; fix handling of subpixel regions380      0.90 (2014-09-17) first released version381382   LICENSE383     See end of file for license information.384*/385386#if !defined(STB_IMAGE_RESIZE_DO_HORIZONTALS) && !defined(STB_IMAGE_RESIZE_DO_VERTICALS) && !defined(STB_IMAGE_RESIZE_DO_CODERS)   // for internal re-includes387388#ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE2_H389#define STBIR_INCLUDE_STB_IMAGE_RESIZE2_H390391#include <stddef.h>392#ifdef _MSC_VER393typedef unsigned char    stbir_uint8;394typedef unsigned short   stbir_uint16;395typedef unsigned int     stbir_uint32;396typedef unsigned __int64 stbir_uint64;397#else398#include <stdint.h>399typedef uint8_t  stbir_uint8;400typedef uint16_t stbir_uint16;401typedef uint32_t stbir_uint32;402typedef uint64_t stbir_uint64;403#endif404405#ifndef STBIRDEF406#ifdef STB_IMAGE_RESIZE_STATIC407#define STBIRDEF static408#else409#ifdef __cplusplus410#define STBIRDEF extern "C"411#else412#define STBIRDEF extern413#endif414#endif415#endif416417//////////////////////////////////////////////////////////////////////////////418////   start "header file" ///////////////////////////////////////////////////419//420// Easy-to-use API:421//422//     * stride is the offset between successive rows of image data423//        in memory, in bytes. specify 0 for packed continuously in memory424//     * colorspace is linear or sRGB as specified by function name425//     * Uses the default filters426//     * Uses edge mode clamped427//     * returned result is 1 for success or 0 in case of an error.428429430// stbir_pixel_layout specifies:431//   number of channels432//   order of channels433//   whether color is premultiplied by alpha434// for back compatibility, you can cast the old channel count to an stbir_pixel_layout435typedef enum436{437  STBIR_1CHANNEL = 1,438  STBIR_2CHANNEL = 2,439  STBIR_RGB      = 3,               // 3-chan, with order specified (for channel flipping)440  STBIR_BGR      = 0,               // 3-chan, with order specified (for channel flipping)441  STBIR_4CHANNEL = 5,442443  STBIR_RGBA = 4,                   // alpha formats, where alpha is NOT premultiplied into color channels444  STBIR_BGRA = 6,445  STBIR_ARGB = 7,446  STBIR_ABGR = 8,447  STBIR_RA   = 9,448  STBIR_AR   = 10,449450  STBIR_RGBA_PM = 11,               // alpha formats, where alpha is premultiplied into color channels451  STBIR_BGRA_PM = 12,452  STBIR_ARGB_PM = 13,453  STBIR_ABGR_PM = 14,454  STBIR_RA_PM   = 15,455  STBIR_AR_PM   = 16,456457  STBIR_RGBA_NO_AW = 11,            // alpha formats, where NO alpha weighting is applied at all!458  STBIR_BGRA_NO_AW = 12,            //   these are just synonyms for the _PM flags (which also do459  STBIR_ARGB_NO_AW = 13,            //   no alpha weighting). These names just make it more clear460  STBIR_ABGR_NO_AW = 14,            //   for some folks).461  STBIR_RA_NO_AW   = 15,462  STBIR_AR_NO_AW   = 16,463464} stbir_pixel_layout;465466//===============================================================467//  Simple-complexity API468//469//    If output_pixels is NULL (0), then we will allocate the buffer and return it to you.470//--------------------------------471472STBIRDEF unsigned char * stbir_resize_uint8_srgb( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes,473                                                        unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,474                                                        stbir_pixel_layout pixel_type );475476STBIRDEF unsigned char * stbir_resize_uint8_linear( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes,477                                                          unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,478                                                          stbir_pixel_layout pixel_type );479480STBIRDEF float * stbir_resize_float_linear( const float *input_pixels , int input_w , int input_h, int input_stride_in_bytes,481                                                  float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,482                                                  stbir_pixel_layout pixel_type );483//===============================================================484485//===============================================================486// Medium-complexity API487//488// This extends the easy-to-use API as follows:489//490//     * Can specify the datatype - U8, U8_SRGB, U16, FLOAT, HALF_FLOAT491//     * Edge wrap can selected explicitly492//     * Filter can be selected explicitly493//--------------------------------494495typedef enum496{497  STBIR_EDGE_CLAMP   = 0,498  STBIR_EDGE_REFLECT = 1,499  STBIR_EDGE_WRAP    = 2,  // this edge mode is slower and uses more memory500  STBIR_EDGE_ZERO    = 3,501} stbir_edge;502503typedef enum504{505  STBIR_FILTER_DEFAULT      = 0,  // use same filter type that easy-to-use API chooses506  STBIR_FILTER_BOX          = 1,  // A trapezoid w/1-pixel wide ramps, same result as box for integer scale ratios507  STBIR_FILTER_TRIANGLE     = 2,  // On upsampling, produces same results as bilinear texture filtering508  STBIR_FILTER_CUBICBSPLINE = 3,  // The cubic b-spline (aka Mitchell-Netrevalli with B=1,C=0), gaussian-esque509  STBIR_FILTER_CATMULLROM   = 4,  // An interpolating cubic spline510  STBIR_FILTER_MITCHELL     = 5,  // Mitchell-Netrevalli filter with B=1/3, C=1/3511  STBIR_FILTER_POINT_SAMPLE = 6,  // Simple point sampling512  STBIR_FILTER_OTHER        = 7,  // User callback specified513} stbir_filter;514515typedef enum516{517  STBIR_TYPE_UINT8            = 0,518  STBIR_TYPE_UINT8_SRGB       = 1,519  STBIR_TYPE_UINT8_SRGB_ALPHA = 2,  // alpha channel, when present, should also be SRGB (this is very unusual)520  STBIR_TYPE_UINT16           = 3,521  STBIR_TYPE_FLOAT            = 4,522  STBIR_TYPE_HALF_FLOAT       = 5523} stbir_datatype;524525// medium api526STBIRDEF void *  stbir_resize( const void *input_pixels , int input_w , int input_h, int input_stride_in_bytes,527                                     void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,528                               stbir_pixel_layout pixel_layout, stbir_datatype data_type,529                               stbir_edge edge, stbir_filter filter );530//===============================================================531532533534//===============================================================535// Extended-complexity API536//537// This API exposes all resize functionality.538//539//     * Separate filter types for each axis540//     * Separate edge modes for each axis541//     * Separate input and output data types542//     * Can specify regions with subpixel correctness543//     * Can specify alpha flags544//     * Can specify a memory callback545//     * Can specify a callback data type for pixel input and output546//     * Can be threaded for a single resize547//     * Can be used to resize many frames without recalculating the sampler info548//549//  Use this API as follows:550//     1) Call the stbir_resize_init function on a local STBIR_RESIZE structure551//     2) Call any of the stbir_set functions552//     3) Optionally call stbir_build_samplers() if you are going to resample multiple times553//        with the same input and output dimensions (like resizing video frames)554//     4) Resample by calling stbir_resize_extended().555//     5) Call stbir_free_samplers() if you called stbir_build_samplers()556//--------------------------------557558559// Types:560561// INPUT CALLBACK: this callback is used for input scanlines562typedef void const * stbir_input_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context );563564// OUTPUT CALLBACK: this callback is used for output scanlines565typedef void stbir_output_callback( void const * output_ptr, int num_pixels, int y, void * context );566567// callbacks for user installed filters568typedef float stbir__kernel_callback( float x, float scale, void * user_data ); // centered at zero569typedef float stbir__support_callback( float scale, void * user_data );570571// internal structure with precomputed scaling572typedef struct stbir__info stbir__info;573574typedef struct STBIR_RESIZE  // use the stbir_resize_init and stbir_override functions to set these values for future compatibility575{576  void * user_data;577  void const * input_pixels;578  int input_w, input_h;579  double input_s0, input_t0, input_s1, input_t1;580  stbir_input_callback * input_cb;581  void * output_pixels;582  int output_w, output_h;583  int output_subx, output_suby, output_subw, output_subh;584  stbir_output_callback * output_cb;585  int input_stride_in_bytes;586  int output_stride_in_bytes;587  int splits;588  int fast_alpha;589  int needs_rebuild;590  int called_alloc;591  stbir_pixel_layout input_pixel_layout_public;592  stbir_pixel_layout output_pixel_layout_public;593  stbir_datatype input_data_type;594  stbir_datatype output_data_type;595  stbir_filter horizontal_filter, vertical_filter;596  stbir_edge horizontal_edge, vertical_edge;597  stbir__kernel_callback * horizontal_filter_kernel; stbir__support_callback * horizontal_filter_support;598  stbir__kernel_callback * vertical_filter_kernel; stbir__support_callback * vertical_filter_support;599  stbir__info * samplers;600} STBIR_RESIZE;601602// extended complexity api603604605// First off, you must ALWAYS call stbir_resize_init on your resize structure before any of the other calls!606STBIRDEF void stbir_resize_init( STBIR_RESIZE * resize,607                                 const void *input_pixels,  int input_w,  int input_h, int input_stride_in_bytes, // stride can be zero608                                       void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, // stride can be zero609                                 stbir_pixel_layout pixel_layout, stbir_datatype data_type );610611//===============================================================612// You can update these parameters any time after resize_init and there is no cost613//--------------------------------614615STBIRDEF void stbir_set_datatypes( STBIR_RESIZE * resize, stbir_datatype input_type, stbir_datatype output_type );616STBIRDEF void stbir_set_pixel_callbacks( STBIR_RESIZE * resize, stbir_input_callback * input_cb, stbir_output_callback * output_cb );   // no callbacks by default617STBIRDEF void stbir_set_user_data( STBIR_RESIZE * resize, void * user_data );                                               // pass back STBIR_RESIZE* by default618STBIRDEF void stbir_set_buffer_ptrs( STBIR_RESIZE * resize, const void * input_pixels, int input_stride_in_bytes, void * output_pixels, int output_stride_in_bytes );619620//===============================================================621622623//===============================================================624// If you call any of these functions, you will trigger a sampler rebuild!625//--------------------------------626627STBIRDEF int stbir_set_pixel_layouts( STBIR_RESIZE * resize, stbir_pixel_layout input_pixel_layout, stbir_pixel_layout output_pixel_layout );  // sets new buffer layouts628STBIRDEF int stbir_set_edgemodes( STBIR_RESIZE * resize, stbir_edge horizontal_edge, stbir_edge vertical_edge );       // CLAMP by default629630STBIRDEF int stbir_set_filters( STBIR_RESIZE * resize, stbir_filter horizontal_filter, stbir_filter vertical_filter ); // STBIR_DEFAULT_FILTER_UPSAMPLE/DOWNSAMPLE by default631STBIRDEF int stbir_set_filter_callbacks( STBIR_RESIZE * resize, stbir__kernel_callback * horizontal_filter, stbir__support_callback * horizontal_support, stbir__kernel_callback * vertical_filter, stbir__support_callback * vertical_support );632633STBIRDEF int stbir_set_pixel_subrect( STBIR_RESIZE * resize, int subx, int suby, int subw, int subh );        // sets both sub-regions (full regions by default)634STBIRDEF int stbir_set_input_subrect( STBIR_RESIZE * resize, double s0, double t0, double s1, double t1 );    // sets input sub-region (full region by default)635STBIRDEF int stbir_set_output_pixel_subrect( STBIR_RESIZE * resize, int subx, int suby, int subw, int subh ); // sets output sub-region (full region by default)636637// when inputting AND outputting non-premultiplied alpha pixels, we use a slower but higher quality technique638//   that fills the zero alpha pixel's RGB values with something plausible.  If you don't care about areas of639//   zero alpha, you can call this function to get about a 25% speed improvement for STBIR_RGBA to STBIR_RGBA640//   types of resizes.641STBIRDEF int stbir_set_non_pm_alpha_speed_over_quality( STBIR_RESIZE * resize, int non_pma_alpha_speed_over_quality );642//===============================================================643644645//===============================================================646// You can call build_samplers to prebuild all the internal data we need to resample.647//   Then, if you call resize_extended many times with the same resize, you only pay the648//   cost once.649// If you do call build_samplers, you MUST call free_samplers eventually.650//--------------------------------651652// This builds the samplers and does one allocation653STBIRDEF int stbir_build_samplers( STBIR_RESIZE * resize );654655// You MUST call this, if you call stbir_build_samplers or stbir_build_samplers_with_splits656STBIRDEF void stbir_free_samplers( STBIR_RESIZE * resize );657//===============================================================658659660// And this is the main function to perform the resize synchronously on one thread.661STBIRDEF int stbir_resize_extended( STBIR_RESIZE * resize );662663664//===============================================================665// Use these functions for multithreading.666//   1) You call stbir_build_samplers_with_splits first on the main thread667//   2) Then stbir_resize_with_split on each thread668//   3) stbir_free_samplers when done on the main thread669//--------------------------------670671// This will build samplers for threading.672//   You can pass in the number of threads you'd like to use (try_splits).673//   It returns the number of splits (threads) that you can call it with.674///  It might be less if the image resize can't be split up that many ways.675676STBIRDEF int stbir_build_samplers_with_splits( STBIR_RESIZE * resize, int try_splits );677678// This function does a split of the resizing (you call this fuction for each679// split, on multiple threads). A split is a piece of the output resize pixel space.680681// Note that you MUST call stbir_build_samplers_with_splits before stbir_resize_extended_split!682683// Usually, you will always call stbir_resize_split with split_start as the thread_index684//   and "1" for the split_count.685// But, if you have a weird situation where you MIGHT want 8 threads, but sometimes686//   only 4 threads, you can use 0,2,4,6 for the split_start's and use "2" for the687//   split_count each time to turn in into a 4 thread resize. (This is unusual).688689STBIRDEF int stbir_resize_extended_split( STBIR_RESIZE * resize, int split_start, int split_count );690//===============================================================691692693//===============================================================694// Pixel Callbacks info:695//--------------------------------696697//   The input callback is super flexible - it calls you with the input address698//   (based on the stride and base pointer), it gives you an optional_output699//   pointer that you can fill, or you can just return your own pointer into700//   your own data.701//702//   You can also do conversion from non-supported data types if necessary - in703//   this case, you ignore the input_ptr and just use the x and y parameters to704//   calculate your own input_ptr based on the size of each non-supported pixel.705//   (Something like the third example below.)706//707//   You can also install just an input or just an output callback by setting the708//   callback that you don't want to zero.709//710//     First example, progress: (getting a callback that you can monitor the progress):711//        void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context )712//        {713//           percentage_done = y / input_height;714//           return input_ptr;  // use buffer from call715//        }716//717//     Next example, copying: (copy from some other buffer or stream):718//        void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context )719//        {720//           CopyOrStreamData( optional_output, other_data_src, num_pixels * pixel_width_in_bytes );721//           return optional_output;  // return the optional buffer that we filled722//        }723//724//     Third example, input another buffer without copying: (zero-copy from other buffer):725//        void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context )726//        {727//           void * pixels = ( (char*) other_image_base ) + ( y * other_image_stride ) + ( x * other_pixel_width_in_bytes );728//           return pixels;       // return pointer to your data without copying729//        }730//731//732//   The output callback is considerably simpler - it just calls you so that you can dump733//   out each scanline. You could even directly copy out to disk if you have a simple format734//   like TGA or BMP. You can also convert to other output types here if you want.735//736//   Simple example:737//        void const * my_output( void * output_ptr, int num_pixels, int y, void * context )738//        {739//           percentage_done = y / output_height;740//           fwrite( output_ptr, pixel_width_in_bytes, num_pixels, output_file );741//        }742//===============================================================743744745746747//===============================================================748// optional built-in profiling API749//--------------------------------750751#ifdef STBIR_PROFILE752753typedef struct STBIR_PROFILE_INFO754{755  stbir_uint64 total_clocks;756757  // how many clocks spent (of total_clocks) in the various resize routines, along with a string description758  //    there are "resize_count" number of zones759  stbir_uint64 clocks[ 8 ];760  char const ** descriptions;761762  // count of clocks and descriptions763  stbir_uint32 count;764} STBIR_PROFILE_INFO;765766// use after calling stbir_resize_extended (or stbir_build_samplers or stbir_build_samplers_with_splits)767STBIRDEF void stbir_resize_build_profile_info( STBIR_PROFILE_INFO * out_info, STBIR_RESIZE const * resize );768769// use after calling stbir_resize_extended770STBIRDEF void stbir_resize_extended_profile_info( STBIR_PROFILE_INFO * out_info, STBIR_RESIZE const * resize );771772// use after calling stbir_resize_extended_split773STBIRDEF void stbir_resize_split_profile_info( STBIR_PROFILE_INFO * out_info, STBIR_RESIZE const * resize, int split_start, int split_num );774775//===============================================================776777#endif778779780////   end header file   /////////////////////////////////////////////////////781#endif // STBIR_INCLUDE_STB_IMAGE_RESIZE2_H782783#if defined(STB_IMAGE_RESIZE_IMPLEMENTATION) || defined(STB_IMAGE_RESIZE2_IMPLEMENTATION)784785#ifndef STBIR_ASSERT786#include <assert.h>787#define STBIR_ASSERT(x) assert(x)788#endif789790#ifndef STBIR_MALLOC791#include <stdlib.h>792#define STBIR_MALLOC(size,user_data) ((void)(user_data), malloc(size))793#define STBIR_FREE(ptr,user_data)    ((void)(user_data), free(ptr))794// (we used the comma operator to evaluate user_data, to avoid "unused parameter" warnings)795#endif796797#ifdef _MSC_VER798799#define stbir__inline __forceinline800801#else802803#define stbir__inline __inline__804805// Clang address sanitizer806#if defined(__has_feature)807  #if __has_feature(address_sanitizer) || __has_feature(memory_sanitizer)808    #ifndef STBIR__SEPARATE_ALLOCATIONS809      #define STBIR__SEPARATE_ALLOCATIONS810    #endif811  #endif812#endif813814#endif815816// GCC and MSVC817#if defined(__SANITIZE_ADDRESS__)818  #ifndef STBIR__SEPARATE_ALLOCATIONS819    #define STBIR__SEPARATE_ALLOCATIONS820  #endif821#endif822823// Always turn off automatic FMA use - use STBIR_USE_FMA if you want.824// Otherwise, this is a determinism disaster.825#ifndef STBIR_DONT_CHANGE_FP_CONTRACT  // override in case you don't want this behavior826#if defined(_MSC_VER) && !defined(__clang__)827#if _MSC_VER > 1200828#pragma fp_contract(off)829#endif830#elif defined(__GNUC__) &&  !defined(__clang__)831#pragma GCC optimize("fp-contract=off")832#else833#pragma STDC FP_CONTRACT OFF834#endif835#endif836837#ifdef _MSC_VER838#define STBIR__UNUSED(v)  (void)(v)839#else840#define STBIR__UNUSED(v)  (void)sizeof(v)841#endif842843#define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0]))844845846#ifndef STBIR_DEFAULT_FILTER_UPSAMPLE847#define STBIR_DEFAULT_FILTER_UPSAMPLE    STBIR_FILTER_CATMULLROM848#endif849850#ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE851#define STBIR_DEFAULT_FILTER_DOWNSAMPLE  STBIR_FILTER_MITCHELL852#endif853854855#ifndef STBIR__HEADER_FILENAME856#define STBIR__HEADER_FILENAME "stb_image_resize2.h"857#endif858859// the internal pixel layout enums are in a different order, so we can easily do range comparisons of types860//   the public pixel layout is ordered in a way that if you cast num_channels (1-4) to the enum, you get something sensible861typedef enum862{863  STBIRI_1CHANNEL = 0,864  STBIRI_2CHANNEL = 1,865  STBIRI_RGB      = 2,866  STBIRI_BGR      = 3,867  STBIRI_4CHANNEL = 4,868869  STBIRI_RGBA = 5,870  STBIRI_BGRA = 6,871  STBIRI_ARGB = 7,872  STBIRI_ABGR = 8,873  STBIRI_RA   = 9,874  STBIRI_AR   = 10,875876  STBIRI_RGBA_PM = 11,877  STBIRI_BGRA_PM = 12,878  STBIRI_ARGB_PM = 13,879  STBIRI_ABGR_PM = 14,880  STBIRI_RA_PM   = 15,881  STBIRI_AR_PM   = 16,882} stbir_internal_pixel_layout;883884// define the public pixel layouts to not compile inside the implementation (to avoid accidental use)885#define STBIR_BGR bad_dont_use_in_implementation886#define STBIR_1CHANNEL STBIR_BGR887#define STBIR_2CHANNEL STBIR_BGR888#define STBIR_RGB STBIR_BGR889#define STBIR_RGBA STBIR_BGR890#define STBIR_4CHANNEL STBIR_BGR891#define STBIR_BGRA STBIR_BGR892#define STBIR_ARGB STBIR_BGR893#define STBIR_ABGR STBIR_BGR894#define STBIR_RA STBIR_BGR895#define STBIR_AR STBIR_BGR896#define STBIR_RGBA_PM STBIR_BGR897#define STBIR_BGRA_PM STBIR_BGR898#define STBIR_ARGB_PM STBIR_BGR899#define STBIR_ABGR_PM STBIR_BGR900#define STBIR_RA_PM STBIR_BGR901#define STBIR_AR_PM STBIR_BGR902903// must match stbir_datatype904static unsigned char stbir__type_size[] = {905  1,1,1,2,4,2 // STBIR_TYPE_UINT8,STBIR_TYPE_UINT8_SRGB,STBIR_TYPE_UINT8_SRGB_ALPHA,STBIR_TYPE_UINT16,STBIR_TYPE_FLOAT,STBIR_TYPE_HALF_FLOAT906};907908// When gathering, the contributors are which source pixels contribute.909// When scattering, the contributors are which destination pixels are contributed to.910typedef struct911{912  int n0; // First contributing pixel913  int n1; // Last contributing pixel914} stbir__contributors;915916typedef struct917{918  int lowest;    // First sample index for whole filter919  int highest;   // Last sample index for whole filter920  int widest;    // widest single set of samples for an output921} stbir__filter_extent_info;922923typedef struct924{925  int n0; // First pixel of decode buffer to write to926  int n1; // Last pixel of decode that will be written to927  int pixel_offset_for_input;  // Pixel offset into input_scanline928} stbir__span;929930typedef struct stbir__scale_info931{932  int input_full_size;933  int output_sub_size;934  float scale;935  float inv_scale;936  float pixel_shift; // starting shift in output pixel space (in pixels)937  int scale_is_rational;938  stbir_uint32 scale_numerator, scale_denominator;939} stbir__scale_info;940941typedef struct942{943  stbir__contributors * contributors;944  float* coefficients;945  stbir__contributors * gather_prescatter_contributors;946  float * gather_prescatter_coefficients;947  stbir__scale_info scale_info;948  float support;949  stbir_filter filter_enum;950  stbir__kernel_callback * filter_kernel;951  stbir__support_callback * filter_support;952  stbir_edge edge;953  int coefficient_width;954  int filter_pixel_width;955  int filter_pixel_margin;956  int num_contributors;957  int contributors_size;958  int coefficients_size;959  stbir__filter_extent_info extent_info;960  int is_gather;  // 0 = scatter, 1 = gather with scale >= 1, 2 = gather with scale < 1961  int gather_prescatter_num_contributors;962  int gather_prescatter_coefficient_width;963  int gather_prescatter_contributors_size;964  int gather_prescatter_coefficients_size;965} stbir__sampler;966967typedef struct968{969  stbir__contributors conservative;970  int edge_sizes[2];    // this can be less than filter_pixel_margin, if the filter and scaling falls off971  stbir__span spans[2]; // can be two spans, if doing input subrect with clamp mode WRAP972} stbir__extents;973974typedef struct975{976#ifdef STBIR_PROFILE977  union978  {979    struct { stbir_uint64 total, looping, vertical, horizontal, decode, encode, alpha, unalpha; } named;980    stbir_uint64 array[8];981  } profile;982  stbir_uint64 * current_zone_excluded_ptr;983#endif984  float* decode_buffer;985986  int ring_buffer_first_scanline;987  int ring_buffer_last_scanline;988  int ring_buffer_begin_index;    // first_scanline is at this index in the ring buffer989  int start_output_y, end_output_y;990  int start_input_y, end_input_y;  // used in scatter only991992  #ifdef STBIR__SEPARATE_ALLOCATIONS993    float** ring_buffers; // one pointer for each ring buffer994  #else995    float* ring_buffer;  // one big buffer that we index into996  #endif997998  float* vertical_buffer;9991000  char no_cache_straddle[64];1001} stbir__per_split_info;10021003typedef float * stbir__decode_pixels_func( float * decode, int width_times_channels, void const * input );1004typedef void stbir__alpha_weight_func( float * decode_buffer, int width_times_channels );1005typedef void stbir__horizontal_gather_channels_func( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer,1006  stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width );1007typedef void stbir__alpha_unweight_func(float * encode_buffer, int width_times_channels );1008typedef void stbir__encode_pixels_func( void * output, int width_times_channels, float const * encode );10091010struct stbir__info1011{1012#ifdef STBIR_PROFILE1013  union1014  {1015    struct { stbir_uint64 total, build, alloc, horizontal, vertical, cleanup, pivot; } named;1016    stbir_uint64 array[7];1017  } profile;1018  stbir_uint64 * current_zone_excluded_ptr;1019#endif1020  stbir__sampler horizontal;1021  stbir__sampler vertical;10221023  void const * input_data;1024  void * output_data;10251026  int input_stride_bytes;1027  int output_stride_bytes;1028  int ring_buffer_length_bytes;   // The length of an individual entry in the ring buffer. The total number of ring buffers is stbir__get_filter_pixel_width(filter)1029  int ring_buffer_num_entries;    // Total number of entries in the ring buffer.10301031  stbir_datatype input_type;1032  stbir_datatype output_type;10331034  stbir_input_callback * in_pixels_cb;1035  void * user_data;1036  stbir_output_callback * out_pixels_cb;10371038  stbir__extents scanline_extents;10391040  void * alloced_mem;1041  stbir__per_split_info * split_info;  // by default 1, but there will be N of these allocated based on the thread init you did10421043  stbir__decode_pixels_func * decode_pixels;1044  stbir__alpha_weight_func * alpha_weight;1045  stbir__horizontal_gather_channels_func * horizontal_gather_channels;1046  stbir__alpha_unweight_func * alpha_unweight;1047  stbir__encode_pixels_func * encode_pixels;10481049  int alloc_ring_buffer_num_entries;    // Number of entries in the ring buffer that will be allocated1050  int splits; // count of splits10511052  stbir_internal_pixel_layout input_pixel_layout_internal;1053  stbir_internal_pixel_layout output_pixel_layout_internal;10541055  int input_color_and_type;1056  int offset_x, offset_y; // offset within output_data1057  int vertical_first;1058  int channels;1059  int effective_channels; // same as channels, except on RGBA/ARGB (7), or XA/AX (3)1060  size_t alloced_total;1061};106210631064#define stbir__max_uint8_as_float             255.0f1065#define stbir__max_uint16_as_float            65535.0f1066#define stbir__max_uint8_as_float_inverted    3.9215689e-03f     // (1.0f/255.0f)1067#define stbir__max_uint16_as_float_inverted   1.5259022e-05f     // (1.0f/65535.0f)1068#define stbir__small_float ((float)1 / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20))10691070// min/max friendly1071#define STBIR_CLAMP(x, xmin, xmax) for(;;) { \1072  if ( (x) < (xmin) ) (x) = (xmin);     \1073  if ( (x) > (xmax) ) (x) = (xmax);     \1074  break;                                \1075}10761077static stbir__inline int stbir__min(int a, int b)1078{1079  return a < b ? a : b;1080}10811082static stbir__inline int stbir__max(int a, int b)1083{1084  return a > b ? a : b;1085}10861087static float stbir__srgb_uchar_to_linear_float[256] = {1088  0.000000f, 0.000304f, 0.000607f, 0.000911f, 0.001214f, 0.001518f, 0.001821f, 0.002125f, 0.002428f, 0.002732f, 0.003035f,1089  0.003347f, 0.003677f, 0.004025f, 0.004391f, 0.004777f, 0.005182f, 0.005605f, 0.006049f, 0.006512f, 0.006995f, 0.007499f,1090  0.008023f, 0.008568f, 0.009134f, 0.009721f, 0.010330f, 0.010960f, 0.011612f, 0.012286f, 0.012983f, 0.013702f, 0.014444f,1091  0.015209f, 0.015996f, 0.016807f, 0.017642f, 0.018500f, 0.019382f, 0.020289f, 0.021219f, 0.022174f, 0.023153f, 0.024158f,1092  0.025187f, 0.026241f, 0.027321f, 0.028426f, 0.029557f, 0.030713f, 0.031896f, 0.033105f, 0.034340f, 0.035601f, 0.036889f,1093  0.038204f, 0.039546f, 0.040915f, 0.042311f, 0.043735f, 0.045186f, 0.046665f, 0.048172f, 0.049707f, 0.051269f, 0.052861f,1094  0.054480f, 0.056128f, 0.057805f, 0.059511f, 0.061246f, 0.063010f, 0.064803f, 0.066626f, 0.068478f, 0.070360f, 0.072272f,1095  0.074214f, 0.076185f, 0.078187f, 0.080220f, 0.082283f, 0.084376f, 0.086500f, 0.088656f, 0.090842f, 0.093059f, 0.095307f,1096  0.097587f, 0.099899f, 0.102242f, 0.104616f, 0.107023f, 0.109462f, 0.111932f, 0.114435f, 0.116971f, 0.119538f, 0.122139f,1097  0.124772f, 0.127438f, 0.130136f, 0.132868f, 0.135633f, 0.138432f, 0.141263f, 0.144128f, 0.147027f, 0.149960f, 0.152926f,1098  0.155926f, 0.158961f, 0.162029f, 0.165132f, 0.168269f, 0.171441f, 0.174647f, 0.177888f, 0.181164f, 0.184475f, 0.187821f,1099  0.191202f, 0.194618f, 0.198069f, 0.201556f, 0.205079f, 0.208637f, 0.212231f, 0.215861f, 0.219526f, 0.223228f, 0.226966f,1100  0.230740f, 0.234551f, 0.238398f, 0.242281f, 0.246201f, 0.250158f, 0.254152f, 0.258183f, 0.262251f, 0.266356f, 0.270498f,1101  0.274677f, 0.278894f, 0.283149f, 0.287441f, 0.291771f, 0.296138f, 0.300544f, 0.304987f, 0.309469f, 0.313989f, 0.318547f,1102  0.323143f, 0.327778f, 0.332452f, 0.337164f, 0.341914f, 0.346704f, 0.351533f, 0.356400f, 0.361307f, 0.366253f, 0.371238f,1103  0.376262f, 0.381326f, 0.386430f, 0.391573f, 0.396755f, 0.401978f, 0.407240f, 0.412543f, 0.417885f, 0.423268f, 0.428691f,1104  0.434154f, 0.439657f, 0.445201f, 0.450786f, 0.456411f, 0.462077f, 0.467784f, 0.473532f, 0.479320f, 0.485150f, 0.491021f,1105  0.496933f, 0.502887f, 0.508881f, 0.514918f, 0.520996f, 0.527115f, 0.533276f, 0.539480f, 0.545725f, 0.552011f, 0.558340f,1106  0.564712f, 0.571125f, 0.577581f, 0.584078f, 0.590619f, 0.597202f, 0.603827f, 0.610496f, 0.617207f, 0.623960f, 0.630757f,1107  0.637597f, 0.644480f, 0.651406f, 0.658375f, 0.665387f, 0.672443f, 0.679543f, 0.686685f, 0.693872f, 0.701102f, 0.708376f,1108  0.715694f, 0.723055f, 0.730461f, 0.737911f, 0.745404f, 0.752942f, 0.760525f, 0.768151f, 0.775822f, 0.783538f, 0.791298f,1109  0.799103f, 0.806952f, 0.814847f, 0.822786f, 0.830770f, 0.838799f, 0.846873f, 0.854993f, 0.863157f, 0.871367f, 0.879622f,1110  0.887923f, 0.896269f, 0.904661f, 0.913099f, 0.921582f, 0.930111f, 0.938686f, 0.947307f, 0.955974f, 0.964686f, 0.973445f,1111  0.982251f, 0.991102f, 1.0f1112};11131114typedef union1115{1116  unsigned int u;1117  float f;1118} stbir__FP32;11191120// From https://gist.github.com/rygorous/220383411211122static const stbir_uint32 fp32_to_srgb8_tab4[104] = {1123  0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d,1124  0x00a7001a, 0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a,1125  0x010e0033, 0x01280033, 0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033,1126  0x01dc0067, 0x020f0067, 0x02430067, 0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067,1127  0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce, 0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5,1128  0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112, 0x09940106, 0x0a1700fc, 0x0a9500f2,1129  0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e, 0x0fbc0150, 0x10630143,1130  0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0, 0x182401af,1131  0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240,1132  0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300,1133  0x31d105b0, 0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401,1134  0x44c20798, 0x488e071e, 0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559,1135  0x5e0c0a23, 0x631c0980, 0x67db08f6, 0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723,1136};11371138static stbir__inline stbir_uint8 stbir__linear_to_srgb_uchar(float in)1139{1140  static const stbir__FP32 almostone = { 0x3f7fffff }; // 1-eps1141  static const stbir__FP32 minval = { (127-13) << 23 };1142  stbir_uint32 tab,bias,scale,t;1143  stbir__FP32 f;11441145  // Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.1146  // The tests are carefully written so that NaNs map to 0, same as in the reference1147  // implementation.1148  if (!(in > minval.f)) // written this way to catch NaNs1149      return 0;1150  if (in > almostone.f)1151      return 255;11521153  // Do the table lookup and unpack bias, scale1154  f.f = in;1155  tab = fp32_to_srgb8_tab4[(f.u - minval.u) >> 20];1156  bias = (tab >> 16) << 9;1157  scale = tab & 0xffff;11581159  // Grab next-highest mantissa bits and perform linear interpolation1160  t = (f.u >> 12) & 0xff;1161  return (unsigned char) ((bias + scale*t) >> 16);1162}11631164#ifndef STBIR_FORCE_GATHER_FILTER_SCANLINES_AMOUNT1165#define STBIR_FORCE_GATHER_FILTER_SCANLINES_AMOUNT 32 // when downsampling and <= 32 scanlines of buffering, use gather. gather used down to 1/8th scaling for 25% win.1166#endif11671168#ifndef STBIR_FORCE_MINIMUM_SCANLINES_FOR_SPLITS1169#define STBIR_FORCE_MINIMUM_SCANLINES_FOR_SPLITS 4 // when threading, what is the minimum number of scanlines for a split?1170#endif11711172#define STBIR_INPUT_CALLBACK_PADDING 311731174#ifdef _M_IX86_FP1175#if ( _M_IX86_FP >= 1 )1176#ifndef STBIR_SSE1177#define STBIR_SSE1178#endif1179#endif1180#endif11811182#ifdef __TINYC__1183  // tiny c has no intrinsics yet - this can become a version check if they add them1184  #define STBIR_NO_SIMD1185#endif11861187#if defined(_x86_64) || defined( __x86_64__ ) || defined( _M_X64 ) || defined(__x86_64) || defined(_M_AMD64) || defined(__SSE2__) || defined(STBIR_SSE) || defined(STBIR_SSE2)1188  #ifndef STBIR_SSE21189    #define STBIR_SSE21190  #endif1191  #if defined(__AVX__) || defined(STBIR_AVX2)1192    #ifndef STBIR_AVX1193      #ifndef STBIR_NO_AVX1194        #define STBIR_AVX1195      #endif1196    #endif1197  #endif1198  #if defined(__AVX2__) || defined(STBIR_AVX2)1199    #ifndef STBIR_NO_AVX21200      #ifndef STBIR_AVX21201        #define STBIR_AVX21202      #endif1203      #if defined( _MSC_VER ) && !defined(__clang__)1204        #ifndef STBIR_FP16C  // FP16C instructions are on all AVX2 cpus, so we can autoselect it here on microsoft - clang needs -mf16c1205          #define STBIR_FP16C1206        #endif1207      #endif1208    #endif1209  #endif1210  #ifdef __F16C__1211    #ifndef STBIR_FP16C  // turn on FP16C instructions if the define is set (for clang and gcc)1212      #define STBIR_FP16C1213    #endif1214  #endif1215#endif12161217#if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || ((__ARM_NEON_FP & 4) != 0) || defined(__ARM_NEON__)1218#ifndef STBIR_NEON1219#define STBIR_NEON1220#endif1221#endif12221223#if defined(_M_ARM) || defined(__arm__)1224#ifdef STBIR_USE_FMA1225#undef STBIR_USE_FMA // no FMA for 32-bit arm on MSVC1226#endif1227#endif12281229#if defined(__wasm__) && defined(__wasm_simd128__)1230#ifndef STBIR_WASM1231#define STBIR_WASM1232#endif1233#endif12341235// restrict pointers for the output pointers, other loop and unroll control1236#if defined( _MSC_VER ) && !defined(__clang__)1237  #define STBIR_STREAMOUT_PTR( star ) star __restrict1238  #define STBIR_NO_UNROLL( ptr ) __assume(ptr) // this oddly keeps msvc from unrolling a loop1239  #if _MSC_VER >= 19001240    #define STBIR_NO_UNROLL_LOOP_START __pragma(loop( no_vector )) 1241  #else1242    #define STBIR_NO_UNROLL_LOOP_START 1243  #endif1244#elif defined( __clang__ )1245  #define STBIR_STREAMOUT_PTR( star ) star __restrict__1246  #define STBIR_NO_UNROLL( ptr ) __asm__ (""::"r"(ptr)) 1247  #if ( __clang_major__ >= 4 ) || ( ( __clang_major__ >= 3 ) && ( __clang_minor__ >= 5 ) )1248    #define STBIR_NO_UNROLL_LOOP_START _Pragma("clang loop unroll(disable)") _Pragma("clang loop vectorize(disable)")1249  #else1250    #define STBIR_NO_UNROLL_LOOP_START1251  #endif 1252#elif defined( __GNUC__ )1253  #define STBIR_STREAMOUT_PTR( star ) star __restrict__1254  #define STBIR_NO_UNROLL( ptr ) __asm__ (""::"r"(ptr))1255  #if __GNUC__ >= 141256    #define STBIR_NO_UNROLL_LOOP_START _Pragma("GCC unroll 0") _Pragma("GCC novector")1257  #else1258    #define STBIR_NO_UNROLL_LOOP_START1259  #endif1260  #define STBIR_NO_UNROLL_LOOP_START_INF_FOR1261#else1262  #define STBIR_STREAMOUT_PTR( star ) star1263  #define STBIR_NO_UNROLL( ptr )1264  #define STBIR_NO_UNROLL_LOOP_START1265#endif12661267#ifndef STBIR_NO_UNROLL_LOOP_START_INF_FOR1268#define STBIR_NO_UNROLL_LOOP_START_INF_FOR STBIR_NO_UNROLL_LOOP_START1269#endif12701271#ifdef STBIR_NO_SIMD // force simd off for whatever reason12721273// force simd off overrides everything else, so clear it all12741275#ifdef STBIR_SSE21276#undef STBIR_SSE21277#endif12781279#ifdef STBIR_AVX1280#undef STBIR_AVX1281#endif12821283#ifdef STBIR_NEON1284#undef STBIR_NEON1285#endif12861287#ifdef STBIR_AVX21288#undef STBIR_AVX21289#endif12901291#ifdef STBIR_FP16C1292#undef STBIR_FP16C1293#endif12941295#ifdef STBIR_WASM1296#undef STBIR_WASM1297#endif12981299#ifdef STBIR_SIMD1300#undef STBIR_SIMD1301#endif13021303#else // STBIR_SIMD13041305#ifdef STBIR_SSE21306  #include <emmintrin.h>13071308  #define stbir__simdf __m1281309  #define stbir__simdi __m128i13101311  #define stbir_simdi_castf( reg ) _mm_castps_si128(reg)1312  #define stbir_simdf_casti( reg ) _mm_castsi128_ps(reg)13131314  #define stbir__simdf_load( reg, ptr ) (reg) = _mm_loadu_ps( (float const*)(ptr) )1315  #define stbir__simdi_load( reg, ptr ) (reg) = _mm_loadu_si128 ( (stbir__simdi const*)(ptr) )1316  #define stbir__simdf_load1( out, ptr ) (out) = _mm_load_ss( (float const*)(ptr) )  // top values can be random (not denormal or nan for perf)1317  #define stbir__simdi_load1( out, ptr ) (out) = _mm_castps_si128( _mm_load_ss( (float const*)(ptr) ))1318  #define stbir__simdf_load1z( out, ptr ) (out) = _mm_load_ss( (float const*)(ptr) )  // top values must be zero1319  #define stbir__simdf_frep4( fvar ) _mm_set_ps1( fvar )1320  #define stbir__simdf_load1frep4( out, fvar ) (out) = _mm_set_ps1( fvar )1321  #define stbir__simdf_load2( out, ptr ) (out) = _mm_castsi128_ps( _mm_loadl_epi64( (__m128i*)(ptr)) ) // top values can be random (not denormal or nan for perf)1322  #define stbir__simdf_load2z( out, ptr ) (out) = _mm_castsi128_ps( _mm_loadl_epi64( (__m128i*)(ptr)) ) // top values must be zero1323  #define stbir__simdf_load2hmerge( out, reg, ptr ) (out) = _mm_castpd_ps(_mm_loadh_pd( _mm_castps_pd(reg), (double*)(ptr) ))13241325  #define stbir__simdf_zeroP() _mm_setzero_ps()1326  #define stbir__simdf_zero( reg ) (reg) = _mm_setzero_ps()13271328  #define stbir__simdf_store( ptr, reg )  _mm_storeu_ps( (float*)(ptr), reg )1329  #define stbir__simdf_store1( ptr, reg ) _mm_store_ss( (float*)(ptr), reg )1330  #define stbir__simdf_store2( ptr, reg ) _mm_storel_epi64( (__m128i*)(ptr), _mm_castps_si128(reg) )1331  #define stbir__simdf_store2h( ptr, reg ) _mm_storeh_pd( (double*)(ptr), _mm_castps_pd(reg) )13321333  #define stbir__simdi_store( ptr, reg )  _mm_storeu_si128( (__m128i*)(ptr), reg )1334  #define stbir__simdi_store1( ptr, reg ) _mm_store_ss( (float*)(ptr), _mm_castsi128_ps(reg) )1335  #define stbir__simdi_store2( ptr, reg ) _mm_storel_epi64( (__m128i*)(ptr), (reg) )13361337  #define stbir__prefetch( ptr ) _mm_prefetch((char*)(ptr), _MM_HINT_T0 )13381339  #define stbir__simdi_expand_u8_to_u32(out0,out1,out2,out3,ireg) \1340  { \1341    stbir__simdi zero = _mm_setzero_si128(); \1342    out2 = _mm_unpacklo_epi8( ireg, zero ); \1343    out3 = _mm_unpackhi_epi8( ireg, zero ); \1344    out0 = _mm_unpacklo_epi16( out2, zero ); \1345    out1 = _mm_unpackhi_epi16( out2, zero ); \1346    out2 = _mm_unpacklo_epi16( out3, zero ); \1347    out3 = _mm_unpackhi_epi16( out3, zero ); \1348  }13491350#define stbir__simdi_expand_u8_to_1u32(out,ireg) \1351  { \1352    stbir__simdi zero = _mm_setzero_si128(); \1353    out = _mm_unpacklo_epi8( ireg, zero ); \1354    out = _mm_unpacklo_epi16( out, zero ); \1355  }13561357  #define stbir__simdi_expand_u16_to_u32(out0,out1,ireg) \1358  { \1359    stbir__simdi zero = _mm_setzero_si128(); \1360    out0 = _mm_unpacklo_epi16( ireg, zero ); \1361    out1 = _mm_unpackhi_epi16( ireg, zero ); \1362  }13631364  #define stbir__simdf_convert_float_to_i32( i, f ) (i) = _mm_cvttps_epi32(f)1365  #define stbir__simdf_convert_float_to_int( f ) _mm_cvtt_ss2si(f)1366  #define stbir__simdf_convert_float_to_uint8( f ) ((unsigned char)_mm_cvtsi128_si32(_mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(f,STBIR__CONSTF(STBIR_max_uint8_as_float)),_mm_setzero_ps()))))1367  #define stbir__simdf_convert_float_to_short( f ) ((unsigned short)_mm_cvtsi128_si32(_mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(f,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps()))))13681369  #define stbir__simdi_to_int( i ) _mm_cvtsi128_si32(i)1370  #define stbir__simdi_convert_i32_to_float(out, ireg) (out) = _mm_cvtepi32_ps( ireg )1371  #define stbir__simdf_add( out, reg0, reg1 ) (out) = _mm_add_ps( reg0, reg1 )1372  #define stbir__simdf_mult( out, reg0, reg1 ) (out) = _mm_mul_ps( reg0, reg1 )1373  #define stbir__simdf_mult_mem( out, reg, ptr ) (out) = _mm_mul_ps( reg, _mm_loadu_ps( (float const*)(ptr) ) )1374  #define stbir__simdf_mult1_mem( out, reg, ptr ) (out) = _mm_mul_ss( reg, _mm_load_ss( (float const*)(ptr) ) )1375  #define stbir__simdf_add_mem( out, reg, ptr ) (out) = _mm_add_ps( reg, _mm_loadu_ps( (float const*)(ptr) ) )1376  #define stbir__simdf_add1_mem( out, reg, ptr ) (out) = _mm_add_ss( reg, _mm_load_ss( (float const*)(ptr) ) )13771378  #ifdef STBIR_USE_FMA           // not on by default to maintain bit identical simd to non-simd1379  #include <immintrin.h>1380  #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = _mm_fmadd_ps( mul1, mul2, add )1381  #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = _mm_fmadd_ss( mul1, mul2, add )1382  #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = _mm_fmadd_ps( mul, _mm_loadu_ps( (float const*)(ptr) ), add )1383  #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = _mm_fmadd_ss( mul, _mm_load_ss( (float const*)(ptr) ), add )1384  #else1385  #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = _mm_add_ps( add, _mm_mul_ps( mul1, mul2 ) )1386  #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = _mm_add_ss( add, _mm_mul_ss( mul1, mul2 ) )1387  #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = _mm_add_ps( add, _mm_mul_ps( mul, _mm_loadu_ps( (float const*)(ptr) ) ) )1388  #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = _mm_add_ss( add, _mm_mul_ss( mul, _mm_load_ss( (float const*)(ptr) ) ) )1389  #endif13901391  #define stbir__simdf_add1( out, reg0, reg1 ) (out) = _mm_add_ss( reg0, reg1 )1392  #define stbir__simdf_mult1( out, reg0, reg1 ) (out) = _mm_mul_ss( reg0, reg1 )13931394  #define stbir__simdf_and( out, reg0, reg1 ) (out) = _mm_and_ps( reg0, reg1 )1395  #define stbir__simdf_or( out, reg0, reg1 ) (out) = _mm_or_ps( reg0, reg1 )13961397  #define stbir__simdf_min( out, reg0, reg1 ) (out) = _mm_min_ps( reg0, reg1 )1398  #define stbir__simdf_max( out, reg0, reg1 ) (out) = _mm_max_ps( reg0, reg1 )1399  #define stbir__simdf_min1( out, reg0, reg1 ) (out) = _mm_min_ss( reg0, reg1 )1400  #define stbir__simdf_max1( out, reg0, reg1 ) (out) = _mm_max_ss( reg0, reg1 )14011402  #define stbir__simdf_0123ABCDto3ABx( out, reg0, reg1 ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_shuffle_ps( reg1,reg0, (0<<0) + (1<<2) + (2<<4) + (3<<6) )), (3<<0) + (0<<2) + (1<<4) + (2<<6) ) )1403  #define stbir__simdf_0123ABCDto23Ax( out, reg0, reg1 ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_shuffle_ps( reg1,reg0, (0<<0) + (1<<2) + (2<<4) + (3<<6) )), (2<<0) + (3<<2) + (0<<4) + (1<<6) ) )14041405  static const stbir__simdf STBIR_zeroones = { 0.0f,1.0f,0.0f,1.0f };1406  static const stbir__simdf STBIR_onezeros = { 1.0f,0.0f,1.0f,0.0f };1407  #define stbir__simdf_aaa1( out, alp, ones ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_movehl_ps( ones, alp ) ), (1<<0) + (1<<2) + (1<<4) + (2<<6) ) )1408  #define stbir__simdf_1aaa( out, alp, ones ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_movelh_ps( ones, alp ) ), (0<<0) + (2<<2) + (2<<4) + (2<<6) ) )1409  #define stbir__simdf_a1a1( out, alp, ones) (out) = _mm_or_ps( _mm_castsi128_ps( _mm_srli_epi64( _mm_castps_si128(alp), 32 ) ), STBIR_zeroones )1410  #define stbir__simdf_1a1a( out, alp, ones) (out) = _mm_or_ps( _mm_castsi128_ps( _mm_slli_epi64( _mm_castps_si128(alp), 32 ) ), STBIR_onezeros )14111412  #define stbir__simdf_swiz( reg, one, two, three, four ) _mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( reg ), (one<<0) + (two<<2) + (three<<4) + (four<<6) ) )14131414  #define stbir__simdi_and( out, reg0, reg1 ) (out) = _mm_and_si128( reg0, reg1 )1415  #define stbir__simdi_or( out, reg0, reg1 ) (out) = _mm_or_si128( reg0, reg1 )1416  #define stbir__simdi_16madd( out, reg0, reg1 ) (out) = _mm_madd_epi16( reg0, reg1 )14171418  #define stbir__simdf_pack_to_8bytes(out,aa,bb) \1419  { \1420    stbir__simdf af,bf; \1421    stbir__simdi a,b; \1422    af = _mm_min_ps( aa, STBIR_max_uint8_as_float ); \1423    bf = _mm_min_ps( bb, STBIR_max_uint8_as_float ); \1424    af = _mm_max_ps( af, _mm_setzero_ps() ); \1425    bf = _mm_max_ps( bf, _mm_setzero_ps() ); \1426    a = _mm_cvttps_epi32( af ); \1427    b = _mm_cvttps_epi32( bf ); \1428    a = _mm_packs_epi32( a, b ); \1429    out = _mm_packus_epi16( a, a ); \1430  }14311432  #define stbir__simdf_load4_transposed( o0, o1, o2, o3, ptr ) \1433      stbir__simdf_load( o0, (ptr) );    \1434      stbir__simdf_load( o1, (ptr)+4 );  \1435      stbir__simdf_load( o2, (ptr)+8 );  \1436      stbir__simdf_load( o3, (ptr)+12 ); \1437      {                                  \1438        __m128 tmp0, tmp1, tmp2, tmp3;   \1439        tmp0 = _mm_unpacklo_ps(o0, o1);  \1440        tmp2 = _mm_unpacklo_ps(o2, o3);  \1441        tmp1 = _mm_unpackhi_ps(o0, o1);  \1442        tmp3 = _mm_unpackhi_ps(o2, o3);  \1443        o0 = _mm_movelh_ps(tmp0, tmp2);  \1444        o1 = _mm_movehl_ps(tmp2, tmp0);  \1445        o2 = _mm_movelh_ps(tmp1, tmp3);  \1446        o3 = _mm_movehl_ps(tmp3, tmp1);  \1447      }14481449  #define stbir__interleave_pack_and_store_16_u8( ptr, r0, r1, r2, r3 ) \1450      r0 = _mm_packs_epi32( r0, r1 ); \1451      r2 = _mm_packs_epi32( r2, r3 ); \1452      r1 = _mm_unpacklo_epi16( r0, r2 ); \1453      r3 = _mm_unpackhi_epi16( r0, r2 ); \1454      r0 = _mm_unpacklo_epi16( r1, r3 ); \1455      r2 = _mm_unpackhi_epi16( r1, r3 ); \1456      r0 = _mm_packus_epi16( r0, r2 ); \1457      stbir__simdi_store( ptr, r0 ); \14581459  #define stbir__simdi_32shr( out, reg, imm ) out = _mm_srli_epi32( reg, imm )14601461  #if defined(_MSC_VER) && !defined(__clang__)1462    // msvc inits with 8 bytes1463    #define STBIR__CONST_32_TO_8( v ) (char)(unsigned char)((v)&255),(char)(unsigned char)(((v)>>8)&255),(char)(unsigned char)(((v)>>16)&255),(char)(unsigned char)(((v)>>24)&255)1464    #define STBIR__CONST_4_32i( v ) STBIR__CONST_32_TO_8( v ), STBIR__CONST_32_TO_8( v ), STBIR__CONST_32_TO_8( v ), STBIR__CONST_32_TO_8( v )1465    #define STBIR__CONST_4d_32i( v0, v1, v2, v3 ) STBIR__CONST_32_TO_8( v0 ), STBIR__CONST_32_TO_8( v1 ), STBIR__CONST_32_TO_8( v2 ), STBIR__CONST_32_TO_8( v3 )1466  #else1467    // everything else inits with long long's1468    #define STBIR__CONST_4_32i( v ) (long long)((((stbir_uint64)(stbir_uint32)(v))<<32)|((stbir_uint64)(stbir_uint32)(v))),(long long)((((stbir_uint64)(stbir_uint32)(v))<<32)|((stbir_uint64)(stbir_uint32)(v)))1469    #define STBIR__CONST_4d_32i( v0, v1, v2, v3 ) (long long)((((stbir_uint64)(stbir_uint32)(v1))<<32)|((stbir_uint64)(stbir_uint32)(v0))),(long long)((((stbir_uint64)(stbir_uint32)(v3))<<32)|((stbir_uint64)(stbir_uint32)(v2)))1470  #endif14711472  #define STBIR__SIMDF_CONST(var, x) stbir__simdf var = { x, x, x, x }1473  #define STBIR__SIMDI_CONST(var, x) stbir__simdi var = { STBIR__CONST_4_32i(x) }1474  #define STBIR__CONSTF(var) (var)1475  #define STBIR__CONSTI(var) (var)14761477  #if defined(STBIR_AVX) || defined(__SSE4_1__)1478    #include <smmintrin.h>1479    #define stbir__simdf_pack_to_8words(out,reg0,reg1) out = _mm_packus_epi32(_mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg0,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())), _mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg1,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())))1480  #else1481    static STBIR__SIMDI_CONST(stbir__s32_32768, 32768);1482    static STBIR__SIMDI_CONST(stbir__s16_32768, ((32768<<16)|32768));14831484    #define stbir__simdf_pack_to_8words(out,reg0,reg1) \1485      { \1486        stbir__simdi tmp0,tmp1; \1487        tmp0 = _mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg0,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())); \1488        tmp1 = _mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg1,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())); \1489        tmp0 = _mm_sub_epi32( tmp0, stbir__s32_32768 ); \1490        tmp1 = _mm_sub_epi32( tmp1, stbir__s32_32768 ); \1491        out = _mm_packs_epi32( tmp0, tmp1 ); \1492        out = _mm_sub_epi16( out, stbir__s16_32768 ); \1493      }14941495  #endif14961497  #define STBIR_SIMD14981499  // if we detect AVX, set the simd8 defines1500  #ifdef STBIR_AVX1501    #include <immintrin.h>1502    #define STBIR_SIMD81503    #define stbir__simdf8 __m2561504    #define stbir__simdi8 __m256i1505    #define stbir__simdf8_load( out, ptr ) (out) = _mm256_loadu_ps( (float const *)(ptr) )1506    #define stbir__simdi8_load( out, ptr ) (out) = _mm256_loadu_si256( (__m256i const *)(ptr) )1507    #define stbir__simdf8_mult( out, a, b ) (out) = _mm256_mul_ps( (a), (b) )1508    #define stbir__simdf8_store( ptr, out ) _mm256_storeu_ps( (float*)(ptr), out )1509    #define stbir__simdi8_store( ptr, reg )  _mm256_storeu_si256( (__m256i*)(ptr), reg )1510    #define stbir__simdf8_frep8( fval ) _mm256_set1_ps( fval )15111512    #define stbir__simdf8_min( out, reg0, reg1 ) (out) = _mm256_min_ps( reg0, reg1 )1513    #define stbir__simdf8_max( out, reg0, reg1 ) (out) = _mm256_max_ps( reg0, reg1 )15141515    #define stbir__simdf8_add4halves( out, bot4, top8 ) (out) = _mm_add_ps( bot4, _mm256_extractf128_ps( top8, 1 ) )1516    #define stbir__simdf8_mult_mem( out, reg, ptr ) (out) = _mm256_mul_ps( reg, _mm256_loadu_ps( (float const*)(ptr) ) )1517    #define stbir__simdf8_add_mem( out, reg, ptr ) (out) = _mm256_add_ps( reg, _mm256_loadu_ps( (float const*)(ptr) ) )1518    #define stbir__simdf8_add( out, a, b ) (out) = _mm256_add_ps( a, b )1519    #define stbir__simdf8_load1b( out, ptr ) (out) = _mm256_broadcast_ss( ptr )1520    #define stbir__simdf_load1rep4( out, ptr ) (out) = _mm_broadcast_ss( ptr )  // avx load instruction15211522    #define stbir__simdi8_convert_i32_to_float(out, ireg) (out) = _mm256_cvtepi32_ps( ireg )1523    #define stbir__simdf8_convert_float_to_i32( i, f ) (i) = _mm256_cvttps_epi32(f)15241525    #define stbir__simdf8_bot4s( out, a, b ) (out) = _mm256_permute2f128_ps(a,b, (0<<0)+(2<<4) )1526    #define stbir__simdf8_top4s( out, a, b ) (out) = _mm256_permute2f128_ps(a,b, (1<<0)+(3<<4) )15271528    #define stbir__simdf8_gettop4( reg ) _mm256_extractf128_ps(reg,1)15291530    #ifdef STBIR_AVX215311532    #define stbir__simdi8_expand_u8_to_u32(out0,out1,ireg) \1533    { \1534      stbir__simdi8 a, zero  =_mm256_setzero_si256();\1535      a = _mm256_permute4x64_epi64( _mm256_unpacklo_epi8( _mm256_permute4x64_epi64(_mm256_castsi128_si256(ireg),(0<<0)+(2<<2)+(1<<4)+(3<<6)), zero ),(0<<0)+(2<<2)+(1<<4)+(3<<6)); \1536      out0 = _mm256_unpacklo_epi16( a, zero ); \1537      out1 = _mm256_unpackhi_epi16( a, zero ); \1538    }15391540    #define stbir__simdf8_pack_to_16bytes(out,aa,bb) \1541    { \1542      stbir__simdi8 t; \1543      stbir__simdf8 af,bf; \1544      stbir__simdi8 a,b; \1545      af = _mm256_min_ps( aa, STBIR_max_uint8_as_floatX ); \1546      bf = _mm256_min_ps( bb, STBIR_max_uint8_as_floatX ); \1547      af = _mm256_max_ps( af, _mm256_setzero_ps() ); \1548      bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \1549      a = _mm256_cvttps_epi32( af ); \1550      b = _mm256_cvttps_epi32( bf ); \1551      t = _mm256_permute4x64_epi64( _mm256_packs_epi32( a, b ), (0<<0)+(2<<2)+(1<<4)+(3<<6) ); \1552      out = _mm256_castsi256_si128( _mm256_permute4x64_epi64( _mm256_packus_epi16( t, t ), (0<<0)+(2<<2)+(1<<4)+(3<<6) ) ); \1553    }15541555    #define stbir__simdi8_expand_u16_to_u32(out,ireg) out = _mm256_unpacklo_epi16( _mm256_permute4x64_epi64(_mm256_castsi128_si256(ireg),(0<<0)+(2<<2)+(1<<4)+(3<<6)), _mm256_setzero_si256() );15561557    #define stbir__simdf8_pack_to_16words(out,aa,bb) \1558      { \1559        stbir__simdf8 af,bf; \1560        stbir__simdi8 a,b; \1561        af = _mm256_min_ps( aa, STBIR_max_uint16_as_floatX ); \1562        bf = _mm256_min_ps( bb, STBIR_max_uint16_as_floatX ); \1563        af = _mm256_max_ps( af, _mm256_setzero_ps() ); \1564        bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \1565        a = _mm256_cvttps_epi32( af ); \1566        b = _mm256_cvttps_epi32( bf ); \1567        (out) = _mm256_permute4x64_epi64( _mm256_packus_epi32(a, b), (0<<0)+(2<<2)+(1<<4)+(3<<6) ); \1568      }15691570    #else15711572    #define stbir__simdi8_expand_u8_to_u32(out0,out1,ireg) \1573    { \1574      stbir__simdi a,zero = _mm_setzero_si128(); \1575      a = _mm_unpacklo_epi8( ireg, zero ); \1576      out0 = _mm256_setr_m128i( _mm_unpacklo_epi16( a, zero ), _mm_unpackhi_epi16( a, zero ) ); \1577      a = _mm_unpackhi_epi8( ireg, zero ); \1578      out1 = _mm256_setr_m128i( _mm_unpacklo_epi16( a, zero ), _mm_unpackhi_epi16( a, zero ) ); \1579    }15801581    #define stbir__simdf8_pack_to_16bytes(out,aa,bb) \1582    { \1583      stbir__simdi t; \1584      stbir__simdf8 af,bf; \1585      stbir__simdi8 a,b; \1586      af = _mm256_min_ps( aa, STBIR_max_uint8_as_floatX ); \1587      bf = _mm256_min_ps( bb, STBIR_max_uint8_as_floatX ); \1588      af = _mm256_max_ps( af, _mm256_setzero_ps() ); \1589      bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \1590      a = _mm256_cvttps_epi32( af ); \1591      b = _mm256_cvttps_epi32( bf ); \1592      out = _mm_packs_epi32( _mm256_castsi256_si128(a), _mm256_extractf128_si256( a, 1 ) ); \1593      out = _mm_packus_epi16( out, out ); \1594      t = _mm_packs_epi32( _mm256_castsi256_si128(b), _mm256_extractf128_si256( b, 1 ) ); \1595      t = _mm_packus_epi16( t, t ); \1596      out = _mm_castps_si128( _mm_shuffle_ps( _mm_castsi128_ps(out), _mm_castsi128_ps(t), (0<<0)+(1<<2)+(0<<4)+(1<<6) ) ); \1597    }15981599    #define stbir__simdi8_expand_u16_to_u32(out,ireg) \1600    { \1601      stbir__simdi a,b,zero = _mm_setzero_si128(); \1602      a = _mm_unpacklo_epi16( ireg, zero ); \1603      b = _mm_unpackhi_epi16( ireg, zero ); \1604      out = _mm256_insertf128_si256( _mm256_castsi128_si256( a ), b, 1 ); \1605    }16061607    #define stbir__simdf8_pack_to_16words(out,aa,bb) \1608      { \1609        stbir__simdi t0,t1; \1610        stbir__simdf8 af,bf; \1611        stbir__simdi8 a,b; \1612        af = _mm256_min_ps( aa, STBIR_max_uint16_as_floatX ); \1613        bf = _mm256_min_ps( bb, STBIR_max_uint16_as_floatX ); \1614        af = _mm256_max_ps( af, _mm256_setzero_ps() ); \1615        bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \1616        a = _mm256_cvttps_epi32( af ); \1617        b = _mm256_cvttps_epi32( bf ); \1618        t0 = _mm_packus_epi32( _mm256_castsi256_si128(a), _mm256_extractf128_si256( a, 1 ) ); \1619        t1 = _mm_packus_epi32( _mm256_castsi256_si128(b), _mm256_extractf128_si256( b, 1 ) ); \1620        out = _mm256_setr_m128i( t0, t1 ); \1621      }16221623    #endif16241625    static __m256i stbir_00001111 = { STBIR__CONST_4d_32i( 0, 0, 0, 0 ), STBIR__CONST_4d_32i( 1, 1, 1, 1 ) };1626    #define stbir__simdf8_0123to00001111( out, in ) (out) = _mm256_permutevar_ps ( in, stbir_00001111 )16271628    static __m256i stbir_22223333 = { STBIR__CONST_4d_32i( 2, 2, 2, 2 ), STBIR__CONST_4d_32i( 3, 3, 3, 3 ) };1629    #define stbir__simdf8_0123to22223333( out, in ) (out) = _mm256_permutevar_ps ( in, stbir_22223333 )16301631    #define stbir__simdf8_0123to2222( out, in ) (out) = stbir__simdf_swiz(_mm256_castps256_ps128(in), 2,2,2,2 )16321633    #define stbir__simdf8_load4b( out, ptr ) (out) = _mm256_broadcast_ps( (__m128 const *)(ptr) )16341635    static __m256i stbir_00112233 = { STBIR__CONST_4d_32i( 0, 0, 1, 1 ), STBIR__CONST_4d_32i( 2, 2, 3, 3 ) };1636    #define stbir__simdf8_0123to00112233( out, in ) (out) = _mm256_permutevar_ps ( in, stbir_00112233 )1637    #define stbir__simdf8_add4( out, a8, b ) (out) = _mm256_add_ps( a8,  _mm256_castps128_ps256( b ) )16381639    static __m256i stbir_load6 = { STBIR__CONST_4_32i( 0x80000000 ), STBIR__CONST_4d_32i(  0x80000000,  0x80000000, 0, 0 ) };1640    #define stbir__simdf8_load6z( out, ptr ) (out) = _mm256_maskload_ps( ptr, stbir_load6 )16411642    #define stbir__simdf8_0123to00000000( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (0<<0)+(0<<2)+(0<<4)+(0<<6) )1643    #define stbir__simdf8_0123to11111111( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (1<<0)+(1<<2)+(1<<4)+(1<<6) )1644    #define stbir__simdf8_0123to22222222( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (2<<0)+(2<<2)+(2<<4)+(2<<6) )1645    #define stbir__simdf8_0123to33333333( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (3<<0)+(3<<2)+(3<<4)+(3<<6) )1646    #define stbir__simdf8_0123to21032103( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (2<<0)+(1<<2)+(0<<4)+(3<<6) )1647    #define stbir__simdf8_0123to32103210( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (3<<0)+(2<<2)+(1<<4)+(0<<6) )1648    #define stbir__simdf8_0123to12301230( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (1<<0)+(2<<2)+(3<<4)+(0<<6) )1649    #define stbir__simdf8_0123to10321032( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (1<<0)+(0<<2)+(3<<4)+(2<<6) )1650    #define stbir__simdf8_0123to30123012( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (3<<0)+(0<<2)+(1<<4)+(2<<6) )16511652    #define stbir__simdf8_0123to11331133( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (1<<0)+(1<<2)+(3<<4)+(3<<6) )1653    #define stbir__simdf8_0123to00220022( out, in ) (out) =  _mm256_shuffle_ps ( in, in, (0<<0)+(0<<2)+(2<<4)+(2<<6) )16541655    #define stbir__simdf8_aaa1( out, alp, ones ) (out) = _mm256_blend_ps( alp, ones, (1<<0)+(1<<1)+(1<<2)+(0<<3)+(1<<4)+(1<<5)+(1<<6)+(0<<7)); (out)=_mm256_shuffle_ps( out,out, (3<<0) + (3<<2) + (3<<4) + (0<<6) )1656    #define stbir__simdf8_1aaa( out, alp, ones ) (out) = _mm256_blend_ps( alp, ones, (0<<0)+(1<<1)+(1<<2)+(1<<3)+(0<<4)+(1<<5)+(1<<6)+(1<<7)); (out)=_mm256_shuffle_ps( out,out, (1<<0) + (0<<2) + (0<<4) + (0<<6) )1657    #define stbir__simdf8_a1a1( out, alp, ones) (out) = _mm256_blend_ps( alp, ones, (1<<0)+(0<<1)+(1<<2)+(0<<3)+(1<<4)+(0<<5)+(1<<6)+(0<<7)); (out)=_mm256_shuffle_ps( out,out, (1<<0) + (0<<2) + (3<<4) + (2<<6) )1658    #define stbir__simdf8_1a1a( out, alp, ones) (out) = _mm256_blend_ps( alp, ones, (0<<0)+(1<<1)+(0<<2)+(1<<3)+(0<<4)+(1<<5)+(0<<6)+(1<<7)); (out)=_mm256_shuffle_ps( out,out, (1<<0) + (0<<2) + (3<<4) + (2<<6) )16591660    #define stbir__simdf8_zero( reg ) (reg) = _mm256_setzero_ps()16611662    #ifdef STBIR_USE_FMA           // not on by default to maintain bit identical simd to non-simd1663    #define stbir__simdf8_madd( out, add, mul1, mul2 ) (out) = _mm256_fmadd_ps( mul1, mul2, add )1664    #define stbir__simdf8_madd_mem( out, add, mul, ptr ) (out) = _mm256_fmadd_ps( mul, _mm256_loadu_ps( (float const*)(ptr) ), add )1665    #define stbir__simdf8_madd_mem4( out, add, mul, ptr )(out) = _mm256_fmadd_ps( _mm256_setr_m128( mul, _mm_setzero_ps() ), _mm256_setr_m128( _mm_loadu_ps( (float const*)(ptr) ), _mm_setzero_ps() ), add )1666    #else1667    #define stbir__simdf8_madd( out, add, mul1, mul2 ) (out) = _mm256_add_ps( add, _mm256_mul_ps( mul1, mul2 ) )1668    #define stbir__simdf8_madd_mem( out, add, mul, ptr ) (out) = _mm256_add_ps( add, _mm256_mul_ps( mul, _mm256_loadu_ps( (float const*)(ptr) ) ) )1669    #define stbir__simdf8_madd_mem4( out, add, mul, ptr )  (out) = _mm256_add_ps( add, _mm256_setr_m128( _mm_mul_ps( mul, _mm_loadu_ps( (float const*)(ptr) ) ), _mm_setzero_ps() ) )1670    #endif1671    #define stbir__if_simdf8_cast_to_simdf4( val ) _mm256_castps256_ps128( val )16721673  #endif16741675  #ifdef STBIR_FLOORF1676  #undef STBIR_FLOORF1677  #endif1678  #define STBIR_FLOORF stbir_simd_floorf1679  static stbir__inline float stbir_simd_floorf(float x)  // martins floorf1680  {1681    #if defined(STBIR_AVX) || defined(__SSE4_1__) || defined(STBIR_SSE41)1682    __m128 t = _mm_set_ss(x);1683    return _mm_cvtss_f32( _mm_floor_ss(t, t) );1684    #else1685    __m128 f = _mm_set_ss(x);1686    __m128 t = _mm_cvtepi32_ps(_mm_cvttps_epi32(f));1687    __m128 r = _mm_add_ss(t, _mm_and_ps(_mm_cmplt_ss(f, t), _mm_set_ss(-1.0f)));1688    return _mm_cvtss_f32(r);1689    #endif1690  }16911692  #ifdef STBIR_CEILF1693  #undef STBIR_CEILF1694  #endif1695  #define STBIR_CEILF stbir_simd_ceilf1696  static stbir__inline float stbir_simd_ceilf(float x)  // martins ceilf1697  {1698    #if defined(STBIR_AVX) || defined(__SSE4_1__) || defined(STBIR_SSE41)1699    __m128 t = _mm_set_ss(x);1700    return _mm_cvtss_f32( _mm_ceil_ss(t, t) );1701    #else1702    __m128 f = _mm_set_ss(x);1703    __m128 t = _mm_cvtepi32_ps(_mm_cvttps_epi32(f));1704    __m128 r = _mm_add_ss(t, _mm_and_ps(_mm_cmplt_ss(t, f), _mm_set_ss(1.0f)));1705    return _mm_cvtss_f32(r);1706    #endif1707  }17081709#elif defined(STBIR_NEON)17101711  #include <arm_neon.h>17121713  #define stbir__simdf float32x4_t1714  #define stbir__simdi uint32x4_t17151716  #define stbir_simdi_castf( reg ) vreinterpretq_u32_f32(reg)1717  #define stbir_simdf_casti( reg ) vreinterpretq_f32_u32(reg)17181719  #define stbir__simdf_load( reg, ptr ) (reg) = vld1q_f32( (float const*)(ptr) )1720  #define stbir__simdi_load( reg, ptr ) (reg) = vld1q_u32( (uint32_t const*)(ptr) )1721  #define stbir__simdf_load1( out, ptr ) (out) = vld1q_dup_f32( (float const*)(ptr) ) // top values can be random (not denormal or nan for perf)1722  #define stbir__simdi_load1( out, ptr ) (out) = vld1q_dup_u32( (uint32_t const*)(ptr) )1723  #define stbir__simdf_load1z( out, ptr ) (out) = vld1q_lane_f32( (float const*)(ptr), vdupq_n_f32(0), 0 ) // top values must be zero1724  #define stbir__simdf_frep4( fvar ) vdupq_n_f32( fvar )1725  #define stbir__simdf_load1frep4( out, fvar ) (out) = vdupq_n_f32( fvar )1726  #define stbir__simdf_load2( out, ptr ) (out) = vcombine_f32( vld1_f32( (float const*)(ptr) ), vcreate_f32(0) ) // top values can be random (not denormal or nan for perf)1727  #define stbir__simdf_load2z( out, ptr ) (out) = vcombine_f32( vld1_f32( (float const*)(ptr) ), vcreate_f32(0) )  // top values must be zero1728  #define stbir__simdf_load2hmerge( out, reg, ptr ) (out) = vcombine_f32( vget_low_f32(reg), vld1_f32( (float const*)(ptr) ) )17291730  #define stbir__simdf_zeroP() vdupq_n_f32(0)1731  #define stbir__simdf_zero( reg ) (reg) = vdupq_n_f32(0)17321733  #define stbir__simdf_store( ptr, reg )  vst1q_f32( (float*)(ptr), reg )1734  #define stbir__simdf_store1( ptr, reg ) vst1q_lane_f32( (float*)(ptr), reg, 0)1735  #define stbir__simdf_store2( ptr, reg ) vst1_f32( (float*)(ptr), vget_low_f32(reg) )1736  #define stbir__simdf_store2h( ptr, reg ) vst1_f32( (float*)(ptr), vget_high_f32(reg) )17371738  #define stbir__simdi_store( ptr, reg )  vst1q_u32( (uint32_t*)(ptr), reg )1739  #define stbir__simdi_store1( ptr, reg ) vst1q_lane_u32( (uint32_t*)(ptr), reg, 0 )1740  #define stbir__simdi_store2( ptr, reg ) vst1_u32( (uint32_t*)(ptr), vget_low_u32(reg) )17411742  #define stbir__prefetch( ptr )17431744  #define stbir__simdi_expand_u8_to_u32(out0,out1,out2,out3,ireg) \1745  { \1746    uint16x8_t l = vmovl_u8( vget_low_u8 ( vreinterpretq_u8_u32(ireg) ) ); \1747    uint16x8_t h = vmovl_u8( vget_high_u8( vreinterpretq_u8_u32(ireg) ) ); \1748    out0 = vmovl_u16( vget_low_u16 ( l ) ); \1749    out1 = vmovl_u16( vget_high_u16( l ) ); \1750    out2 = vmovl_u16( vget_low_u16 ( h ) ); \1751    out3 = vmovl_u16( vget_high_u16( h ) ); \1752  }17531754  #define stbir__simdi_expand_u8_to_1u32(out,ireg) \1755  { \1756    uint16x8_t tmp = vmovl_u8( vget_low_u8( vreinterpretq_u8_u32(ireg) ) ); \1757    out = vmovl_u16( vget_low_u16( tmp ) ); \1758  }17591760  #define stbir__simdi_expand_u16_to_u32(out0,out1,ireg) \1761  { \1762    uint16x8_t tmp = vreinterpretq_u16_u32(ireg); \1763    out0 = vmovl_u16( vget_low_u16 ( tmp ) ); \1764    out1 = vmovl_u16( vget_high_u16( tmp ) ); \1765  }17661767  #define stbir__simdf_convert_float_to_i32( i, f ) (i) = vreinterpretq_u32_s32( vcvtq_s32_f32(f) )1768  #define stbir__simdf_convert_float_to_int( f ) vgetq_lane_s32(vcvtq_s32_f32(f), 0)1769  #define stbir__simdi_to_int( i ) (int)vgetq_lane_u32(i, 0)1770  #define stbir__simdf_convert_float_to_uint8( f ) ((unsigned char)vgetq_lane_s32(vcvtq_s32_f32(vmaxq_f32(vminq_f32(f,STBIR__CONSTF(STBIR_max_uint8_as_float)),vdupq_n_f32(0))), 0))1771  #define stbir__simdf_convert_float_to_short( f ) ((unsigned short)vgetq_lane_s32(vcvtq_s32_f32(vmaxq_f32(vminq_f32(f,STBIR__CONSTF(STBIR_max_uint16_as_float)),vdupq_n_f32(0))), 0))1772  #define stbir__simdi_convert_i32_to_float(out, ireg) (out) = vcvtq_f32_s32( vreinterpretq_s32_u32(ireg) )1773  #define stbir__simdf_add( out, reg0, reg1 ) (out) = vaddq_f32( reg0, reg1 )1774  #define stbir__simdf_mult( out, reg0, reg1 ) (out) = vmulq_f32( reg0, reg1 )1775  #define stbir__simdf_mult_mem( out, reg, ptr ) (out) = vmulq_f32( reg, vld1q_f32( (float const*)(ptr) ) )1776  #define stbir__simdf_mult1_mem( out, reg, ptr ) (out) = vmulq_f32( reg, vld1q_dup_f32( (float const*)(ptr) ) )1777  #define stbir__simdf_add_mem( out, reg, ptr ) (out) = vaddq_f32( reg, vld1q_f32( (float const*)(ptr) ) )1778  #define stbir__simdf_add1_mem( out, reg, ptr ) (out) = vaddq_f32( reg, vld1q_dup_f32( (float const*)(ptr) ) )17791780  #ifdef STBIR_USE_FMA           // not on by default to maintain bit identical simd to non-simd (and also x64 no madd to arm madd)1781  #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = vfmaq_f32( add, mul1, mul2 )1782  #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = vfmaq_f32( add, mul1, mul2 )1783  #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = vfmaq_f32( add, mul, vld1q_f32( (float const*)(ptr) ) )1784  #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = vfmaq_f32( add, mul, vld1q_dup_f32( (float const*)(ptr) ) )1785  #else1786  #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = vaddq_f32( add, vmulq_f32( mul1, mul2 ) )1787  #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = vaddq_f32( add, vmulq_f32( mul1, mul2 ) )1788  #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = vaddq_f32( add, vmulq_f32( mul, vld1q_f32( (float const*)(ptr) ) ) )1789  #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = vaddq_f32( add, vmulq_f32( mul, vld1q_dup_f32( (float const*)(ptr) ) ) )1790  #endif17911792  #define stbir__simdf_add1( out, reg0, reg1 ) (out) = vaddq_f32( reg0, reg1 )1793  #define stbir__simdf_mult1( out, reg0, reg1 ) (out) = vmulq_f32( reg0, reg1 )17941795  #define stbir__simdf_and( out, reg0, reg1 ) (out) = vreinterpretq_f32_u32( vandq_u32( vreinterpretq_u32_f32(reg0), vreinterpretq_u32_f32(reg1) ) )1796  #define stbir__simdf_or( out, reg0, reg1 ) (out) = vreinterpretq_f32_u32( vorrq_u32( vreinterpretq_u32_f32(reg0), vreinterpretq_u32_f32(reg1) ) )17971798  #define stbir__simdf_min( out, reg0, reg1 ) (out) = vminq_f32( reg0, reg1 )1799  #define stbir__simdf_max( out, reg0, reg1 ) (out) = vmaxq_f32( reg0, reg1 )1800  #define stbir__simdf_min1( out, reg0, reg1 ) (out) = vminq_f32( reg0, reg1 )1801  #define stbir__simdf_max1( out, reg0, reg1 ) (out) = vmaxq_f32( reg0, reg1 )18021803  #define stbir__simdf_0123ABCDto3ABx( out, reg0, reg1 ) (out) = vextq_f32( reg0, reg1, 3 )1804  #define stbir__simdf_0123ABCDto23Ax( out, reg0, reg1 ) (out) = vextq_f32( reg0, reg1, 2 )18051806  #define stbir__simdf_a1a1( out, alp, ones ) (out) = vzipq_f32(vuzpq_f32(alp, alp).val[1], ones).val[0]1807  #define stbir__simdf_1a1a( out, alp, ones ) (out) = vzipq_f32(ones, vuzpq_f32(alp, alp).val[0]).val[0]18081809  #if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ )18101811    #define stbir__simdf_aaa1( out, alp, ones ) (out) = vcopyq_laneq_f32(vdupq_n_f32(vgetq_lane_f32(alp, 3)), 3, ones, 3)1812    #define stbir__simdf_1aaa( out, alp, ones ) (out) = vcopyq_laneq_f32(vdupq_n_f32(vgetq_lane_f32(alp, 0)), 0, ones, 0)18131814    #if defined( _MSC_VER ) && !defined(__clang__)1815      #define stbir_make16(a,b,c,d) vcombine_u8( \1816        vcreate_u8( (4*a+0) | ((4*a+1)<<8) | ((4*a+2)<<16) | ((4*a+3)<<24) | \1817          ((stbir_uint64)(4*b+0)<<32) | ((stbir_uint64)(4*b+1)<<40) | ((stbir_uint64)(4*b+2)<<48) | ((stbir_uint64)(4*b+3)<<56)), \1818        vcreate_u8( (4*c+0) | ((4*c+1)<<8) | ((4*c+2)<<16) | ((4*c+3)<<24) | \1819          ((stbir_uint64)(4*d+0)<<32) | ((stbir_uint64)(4*d+1)<<40) | ((stbir_uint64)(4*d+2)<<48) | ((stbir_uint64)(4*d+3)<<56) ) )18201821      static stbir__inline uint8x16x2_t stbir_make16x2(float32x4_t rega,float32x4_t regb)1822      {1823        uint8x16x2_t r = { vreinterpretq_u8_f32(rega), vreinterpretq_u8_f32(regb) };1824        return r;1825      }1826    #else1827      #define stbir_make16(a,b,c,d) (uint8x16_t){4*a+0,4*a+1,4*a+2,4*a+3,4*b+0,4*b+1,4*b+2,4*b+3,4*c+0,4*c+1,4*c+2,4*c+3,4*d+0,4*d+1,4*d+2,4*d+3}1828      #define stbir_make16x2(a,b) (uint8x16x2_t){{vreinterpretq_u8_f32(a),vreinterpretq_u8_f32(b)}}1829    #endif18301831    #define stbir__simdf_swiz( reg, one, two, three, four ) vreinterpretq_f32_u8( vqtbl1q_u8( vreinterpretq_u8_f32(reg), stbir_make16(one, two, three, four) ) )1832    #define stbir__simdf_swiz2( rega, regb, one, two, three, four ) vreinterpretq_f32_u8( vqtbl2q_u8( stbir_make16x2(rega,regb), stbir_make16(one, two, three, four) ) )18331834    #define stbir__simdi_16madd( out, reg0, reg1 ) \1835    { \1836      int16x8_t r0 = vreinterpretq_s16_u32(reg0); \1837      int16x8_t r1 = vreinterpretq_s16_u32(reg1); \1838      int32x4_t tmp0 = vmull_s16( vget_low_s16(r0), vget_low_s16(r1) ); \1839      int32x4_t tmp1 = vmull_s16( vget_high_s16(r0), vget_high_s16(r1) ); \1840      (out) = vreinterpretq_u32_s32( vpaddq_s32(tmp0, tmp1) ); \1841    }18421843  #else18441845    #define stbir__simdf_aaa1( out, alp, ones ) (out) = vsetq_lane_f32(1.0f, vdupq_n_f32(vgetq_lane_f32(alp, 3)), 3)1846    #define stbir__simdf_1aaa( out, alp, ones ) (out) = vsetq_lane_f32(1.0f, vdupq_n_f32(vgetq_lane_f32(alp, 0)), 0)18471848    #if defined( _MSC_VER ) && !defined(__clang__)1849      static stbir__inline uint8x8x2_t stbir_make8x2(float32x4_t reg)1850      {1851        uint8x8x2_t r = { { vget_low_u8(vreinterpretq_u8_f32(reg)), vget_high_u8(vreinterpretq_u8_f32(reg)) } };1852        return r;1853      }1854      #define stbir_make8(a,b) vcreate_u8( \1855        (4*a+0) | ((4*a+1)<<8) | ((4*a+2)<<16) | ((4*a+3)<<24) | \1856        ((stbir_uint64)(4*b+0)<<32) | ((stbir_uint64)(4*b+1)<<40) | ((stbir_uint64)(4*b+2)<<48) | ((stbir_uint64)(4*b+3)<<56) )1857    #else1858      #define stbir_make8x2(reg) (uint8x8x2_t){ { vget_low_u8(vreinterpretq_u8_f32(reg)), vget_high_u8(vreinterpretq_u8_f32(reg)) } }1859      #define stbir_make8(a,b) (uint8x8_t){4*a+0,4*a+1,4*a+2,4*a+3,4*b+0,4*b+1,4*b+2,4*b+3}1860    #endif18611862    #define stbir__simdf_swiz( reg, one, two, three, four ) vreinterpretq_f32_u8( vcombine_u8( \1863        vtbl2_u8( stbir_make8x2( reg ), stbir_make8( one, two ) ), \1864        vtbl2_u8( stbir_make8x2( reg ), stbir_make8( three, four ) ) ) )18651866    #define stbir__simdi_16madd( out, reg0, reg1 ) \1867    { \1868      int16x8_t r0 = vreinterpretq_s16_u32(reg0); \1869      int16x8_t r1 = vreinterpretq_s16_u32(reg1); \1870      int32x4_t tmp0 = vmull_s16( vget_low_s16(r0), vget_low_s16(r1) ); \1871      int32x4_t tmp1 = vmull_s16( vget_high_s16(r0), vget_high_s16(r1) ); \1872      int32x2_t out0 = vpadd_s32( vget_low_s32(tmp0), vget_high_s32(tmp0) ); \1873      int32x2_t out1 = vpadd_s32( vget_low_s32(tmp1), vget_high_s32(tmp1) ); \1874      (out) = vreinterpretq_u32_s32( vcombine_s32(out0, out1) ); \1875    }18761877  #endif18781879  #define stbir__simdi_and( out, reg0, reg1 ) (out) = vandq_u32( reg0, reg1 )1880  #define stbir__simdi_or( out, reg0, reg1 ) (out) = vorrq_u32( reg0, reg1 )18811882  #define stbir__simdf_pack_to_8bytes(out,aa,bb) \1883  { \1884    float32x4_t af = vmaxq_f32( vminq_f32(aa,STBIR__CONSTF(STBIR_max_uint8_as_float) ), vdupq_n_f32(0) ); \1885    float32x4_t bf = vmaxq_f32( vminq_f32(bb,STBIR__CONSTF(STBIR_max_uint8_as_float) ), vdupq_n_f32(0) ); \1886    int16x4_t ai = vqmovn_s32( vcvtq_s32_f32( af ) ); \1887    int16x4_t bi = vqmovn_s32( vcvtq_s32_f32( bf ) ); \1888    uint8x8_t out8 = vqmovun_s16( vcombine_s16(ai, bi) ); \1889    out = vreinterpretq_u32_u8( vcombine_u8(out8, out8) ); \1890  }18911892  #define stbir__simdf_pack_to_8words(out,aa,bb) \1893  { \1894    float32x4_t af = vmaxq_f32( vminq_f32(aa,STBIR__CONSTF(STBIR_max_uint16_as_float) ), vdupq_n_f32(0) ); \1895    float32x4_t bf = vmaxq_f32( vminq_f32(bb,STBIR__CONSTF(STBIR_max_uint16_as_float) ), vdupq_n_f32(0) ); \1896    int32x4_t ai = vcvtq_s32_f32( af ); \1897    int32x4_t bi = vcvtq_s32_f32( bf ); \1898    out = vreinterpretq_u32_u16( vcombine_u16(vqmovun_s32(ai), vqmovun_s32(bi)) ); \1899  }19001901  #define stbir__interleave_pack_and_store_16_u8( ptr, r0, r1, r2, r3 ) \1902  { \1903    int16x4x2_t tmp0 = vzip_s16( vqmovn_s32(vreinterpretq_s32_u32(r0)), vqmovn_s32(vreinterpretq_s32_u32(r2)) ); \1904    int16x4x2_t tmp1 = vzip_s16( vqmovn_s32(vreinterpretq_s32_u32(r1)), vqmovn_s32(vreinterpretq_s32_u32(r3)) ); \1905    uint8x8x2_t out = \1906    { { \1907      vqmovun_s16( vcombine_s16(tmp0.val[0], tmp0.val[1]) ), \1908      vqmovun_s16( vcombine_s16(tmp1.val[0], tmp1.val[1]) ), \1909    } }; \1910    vst2_u8(ptr, out); \1911  }19121913  #define stbir__simdf_load4_transposed( o0, o1, o2, o3, ptr ) \1914  { \1915    float32x4x4_t tmp = vld4q_f32(ptr); \1916    o0 = tmp.val[0]; \1917    o1 = tmp.val[1]; \1918    o2 = tmp.val[2]; \1919    o3 = tmp.val[3]; \1920  }19211922  #define stbir__simdi_32shr( out, reg, imm ) out = vshrq_n_u32( reg, imm )19231924  #if defined( _MSC_VER ) && !defined(__clang__)1925    #define STBIR__SIMDF_CONST(var, x) __declspec(align(8)) float var[] = { x, x, x, x }1926    #define STBIR__SIMDI_CONST(var, x) __declspec(align(8)) uint32_t var[] = { x, x, x, x }1927    #define STBIR__CONSTF(var) (*(const float32x4_t*)var)1928    #define STBIR__CONSTI(var) (*(const uint32x4_t*)var)1929  #else1930    #define STBIR__SIMDF_CONST(var, x) stbir__simdf var = { x, x, x, x }1931    #define STBIR__SIMDI_CONST(var, x) stbir__simdi var = { x, x, x, x }1932    #define STBIR__CONSTF(var) (var)1933    #define STBIR__CONSTI(var) (var)1934  #endif19351936  #ifdef STBIR_FLOORF1937  #undef STBIR_FLOORF1938  #endif1939  #define STBIR_FLOORF stbir_simd_floorf1940  static stbir__inline float stbir_simd_floorf(float x)1941  {1942    #if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ )1943    return vget_lane_f32( vrndm_f32( vdup_n_f32(x) ), 0);1944    #else1945    float32x2_t f = vdup_n_f32(x);1946    float32x2_t t = vcvt_f32_s32(vcvt_s32_f32(f));1947    uint32x2_t a = vclt_f32(f, t);1948    uint32x2_t b = vreinterpret_u32_f32(vdup_n_f32(-1.0f));1949    float32x2_t r = vadd_f32(t, vreinterpret_f32_u32(vand_u32(a, b)));1950    return vget_lane_f32(r, 0);1951    #endif1952  }19531954  #ifdef STBIR_CEILF1955  #undef STBIR_CEILF1956  #endif1957  #define STBIR_CEILF stbir_simd_ceilf1958  static stbir__inline float stbir_simd_ceilf(float x)1959  {1960    #if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ )1961    return vget_lane_f32( vrndp_f32( vdup_n_f32(x) ), 0);1962    #else1963    float32x2_t f = vdup_n_f32(x);1964    float32x2_t t = vcvt_f32_s32(vcvt_s32_f32(f));1965    uint32x2_t a = vclt_f32(t, f);1966    uint32x2_t b = vreinterpret_u32_f32(vdup_n_f32(1.0f));1967    float32x2_t r = vadd_f32(t, vreinterpret_f32_u32(vand_u32(a, b)));1968    return vget_lane_f32(r, 0);1969    #endif1970  }19711972  #define STBIR_SIMD19731974#elif defined(STBIR_WASM)19751976  #include <wasm_simd128.h>19771978  #define stbir__simdf v128_t1979  #define stbir__simdi v128_t19801981  #define stbir_simdi_castf( reg ) (reg)1982  #define stbir_simdf_casti( reg ) (reg)19831984  #define stbir__simdf_load( reg, ptr )             (reg) = wasm_v128_load( (void const*)(ptr) )1985  #define stbir__simdi_load( reg, ptr )             (reg) = wasm_v128_load( (void const*)(ptr) )1986  #define stbir__simdf_load1( out, ptr )            (out) = wasm_v128_load32_splat( (void const*)(ptr) ) // top values can be random (not denormal or nan for perf)1987  #define stbir__simdi_load1( out, ptr )            (out) = wasm_v128_load32_splat( (void const*)(ptr) )1988  #define stbir__simdf_load1z( out, ptr )           (out) = wasm_v128_load32_zero( (void const*)(ptr) ) // top values must be zero1989  #define stbir__simdf_frep4( fvar )                wasm_f32x4_splat( fvar )1990  #define stbir__simdf_load1frep4( out, fvar )      (out) = wasm_f32x4_splat( fvar )1991  #define stbir__simdf_load2( out, ptr )            (out) = wasm_v128_load64_splat( (void const*)(ptr) ) // top values can be random (not denormal or nan for perf)1992  #define stbir__simdf_load2z( out, ptr )           (out) = wasm_v128_load64_zero( (void const*)(ptr) ) // top values must be zero1993  #define stbir__simdf_load2hmerge( out, reg, ptr ) (out) = wasm_v128_load64_lane( (void const*)(ptr), reg, 1 )19941995  #define stbir__simdf_zeroP() wasm_f32x4_const_splat(0)1996  #define stbir__simdf_zero( reg ) (reg) = wasm_f32x4_const_splat(0)19971998  #define stbir__simdf_store( ptr, reg )   wasm_v128_store( (void*)(ptr), reg )1999  #define stbir__simdf_store1( ptr, reg )  wasm_v128_store32_lane( (void*)(ptr), reg, 0 )2000  #define stbir__simdf_store2( ptr, reg )  wasm_v128_store64_lane( (void*)(ptr), reg, 0 )

The first 2,000 of 10,679 lines.