1// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file contains a helper template class used to access bit fields in
6// unsigned int_ts.
7
8#ifndef GPU_COMMAND_BUFFER_COMMON_BITFIELD_HELPERS_H_
9#define GPU_COMMAND_BUFFER_COMMON_BITFIELD_HELPERS_H_
10
11namespace gpu {
12
13// Bitfield template class, used to access bit fields in unsigned int_ts.
14template<int shift, int length> class BitField {
15 public:
16  static const unsigned int kShift = shift;
17  static const unsigned int kLength = length;
18  // the following is really (1<<length)-1 but also work for length == 32
19  // without compiler warning.
20  static const unsigned int kMask = 1U + ((1U << (length-1)) - 1U) * 2U;
21
22  // Gets the value contained in this field.
23  static unsigned int Get(unsigned int container) {
24    return (container >> kShift) & kMask;
25  }
26
27  // Makes a value that can be or-ed into this field.
28  static unsigned int MakeValue(unsigned int value) {
29    return (value & kMask) << kShift;
30  }
31
32  // Changes the value of this field.
33  static void Set(unsigned int *container, unsigned int field_value) {
34    *container = (*container & ~(kMask << kShift)) | MakeValue(field_value);
35  }
36};
37
38}  // namespace gpu
39
40#endif  // GPU_COMMAND_BUFFER_COMMON_BITFIELD_HELPERS_H_
41