/* * (c) 2008-2009 Adam Lackorzynski , * Alexander Warg , * Frank Mehnert , * Michael Hohmuth , * Jork Löser , * Lars Reuther * economic rights: Technische Universität Dresden (Germany) * This file is part of TUD:OS and distributed under the terms of the * GNU Lesser General Public License 2.1. * Please see the COPYING-LGPL-2.1 file for details. */ /* */ /***************************************************************************** * libl4util/src/micros2l4to.c * * calculate L4 timeout * *****************************************************************************/ #include /* for static_assert() */ #include #include #include L4_CV l4_timeout_s l4util_micros2l4to(unsigned int mus) { static_assert(sizeof(mus) <= 4, "Verify the correctness of log2(mus) and the number of bits for e!"); l4_timeout_s t; if (mus == 0) t = L4_IPC_TIMEOUT_0; else if (mus == ~0U) t = L4_IPC_TIMEOUT_NEVER; else { /* Here it is certain that at least one bit in 'mus' is set. */ int e = l4util_log2(mus) - 7; if (e < 0) e = 0; /* Here it is certain that '0 <= e <= 24' and '1 <= 2^e <= 2^24'. */ unsigned m = mus >> e; /* Here it is certain that '1 <= m <= 255. Consider the following cases: * o 1 <= mus <= 255: e = 0; 2^e = 1; 1 <= mus/1 <= 255 * o 256 <= mus <= 511: e = 1; 2^e = 2; 128 <= mus/2 <= 255 * o 512 <= mus <= 1023: e = 2; 2^e = 4; 128 <= mus/4 <= 255 * o 1024 <= mus <= 2047: e = 3; 2^e = 8; 128 <= mus/8 <= 255 * ... * o 2^31 <= mus <= 2^32-1: e = 24; 128 <= mus/2^24 <= 255 * * Dividing by (1<