1cc4f247fSJohn Baldwin /*-
2cc4f247fSJohn Baldwin * SPDX-License-Identifier: BSD-3-Clause
3cc4f247fSJohn Baldwin *
4cc4f247fSJohn Baldwin * Copyright (c) 1992, 1993
5cc4f247fSJohn Baldwin * The Regents of the University of California. All rights reserved.
6cc4f247fSJohn Baldwin *
7cc4f247fSJohn Baldwin * This software was developed by the Computer Systems Engineering group
8cc4f247fSJohn Baldwin * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9cc4f247fSJohn Baldwin * contributed to Berkeley.
10cc4f247fSJohn Baldwin *
11cc4f247fSJohn Baldwin * Redistribution and use in source and binary forms, with or without
12cc4f247fSJohn Baldwin * modification, are permitted provided that the following conditions
13cc4f247fSJohn Baldwin * are met:
14cc4f247fSJohn Baldwin * 1. Redistributions of source code must retain the above copyright
15cc4f247fSJohn Baldwin * notice, this list of conditions and the following disclaimer.
16cc4f247fSJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright
17cc4f247fSJohn Baldwin * notice, this list of conditions and the following disclaimer in the
18cc4f247fSJohn Baldwin * documentation and/or other materials provided with the distribution.
19cc4f247fSJohn Baldwin * 3. Neither the name of the University nor the names of its contributors
20cc4f247fSJohn Baldwin * may be used to endorse or promote products derived from this software
21cc4f247fSJohn Baldwin * without specific prior written permission.
22cc4f247fSJohn Baldwin *
23cc4f247fSJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24cc4f247fSJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25cc4f247fSJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26cc4f247fSJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27cc4f247fSJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28cc4f247fSJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29cc4f247fSJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30cc4f247fSJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31cc4f247fSJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32cc4f247fSJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33cc4f247fSJohn Baldwin * SUCH DAMAGE.
34cc4f247fSJohn Baldwin */
35cc4f247fSJohn Baldwin
36cc4f247fSJohn Baldwin #include <libkern/quad.h>
37cc4f247fSJohn Baldwin
38cc4f247fSJohn Baldwin /*
39cc4f247fSJohn Baldwin * Divide two signed quads.
40cc4f247fSJohn Baldwin */
41cc4f247fSJohn Baldwin quad_t
__divmoddi4(quad_t a,quad_t b,quad_t * rem)42cc4f247fSJohn Baldwin __divmoddi4(quad_t a, quad_t b, quad_t *rem)
43cc4f247fSJohn Baldwin {
44cc4f247fSJohn Baldwin u_quad_t ua, ub, uq, urem;
45df11fb9bSJohn Baldwin int negq, negr;
46cc4f247fSJohn Baldwin
47*9ac841e9SJohn Baldwin if (a < 0) {
48*9ac841e9SJohn Baldwin ua = -(u_quad_t)a;
49*9ac841e9SJohn Baldwin negq = 1;
50*9ac841e9SJohn Baldwin negr = 1;
51*9ac841e9SJohn Baldwin } else {
52*9ac841e9SJohn Baldwin ua = a;
53*9ac841e9SJohn Baldwin negq = 0;
54*9ac841e9SJohn Baldwin negr = 0;
55*9ac841e9SJohn Baldwin }
56*9ac841e9SJohn Baldwin if (b < 0) {
57*9ac841e9SJohn Baldwin ub = -(u_quad_t)b;
58*9ac841e9SJohn Baldwin negq ^= 1;
59*9ac841e9SJohn Baldwin } else
60cc4f247fSJohn Baldwin ub = b;
61cc4f247fSJohn Baldwin uq = __qdivrem(ua, ub, &urem);
62cc4f247fSJohn Baldwin if (rem != 0)
63df11fb9bSJohn Baldwin *rem = negr ? -urem : urem;
64df11fb9bSJohn Baldwin return (negq ? -uq : uq);
65cc4f247fSJohn Baldwin }
66