/mordor/util.cpp
C++ | 59 lines | 26 code | 5 blank | 28 comment | 1 complexity | bb1c3c8ebb7ee9339a9aae9d45445bae MD5 | raw file
Possible License(s): BSD-3-Clause
- #include "util.h"
- #include "endian.h"
- namespace Mordor
- {
- // muldiv64() was adapted from
- // http://www.virtualbox.org/browser/trunk/src/VBox/Devices/muldiv64.c?rev=1
- //
- // the following copyright notice applies:
- /*
- * QEMU System Emulator
- *
- * Copyright (c) 2003-2004 Fabrice Bellard
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- /* compute with 96 bit intermediate result: (a*b)/c */
- uint64_t muldiv64(uint64_t a, uint32_t b, uint64_t c)
- {
- union {
- uint64_t ll;
- struct {
- #if MORDOR_BYTE_ORDER == MORDOR_BIG_ENDIAN
- uint32_t high, low;
- #else
- uint32_t low, high;
- #endif
- } l;
- } u, res;
- uint64_t rl, rh;
- u.ll = a;
- rl = (uint64_t)u.l.low * (uint64_t)b;
- rh = (uint64_t)u.l.high * (uint64_t)b;
- rh += (rl >> 32);
- res.l.high = (uint32_t)(rh / c);
- res.l.low = (uint32_t)((((rh % c) << 32) + (rl & 0xffffffff)) / c);
- return res.ll;
- }
- };