1563a794dSFrançois Tigeot /*- 2563a794dSFrançois Tigeot * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved. 34b77aaafSFrançois Tigeot * Copyright (c) 2014-2015 Mellanox Technologies, Ltd. All rights reserved. 4*f77dbd6cSFrançois Tigeot * Copyright (c) 2016-2017 François Tigeot 5563a794dSFrançois Tigeot * All rights reserved. 6563a794dSFrançois Tigeot * 7563a794dSFrançois Tigeot * Redistribution and use in source and binary forms, with or without 8563a794dSFrançois Tigeot * modification, are permitted provided that the following conditions 9563a794dSFrançois Tigeot * are met: 10563a794dSFrançois Tigeot * 1. Redistributions of source code must retain the above copyright 11563a794dSFrançois Tigeot * notice unmodified, this list of conditions, and the following 12563a794dSFrançois Tigeot * disclaimer. 13563a794dSFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright 14563a794dSFrançois Tigeot * notice, this list of conditions and the following disclaimer in the 15563a794dSFrançois Tigeot * documentation and/or other materials provided with the distribution. 16563a794dSFrançois Tigeot * 17563a794dSFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18563a794dSFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19563a794dSFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20563a794dSFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21563a794dSFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22563a794dSFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23563a794dSFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24563a794dSFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25563a794dSFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26563a794dSFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 274b77aaafSFrançois Tigeot * 284b77aaafSFrançois Tigeot * $FreeBSD: head/sys/compat/linuxkpi/common/include/linux/math64.h 290135 2015-10-29 08:28:39Z hselasky $ 29563a794dSFrançois Tigeot */ 30fa3094dbSzrj 31563a794dSFrançois Tigeot #ifndef _LINUX_MATH64_H 32563a794dSFrançois Tigeot #define _LINUX_MATH64_H 33563a794dSFrançois Tigeot 34563a794dSFrançois Tigeot #include <linux/types.h> 35563a794dSFrançois Tigeot 36563a794dSFrançois Tigeot #define do_div(n, base) ({ \ 37563a794dSFrançois Tigeot uint32_t __base = (base); \ 38563a794dSFrançois Tigeot uint32_t __rem; \ 39563a794dSFrançois Tigeot __rem = ((uint64_t)(n)) % __base; \ 40563a794dSFrançois Tigeot (n) = ((uint64_t)(n)) / __base; \ 41563a794dSFrançois Tigeot __rem; \ 42563a794dSFrançois Tigeot }) 43563a794dSFrançois Tigeot 444b77aaafSFrançois Tigeot static inline uint64_t 454b77aaafSFrançois Tigeot div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder) 46563a794dSFrançois Tigeot { 47563a794dSFrançois Tigeot *remainder = dividend % divisor; 484b77aaafSFrançois Tigeot return (dividend / divisor); 49563a794dSFrançois Tigeot } 50563a794dSFrançois Tigeot 514b77aaafSFrançois Tigeot static inline uint64_t 524b77aaafSFrançois Tigeot div64_u64(uint64_t dividend, uint64_t divisor) 5357e252bfSMichael Neumann { 544b77aaafSFrançois Tigeot return (dividend / divisor); 5557e252bfSMichael Neumann } 5657e252bfSMichael Neumann 574b77aaafSFrançois Tigeot static inline int64_t 584b77aaafSFrançois Tigeot div64_s64(int64_t dividend, int64_t divisor) 5957e252bfSMichael Neumann { 604b77aaafSFrançois Tigeot return (dividend / divisor); 6157e252bfSMichael Neumann } 62563a794dSFrançois Tigeot 634b77aaafSFrançois Tigeot static inline uint64_t 644b77aaafSFrançois Tigeot div_u64(uint64_t dividend, uint32_t divisor) 65563a794dSFrançois Tigeot { 664b77aaafSFrançois Tigeot return (dividend / divisor); 67563a794dSFrançois Tigeot } 68563a794dSFrançois Tigeot 690a8d41c2SFrançois Tigeot static inline int64_t 700a8d41c2SFrançois Tigeot div_s64(int64_t dividend, int32_t divisor) 710a8d41c2SFrançois Tigeot { 720a8d41c2SFrançois Tigeot return (dividend / divisor); 730a8d41c2SFrançois Tigeot } 740a8d41c2SFrançois Tigeot 75*f77dbd6cSFrançois Tigeot static inline u64 76*f77dbd6cSFrançois Tigeot div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder) 77fa3094dbSzrj { 78fa3094dbSzrj *remainder = dividend % divisor; 79fa3094dbSzrj return (dividend / divisor); 80fa3094dbSzrj } 810a8d41c2SFrançois Tigeot 82563a794dSFrançois Tigeot #endif /* _LINUX_MATH64_H */ 83