Reactos
at master 40 lines 973 B view raw
1// 2// imaxdiv.cpp 3// 4// Copyright (c) Microsoft Corporation. All rights reserved. 5// 6// Defines imaxdiv(), which performs a signed divide using intmax_t operands 7// and returns the quotient and remainder. No validation of the arguments is 8// done. 9// 10#include <inttypes.h> 11 12 13 14extern "C" imaxdiv_t __cdecl imaxdiv(intmax_t const numerator, intmax_t const denominator) 15{ 16 imaxdiv_t result; 17 18 result.quot = numerator / denominator; 19 result.rem = numerator - denominator * result.quot; 20 21 // Fix incorrect truncation: 22 #pragma warning(push) 23 #pragma warning(disable: 4127) 24 static bool const fix_required = (-1 / 2) < 0; 25 if (fix_required && result.quot < 0 && result.rem != 0) 26 { 27 result.quot += 1; 28 result.rem -= denominator; 29 } 30 #pragma warning(pop) 31 32 return result; 33} 34 35 36 37/* 38 * Copyright (c) 1992-2010 by P.J. Plauger. ALL RIGHTS RESERVED. 39 * Consult your license regarding permissions and restrictions. 40V5.30:0009 */