Subject: types.h - unnecessary 'unsigned' causes extra code Index: sys/h/types.h 2.11BSD Description: An unnecessary 'unsigned' in the major() macro causes an extra instruction to be emitted by the compiler. Since the result of the shift is going to be masked by 0377 there's no need to cast to an unsigned first. Repeat-By: Compile the following test program and examine the generated code: #include int m, c; main() { c = major(m); } _main: /* before patch */ mov _m,r1 clr r0 <----- ashc $-10,r0 bic $-400,r0 mov r0,_c _main: /* after patch */ mov _m,r0 ash $-10,r0 bic $-400,r0 mov r0,_c Note the extra "clr r0" instruction generated. It would be nice if the optimizer reduced the "ash $-10,r0 + bic $-400,r0" to "clrb r0 + swab r0". Which would be even smaller and faster than skipping the masking by 0377. Has anyone done any work on the C compiler and/or optimizer lately? Maybe someday.... Fix: Apply the patch below. The kernel has many uses of the major() macro - especially in the device drivers and tty code. --------------------------------------------------------------------- *** /usr/src/sys/h/types.h.old Sat Apr 30 00:33:52 1988 --- /usr/src/sys/h/types.h Mon Sep 23 15:40:18 1991 *************** *** 3,9 **** * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. * ! * @(#)types.h 1.1 (2.10BSD Berkeley) 12/1/86 */ #ifndef _TYPES_ --- 3,9 ---- * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. * ! * @(#)types.h 1.2 (2.11BSD Berkeley) 9/23/91 */ #ifndef _TYPES_ *************** *** 14,20 **** */ /* major part of a device */ ! #define major(x) ((int)(((unsigned)(x)>>8)&0377)) /* minor part of a device */ #define minor(x) ((int)((x)&0377)) --- 14,20 ---- */ /* major part of a device */ ! #define major(x) ((int)(((int)(x)>>8)&0377)) /* minor part of a device */ #define minor(x) ((int)((x)&0377))