/* -*- Mode:C; c-basic-offset:4; tab-width:4 -*- **************************************************************************** * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge **************************************************************************** * * File: math.c * Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk) * Changes: * * Date: Aug 2003 * * Environment: Xen Minimal OS * Description: Library functions for 64bit arith and other * from freebsd, files in sys/libkern/ (qdivrem.c, etc) * **************************************************************************** * $Id: c-insert.c,v 1.7 2002/11/08 16:04:34 rn Exp $ **************************************************************************** *- * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and * contributed to Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD: src/sys/libkern/divdi3.c,v 1.6 1999/08/28 00:46:31 peter Exp $ */ #include #include #include /* On ia64 these functions lead to crashes. These are replaced by * assembler functions. */ #if !defined(__ia64__) /* * Depending on the desired operation, we view a `long long' (aka quad_t) in * one or more of the following formats. */ union uu { int64_t q; /* as a (signed) quad */ int64_t uq; /* as an unsigned quad */ long sl[2]; /* as two signed longs */ unsigned long ul[2]; /* as two unsigned longs */ }; /* XXX RN: Yuck hardcoded endianess :) */ #define _QUAD_HIGHWORD 1 #define _QUAD_LOWWORD 0 /* * Define high and low longwords. */ #define H _QUAD_HIGHWORD #define L _QUAD_LOWWORD /* * Total number of bits in a quad_t and in the pieces that make it up. * These are used for shifting, and also below for halfword extraction * and assembly. */ #ifndef HAVE_LIBC #define CHAR_BIT 8 /* number of bits in a char */ #endif #define QUAD_BITS (sizeof(int64_t) * CHAR_BIT) #define LONG_BITS (sizeof(long) * CHAR_BIT) #define HALF_BITS (sizeof(long) * CHAR_BIT / 2) /* * Extract high and low shortwords from longword, and move low shortword of * longword to upper half of long, i.e., prod
obj-${CONFIG_BUTTON_HOTPLUG}	+= button-hotplug.o
} q[j] = qhat; } while (++j <= m); /* D7: loop on j. */ /* * If caller wants the remainder, we have to calculate it as * u[m..m+n] >> d (this is at most n digits and thus fits in * u[m+1..m+n], but we may need more source digits). */ if (arq) { if (d) { for (i = m + n; i > m; --i) u[i] = (u[i] >> d) | LHALF(u[i - 1] << (HALF_BITS - d)); u[i] = 0; } tmp.ul[H] = COMBINE(uspace[1], uspace[2]); tmp.ul[L] = COMBINE(uspace[3], uspace[4]); *arq = tmp.q; } tmp.ul[H] = COMBINE(qspace[1], qspace[2]); tmp.ul[L] = COMBINE(qspace[3], qspace[4]); return (tmp.q); } /* * Divide two signed quads. * ??? if -1/2 should produce -1 on this machine, this code is wrong */ int64_t __divdi3(int64_t a, int64_t b) { uint64_t ua, ub, uq; int neg; if (a < 0) ua = -(uint64_t)a, neg = 1; else ua = a, neg = 0; if (b < 0) ub = -(uint64_t)b, neg ^= 1; else ub = b; uq = __qdivrem(ua, ub, (uint64_t *)0); return (neg ? -uq : uq); } /* * Divide two unsigned quads. */ uint64_t __udivdi3(uint64_t a, uint64_t b) { return (__qdivrem(a, b, (uint64_t *)0)); } /* * Return remainder after dividing two unsigned quads. */ u_quad_t __umoddi3(u_quad_t a, u_quad_t b) { u_quad_t r; (void)__qdivrem(a, b, &r); return (r); } /* * Return remainder after dividing two signed quads. * * XXX * If -1/2 should produce -1 on this machine, this code is wrong. */ quad_t __moddi3(quad_t a, quad_t b) { u_quad_t ua, ub, ur; int neg; if (a < 0) ua = -(u_quad_t)a, neg = 1; else ua = a, neg = 0; if (b < 0) ub = -(u_quad_t)b; else ub = b; (void)__qdivrem(ua, ub, &ur); return (neg ? -ur : ur); } #endif /* !defined(__ia64__) */ #ifndef HAVE_LIBC /* Should be random enough for our uses */ int rand(void) { static unsigned int previous; struct timeval tv; gettimeofday(&tv, NULL); previous += tv.tv_sec + tv.tv_usec; previous *= RAND_MIX; return previous; } #endif