stb_divide.h
This file
Path
stb_divide.h
421 lines · 13.8 KiB · C
Last changed by
update version numbersSean Barrettbefore adoptionc963e40
History of this fileTake it
The walk stopped after 200 commits: lines older than 8879908 point at it.
update version numbersSean Barrettbefore adoptionc963e40
line 1why, one step back1// stb_divide.h - v0.92 - public domain - Sean Barrett, Feb 2010update readmeSean Barrettbefore adoption8879908
lines 2–5why, one step back2// Three kinds of divide/modulus of signed integers.3//4// HISTORY5//update version numbersSean Barrettbefore adoptionc963e40
line 6why, one step back6// v0.92 2019-02-25 Fix warningupdate readmeSean Barrettbefore adoption8879908
lines 7–166why, one step back7// v0.91 2010-02-27 Fix euclidean division by INT_MIN for non-truncating C8// Check result with 64-bit math to catch such cases9// v0.90 2010-02-24 First public release10//11// USAGE12//13// In *ONE* source file, put:14//15// #define STB_DIVIDE_IMPLEMENTATION16// // #define C_INTEGER_DIVISION_TRUNCATES // see Note 117// // #define C_INTEGER_DIVISION_FLOORS // see Note 218// #include "stb_divide.h"19//20// Other source files should just include stb_divide.h21//22// Note 1: On platforms/compilers that you know signed C division23// truncates, you can #define C_INTEGER_DIVISION_TRUNCATES.24//25// Note 2: On platforms/compilers that you know signed C division26// floors (rounds to negative infinity), you can #define27// C_INTEGER_DIVISION_FLOORS.28//29// You can #define STB_DIVIDE_TEST in which case the implementation30// will generate a main() and compiling the result will create a31// program that tests the implementation. Run it with no arguments32// and any output indicates an error; run it with any argument and33// it will also print the test results. Define STB_DIVIDE_TEST_6434// to a 64-bit integer type to avoid overflows in the result-checking35// which give false negatives.36//37// ABOUT38//39// This file provides three different consistent divide/mod pairs40// implemented on top of arbitrary C/C++ division, including correct41// handling of overflow of intermediate calculations:42//43// trunc: a/b truncates to 0, a%b has same sign as a44// floor: a/b truncates to -inf, a%b has same sign as b45// eucl: a/b truncates to sign(b)*inf, a%b is non-negative46//47// Not necessarily optimal; I tried to keep it generally efficient,48// but there may be better ways.49//50// Briefly, for those who are not familiar with the problem, we note51// the reason these divides exist and are interesting:52//53// 'trunc' is easy to implement in hardware (strip the signs,54// compute, reapply the signs), thus is commonly defined55// by many languages (including C99)56//57// 'floor' is simple to define and better behaved than trunc;58// for example it divides integers into fixed-size buckets59// without an extra-wide bucket at 0, and for a fixed60// divisor N there are only |N| possible moduli.61//62// 'eucl' guarantees fixed-sized buckets *and* a non-negative63// modulus and defines division to be whatever is needed64// to achieve that result.65//66// See "The Euclidean definition of the functions div and mod"67// by Raymond Boute (1992), or "Division and Modulus for Computer68// Scientists" by Daan Leijen (2001)69//70// We assume of the built-in C division:71// (a) modulus is the remainder for the corresponding division72// (b) a/b truncates if a and b are the same sign73//74// Property (a) requires (a/b)*b + (a%b)==a, and is required by C.75// Property (b) seems to be true of all hardware but is *not* satisfied76// by the euclidean division operator we define, so it's possibly not77// always true. If any such platform turns up, we can add more cases.78// (Possibly only stb_div_trunc currently relies on property (b).)79//80// LICENSE81//82// See end of file for license information.838485#ifndef INCLUDE_STB_DIVIDE_H86#define INCLUDE_STB_DIVIDE_H8788#ifdef __cplusplus89extern "C" {90#endif9192extern int stb_div_trunc(int value_to_be_divided, int value_to_divide_by);93extern int stb_div_floor(int value_to_be_divided, int value_to_divide_by);94extern int stb_div_eucl (int value_to_be_divided, int value_to_divide_by);95extern int stb_mod_trunc(int value_to_be_divided, int value_to_divide_by);96extern int stb_mod_floor(int value_to_be_divided, int value_to_divide_by);97extern int stb_mod_eucl (int value_to_be_divided, int value_to_divide_by);9899#ifdef __cplusplus100}101#endif102103#ifdef STB_DIVIDE_IMPLEMENTATION104105#if defined(__STDC_VERSION) && __STDC_VERSION__ >= 19901106 #ifndef C_INTEGER_DIVISION_TRUNCATES107 #define C_INTEGER_DIVISION_TRUNCATES108 #endif109#endif110111#ifndef INT_MIN112#include <limits.h> // if you have no limits.h, #define INT_MIN yourself113#endif114115// the following macros are designed to allow testing116// other platforms by simulating them117#ifndef STB_DIVIDE_TEST_FLOOR118 #define stb__div(a,b) ((a)/(b))119 #define stb__mod(a,b) ((a)%(b))120#else121 // implement floor-style divide on trunc platform122 #ifndef C_INTEGER_DIVISION_TRUNCATES123 #error "floor test requires truncating division"124 #endif125 #undef C_INTEGER_DIVISION_TRUNCATES126 int stb__div(int v1, int v2)127 {128 int q = v1/v2, r = v1%v2;129 if ((r > 0 && v2 < 0) || (r < 0 && v2 > 0))130 return q-1;131 else132 return q;133 }134135 int stb__mod(int v1, int v2)136 {137 int r = v1%v2;138 if ((r > 0 && v2 < 0) || (r < 0 && v2 > 0))139 return r+v2;140 else141 return r;142 }143#endif144145int stb_div_trunc(int v1, int v2)146{147 #ifdef C_INTEGER_DIVISION_TRUNCATES148 return v1/v2;149 #else150 if (v1 >= 0 && v2 <= 0)151 return -stb__div(-v1,v2); // both negative to avoid overflow152 if (v1 <= 0 && v2 >= 0)153 if (v1 != INT_MIN)154 return -stb__div(v1,-v2); // both negative to avoid overflow155 else156 return -stb__div(v1+v2,-v2)-1; // push v1 away from wrap point157 else158 return v1/v2; // same sign, so expect truncation159 #endif160}161162int stb_div_floor(int v1, int v2)163{164 #ifdef C_INTEGER_DIVISION_FLOORS165 return v1/v2;166 #elsevarious fixes for clangSean Barrettbefore adoption5fe7fb5
line 167why, one step back167 if (v1 >= 0 && v2 < 0) {update readmeSean Barrettbefore adoption8879908
lines 168–171why, one step back168 if ((-v1)+v2+1 < 0) // check if increasing v1's magnitude overflows169 return -stb__div(-v1+v2+1,v2); // nope, so just compute it170 else171 return -stb__div(-v1,v2) + ((-v1)%v2 ? -1 : 0);various fixes for clangSean Barrettbefore adoption5fe7fb5
lines 172–174why, one step back172 }173 if (v1 < 0 && v2 >= 0) {174 if (v1 != INT_MIN) {update readmeSean Barrettbefore adoption8879908
lines 175–178why, one step back175 if (v1-v2+1 < 0) // check if increasing v1's magnitude overflows176 return -stb__div(v1-v2+1,-v2); // nope, so just compute it177 else178 return -stb__div(-v1,v2) + (stb__mod(v1,-v2) ? -1 : 0);various fixes for clangSean Barrettbefore adoption5fe7fb5
line 179why, one step back179 } else // it must be possible to compute -(v1+v2) without overflowingupdate readmeSean Barrettbefore adoption8879908
line 180why, one step back180 return -stb__div(-(v1+v2),v2) + (stb__mod(-(v1+v2),v2) ? -2 : -1);various fixes for clangSean Barrettbefore adoption5fe7fb5
line 181why, one step back181 } elseupdate readmeSean Barrettbefore adoption8879908
lines 182–421why, one step back182 return v1/v2; // same sign, so expect truncation183 #endif184}185186int stb_div_eucl(int v1, int v2)187{188 int q,r;189 #ifdef C_INTEGER_DIVISION_TRUNCATES190 q = v1/v2;191 r = v1%v2;192 #else193 // handle every quadrant separately, since we can't rely on q and r flor194 if (v1 >= 0)195 if (v2 >= 0)196 return stb__div(v1,v2);197 else if (v2 != INT_MIN)198 q = -stb__div(v1,-v2), r = stb__mod(v1,-v2);199 else200 q = 0, r = v1;201 else if (v1 != INT_MIN)202 if (v2 >= 0)203 q = -stb__div(-v1,v2), r = -stb__mod(-v1,v2);204 else if (v2 != INT_MIN)205 q = stb__div(-v1,-v2), r = -stb__mod(-v1,-v2);206 else // if v2 is INT_MIN, then we can't use -v2, but we can't divide by v2207 q = 1, r = v1-q*v2;208 else // if v1 is INT_MIN, we have to move away from overflow place209 if (v2 >= 0)210 q = -stb__div(-(v1+v2),v2)-1, r = -stb__mod(-(v1+v2),v2);211 else212 q = stb__div(-(v1-v2),-v2)+1, r = -stb__mod(-(v1-v2),-v2);213 #endif214 if (r >= 0)215 return q;216 else217 return q + (v2 > 0 ? -1 : 1);218}219220int stb_mod_trunc(int v1, int v2)221{222 #ifdef C_INTEGER_DIVISION_TRUNCATES223 return v1%v2;224 #else225 if (v1 >= 0) { // modulus result should always be positive226 int r = stb__mod(v1,v2);227 if (r >= 0)228 return r;229 else230 return r + (v2 > 0 ? v2 : -v2);231 } else { // modulus result should always be negative232 int r = stb__mod(v1,v2);233 if (r <= 0)234 return r;235 else236 return r - (v2 > 0 ? v2 : -v2);237 }238 #endif239}240241int stb_mod_floor(int v1, int v2)242{243 #ifdef C_INTEGER_DIVISION_FLOORS244 return v1%v2;245 #else246 if (v2 >= 0) { // result should always be positive247 int r = stb__mod(v1,v2);248 if (r >= 0)249 return r;250 else251 return r + v2;252 } else { // result should always be negative253 int r = stb__mod(v1,v2);254 if (r <= 0)255 return r;256 else257 return r + v2;258 }259 #endif260}261262int stb_mod_eucl(int v1, int v2)263{264 int r = stb__mod(v1,v2);265266 if (r >= 0)267 return r;268 else269 return r + (v2 > 0 ? v2 : -v2); // abs()270}271272#ifdef STB_DIVIDE_TEST273#include <stdio.h>274#include <math.h>275#include <limits.h>276277int show=0;278279void stbdiv_check(int q, int r, int a, int b, char *type, int dir)280{281 if ((dir > 0 && r < 0) || (dir < 0 && r > 0))282 fprintf(stderr, "FAILED: %s(%d,%d) remainder %d in wrong direction\n", type,a,b,r);283 else284 if (b != INT_MIN) // can't compute abs(), but if b==INT_MIN all remainders are valid285 if (r <= -abs(b) || r >= abs(b))286 fprintf(stderr, "FAILED: %s(%d,%d) remainder %d out of range\n", type,a,b,r);287 #ifdef STB_DIVIDE_TEST_64288 {289 STB_DIVIDE_TEST_64 q64 = q, r64=r, a64=a, b64=b;290 if (q64*b64+r64 != a64)291 fprintf(stderr, "FAILED: %s(%d,%d) remainder %d doesn't match quotient %d\n", type,a,b,r,q);292 }293 #else294 if (q*b+r != a)295 fprintf(stderr, "FAILED: %s(%d,%d) remainder %d doesn't match quotient %d\n", type,a,b,r,q);296 #endif297}298299void test(int a, int b)300{301 int q,r;302 if (show) printf("(%+11d,%+d) | ", a,b);303 q = stb_div_trunc(a,b), r = stb_mod_trunc(a,b);304 if (show) printf("(%+11d,%+2d) ", q,r); stbdiv_check(q,r,a,b, "trunc",a);305 q = stb_div_floor(a,b), r = stb_mod_floor(a,b); 306 if (show) printf("(%+11d,%+2d) ", q,r); stbdiv_check(q,r,a,b, "floor",b);307 q = stb_div_eucl (a,b), r = stb_mod_eucl (a,b);308 if (show) printf("(%+11d,%+2d)\n", q,r); stbdiv_check(q,r,a,b, "euclidean",1);309}310311void testh(int a, int b)312{313 int q,r;314 if (show) printf("(%08x,%08x) |\n", a,b);315 q = stb_div_trunc(a,b), r = stb_mod_trunc(a,b); stbdiv_check(q,r,a,b, "trunc",a);316 if (show) printf(" (%08x,%08x)", q,r);317 q = stb_div_floor(a,b), r = stb_mod_floor(a,b); stbdiv_check(q,r,a,b, "floor",b);318 if (show) printf(" (%08x,%08x)", q,r);319 q = stb_div_eucl (a,b), r = stb_mod_eucl (a,b); stbdiv_check(q,r,a,b, "euclidean",1);320 if (show) printf(" (%08x,%08x)\n ", q,r);321}322323int main(int argc, char **argv)324{325 if (argc > 1) show=1;326327 test(8,3);328 test(8,-3);329 test(-8,3);330 test(-8,-3);331 test(1,2);332 test(1,-2);333 test(-1,2);334 test(-1,-2);335 test(8,4);336 test(8,-4);337 test(-8,4);338 test(-8,-4);339340 test(INT_MAX,1);341 test(INT_MIN,1);342 test(INT_MIN+1,1);343 test(INT_MAX,-1);344 //test(INT_MIN,-1); // this traps in MSVC, so we leave it untested345 test(INT_MIN+1,-1);346 test(INT_MIN,-2);347 test(INT_MIN+1,2);348 test(INT_MIN+1,-2);349 test(INT_MAX,2);350 test(INT_MAX,-2);351 test(INT_MIN+1,2);352 test(INT_MIN+1,-2);353 test(INT_MIN,2);354 test(INT_MIN,-2);355 test(INT_MIN,7);356 test(INT_MIN,-7);357 test(INT_MIN+1,4);358 test(INT_MIN+1,-4);359360 testh(-7, INT_MIN);361 testh(-1, INT_MIN);362 testh(1, INT_MIN);363 testh(7, INT_MIN);364365 testh(INT_MAX-1, INT_MIN);366 testh(INT_MAX, INT_MIN);367 testh(INT_MIN, INT_MIN);368 testh(INT_MIN+1, INT_MIN);369370 testh(INT_MAX-1, INT_MAX);371 testh(INT_MAX , INT_MAX);372 testh(INT_MIN , INT_MAX);373 testh(INT_MIN+1, INT_MAX);374375 return 0;376}377#endif // STB_DIVIDE_TEST378#endif // STB_DIVIDE_IMPLEMENTATION379#endif // INCLUDE_STB_DIVIDE_H380381/*382------------------------------------------------------------------------------383This software is available under 2 licenses -- choose whichever you prefer.384------------------------------------------------------------------------------385ALTERNATIVE A - MIT License386Copyright (c) 2017 Sean Barrett387Permission is hereby granted, free of charge, to any person obtaining a copy of 388this software and associated documentation files (the "Software"), to deal in 389the Software without restriction, including without limitation the rights to 390use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 391of the Software, and to permit persons to whom the Software is furnished to do 392so, subject to the following conditions:393The above copyright notice and this permission notice shall be included in all 394copies or substantial portions of the Software.395THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 396IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 397FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 398AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 399LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 400OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 401SOFTWARE.402------------------------------------------------------------------------------403ALTERNATIVE B - Public Domain (www.unlicense.org)404This is free and unencumbered software released into the public domain.405Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 406software, either in source code form or as a compiled binary, for any purpose, 407commercial or non-commercial, and by any means.408In jurisdictions that recognize copyright laws, the author or authors of this 409software dedicate any and all copyright interest in the software to the public 410domain. We make this dedication for the benefit of the public at large and to 411the detriment of our heirs and successors. We intend this dedication to be an 412overt act of relinquishment in perpetuity of all present and future rights to 413this software under copyright law.414THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 415IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 416FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 417AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 418ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 419WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.420------------------------------------------------------------------------------421*/