- UInt32 mask = 0x0000ffff;
- UInt32 newbits = (UInt32) newValue16;
- int shift;
- UInt32 alignment = ((UInt32) value16) & (sizeof(UInt32) - 1);
- UInt32 oldValue;
- UInt32 newValue;
- UInt32 * value;
-
- if (alignment == 2) {
- value = (UInt32 *) (value16 - 1);
- shift = 0;
- }
- else {
- // assert(alignment == 0);
- value = (UInt32 *) value16;
- shift = 16;
- }
-
- mask <<= shift;
- newbits <<= shift;
-
- oldValue = *value;
- newValue = (oldValue & ~mask) | (newbits & mask);
-
- return OSCompareAndSwap(oldValue, newValue, value);
+ UInt32 mask = 0x0000ffff;
+ UInt32 alignment = ((UInt32) value16) & (sizeof(UInt32) - 1);
+ UInt32 shiftValues = (16 << 24) | (16 << 16);
+ UInt32 shift = (UInt32) *(((UInt8 *) &shiftValues) + alignment);
+ UInt32 * value32 = (UInt32 *) (((UInt32) value16) - alignment);
+ UInt32 oldValue;
+ UInt32 newValue;
+
+ mask <<= shift;
+
+ oldValue = *value32;
+ oldValue = (oldValue & ~mask) | (oldValue16 << shift);
+ newValue = (oldValue & ~mask) | (newValue16 << shift);
+
+ return OSCompareAndSwap(oldValue, newValue, value32);