Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / gcc / config / picochip / libgccExtras / lshrsi3.c
blob03423b71599ea1497d687a648ed199638fc4cd72
1 /*
3 picoChip GCC support for 32-bit logical shift right.
5 Copyright (C) 2003, 2004, 2005, 2008 Free Software Foundation, Inc.
6 Contributed by picoChip Designs Ltd.
7 Maintained by Daniel Towner (daniel.towner@picochip.com)
9 This file is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
12 later version.
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file into combinations with other programs,
17 and to distribute those combinations without any restriction coming
18 from the use of this file. (The General Public License restrictions
19 do apply in other respects; for example, they cover modification of
20 the file, and distribution when not linked into a combine
21 executable.)
23 This file is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; see the file COPYING. If not, write to
30 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
31 Boston, MA 02110-1301, USA. */
33 typedef int HItype __attribute__ ((mode (HI)));
34 typedef unsigned int UHItype __attribute__ ((mode (HI)));
35 typedef unsigned int USItype __attribute__ ((mode (SI)));
37 typedef struct USIstruct {
38 UHItype low, high;
39 } USIstruct;
41 typedef union USIunion {
42 USItype l;
43 USIstruct s;
44 } USIunion;
46 USItype __lshrsi3(USIunion value, HItype count) {
47 USIunion result;
48 int temp;
50 /* Ignore a zero count until we get into the (count < 16)
51 clause. This is slightly slower when shifting by zero, but faster
52 and smaller in all other cases (due to the better scheduling
53 opportunities available by putting the test near computational
54 instructions. */
56 if (count < 16) {
57 /* Shift low and high words by the count. */
58 result.s.low = value.s.low >> count;
59 result.s.high = value.s.high >> count;
61 /* There is now a hole in the upper `count' bits of the low
62 word. Shift the lower `count' bits of the upper word into the
63 low word. This only works when count isn't zero. */
64 if (count != 0) {
65 temp = value.s.high << (16 - count);
66 result.s.low |= temp;
69 } else {
70 /* Shift the upper word of the source into the lower word of the
71 result, and zero the result's upper word. Note that we actually
72 ned to shift by (count - 16), but as we are only using the
73 bottom 4 bits, this is equivalent to shifting by count. */
74 result.s.low = value.s.high >> count;
75 result.s.high = 0;
79 return result.l;