paul@93 | 1 | /* |
paul@93 | 2 | * Memory quantity calculation utilities. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@0 | 22 | #include "memory_utils.h" |
paul@0 | 23 | |
paul@0 | 24 | |
paul@0 | 25 | |
paul@0 | 26 | /* Return page 'n' for the configured page size. */ |
paul@0 | 27 | |
paul@0 | 28 | offset_t page(unsigned int n) |
paul@0 | 29 | { |
paul@0 | 30 | return PAGE_SIZE * n; |
paul@0 | 31 | } |
paul@0 | 32 | |
paul@0 | 33 | /* Return the order of 'size', where 2 ** order yields the size. */ |
paul@0 | 34 | |
paul@0 | 35 | unsigned int page_order(offset_t size) |
paul@0 | 36 | { |
paul@0 | 37 | /* Count zeros from the left, stopping at the first set bit, using the width |
paul@0 | 38 | of the size value (in bits, starting with the width in bytes) to |
paul@0 | 39 | calculate the position of this bit and thus the order of the value. */ |
paul@0 | 40 | |
paul@0 | 41 | return sizeof(unsigned long) * 8 - 1 - __builtin_clzl(size); |
paul@0 | 42 | } |
paul@0 | 43 | |
paul@0 | 44 | /* Return 'value' rounded up to the nearest 'increment'. */ |
paul@0 | 45 | |
paul@0 | 46 | offset_t round(offset_t value, offset_t increment) |
paul@0 | 47 | { |
paul@0 | 48 | return trunc(value + increment - 1, increment); |
paul@0 | 49 | } |
paul@0 | 50 | |
paul@0 | 51 | /* Return 'value' rounded up to the nearest multiple of 'increment'. */ |
paul@0 | 52 | |
paul@0 | 53 | offset_t round_multiple(offset_t value, offset_t increment) |
paul@0 | 54 | { |
paul@0 | 55 | offset_t last = increment; |
paul@0 | 56 | |
paul@0 | 57 | while (1) |
paul@0 | 58 | { |
paul@0 | 59 | if (value < increment) |
paul@0 | 60 | return round(value, last); |
paul@0 | 61 | |
paul@0 | 62 | last = increment; |
paul@0 | 63 | increment *= 2; |
paul@0 | 64 | } |
paul@0 | 65 | } |
paul@0 | 66 | |
paul@0 | 67 | /* Return 'value' rounded down (or truncated) to the nearest 'increment'. */ |
paul@0 | 68 | |
paul@0 | 69 | offset_t trunc(offset_t value, offset_t increment) |
paul@0 | 70 | { |
paul@0 | 71 | return (value / increment) * increment; |
paul@0 | 72 | } |
paul@0 | 73 | |
paul@0 | 74 | /* Return 'value' rounded down (or truncated) to the nearest multiple of |
paul@0 | 75 | 'increment'. */ |
paul@0 | 76 | |
paul@0 | 77 | offset_t trunc_multiple(offset_t value, offset_t increment) |
paul@0 | 78 | { |
paul@0 | 79 | offset_t last = increment; |
paul@0 | 80 | |
paul@0 | 81 | while (1) |
paul@0 | 82 | { |
paul@0 | 83 | if (value < increment) |
paul@0 | 84 | return trunc(value, last); |
paul@0 | 85 | |
paul@0 | 86 | last = increment; |
paul@0 | 87 | increment *= 2; |
paul@0 | 88 | } |
paul@0 | 89 | } |
paul@0 | 90 | |
paul@0 | 91 | /* Find the maximum size aligned region within the region from 'start' to (but |
paul@0 | 92 | not including) 'end', with the given initial 'increment'. */ |
paul@0 | 93 | |
paul@0 | 94 | offset_t max_multiple(offset_t start, offset_t end, offset_t increment) |
paul@0 | 95 | { |
paul@0 | 96 | /* The largest possible aligned region is derived from the region size. */ |
paul@0 | 97 | |
paul@0 | 98 | offset_t size = trunc_multiple(end - start, increment); |
paul@0 | 99 | |
paul@0 | 100 | /* Apply the alignment to the start. */ |
paul@0 | 101 | |
paul@0 | 102 | offset_t aligned = round(start, size); |
paul@0 | 103 | |
paul@0 | 104 | /* If the region is aligned, return the size. */ |
paul@0 | 105 | |
paul@0 | 106 | if (aligned == start) |
paul@0 | 107 | return size; |
paul@0 | 108 | |
paul@0 | 109 | /* If the region is not aligned to the current size, recalculate the aligned |
paul@0 | 110 | size. */ |
paul@0 | 111 | |
paul@0 | 112 | offset_t aligned_size; |
paul@0 | 113 | |
paul@0 | 114 | do |
paul@0 | 115 | { |
paul@0 | 116 | aligned_size = trunc_multiple(end - aligned, increment); |
paul@0 | 117 | size /= 2; |
paul@0 | 118 | |
paul@0 | 119 | /* Determine whether a smaller alignment could yield a larger aligned |
paul@0 | 120 | size. */ |
paul@0 | 121 | |
paul@0 | 122 | if (aligned_size >= size) |
paul@0 | 123 | return aligned_size; |
paul@0 | 124 | |
paul@0 | 125 | aligned = round(start, size); |
paul@0 | 126 | } |
paul@0 | 127 | while (aligned > start); |
paul@0 | 128 | |
paul@0 | 129 | return aligned_size; |
paul@0 | 130 | } |
paul@0 | 131 | |
paul@0 | 132 | // vim: tabstop=4 expandtab shiftwidth=4 |