xref: /openbsd-src/sys/dev/pci/drm/include/linux/math64.h (revision c349dbc7938c71a30e13c1be4acc1976165f4630)
17f4dd379Sjsg /* Public domain. */
27f4dd379Sjsg 
37f4dd379Sjsg #ifndef _LINUX_MATH64_H
47f4dd379Sjsg #define _LINUX_MATH64_H
57f4dd379Sjsg 
67f4dd379Sjsg #include <sys/types.h>
77f4dd379Sjsg #include <asm/div64.h>
87f4dd379Sjsg 
97f4dd379Sjsg static inline uint64_t
div_u64(uint64_t x,uint32_t y)107f4dd379Sjsg div_u64(uint64_t x, uint32_t y)
117f4dd379Sjsg {
127f4dd379Sjsg 	return (x / y);
137f4dd379Sjsg }
147f4dd379Sjsg 
157f4dd379Sjsg static inline int64_t
div_s64(int64_t x,int64_t y)167f4dd379Sjsg div_s64(int64_t x, int64_t y)
177f4dd379Sjsg {
187f4dd379Sjsg 	return (x / y);
197f4dd379Sjsg }
207f4dd379Sjsg 
217f4dd379Sjsg static inline uint64_t
div64_u64(uint64_t x,uint64_t y)227f4dd379Sjsg div64_u64(uint64_t x, uint64_t y)
237f4dd379Sjsg {
247f4dd379Sjsg 	return (x / y);
257f4dd379Sjsg }
267f4dd379Sjsg 
277f4dd379Sjsg static inline uint64_t
div64_u64_rem(uint64_t x,uint64_t y,uint64_t * rem)287f4dd379Sjsg div64_u64_rem(uint64_t x, uint64_t y, uint64_t *rem)
297f4dd379Sjsg {
307f4dd379Sjsg 	*rem = x % y;
317f4dd379Sjsg 	return (x / y);
327f4dd379Sjsg }
337f4dd379Sjsg 
347f4dd379Sjsg static inline uint64_t
div_u64_rem(uint64_t x,uint32_t y,uint32_t * rem)357f4dd379Sjsg div_u64_rem(uint64_t x, uint32_t y, uint32_t *rem)
367f4dd379Sjsg {
377f4dd379Sjsg 	*rem = x % y;
387f4dd379Sjsg 	return (x / y);
397f4dd379Sjsg }
407f4dd379Sjsg 
417f4dd379Sjsg static inline int64_t
div64_s64(int64_t x,int64_t y)427f4dd379Sjsg div64_s64(int64_t x, int64_t y)
437f4dd379Sjsg {
447f4dd379Sjsg 	return (x / y);
457f4dd379Sjsg }
467f4dd379Sjsg 
477f4dd379Sjsg static inline uint64_t
mul_u32_u32(uint32_t x,uint32_t y)487f4dd379Sjsg mul_u32_u32(uint32_t x, uint32_t y)
497f4dd379Sjsg {
507f4dd379Sjsg 	return (uint64_t)x * y;
517f4dd379Sjsg }
527f4dd379Sjsg 
537f4dd379Sjsg static inline uint64_t
mul_u64_u32_div(uint64_t x,uint32_t y,uint32_t div)547f4dd379Sjsg mul_u64_u32_div(uint64_t x, uint32_t y, uint32_t div)
557f4dd379Sjsg {
567f4dd379Sjsg 	return (x * y) / div;
577f4dd379Sjsg }
587f4dd379Sjsg 
59*c349dbc7Sjsg #define DIV64_U64_ROUND_UP(x, y)	\
60*c349dbc7Sjsg ({					\
61*c349dbc7Sjsg 	uint64_t _t = (y);		\
62*c349dbc7Sjsg 	div64_u64((x) + _t - 1, _t);	\
63*c349dbc7Sjsg })
64*c349dbc7Sjsg 
65*c349dbc7Sjsg static inline uint64_t
mul_u64_u32_shr(uint64_t x,uint32_t y,unsigned int shift)66*c349dbc7Sjsg mul_u64_u32_shr(uint64_t x, uint32_t y, unsigned int shift)
67*c349dbc7Sjsg {
68*c349dbc7Sjsg 	uint32_t hi, lo;
69*c349dbc7Sjsg 	hi = x >> 32;
70*c349dbc7Sjsg 	lo = x & 0xffffffff;
71*c349dbc7Sjsg 
72*c349dbc7Sjsg 	return (mul_u32_u32(lo, y) >> shift) +
73*c349dbc7Sjsg 	    (mul_u32_u32(hi, y) << (32 - shift));
74*c349dbc7Sjsg }
75*c349dbc7Sjsg 
767f4dd379Sjsg #endif
77