update copyright date to 2013
[dpdk.git] / lib / librte_eal / common / include / rte_common.h
index 639b79b..96aeeee 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  * 
- *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
  *   All rights reserved.
  * 
  *   Redistribution and use in source and binary forms, with or without 
@@ -51,6 +51,7 @@ extern "C" {
 #include <ctype.h>
 #include <errno.h>
 
+
 /*********** Macros to eliminate unused variable warnings ********/
 
 /**
@@ -111,26 +112,53 @@ rte_align_floor_int(uintptr_t ptr, uintptr_t align)
  * point to an address no higher than the first parameter. Second parameter
  * must be a power-of-two value.
  */
-#define RTE_ALIGN_FLOOR(ptr, align) \
+#define RTE_PTR_ALIGN_FLOOR(ptr, align) \
        (typeof(ptr))rte_align_floor_int((uintptr_t)ptr, align)
 
+/**
+ * Macro to align a value to a given power-of-two. The resultant value
+ * will be of the same type as the first parameter, and will be no
+ * bigger than the first parameter. Second parameter must be a
+ * power-of-two value.
+ */
+#define RTE_ALIGN_FLOOR(val, align) \
+       (typeof(val))(val & (~((typeof(val))(align - 1))))
+
 /**
  * Macro to align a pointer to a given power-of-two. The resultant
  * pointer will be a pointer of the same type as the first parameter, and
  * point to an address no lower than the first parameter. Second parameter
  * must be a power-of-two value.
  */
-#define RTE_ALIGN_CEIL(ptr, align) \
-       RTE_ALIGN_FLOOR(RTE_PTR_ADD(ptr, align - 1), align)
+#define RTE_PTR_ALIGN_CEIL(ptr, align) \
+       RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(ptr, align - 1), align)
+
+/**
+ * Macro to align a value to a given power-of-two. The resultant value
+ * will be of the same type as the first parameter, and will be no lower
+ * than the first parameter. Second parameter must be a power-of-two
+ * value.
+ */
+#define RTE_ALIGN_CEIL(val, align) \
+       RTE_ALIGN_FLOOR((val + ((typeof(val)) align - 1)), align)
 
 /**
  * Macro to align a pointer to a given power-of-two. The resultant
  * pointer will be a pointer of the same type as the first parameter, and
  * point to an address no lower than the first parameter. Second parameter
  * must be a power-of-two value.
+ * This function is the same as RTE_PTR_ALIGN_CEIL
+ */
+#define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
+
+/**
+ * Macro to align a value to a given power-of-two. The resultant
+ * value will be of the same type as the first parameter, and
+ * will be no lower than the first parameter. Second parameter
+ * must be a power-of-two value.
  * This function is the same as RTE_ALIGN_CEIL
  */
-#define RTE_ALIGN(ptr, align) RTE_ALIGN_CEIL(ptr, align)
+#define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
 
 /**
  * Checks if a pointer is aligned to a given power-of-two value
@@ -146,7 +174,7 @@ rte_align_floor_int(uintptr_t ptr, uintptr_t align)
 static inline int
 rte_is_aligned(void *ptr, unsigned align)
 {
-       return RTE_ALIGN(ptr, align) == ptr;
+       return RTE_PTR_ALIGN(ptr, align) == ptr;
 }
 
 /*********** Macros for compile type checks ********/
@@ -232,6 +260,26 @@ rte_pause (void)
        asm volatile ("pause");
 }
 
+/**
+ * Searches the input parameter for the least significant set bit
+ * (starting from zero).
+ * If a least significant 1 bit is found, its bit index is returned.
+ * If the content of the input paramer is zero, then the content of the return
+ * value is undefined.
+ * @param v
+ *     input parameter, should not be zero.
+ * @return
+ *     least significant set bit in the input parameter.
+ */
+static inline uint32_t
+rte_bsf32(uint32_t v)
+{
+       asm("bsf %1,%0"
+               : "=r" (v)
+               : "rm" (v));
+       return (v);
+}
+
 #ifndef offsetof
 /** Return the offset of a field in a structure. */
 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
@@ -291,11 +339,11 @@ rte_str_to_size(const char *str)
  * This function never returns
  *
  * @param exit_code
- *     The exit code to be returned by the application
+ *     The exit code to be returned by the application
  * @param format
- *     The format string to be used for printing the message. This can include
- *     printf format characters which will be expanded using any further parameters
- *     to the function.
+ *     The format string to be used for printing the message. This can include
+ *     printf format characters which will be expanded using any further parameters
+ *     to the function.
  */
 void
 rte_exit(int exit_code, const char *format, ...)