xref: /original-bsd/lib/libc/quad/ashrdi3.c (revision 68d9582f)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * %sccs.include.redist.c%
10  */
11 
12 #if defined(LIBC_SCCS) && !defined(lint)
13 static char sccsid[] = "@(#)ashrdi3.c	5.5 (Berkeley) 06/02/92";
14 #endif /* LIBC_SCCS and not lint */
15 
16 #include "quad.h"
17 
18 /*
19  * Shift a (signed) quad value right (arithmetic shift right).
20  */
21 quad
22 __ashrdi3(quad a, u_quad shift)
23 {
24 	union uu aa;
25 
26 	aa.q = a;
27 	if (shift >= LONG_BITS) {
28 		long s;
29 
30 		/*
31 		 * Smear bits rightward using the machine's right-shift
32 		 * method, whether that is sign extension or zero fill,
33 		 * to get the `sign word' s.  Note that shifting by
34 		 * LONG_BITS is undefined, so we shift (LONG_BITS-1),
35 		 * then 1 more, to get our answer.
36 		 */
37 		s = (aa.sl[H] >> (LONG_BITS - 1)) >> 1;
38 		aa.ul[L] = shift >= QUAD_BITS ? s :
39 		    aa.sl[H] >> (shift - LONG_BITS);
40 		aa.ul[H] = s;
41 	} else if (shift > 0) {
42 		aa.ul[L] = (aa.ul[L] >> shift) |
43 		    (aa.ul[H] << (LONG_BITS - shift));
44 		aa.sl[H] >>= shift;
45 	}
46 	return (aa.q);
47 }
48